Skip to content

Commit dd068c4

Browse files
committed
Finish provisioning script tools and templates. Set ubuntu-version to 22.04
1 parent 669a775 commit dd068c4

9 files changed

+198
-68
lines changed

README.md

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Use for **local development of provisioning scripts** (eg. bash scripts) to setu
1313
- [Overview](#overview) | [Repo Highlights](#repo-highlights)
1414
- [Install & Use](#install--use)
1515
- [Pre-Runs](#configure-pre-run-scripts) | [Changing Pre-Runs & Docker Re-Caching](#changing-the-pre-run-script)
16-
- [Writing Scripts](#scripts) | [Running Scripts](#running-scripts)
16+
- [Writing Scripts](#scripts) | [Running Scripts](#running-scripts) | [Compiling Scripts](#compiling-scripts)
1717
- [Configure Dockerfile & docker-compose.yml](#configure-dockerfile-or-docker-compose)
1818
- [Webserver & Local Development w/ one](#webserver--local-development-with-a-webserver)
1919

@@ -151,15 +151,15 @@ Each time you change your pre-run script, you may need to force the docker conta
151151
152152
- **Starting from scratch? Want a boilerplate?**
153153
154-
If you do not already have scripts or a concept of setup scripts for your stuff yet, then you could use the pre-run example with LEMP-setup-guide.
154+
If you do not already have scripts or a concept of setup scripts for your stuff yet, then you can use `./docker-script -n <server-name>` along with `scripts/template/template-main` to generate a boilerplate for you to start with.
155155
156-
---
156+
You can make your own templates too! Just copy the template-main and edit them to your liking. The variables defined in this file will be dynamically read and prompted for by the `docker-script` command.
157157
158-
If using this approach, then the script-example could be modified to your own setup:
158+
```
159+
./docker-script -t <template> -n <server-name>
160+
```
159161
160-
1. In your IDE, open up script-example and copy to a new file - `script-<server-name>`.
161-
2. Do a search for all matching "variable" placeholders in the file: `<[^>]*>` with regular expression matching
162-
3. Replace all the variables with your own matching information. Change a lot of things to match your situation!
162+
This will create a new folder `scripts/script-<your-servername>` with init scripts based on the template-main and other templates scripts we prrovide. You can edit these as needed.
163163
164164
---
165165
@@ -224,16 +224,30 @@ Next, make sure you have put your script into the `scripts` folder with prefix `
224224
225225
Each script should log the output to corresponding log file in your scripts folder (or wherever you want really) eg. `script-my_server_dev.log` or `script-log-my_server_dev.log` to avoid the naming completion being too similar to the script itself.
226226
227-
Then, ssh into your container, ensure your script is executable, and trigger it:
228227
229228
```
230-
./docker-ssh
229+
./docker-run -n my_server_dev
230+
```
231+
232+
[↑ Contents](#contents)
233+
234+
---
235+
236+
## Compiling Scripts
237+
238+
When you are done testing your scripts, you can compile both the server scripts with the pre-run into one script to run on your cloud server.
239+
240+
```
241+
./docker-compile -n my_server_dev -p pre-run-example -r
242+
```
231243
232-
cd /var/www/simple-docker/scripts
244+
The `-r` flag is optional and means removing emojis from the script. _(linode stack scripts do not like emojis)_.
245+
The `-p` flag is optional and will default to `pre-run` if not passed.
233246
234-
chmod +x script-my_server_dev
247+
You will still need the entire `server-<server-name>` folder on your server, which will contain the compiled script. On the cloud, you will then run the compiled script.
235248
236-
./script-my_server_dev
249+
```
250+
./compiled-my_server_dev
237251
```
238252
239253
[↑ Contents](#contents)

docker-compile

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,34 @@
33
set -e
44

55
PRE_RUN='pre-run'
6-
HELP="ℹ️ How to use:\n\t./docker-compile -n <server-name> -p <pre-run>, where the -p is optional if there's an active pre-run already.\n\teg. ./docker-compile -n mysite.com -p pre-run-mysite.com\n"
7-
# getopts for -p for pre-run (default:pre-run) and require -n for server name, take a -h for help too
8-
while getopts ":n:p:h" opt; do
6+
HELP="
7+
Usage: ./docker-compile -n <server-name> [options]
8+
9+
Options:
10+
-n <server-name> : Name of the server. (Required)
11+
-p <pre-run> : Pre-run script name. (Optional: default is 'pre-run')
12+
-r : Remove emojis from output. (Optional)
13+
-h : Display this help message.
14+
15+
Example:
16+
./docker-compile -n mysite.com -p pre-run-mysite.com
17+
18+
Note:
19+
The '-p' option is optional if there's an active pre-run already set.
20+
"
21+
REMOVE_EMOJIS=false
22+
# getopts for -p for pre-run (default:pre-run) and require -n for server name, take a -h for help too, -r for remove emojis (optional)
23+
while getopts ":n:p:rh" opt; do
924
case ${opt} in
1025
n ) # process option n
1126
SERVER_NAME=$OPTARG
1227
;;
1328
p ) # process option p
1429
PRE_RUN=$OPTARG
1530
;;
31+
r ) # process option r
32+
REMOVE_EMOJIS=true
33+
;;
1634
h ) # process option h
1735
printf "$HELP"
1836
exit 1
@@ -77,6 +95,20 @@ cp scripts/server-$SERVER_NAME/run-$SERVER_NAME scripts/server-$SERVER_NAME/comp
7795
# Insert the content of $PRE_RUN into scripts/server-<server-name>/compiled-<server-name> at the #### INSERT PRE-RUN SCRIPT HERE line
7896
sed -i '' -e "/#### INSERT PRE-RUN SCRIPT HERE/r /dev/stdin" scripts/server-$SERVER_NAME/compiled-$SERVER_NAME <<< "$PRE_RUN"
7997

98+
if [ "$REMOVE_EMOJIS" = true ]; then
99+
printf "============ 🧹 Removing emojis from scripts/server-$SERVER_NAME/compiled-$SERVER_NAME\n"
100+
# Remove any emojis from scripts/server-<server-name>/compiled-<server-name>
101+
tmpfile=$(mktemp)
102+
while IFS= read -r line; do
103+
if [[ $line == *printf* ]] || [[ $line == *echo* ]]; then
104+
echo "$line" | tr -cd '\11\12\15\40-\176' >> "$tmpfile"
105+
else
106+
echo "$line" >> "$tmpfile"
107+
fi
108+
done < "scripts/server-$SERVER_NAME/compiled-$SERVER_NAME"
109+
mv "$tmpfile" "scripts/server-$SERVER_NAME/compiled-$SERVER_NAME"
110+
fi
111+
80112
# chmod +x scripts/server-<server-name>/compiled-<server-name>
81113
chmod +x scripts/server-$SERVER_NAME/compiled-$SERVER_NAME
82114

docker-script

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,15 @@ function prompt {
149149
# replace <variable> with $REPLY but reply is multiline with newlines.
150150
# escape the REPLY so that it can be used in sed - for /, &, and newlines
151151
REPLY=$(echo "$REPLY" | sed -e 's/[\/&]/\\&/g' -e 's/$/\\n/' | tr -d '\n')
152+
153+
# if reply has "null" in it (and possibly new lines), REPLY=null
154+
if [[ "$REPLY" == *"null"* ]]; then
155+
REPLY="null"
156+
fi
157+
152158
sed -i '' -e "s/<$2>/$REPLY/g" scripts/server-$SERVER_NAME/run-$SERVER_NAME
153159
else
154-
sed -i '' -e "s/<$2>/$REPLY/g" scripts/server-$SERVER_NAME/run-$SERVER_NAME
160+
sed -i '' -e "s|<$2>|$REPLY|g" scripts/server-$SERVER_NAME/run-$SERVER_NAME
155161
fi
156162
}
157163

scripts/templates/040-php-config.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
3+
printf "============ Script: PHP Config\n"
4+
5+
PHP_VERSION=$(cat /var/www/LEMP-setup-guide/config/versions/php-version)
6+
# if override-php-version is set, use that
7+
if [ -f "/var/www/LEMP-setup-guide/config/versions/override-php-version" ]; then
8+
PHP_VERSION=$(cat /var/www/LEMP-setup-guide/config/versions/override-php-version)
9+
fi
10+
PHPINI="/etc/php/$PHP_VERSION/fpm/php.ini"
11+
12+
## Adjust INI
13+
printf "============ Adjust PHP ini - upload max size\n"
14+
if [ -f "$PHPINI" ]; then
15+
sudo sed -i "/;upload_max_filesize = .*/c\upload_max_filesize = 20M" $PHPINI
16+
sudo sed -i "/upload_max_filesize = .*/c\upload_max_filesize = 20M" $PHPINI
17+
sudo sed -i "/;post_max_size = .*/c\post_max_size = 64M" $PHPINI
18+
sudo sed -i "/post_max_size = .*/c\post_max_size = 64M" $PHPINI
19+
sudo sed -i "/;memory_limit = .*/c\memory_limit = 256M" $PHPINI
20+
sudo sed -i "/memory_limit = .*/c\memory_limit = 256M" $PHPINI
21+
fi
22+

scripts/templates/050-site-example.sh

Lines changed: 77 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,35 @@ printf "============ Script: Site Example\n"
1313
OWNER_USER='ubuntu'
1414
DAEMON_USER='www-data'
1515
SCRIPT_USER='root'
16+
SERVER='example.com'
1617
DOMAIN='example.com'
1718
GITHUB_REPO='https://github.com:amurrell/developer-wordpress.git'
1819
PHP_VERSION=$(cat /var/www/LEMP-setup-guide/config/versions/php-version)
1920
# use for location of keys on prod!
2021
BASE_PATH_DEPLOY_KEYS=/root/devops/config/deployment-keys
22+
DEPLOY_FOLDER=false
23+
WEB_ROOT_PATH='html'
24+
25+
THEMENAME='devwp'
26+
DB_NAME='example_com'
27+
DB_USER='example'
28+
DB_PASS='sadhjfhkdsjhfkjdsf'
29+
2130
# if override-php-version is set, use that
2231
if [ -f "/var/www/LEMP-setup-guide/config/versions/override-php-version" ]; then
2332
PHP_VERSION=$(cat /var/www/LEMP-setup-guide/config/versions/override-php-version)
2433
fi
2534

26-
# if using simple-docker, deploy keys are found at /var/www/simple-docker/scripts/server-$DOMAIN/
35+
# if using simple-docker, deploy keys are found at /var/www/simple-docker/scripts/server-$SERVER/
2736
# if running via devops repo, deploy keys are found at $BASE_PATH_DEPLOY_KEYS/
28-
if [ -f "/var/www/simple-docker/scripts/server-$DOMAIN/$DOMAIN-deploy_key.pub" ]; then
29-
DEPLOY_KEY_PUBLIC_FILE=/var/www/simple-docker/scripts/server-$DOMAIN/$DOMAIN-deploy_key.pub
37+
if [ -f "/var/www/simple-docker/scripts/server-$SERVER/$DOMAIN-deploy_key.pub" ]; then
38+
DEPLOY_KEY_PUBLIC_FILE=/var/www/simple-docker/scripts/server-$SERVER/$DOMAIN-deploy_key.pub
3039
else
3140
DEPLOY_KEY_PUBLIC_FILE=$BASE_PATH_DEPLOY_KEYS/$DOMAIN-deploy_key.pub
3241
fi
3342

34-
if [ -f "/var/www/simple-docker/scripts/server-$DOMAIN/$DOMAIN-deploy_key" ]; then
35-
DEPLOY_KEY_PRIVATE_FILE=/var/www/simple-docker/scripts/server-$DOMAIN/$DOMAIN-deploy_key
43+
if [ -f "/var/www/simple-docker/scripts/server-$SERVER/$DOMAIN-deploy_key" ]; then
44+
DEPLOY_KEY_PRIVATE_FILE=/var/www/simple-docker/scripts/server-$SERVER/$DOMAIN-deploy_key
3645
else
3746
DEPLOY_KEY_PRIVATE_FILE=$BASE_PATH_DEPLOY_KEYS/$DOMAIN-deploy_key
3847
fi
@@ -63,7 +72,7 @@ sudo service nginx start &
6372

6473
# Makr sure php-fpm is running
6574
printf "============ Make sure php-fpm is running - bc we need it for setup-site\n"
66-
sudo service php8.2-fpm start &
75+
sudo service php$PHP_VERSION-fpm start &
6776

6877

6978
## INSTALL Setup-Site
@@ -78,37 +87,31 @@ printf "============ Run as $SCRIPT_USER: Install Site Example Using Setup-Site\
7887
cd /var/www/LEMP-setup-guide/scripts
7988

8089
# Deployment keys trick:
81-
# 1st time you run, set these to null and answer prompts to generate keys
90+
# 1st time you run, cut these out and answer prompts to generate keys
8291
# - add to github, and store contents (from logs) into files in this servers' scripts folder.
83-
# --deploy-key-public-file=null \
84-
# --deploy-key-private-file=null \
85-
# and when you have keys:
8692
# --deploy-key-public-file=$DEPLOY_KEY_PUBLIC_FILE \
8793
# --deploy-key-private-file=$DEPLOY_KEY_PRIVATE_FILE \
8894
# or use public repo with https: to avoid needing keys or being prompted for ssh password
8995

90-
# with simple docker testing - use these values
96+
# with simple docker testing - use these values - or leave them out to use defaults from setup-site
9197
# --nginx-site-conf-path=/var/www/simple-docker/scripts/templates/site.nginx.conf \
9298
# --php-site-conf-path=/var/www/simple-docker/scripts/templates/site.php-fpm.conf \
93-
# but leave these out to use defaults if on production server
9499

95100
./setup-site \
96101
--domain=$DOMAIN \
97102
--owner-user=$OWNER_USER \
98103
--github=$GITHUB_REPO \
99-
--deploy-key-public-file=null \
100-
--deploy-key-private-file=null \
104+
--deploy-subfolder=$DEPLOY_FOLDER \
105+
--web-root-path=$WEB_ROOT_PATH \
101106
--php-pools=true \
102107
--nginx-with-php=true \
103-
--nginx-site-conf-path=/var/www/simple-docker/scripts/templates/site.nginx.conf \
104108
--php-with-mysql=true \
105-
--php-site-conf-path=/var/www/simple-docker/scripts/templates/site.php-fpm.conf \
106109
--mysql-create-db=true \
107110
--mysql-root-user=root \
108111
--mysql-root-pass=password \
109-
--database-name=site_com \
110-
--database-user=site.com \
111-
--database-pass=cRaZyPaSs \
112+
--database-name=$DB_NAME \
113+
--database-user=$DB_USER \
114+
--database-pass=$DB_PASS \
112115
--database-host=localhost \
113116
--database-port=3306
114117

@@ -130,14 +133,64 @@ if [ ! -d "/var/www/$DOMAIN/html/wp" ]; then
130133
rm latest.zip
131134
fi
132135

136+
# each site needs deploy-commands repo
137+
printf "============ Installing deploy-commands repo in /var/www/$DOMAIN\n"
138+
cd /var/www/$DOMAIN
139+
git clone https://github.com/amurrell/deploy-commands.git
140+
141+
# wordpress deploy
142+
# if /var/www/deploy-commands/wordpress-deploy exists, ln -s to /var/www/$DOMAIN/commands
143+
if [ -d "/var/www/$DOMAIN/deploy-commands/wordpress-deploy" ]; then
144+
printf "============ /var/www/$DOMAIN/deploy-commands/wordpress-deploy exists so, ln -s to /var/www/$DOMAIN/commands\n"
145+
ln -s /var/www/$DOMAIN/deploy-commands/wordpress-deploy /var/www/$DOMAIN/commands
146+
147+
# add configuration files to this commands folder
148+
printf "============ Add configuration files to this commands folder\n"
149+
echo "DockerLocal/logs" > /var/www/$DOMAIN/commands/logsfolder
150+
echo "html/wp-content/themes/$THEMENAME" > /var/www/$DOMAIN/commands/assetsfolder
151+
152+
# set OWNER_USER and OWNER_GROUP
153+
echo "$OWNER_USER" > /var/www/$DOMAIN/commands/owner_user
154+
echo "$DAEMON_USER" > /var/www/$DOMAIN/commands/owner_group
155+
156+
# need to setup config file apprepo - but make sure we get it with it's special alias
157+
# Extract the repository name out of GITHUB_REPO
158+
REPO_NAME=${GITHUB_REPO##*/}
159+
REPO_NAME=${REPO_NAME%.git}
160+
MODIFIED_REPO=${GITHUB_REPO/:/-${REPO_NAME}:}
161+
echo $MODIFIED_REPO > /var/www/$DOMAIN/commands/apprepo
162+
163+
# Example scripts can all be made into real scripts
164+
# check if there are any files starting with example_
165+
printf "============ Check if there are any bash scripts starting with example_\n"
166+
cd /var/www/$DOMAIN/commands
167+
if ls example_* 1> /dev/null 2>&1; then
168+
printf "============ Found example_ scripts, copying them to scripts without the prefix.\n"
169+
for file in example_*; do
170+
cp "$file" "${file#example_}"
171+
chmod +x "${file#example_}"
172+
done
173+
fi
174+
175+
# mkdir for uploads - or copy a zip here in future?
176+
printf "============ mkdir for uploads - or copy a zip file later\n"
177+
mkdir -p /var/www/$DOMAIN/uploads
178+
# ln -s the release - if /var/www/$DOMAIN/current exists, ln -s /var/www/$DOMAIN/uploads /var/www/$DOMAIN/current/html/wp-content/uploads
179+
if [ -d "/var/www/$DOMAIN/current" ]; then
180+
printf "============ /var/www/$DOMAIN/current exists, ln -s /var/www/$DOMAIN/uploads /var/www/$DOMAIN/current/html/wp-content/uploads\n"
181+
ln -s /var/www/$DOMAIN/uploads /var/www/$DOMAIN/current/html/wp-content/uploads
182+
fi
183+
fi
184+
133185
# adjust permissions and ownership on /var/www/$DOMAIN
134186
printf "============ Adjust permissions and ownership on /var/www/$DOMAIN\n"
135187
chmod -R 775 /var/www/$DOMAIN
136188
chown -R $OWNER_USER:$DAEMON_USER /var/www/$DOMAIN
137189

138-
139190
# print that we are done
140-
printf "============ ✅ Setup-Site Script Done - Site Example on simple-docker is at http://localhost:3090/\n\n"
141-
142-
# note that if testing in simple-docker and there are multiple sites, may need to use ProxyLocal with local site.yml configuration
143-
# that's not tested yet.
191+
# if /var/www/simple-docker exists, then print message about "SimpleDocker Site Example is at http://localhost:3090"
192+
if [ -d "/var/www/simple-docker" ]; then
193+
printf "============ ✅ Setup-Site Script Done - SimpleDocker Site Example is at http://localhost:3090/\n\n"
194+
else
195+
printf "============ ✅ Setup-Site Script Done.\n\n"
196+
fi

scripts/templates/site.nginx.conf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
upstream SITE_COM_backend {
2-
server unix:/var/run/php7-fpm.SITEDOTCOM.sock1 weight=100 max_fails=5 fail_timeout=5;
3-
server unix:/var/run/php7-fpm.SITEDOTCOM.sock2 weight=100 max_fails=5 fail_timeout=5;
2+
server unix:/var/run/php-fpm.SITEDOTCOM.sock1 weight=100 max_fails=5 fail_timeout=5;
3+
server unix:/var/run/php-fpm.SITEDOTCOM.sock2 weight=100 max_fails=5 fail_timeout=5;
44
}
55

66
server {
77
listen 80 default_server;
88
server_name _;
9-
root /var/www/SITEDOTCOM/html;
9+
root /var/www/SITEDOTCOM/WEBROOTPATH;
1010
index index.php index.html;
1111

1212
access_log /var/www/SITEDOTCOM/DockerLocal/logs/access.log;

scripts/templates/site.php-fpm.conf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[SITE_COM_1]
2-
listen = /var/run/php7-fpm.SITEDOTCOM.sock1
2+
listen = /var/run/php-fpm.SITEDOTCOM.sock1
33
listen.allowed_clients = 0.0.0.0
44
listen.mode = 0666
55
user = www-data
@@ -41,7 +41,7 @@ php_admin_value[session.hash_function] = sha256
4141
php_admin_value[session.hash_bits_per_character] = 5
4242

4343
[SITE_COM_2]
44-
listen = /var/run/php7-fpm.SITEDOTCOM.sock2
44+
listen = /var/run/php-fpm.SITEDOTCOM.sock2
4545
listen.allowed_clients = 0.0.0.0
4646
listen.mode = 0666
4747
user = www-data
@@ -79,4 +79,4 @@ php_admin_value[session.cookie_httponly] = 1
7979
php_admin_value[session.entropy_length] = 32
8080
php_admin_value[session.entropy_file] = /dev/urandom
8181
php_admin_value[session.hash_function] = sha256
82-
php_admin_value[session.hash_bits_per_character] = 5
82+
php_admin_value[session.hash_bits_per_character] = 5

0 commit comments

Comments
 (0)