Repository navigation
Expand file tree
/
Copy pathrun.sh
More file actions
333 lines (275 loc) · 8.78 KB
/
Copy pathrun.sh
File metadata and controls
333 lines (275 loc) · 8.78 KB
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
#!/bin/bash
# shellcheck disable=SC1091
if ! sudo mount -a 2>/dev/null; then
printf '\e[1;31mERROR:\e[0m %s' \
'Container running with improper privileges.
Be sure your service is configured with the following options:
___
services:
wordpress:
cap_add:
- SYS_ADMIN
devices:
- /dev/fuse
___
OR (use first option if possible)
___
services:
wordpress:
privileged: true
___
' | sed 's/^ //'
exit 1
fi
# Environment
# ------------
declare -x TERM="${TERM:-xterm}"
declare PLUGINS="${PLUGINS//,/}"
declare THEMES="${THEMES//,/}"
declare -A plugin_deps
declare -A theme_deps
# FIXME: Deprecation of old version of $URL_REPLACE
if [[ $URL_REPLACE =~ , ]]; then
URL_REPLACE=${URL_REPLACE%%,*}
printf '\e[1;33;7mDEPRECATED\e[0m %s\n' "URL_REPLACE must only contain AFTER_URL. BEFORE_URL,AFTER_URL form has been deprecated"
fi
# Configuration
# -------------
mkdir -p ~/.wp-cli
echo -e "
apache_modules:
- mod_rewrite
config create:
dbhost: ${DB_HOST:-db}:3306
dbname: ${DB_NAME:-wordpress}
dbpass: ${DB_PASS:-root}
dbprefix: ${DB_PREFIX:-wp_}
dbuser: ${DB_USER:-root}
extra-php: |
define('WP_DEBUG', ${WP_DEBUG:-false});
define('WP_DEBUG_LOG', ${WP_DEBUG_LOG:-false});
define('WP_DEBUG_DISPLAY', ${WP_DEBUG_DISPLAY:-true});
$(sed '1 ! s/.*/ \0/' < <(echo -e "${EXTRA_PHP:-}"))
core download:
locale: ${WP_LOCALE:-en_US}
skip-content: true
version: ${WP_VERSION:-latest}
core install:
admin_email: ${ADMIN_EMAIL:-admin@${SERVER_NAME:-localhost.com}}
admin_password: ${DB_PASS:-root}
admin_user: ${DB_USER:-root}
skip-email: true
title: ${DB_NAME:-wordpress}
url: ${URL_REPLACE:-localhost:8080}
rewrite structure:
hard: true
" >~/.wp-cli/config.yml
# Apache config adustments
sudo sed -i \
-e "/^[[:blank:]]*.ServerName www.example.com/{c\\" \
-e "\\tServerName ${SERVER_NAME:-localhost} \\" \
-e "\\tServerAlias www.${SERVER_NAME:-localhost}" \
-e '}' /etc/apache2/sites-available/000-default.conf
main() {
h1 'Begin WordPress Installation'
init
h2 'Waiting for MySQL to initialize...'
while ! mysqladmin ping \
--host="${DB_HOST:-db}" \
--user="${DB_USER:-root}" \
--password="${DB_PASS:-root}" \
--silent >/dev/null; do
sleep 1
done
h2 'Configuring WordPress'
wp --color config create --force |& logger
h2 'Checking database'
check_database
if [[ "$MULTISITE" == 'true' ]]; then
h2 'Enabling Multisite'
wp --color core multisite-convert |& logger
fi
h2 'Checking themes'
check_themes
h2 'Checking plugins'
check_plugins
h2 'Finalizing'
if [[ "$MULTISITE" != 'true' ]]; then
wp --color rewrite structure \
"${PERMALINKS:-/%year%/%monthnum%/%postname%/}" |& logger
fi
h1 'WordPress Configuration Complete!'
sudo rm -f /var/run/apache2/apache2.pid
sudo apache2-foreground
}
# Config Functions
# ---------------------
init() {
declare raw_line
declare -a keyvalue
check_volumes
for raw_line in $PLUGINS; do
mapfile -t keyvalue < <(
sed -n '
s/.*\[\(.*\)\]\([^[:blank:]]*\).*/\1\n\2/p # Matches [key]value form
t # If previous match succeeds, skip to end
{p; p;} # Assumes normal form
' <<<"$raw_line")
plugin_deps[${keyvalue[0]}]="${keyvalue[1]}"
done
for raw_line in $THEMES; do
mapfile -t keyvalue < <(
sed -n '
s/.*\[\(.*\)\]\([^[:blank:]]*\).*/\1\n\2/p # Matches [key]value form
t # If previous match succeeds, skip to end
{p; p;} # Assumes normal form
' <<<"$raw_line")
theme_deps[${keyvalue[0]}]="${keyvalue[1]}"
done
# If no theme dependencies or volumes exist, fall back to default
if [[ ${#theme_deps[@]} == 0 && $(check_volumes -t) == "" ]]; then
theme_deps[twentyseventeen]=twentyseventeen
fi
sudo chown -R admin:admin /app
if [[ ! -f /app/wp-settings.php ]]; then
h2 'Downloading WordPress'
wp --color core download |& logger
fi
}
check_database() {
wp core is-installed 2>/dev/null && return
wp --color db create |& logger
declare data_path
data_path=$(find /data -name '*.sql' -print -quit 2>/dev/null)
if [[ ! "$data_path" ]]; then
wp --color core install |& logger
return
fi
wp --color db import "$data_path" |& logger
if [[ -n "$URL_REPLACE" ]]; then
wp --color search-replace \
--skip-columns=guid \
--report-changed-only \
--no-report \
"$(wp option get siteurl)" \
"$URL_REPLACE" |& logger
fi
}
# Install / remove plugins based on $PLUGINS in parallel threads
check_plugins() {
declare -a plugin_volumes
mapfile -t plugin_volumes < <(check_volumes -p)
(
# Obtain keys of plugins to install
mapfile -t plugin_keys < <(comm -23 \
<(echo "${!plugin_deps[@]}" | tr ' ' '\n' | sort -u) \
<(wp plugin list --field=name | sort -u))
# Transform keys to values
mapfile -t plugin_values < <(
for key in "${plugin_keys[@]}"; do
echo "${plugin_deps[$key]}"
done)
if [[ "${#plugin_keys[@]}" -gt 0 ]]; then
wp --color plugin install "${plugin_values[@]}" |& logger
# Silence nonsensical "plugin already activated" warning messages
wp plugin activate "${plugin_keys[@]}" --quiet
fi
) &
(
mapfile -t remove_list < <(comm -13 \
<(echo "${!plugin_deps[@]}" "${plugin_volumes[@]}" | tr ' ' '\n' | sort -u) \
<(wp plugin list --field=name | sort -u))
if [[ ${#remove_list[@]} -gt 0 ]]; then
wp --color plugin uninstall --deactivate "${remove_list[@]}" |& logger
fi
) &
wait
}
# Install / remove themes based on $THEMES in parallel threads
check_themes() {
declare -a theme_volumes
mapfile -t theme_volumes < <(check_volumes -t)
(
# Obtain keys of themes to install
mapfile -t theme_keys < <(comm -23 \
<(echo "${!theme_deps[@]}" "${theme_volumes[@]}" | tr ' ' '\n' | sort -u) \
<(wp theme list --field=name | sort -u))
# Transform keys to values
mapfile -t theme_values < <(
for key in "${theme_keys[@]}"; do
echo "${theme_deps[$key]}"
done)
if [[ "${#theme_values[@]}" -gt 0 ]]; then
wp --color theme install "${theme_values[@]}" |& logger
fi
) &
(
mapfile -t remove_list < <(comm -13 \
<(echo "${!theme_deps[@]}" "${theme_volumes[@]}" | tr ' ' '\n' | sort -u) \
<(wp theme list --field=name | sort -u))
if [[ ${#remove_list[@]} -gt 0 ]]; then
wp --color theme delete "${remove_list[@]}" |& logger
fi
) &
wait
}
check_volumes() {
if [[ ! -f ~/.dockercache ]]; then
{
(
find /app/wp-content/plugins/* \
-maxdepth 0 \
-type d \
-printf 'plugin\t%f\n' 2>/dev/null
) &
(
find /app/wp-content/themes/* \
-maxdepth 0 \
-type d \
-printf 'theme\t%f\n' 2>/dev/null
) &
wait
} >~/.dockercache
fi
declare opt OPTIND
while getopts 'pt' opt; do
case "$opt" in
p)
awk '/^plugin/{ print $2 }' ~/.dockercache
;;
t)
awk '/^theme/{ print $2 }' ~/.dockercache
;;
*)
exit 1
;;
esac
done
shift "$((OPTIND - 1))"
}
# Helpers
# ---------------------
declare -i term_width=70
h1() {
declare border padding text
border='\e[1;34m'"$(printf '=%.0s' $(seq 1 "$term_width"))"'\e[0m'
padding="$(printf ' %.0s' $(seq 1 $(((term_width - $(wc -m <<<"$*")) / 2))))"
text="\\e[1m$*\\e[0m"
echo -e "$border"
echo -e "${padding}${text}${padding}"
echo -e "$border"
}
h2() {
printf '\e[1;33m==>\e[37;1m %s\e[0m\n' "$*"
}
logger() {
fold --width $((term_width - 9)) -s | sed -n '
/^\x1b\[[0-9;]*m/{ # match any line beginning with colorized text
s/Error:/ \0/ # pads line so its length matches others
p # any lines containing color
b # branch to end
}
s/.*/ \0/p # pads all other lines with 9 spaces
'
}
main