-
Notifications
You must be signed in to change notification settings - Fork 167
/
run.sh
265 lines (217 loc) · 6.74 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
#!/bin/bash
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
# needed on certain cloud hosts
security_opt:
- apparmor:unconfined
___
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//,/}"
# Runtime
# ------------
declare default_theme=twentytwenty
declare -A plugin_deps
declare -A theme_deps
# Configuration
# -------------
mkdir -p ~/.wp-cli
echo -e "
path: /app
color: true
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}
dbcharset: ${DB_CHARSET:-utf8}
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
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 config create --force |& logger
h2 'Checking database'
check_database
if [[ "$MULTISITE" == 'true' ]]; then
h2 'Enabling Multisite'
wp core multisite-convert |& logger
fi
h2 'Checking themes'
check_themes
h2 'Checking plugins'
check_plugins
h2 'Finalizing'
if [[ $MULTISITE != true ]]; then
wp rewrite structure \
"${PERMALINKS:-/%year%/%monthnum%/%postname%/}" |& logger
fi
if [[ -e /docker-entrypoint-initwp.d ]]; then
h2 'Executing user init scripts'
for file in /docker-entrypoint-initwp.d/*; do
[[ -x $file ]] && "$file"
done
fi
h1 'WordPress Installation Complete!'
sudo rm -f /var/run/apache2/apache2.pid
sudo apache2-foreground
}
# Config Functions
# ---------------------
init() {
declare raw_line
declare -a keyvalue
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 && ! -d /app/wp-content/themes ]]; then
theme_deps["$default_theme"]="$default_theme"
fi
# 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
sudo chown -R admin:admin /app
if [[ ! -f /app/wp-settings.php ]]; then
h2 'Downloading WordPress'
wp core download |& logger
fi
}
# this is ran in a subshell to isolate nullglob as it for whatever reason
# interacts with our $PLUGINS and $THEMES variables.
# See: https://github.com/visiblevc/wordpress-starter/issues/176
check_database() (
shopt -s nullglob
declare file
declare -i num_imported=0
wp core is-installed 2> /dev/null && return
wp db create |& logger
wp core install |& logger
for file in /data/*.sql; do
h2 "Importing $file (this might take a while)..."
wp db import "$file" |& logger
((num_imported++))
done
if ((num_imported > 0)); then
if [[ -n $URL_REPLACE ]]; then
h2 "Replacing URLs in database (this might take a while)..."
wp search-replace \
--skip-columns=guid \
--report-changed-only \
--no-report \
"$(wp option get siteurl)" \
"$URL_REPLACE" |& logger
fi
h2 'Updating database...'
wp core update-db |& logger
fi
)
check_plugins() {
declare key
for key in "${!plugin_deps[@]}"; do
wp plugin is-installed "$key" || wp plugin install "${plugin_deps[$key]}" |& logger
wp plugin is-active "$key" || wp plugin activate "$key" |& logger
done
}
check_themes() {
declare key
for key in "${!theme_deps[@]}"; do
wp theme is-installed "$key" || wp theme install "${theme_deps[$key]}" |& logger
wp theme is-active "$key" || wp theme activate "$key" |& logger
done
}
# 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