-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
docker-entrypoint.sh
executable file
·371 lines (310 loc) · 11.9 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
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
#!/usr/bin/env bash
set -e
set -u
set -o pipefail
###################################################################################################
###################################################################################################
###
### GLOBAL VARIABLES
###
###################################################################################################
###################################################################################################
### The following env variables are set inside the Dockerfiles
### MY_USER
### MY_GROUP
### HTTPD_START
### HTTPD_RELOAD
### VHOSTGEN_HTTPD_SERVER # 'nginx', 'apache22' or 'apache24'
###
### Can be any of 'nginx', 'apache22' or 'apache24'
###
# VHOSTGEN_HTTPD_SERVER is set via Dockerfile
VHOSTGEN_HTTPD_SERVER_TEMPLATE="${VHOSTGEN_HTTPD_SERVER}.yml"
###
### Base path for main (default) document root
###
MAIN_DOCROOT_BASE="/var/www/default"
MASS_DOCROOT_BASE="/shared/httpd"
###
### OpenSSL Certificate Authority file to generate
###
### If the /ca directory is mounted and those files already exist
### a new ca will not be generated, but reused.
###
CA_KEY_FILE=/ca/devilbox-ca.key
CA_CRT_FILE=/ca/devilbox-ca.crt
###
### Path to scripts to source
###
ENTRYPOINT_DIR="/docker-entrypoint.d" # All entrypoint scripts
VHOSTGEN_TEMPLATE_DIR="/etc/vhost-gen/templates" # vhost-gen default templates
VHOSTGEN_CUST_TEMPLATE_DIR="/etc/vhost-gen.d" # vhost-gen custom templates (must be mounted to add)
###
### Defailt aliases copied from previous images, just for the record
###
#MAIN_VHOST_ALIASES_ALLOW='/devilbox-api/:/var/www/default/api, /vhost.d/:/etc/httpd'
#MASS_VHOST_ALIASES_ALLOW='/devilbox-api/:/var/www/default/api:http(s)?://(.*)$'
###
### Wait this many seconds to start watcherd after httpd has been started
###
WATCHERD_STARTUP_DELAY="3"
###################################################################################################
###################################################################################################
###
### INCLUDES
###
###################################################################################################
###################################################################################################
###
### Bootstrap
###
# shellcheck disable=SC1090,SC1091
. "${ENTRYPOINT_DIR}/bootstrap/bootstrap.sh"
###
### Source available entrypoint scripts
###
# shellcheck disable=SC2012
for f in $( ls -1 "${ENTRYPOINT_DIR}/"*.sh | sort -u ); do
# shellcheck disable=SC1090
. "${f}"
done
###################################################################################################
###################################################################################################
###
### MAIN ENTRYPOINT
###
###################################################################################################
###################################################################################################
# -------------------------------------------------------------------------------------------------
# SET ENVIRONMENT VARIABLES AND DEFAULT VALUES
# -------------------------------------------------------------------------------------------------
###
### Show Debug level
###
log "info" "Entrypoint debug: $( env_get "DEBUG_ENTRYPOINT" )"
log "info" "Runtime debug: $( env_get "DEBUG_RUNTIME" )"
###
### Show environment vars
###
log "info" "-------------------------------------------------------------------------"
log "info" "Environment Variables (set/default)"
log "info" "-------------------------------------------------------------------------"
log "info" "Variables: General:"
env_var_export "NEW_UID"
env_var_export "NEW_GID"
env_var_export "TIMEZONE" "UTC"
log "info" "Variables: Main Vhost:"
env_var_export "MAIN_VHOST_ENABLE" "1"
env_var_export "MAIN_VHOST_DOCROOT_DIR" "htdocs"
env_var_export "MAIN_VHOST_TEMPLATE_DIR" "cfg"
env_var_export "MAIN_VHOST_ALIASES_ALLOW" ""
env_var_export "MAIN_VHOST_ALIASES_DENY" '/\.git, /\.ht.*'
env_var_export "MAIN_VHOST_BACKEND"
env_var_export "MAIN_VHOST_BACKEND_TIMEOUT" "180"
env_var_export "MAIN_VHOST_SSL_TYPE" "plain"
env_var_export "MAIN_VHOST_SSL_CN" "localhost"
env_var_export "MAIN_VHOST_STATUS_ENABLE" "0"
env_var_export "MAIN_VHOST_STATUS_ALIAS" "/httpd-status"
log "info" "Variables: Mass Vhost:"
env_var_export "MASS_VHOST_ENABLE" "0"
env_var_export "MASS_VHOST_DOCROOT_DIR" "htdocs"
env_var_export "MASS_VHOST_TEMPLATE_DIR" "cfg"
env_var_export "MASS_VHOST_ALIASES_ALLOW" ""
env_var_export "MASS_VHOST_ALIASES_DENY" '/\.git, /\.ht.*'
env_var_export "MASS_VHOST_BACKEND"
env_var_export "MASS_VHOST_BACKEND_REWRITE"
env_var_export "MASS_VHOST_BACKEND_TIMEOUT" "180"
env_var_export "MASS_VHOST_SSL_TYPE" "plain"
env_var_export "MASS_VHOST_TLD_SUFFIX" ".loc"
log "info" "Variables: Misc:"
if [ "${VHOSTGEN_HTTPD_SERVER}" = "nginx" ]; then
env_var_export "WORKER_CONNECTIONS" "1024"
env_var_export "WORKER_PROCESSES" "auto"
fi
# Apache 2.2 does not have HTTP/2 support
if [ "${VHOSTGEN_HTTPD_SERVER}" != "apache22" ]; then
env_var_export "HTTP2_ENABLE" "1"
else
export HTTP2_ENABLE=0
fi
env_var_export "DOCKER_LOGS" "1"
# -------------------------------------------------------------------------------------------------
# VERIFY ENVIRONMENT VARIABLES
# -------------------------------------------------------------------------------------------------
log "info" "-------------------------------------------------------------------------"
log "info" "Validate Settings"
log "info" "-------------------------------------------------------------------------"
log "info" "Settings: General:"
env_var_validate "NEW_UID"
env_var_validate "NEW_GID"
env_var_validate "TIMEZONE"
log "info" "Settings: Main Vhost:"
env_var_validate "MAIN_VHOST_ENABLE"
env_var_validate "MAIN_VHOST_DOCROOT_DIR"
env_var_validate "MAIN_VHOST_TEMPLATE_DIR"
env_var_validate "MAIN_VHOST_ALIASES_ALLOW"
env_var_validate "MAIN_VHOST_ALIASES_DENY"
env_var_validate "MAIN_VHOST_BACKEND"
env_var_validate "MAIN_VHOST_BACKEND_TIMEOUT"
env_var_validate "MAIN_VHOST_SSL_TYPE"
env_var_validate "MAIN_VHOST_SSL_CN"
env_var_validate "MAIN_VHOST_STATUS_ENABLE"
env_var_validate "MAIN_VHOST_STATUS_ALIAS"
log "info" "Settings: Mass Vhost:"
env_var_validate "MASS_VHOST_ENABLE"
env_var_validate "MASS_VHOST_DOCROOT_DIR"
env_var_validate "MASS_VHOST_TEMPLATE_DIR"
env_var_validate "MASS_VHOST_ALIASES_ALLOW"
env_var_validate "MASS_VHOST_ALIASES_DENY"
env_var_validate "MASS_VHOST_BACKEND"
env_var_validate "MASS_VHOST_BACKEND_REWRITE"
env_var_validate "MASS_VHOST_BACKEND_TIMEOUT"
env_var_validate "MASS_VHOST_SSL_TYPE"
env_var_validate "MASS_VHOST_TLD_SUFFIX"
log "info" "Settings: Misc:"
if [ "${VHOSTGEN_HTTPD_SERVER}" = "nginx" ]; then
env_var_validate "WORKER_CONNECTIONS"
env_var_validate "WORKER_PROCESSES"
fi
# Apache 2.2 does not have HTTP/2 support
if [ "${VHOSTGEN_HTTPD_SERVER}" != "apache22" ]; then
env_var_validate "HTTP2_ENABLE"
fi
env_var_validate "DOCKER_LOGS"
# -------------------------------------------------------------------------------------------------
# APPLY SETTINGS
# -------------------------------------------------------------------------------------------------
log "info" "-------------------------------------------------------------------------"
log "info" "Apply Settings"
log "info" "-------------------------------------------------------------------------"
###
### Change uid/gid
###
set_uid "${NEW_UID}" "${MY_USER}"
set_gid "${NEW_GID}" "${MY_USER}" "${MY_GROUP}"
###
### Set timezone
###
set_timezone "${TIMEZONE}"
###
### Copy custom user-mounted vhost-gen template (if they are mounted and exist)
###
vhostgen_copy_custom_template \
"${VHOSTGEN_CUST_TEMPLATE_DIR}" \
"${VHOSTGEN_TEMPLATE_DIR}" \
"${VHOSTGEN_HTTPD_SERVER_TEMPLATE}"
###
### Generate vhost-gen config file (MAIN_VHOST)
###
vhostgen_main_generate_config \
"${VHOSTGEN_HTTPD_SERVER}" \
"${MAIN_VHOST_BACKEND}" \
"${HTTP2_ENABLE}" \
"${MAIN_VHOST_ALIASES_ALLOW}" \
"${MAIN_VHOST_ALIASES_DENY}" \
"${MAIN_VHOST_STATUS_ENABLE}" \
"${MAIN_VHOST_STATUS_ALIAS}" \
"${DOCKER_LOGS}" \
"${MAIN_VHOST_BACKEND_TIMEOUT}" \
"/etc/vhost-gen/main.yml"
###
### Generate vhost (MAIN_VHOST)
###
if [ "${VHOSTGEN_HTTPD_SERVER}" = "nginx" ]; then
# Adding custom nginx vhost template to ensure paths like:
# /vendor/index.php/arg1/arg2 will also work (just like Apache)
# https://www.reddit.com/r/nginx/comments/a6pw31/phpfpm_does_not_handle_subpathindexphparg1arg2/
vhostgen_main_generate \
"${MAIN_VHOST_ENABLE}" \
"${MAIN_DOCROOT_BASE}/${MAIN_VHOST_DOCROOT_DIR}" \
"${MAIN_VHOST_BACKEND}" \
"/etc/vhost-gen/main.yml" \
"${MAIN_DOCROOT_BASE}/${MAIN_VHOST_TEMPLATE_DIR}" \
"${MAIN_VHOST_SSL_TYPE}" \
"/etc/vhost-gen/templates-main/"
else
vhostgen_main_generate \
"${MAIN_VHOST_ENABLE}" \
"${MAIN_DOCROOT_BASE}/${MAIN_VHOST_DOCROOT_DIR}" \
"${MAIN_VHOST_BACKEND}" \
"/etc/vhost-gen/main.yml" \
"${MAIN_DOCROOT_BASE}/${MAIN_VHOST_TEMPLATE_DIR}" \
"${MAIN_VHOST_SSL_TYPE}"
fi
###
### Create Certificate Signing request
###
cert_gen_generate_ca "${CA_KEY_FILE}" "${CA_CRT_FILE}"
###
### Generate main vhost ssl certificate (MAIN_VHOST)
###
# shellcheck disable=SC2153
cert_gen_generate_cert \
"${MAIN_VHOST_ENABLE}" \
"${MAIN_VHOST_SSL_TYPE}" \
"${CA_KEY_FILE}" \
"${CA_CRT_FILE}" \
"/etc/httpd/cert/main/localhost.key" \
"/etc/httpd/cert/main/localhost.csr" \
"/etc/httpd/cert/main/localhost.crt" \
"${MAIN_VHOST_SSL_CN}"
###
### Fix CA directory/file permissions (in case it is mounted)
###
fix_perm "/ca" "1"
# -------------------------------------------------------------------------------------------------
# NGINX-SPECIFIC BASIC SETTINGS
# -------------------------------------------------------------------------------------------------
###
### Nginx settings
###
if [ "${VHOSTGEN_HTTPD_SERVER}" = "nginx" ]; then
nginx_set_worker_processess "${WORKER_PROCESSES}"
nginx_set_worker_connections "${WORKER_CONNECTIONS}"
fi
# -------------------------------------------------------------------------------------------------
# MAIN ENTRYPOINT
# -------------------------------------------------------------------------------------------------
# shellcheck disable=SC2153
_HTTPD_VERSION="$( eval "${HTTPD_VERSION}" || true )" # Set via Dockerfile
_SUPVD_VERSION="$( supervisord -v )"
log "info" "-------------------------------------------------------------------------"
log "info" "Main Entrypoint"
log "info" "-------------------------------------------------------------------------"
###
### MASS_VHOST requires supervisor to run (watcherd)
###
if [ "${MASS_VHOST_ENABLE}" -eq "1" ]; then
# Create watcherd sub commands
watcherd_add="create-vhost.sh"
watcherd_add+=" \\\"%%n\\\"" # vhost project directory name
watcherd_add+=" \\\"%%p\\\"" # vhost project directory path (absolute)
watcherd_add+=" \\\"${MASS_VHOST_DOCROOT_DIR}\\\""
watcherd_add+=" \\\"${MASS_VHOST_TLD_SUFFIX}\\\""
watcherd_add+=" \\\"${MASS_VHOST_ALIASES_ALLOW}\\\""
watcherd_add+=" \\\"${MASS_VHOST_ALIASES_DENY}\\\""
watcherd_add+=" \\\"${MASS_VHOST_SSL_TYPE}\\\""
watcherd_add+=" \\\"${MASS_VHOST_BACKEND}\\\""
watcherd_add+=" \\\"${MASS_VHOST_BACKEND_REWRITE}\\\""
watcherd_add+=" \\\"${MASS_VHOST_BACKEND_TIMEOUT}\\\""
watcherd_add+=" \\\"${HTTP2_ENABLE}\\\""
watcherd_add+=" \\\"${DOCKER_LOGS}\\\""
watcherd_add+=" \\\"${CA_KEY_FILE}\\\""
watcherd_add+=" \\\"${CA_CRT_FILE}\\\""
watcherd_add+=" \\\"%%p/${MASS_VHOST_TEMPLATE_DIR}/\\\""
watcherd_add+=" \\\"${VHOSTGEN_HTTPD_SERVER}\\\""
watcherd_del="rm /etc/httpd/vhost.d/%%n.conf"
watcherd_tri="${HTTPD_RELOAD}"
supervisord_create \
"${HTTPD_START}" \
"bash -c 'sleep ${WATCHERD_STARTUP_DELAY} && exec watcherd -e \"^\\.\" -c -v -p ${MASS_DOCROOT_BASE} -a \"${watcherd_add}\" -d \"${watcherd_del}\" -t \"${watcherd_tri}\"'" \
"/etc/supervisord.conf"
log "done" "Starting supervisord: ${_SUPVD_VERSION} [HTTPD: ${_HTTPD_VERSION}]"
exec /usr/bin/supervisord -c /etc/supervisord.conf
###
### No MASS_VHOST: just start HTTPD as process 1.
###
else
log "done" "Starting webserver: ${_HTTPD_VERSION}"
exec "${HTTPD_START}"
fi