/
docker-entrypoint.sh
230 lines (189 loc) · 6.85 KB
/
docker-entrypoint.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
#! /bin/bash
set -e
# Check if all required variables defined.
# -----------------------------------------------------------------------------
required_vars=(\
DWC_WP_DB_NAME \
DWC_WP_DB_USER \
DWC_WP_DB_PASS \
DWC_WP_ROOT \
DWC_WP_HOME \
DWC_WP_ADMIN_USER \
DWC_WP_ADMIN_PASS \
DWC_WP_ADMIN_EMAIL \
)
for var in "${required_vars[@]}" ; do
if [[ "" == ${!var} ]] ; then
echo "Error: Required variable $var is not defined."
exit 1
fi
done
# Setup optional variables with default values
# -----------------------------------------------------------------------------
DWC_WP_DB_TABLE_PREFIX=${DWC_WP_DB_TABLE_PREFIX:=wp_}
DWC_WP_LANG=${DWC_WP_LANG:="en_US"}
DWC_WP_VERSION=${DWC_WP_VERSION:="latest"}
DWC_WP_DEBUG=${DWC_WP_DEBUG:=false}
# Alter PHP configuration directives for development mode.
# -----------------------------------------------------------------------------
if [[ $DWC_WP_DEBUG == true ]] ; then
echo "Notice: Altering PHP configuration for development mode."
sed -i "/opcache.revalidate_freq=.*/c\opcache.revalidate_freq=0" /usr/local/etc/php/conf.d/opcache-recommended.ini
fi
# Prepare WordPress root directory.
# -----------------------------------------------------------------------------
if [[ ! -e $DWC_WP_ROOT ]] ; then
echo "Notice: Creating WordPress root directory..."
mkdir -p $DWC_WP_ROOT
fi
cd $DWC_WP_ROOT
# Normalize access rights for wp-content directory
# -----------------------------------------------------------------------------
mkdir -p "$DWC_WP_ROOT/wp-content"
mkdir -p "$DWC_WP_ROOT/wp-content/themes"
mkdir -p "$DWC_WP_ROOT/wp-content/plugins"
chown www-data:www-data "$DWC_WP_ROOT"
chown www-data:www-data "$DWC_WP_ROOT/wp-content"
chown www-data:www-data "$DWC_WP_ROOT/wp-content/themes"
chown www-data:www-data "$DWC_WP_ROOT/wp-content/plugins"
# Start installation process
# -----------------------------------------------------------------------------
if ! $(wp core is-installed) ; then
# Download & extract WP
# ---------------------------
wp_entries=(wp-admin wp-content wp-includes index.php)
wp_valid=true
for wp_entry in "${wp_entries[@]}" ; do
if [[ ! -e "$DWC_WP_ROOT/$wp_entry" ]] ; then
wp_valid=false
fi
done
if [[ $wp_valid == true ]] ; then
echo "Notice: WordPress installation seems fine, no need to re-download."
else
wp core download --locale=$DWC_WP_LANG --path=$DWC_WP_ROOT --version=$DWC_WP_VERSION
fi
# (Re)Generate wp-config.php file
# ---------------------------
rm -f "$DWC_WP_ROOT/wp-config.php"
extra_php=()
extra_php+="define( 'WP_HOME', '$DWC_WP_HOME' );"
extra_php+="define( 'WP_SITEURL', '$DWC_WP_HOME' );"
if [[ $DWC_WP_DEBUG == true ]] ; then
extra_php+="define( 'WP_DEBUG', true );"
extra_php+="define( 'SAVEQUERIES', true );"
fi
wp core config \
--dbhost=database \
--dbname=$DWC_WP_DB_NAME \
--dbuser=$DWC_WP_DB_USER \
--dbpass=$DWC_WP_DB_PASS \
--dbprefix=$DWC_WP_DB_TABLE_PREFIX \
--locale=$DWC_WP_LANG \
--skip-check \
--extra-php <<PHP
$extra_php
PHP
# Next step requires working database connection, so let's wait for it if necessary.
# ---------------------------
dbhost_counter=0
until [[ $(mysqladmin -h database -u$DWC_WP_DB_USER -p$DWC_WP_DB_PASS status | awk '{print $1}') == "Uptime:" ]] &>/dev/null ; do
echo "Notice: Database server is not ready yet - waiting…"
sleep 1
let dbhost_counter=dbhost_counter+1
if [[ $dbhost_counter -gt 30 ]] ; then
echo "Error: Could not connect to database server. Exiting."
exit 1
fi
done
echo "Notice: Database server is ready - continuing…"
# Install WordPress by importing DB dump or run installation process.
# ---------------------------
if ! $(wp core is-installed) ; then
if [[ -n $DWC_WP_DB_IMPORT_FILENAME ]] ; then
if [[ -f "/database/$DWC_WP_DB_IMPORT_FILENAME" ]] ; then
wp db import "/database/$DWC_WP_DB_IMPORT_FILENAME"
else
echo "Error: \$DWC_WP_DB_IMPORT_FILENAME variable was set but file is not present"
exit 1
fi
else
wp core install \
--url=$DWC_WP_HOME \
--title="WordPress" \
--admin_user=$DWC_WP_ADMIN_USER \
--admin_password=$DWC_WP_ADMIN_PASS \
--admin_email=$DWC_WP_ADMIN_EMAIL \
--skip-email
fi
else
echo "Notice: WordPress database tables are already present. Skiping database installation."
fi
# Install required plugins
# ---------------------------
development_plugins=(\
debug-bar \
debug-bar-console \
debug-bar-cron \
debug-bar-extender \
rewrite-rules-inspector \
log-deprecated-notices \
log-viewer \
monster-widget \
user-switching \
rtl-tester \
regenerate-thumbnails \
simply-show-ids \
theme-check \
)
user_plugins=(${DWC_WP_PLUGINS})
installed_plugins=($(wp plugin list --field=name))
if [[ $DWC_WP_DEBUG == true ]] ; then
for plugin in "${development_plugins[@]}" ; do
if ! [[ ${installed_plugins[*]} =~ $plugin ]]; then
wp plugin install $plugin
fi
done
fi
for plugin in "${user_plugins[@]}" ; do
if ! [[ ${installed_plugins[*]} =~ $plugin ]]; then
wp plugin install $plugin
fi
done
# Activate installed plugins
# ---------------------------
activated_plugins=($(wp plugin list --field=name --status=active))
if [[ $DWC_WP_DEBUG == true ]] ; then
for plugin in "${development_plugins[@]}" ; do
if ! [[ ${activated_plugins[*]} =~ $plugin ]]; then
wp plugin activate $plugin
fi
done
fi
for plugin in "${user_plugins[@]}" ; do
if ! [[ ${activated_plugins[*]} =~ $plugin ]]; then
wp plugin activate $plugin
fi
done
# Install required theme
# ---------------------------
if [[ -n $DWC_WP_THEME ]] ; then
wp theme install $DWC_WP_THEME --activate
fi
fi
# Load user entry point additions
# -----------------------------------------------------------------------------
if [[ -e /user-entrypoint.sh ]] ; then
if [[ ! -x /user-entrypoint.sh ]] ; then
chmod +x /user-entrypoint.sh
fi
source /user-entrypoint.sh
fi
# Unsetting sensitive variables
# -----------------------------------------------------------------------------
unset DWC_WP_ADMIN_USER
unset DWC_WP_ADMIN_PASS
unset DWC_WP_ADMIN_EMAIL
# Start container's main process
# -----------------------------------------------------------------------------
exec php-fpm