forked from visiblevc/wordpress-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.sh
413 lines (351 loc) · 11.2 KB
/
run.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/bin/bash
# Runtime
# --------
export TERM=${TERM:-xterm}
VERBOSE=${VERBOSE:-false}
# Environment
# ------------
DB_HOST=${DB_HOST:-'db'}
DB_NAME=${DB_NAME:-'wordpress'}
DB_PASS=${DB_PASS:-'root'}
DB_PREFIX=${DB_PREFIX:-'wp_'}
ADMIN_EMAIL=${ADMIN_EMAIL:-"admin@${DB_NAME}.com"}
PERMALINKS=${PERMALINKS:-'/%year%/%monthnum%/%postname%/'}
WP_DEBUG_DISPLAY=${WP_DEBUG_DISPLAY:-'true'}
WP_DEBUG_LOG=${WB_DEBUG_LOG:-'false'}
WP_DEBUG=${WP_DEBUG:-'false'}
WP_VERSION=${WP_VERSION:-'latest'}
[ "$SEARCH_REPLACE" ] && \
BEFORE_URL=$(echo "$SEARCH_REPLACE" | cut -d ',' -f 1) && \
AFTER_URL=$(echo "$SEARCH_REPLACE" | cut -d ',' -f 2) || \
SEARCH_REPLACE=false
# WP-CLI configuration
# ---------------------
cat > /app/wp-cli.yml <<EOF
apache_modules:
- mod_rewrite
core config:
dbuser: root
dbpass: $DB_PASS
dbname: $DB_NAME
dbprefix: $DB_PREFIX
dbhost: $DB_HOST:3306
extra-php: |
define('WP_DEBUG', ${WP_DEBUG,,});
define('WP_DEBUG_LOG', ${WP_DEBUG_LOG,,});
define('WP_DEBUG_DISPLAY', ${WP_DEBUG_DISPLAY,,});
core install:
url: $([ "$AFTER_URL" ] && echo "$AFTER_URL" || echo localhost:8080)
title: $DB_NAME
admin_user: root
admin_password: $DB_PASS
admin_email: $ADMIN_EMAIL
skip-email: true
EOF
main() {
h1 "Begin WordPress Installation"
# Download WordPress
# ------------------
if [ ! -f /app/wp-settings.php ]; then
h2 "Installing WordPress"
h3 "Downloading..."
chown -R www-data:www-data /app /var/www/html
WP core download --version="$WP_VERSION" |& loglevel
STATUS "${PIPESTATUS[0]}"
fi
# Wait for MySQL
# --------------
h2 "Waiting for MySQL to initialize..."
printf "%b " "${CYAN}${BOLD} ->${NC} "
while ! mysqladmin ping --host="$DB_HOST" --password="$DB_PASS" --silent; do
sleep 1
done
h2 "Configuring WordPress"
h3 "Generating wp-config.php file..."
rm -f /app/wp-config.php
WP core config |& loglevel
STATUS "${PIPESTATUS[0]}"
h2 "Checking database"
check_database
# Make multisite
# NOTE: This will likely cause issues down the road.
# Multisite should ideally be a completely separate build.
# ---------------
h2 "Checking for multisite"
if [ "$MULTISITE" == "true" ]; then
h3 "Multisite found. Enabling..."
WP core multisite-convert |& loglevel
STATUS "${PIPESTATUS[0]}"
else
h3 "Multisite not found. SKIPPING..."
STATUS SKIP
fi
h2 "Checking themes"
check_themes
h2 "Checking plugins"
check_plugins
h2 "Finalizing"
if [ ! -f /app/.htaccess ]; then
h3 "Generating .htaccess file"
if [[ "$MULTISITE" == 'true' ]]; then
STATUS 1
h3warn "Cannot generate .htaccess for multisite!"
else
WP rewrite structure "$PERMALINKS" |& loglevel
WP rewrite flush --hard |& loglevel
STATUS "${PIPESTATUS[0]}"
fi
else
h3 ".htaccess exists. SKIPPING..."
STATUS SKIP
fi
h3 "Adjusting file permissions"
groupadd -f docker && usermod -aG docker www-data
find /app -type d -exec chmod 755 {} \;
find /app -type f -exec chmod 644 {} \;
mkdir -p /app/wp-content/uploads
chmod -R 775 /app/wp-content/uploads && \
chown -R :docker /app/wp-content/uploads
STATUS $?
h1 "WordPress Configuration Complete!"
rm -f /var/run/apache2/apache2.pid
source /etc/apache2/envvars
exec apache2 -D FOREGROUND
}
check_database() {
WP core is-installed |& loglevel
if [ "${PIPESTATUS[0]}" == '1' ]; then
h3 "Creating database $DB_NAME"
WP db create |& loglevel
STATUS "${PIPESTATUS[0]}"
# If an SQL file exists in /data => load it
if [[ "$(find /data -name '*.sql' 2>/dev/null | wc -l)" != "0" ]]; then
DATA_PATH=$(find /data/*.sql | head -n 1)
h3 "Loading data backup from $DATA_PATH"
WP db import "$DATA_PATH" |& loglevel
STATUS "${PIPESTATUS[0]}"
# If SEARCH_REPLACE is set => Replace URLs
if [ "$SEARCH_REPLACE" != false ]; then
h3 "Replacing URLs"
REPLACEMENTS=$(WP search-replace "$BEFORE_URL" "$AFTER_URL" \
--skip-columns=guid | grep replacement) || \
ERROR $((LINENO-2)) "Could not execute SEARCH_REPLACE on database"
echo -ne "$REPLACEMENTS\n"
fi
else
h3 "No database backup found. Initializing new database"
WP core install |& loglevel
STATUS "${PIPESTATUS[0]}"
fi
else
h3 "Database exists. SKIPPING..."
STATUS SKIP
fi
}
check_themes() {
declare -A themes
local -i theme_count=0
local -i i=1
local theme_name
local theme_url
# If $THEMES is not set => prune all existing themes
if [[ ! "${THEMES-}" ]]; then
h3 "No theme dependencies listed"
STATUS SKIP
h2 "Checking for orphaned themes"
while read -r theme_name; do
if [[ "$theme_name" == 'twentyseventeen' ]]; then continue; fi
h3 "'$theme_name' no longer needed. Pruning"
WP theme delete --quiet "$theme_name"
STATUS $?
done <<< "$(WP theme list --field=name)"
return
fi
# Correct for cases where user forgets to add trailing comma
[[ "${THEMES:(-1)}" != ',' ]] && THEMES+=','
# Set $theme_count to the total number of themes set in $THEMES
while read -r -d,; do ((theme_count++)); done <<< "$THEMES"
# Iterate over each theme set in $THEMES
while read -r -d, theme_name; do
theme_url= # reset to null
# If $theme_name matches a URL using the old format => attempt to install it and continue
if [[ $theme_name =~ ^https?://[www]?.+ ]]; then
h3warn "$theme_name"
h3warn "Can't check if theme is already installed using above format!"
h3warn "Switch your compose file to '[theme-slug]http://themeurl.com/themefile.zip' for better checks"
h3 "($i/$theme_count) '$theme_name' not found. Installing"
WP theme install --quiet "$theme_name"
STATUS $?
((i++))
continue
fi
# Locally volumed themes
if [[ $theme_name =~ ^\[local\] ]]; then
themes["${theme_name##*]}"]="${theme_name##*]}"
h3 "($i/$theme_count) '${theme_name##*]}' listed as a local volume. SKIPPING..."
STATUS SKIP
((i++))
continue
fi
# If $theme_name matches a URL using the new format => set $theme_name & $theme_url
if [[ $theme_name =~ ^\[.+\]https?://[www]?.+ ]]; then
theme_url=${theme_name##\[*\]}
theme_name="$(echo "$theme_name" | grep -oP '\[\K(.+)(?=\])')"
fi
theme_url=${theme_url:-$theme_name}
if WP theme is-installed "$theme_name"; then
h3 "($i/$theme_count) '$theme_name' found. SKIPPING..."
STATUS SKIP
else
h3 "($i/$theme_count) '$theme_name' not found. Installing"
WP theme install --quiet "$theme_url"
STATUS $?
fi
# Make sure the first listed theme is active so that others can be removed
if [[ $i == 1 && $(WP theme status "$theme_name" | grep -Po 'Status.+' | awk '{print $2}') != 'Active' ]]; then
h3 "Activating '$theme_name'"
WP theme activate --quiet "$theme_name"
STATUS $?
fi
themes[$theme_name]=$theme_url
((i++))
done <<< "$THEMES"
h2 "Checking for orphaned themes"
while read -r theme_name; do
if [[ ! ${themes[$theme_name]} ]]; then
h3 "'$theme_name' no longer needed. Pruning"
WP theme delete --quiet "$theme_name"
STATUS $?
fi
done <<< "$(WP theme list --field=name)"
}
check_plugins() {
declare -A plugins
local -i plugin_count=0
local -i i=1
local plugin_name
local plugin_url
# If $PLUGINS is not set => prune all existing plugins
if [[ ! "${PLUGINS-}" ]]; then
h3 "No plugin dependencies listed"
STATUS SKIP
h2 "Checking for orphaned plugins"
while read -r plugin_name; do
h3 "'$plugin_name' no longer needed. Pruning..."
WP plugin uninstall --deactivate --quiet "$plugin_name"
STATUS $?
done <<< "$(WP plugin list --field=name)"
return
fi
# Correct for cases where user forgets to add trailing comma
[[ "${PLUGINS:(-1)}" != ',' ]] && PLUGINS+=','
# Set $plugin_count to the total number of plugins set in $PLUGINS
while read -r -d,; do ((plugin_count++)); done <<< "$PLUGINS"
# Iterate over each plugin set in $PLUGINS
while read -r -d, plugin_name; do
plugin_url= # reset to null
# If $plugin_name matches a URL using the old format => attempt to install it and continue
if [[ $plugin_name =~ ^https?://[www]?.+ ]]; then
h3warn "$plugin_name"
h3warn "Can't check if plugin is already installed using above format!"
h3warn "Switch your compose file to '[plugin-slug]http://pluginurl.com/pluginfile.zip' for better checks"
h3 "($i/$plugin_count) '$plugin_name' not found. Installing..."
WP plugin install --activate --quiet "$plugin_name"
STATUS $?
((i++))
continue
fi
# Locally volumed plugins
if [[ $plugin_name =~ ^\[local\] ]]; then
plugins["${plugin_name##*]}"]="${plugin_name##*]}"
h3 "($i/$plugin_count) '${plugin_name##*]}' listed as a local volume. Activating..."
WP plugin activate --quiet "${plugin_name##*]}"
STATUS SKIP
((i++))
continue
fi
# If $plugin_name matches a URL using the new format => set $plugin_name & $plugin_url
if [[ $plugin_name =~ ^\[.+\]https?://[www]?.+ ]]; then
plugin_url=${plugin_name##\[*\]}
plugin_name="$(echo "$plugin_name" | grep -oP '\[\K(.+)(?=\])')"
fi
plugin_url=${plugin_url:-$plugin_name}
if WP plugin is-installed "$plugin_name"; then
h3 "($i/$plugin_count) '$plugin_name' found. SKIPPING..."
STATUS SKIP
else
h3 "($i/$plugin_count) '$plugin_name' not found. Installing..."
WP plugin install --activate --quiet "$plugin_url"
STATUS $?
# Pretty much guarenteed to need/want 'restful' if you are using 'rest-api'
if [ "$plugin_name" == 'rest-api' ]; then
h3 "($i.5/$plugin_count) Installing 'restful' WP-CLI package..."
wp package install wp-cli/restful --quiet --allow-root
STATUS $?
fi
fi
plugins[$plugin_name]=$plugin_url
((i++))
done <<< "$PLUGINS"
h2 "Checking for orphaned plugins"
while read -r plugin_name; do
if [[ ! ${plugins[$plugin_name]} ]]; then
h3 "'$plugin_name' no longer needed. Pruning..."
WP plugin uninstall --deactivate --quiet "$plugin_name"
STATUS $?
fi
done <<< "$(WP plugin list --field=name)"
}
# Helpers
# --------------
RED='\033[0;31m'
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
PURPLE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\E[1m'
NC='\033[0m'
h1() {
local len=$(($(tput cols)-1))
local input=$*
local size=$(((len - ${#input})/2))
for ((i = 0; i < len; i++)); do echo -ne "${PURPLE}${BOLD}="; done; echo ""
for ((i = 0; i < size; i++)); do echo -n " "; done; echo -e "${NC}${BOLD}$input"
for ((i = 0; i < len; i++)); do echo -ne "${PURPLE}${BOLD}="; done; echo -e "${NC}"
}
h2() {
echo -e "${ORANGE}${BOLD}==>${NC}${BOLD} $*${NC}"
}
h3() {
printf "%b " "${CYAN}${BOLD} ->${NC} $*"
}
h3warn() {
printf "%b " "${RED}${BOLD} [!]|${NC} $*" && echo ""
}
STATUS() {
local status=$1
if [[ $1 == 'SKIP' ]]; then
echo ""
return
fi
if [[ $status != 0 ]]; then
echo -e "${RED}✘${NC}"
return
fi
echo -e "${GREEN}✓${NC}"
}
ERROR() {
echo -e "${RED}=> ERROR (Line $1): $2.${NC}";
exit 1;
}
WP() {
sudo -u www-data wp "$@"
}
loglevel() {
[[ "$VERBOSE" == "false" ]] && return
local IN
while read -r IN; do
echo "$IN"
done
}
main