@@ -4,38 +4,215 @@ set -e
4
4
set -u
5
5
set -o pipefail
6
6
7
- VHOST_PATH=" ${1} "
8
- VHOST_NAME=" ${2} "
9
- VHOST_TLD=" ${3} "
10
- VHOST_TPL=" ${4} "
11
- CA_KEY=" ${5} "
12
- CA_CRT=" ${6} "
13
- GENERATE_SSL=" ${7} "
14
- GEN_MODE=" ${8} "
15
- VERBOSE=" ${9:- } "
16
-
17
- if [ " ${GENERATE_SSL} " = " 1" ]; then
7
+
8
+ # ##
9
+ # ## Inputs (watcherd will call this script)
10
+ # ##
11
+ VHOST_NAME=" ${1} " # vhost project directory name (via watcherd: "%n")
12
+ VHOST_PATH=" ${2} " # vhost project directory path (via watcherd: "%p")
13
+ VHOST_DOCROOT_NAME=" ${3} " # Document root subdir inside VHOST_PATH
14
+ VHOST_TLD_SUFFIX=" ${4} " # TLD_SUFFIX to append to VHOST_NAME
15
+ VHOST_ALIASES_ALLOW=" ${5} " # Additional allow aliases to generate (path:, url: cors:)
16
+ VHOST_ALIASES_DENY=" ${6} " # Additional deny aliases to generate
17
+ VHOST_SSL_TYPE=" ${7} " # SSL_TYPE: "plain", "ssl", "both", "redir"
18
+ VHOST_BACKEND=" ${8} " # Backend string: file:* or cfg:*
19
+ VHOST_BACKEND_REWRITE=" ${9} " # Backend Rewrite string: file:*
20
+ VHOST_BACKEND_TIMEOUT=" ${10} " # Timeout for backend in seconds
21
+ HTTP2_ENABLE=" ${11} " # Enable HTTP2?
22
+ DOCKER_LOGS=" ${12} " # Enable Docker logs?
23
+ CA_KEY_FILE=" ${13} " # Path to CA key file
24
+ CA_CRT_FILE=" ${14} " # Path to CA crt file
25
+ VHOSTGEN_TEMPLATE_DIR=" ${15} " # vhost-gen template dir (via watcherd: "%p/${MASS_VHOST_TPL_DIR}")
26
+ VHOSTGEN_HTTPD_SERVER=" ${16} " # nginx, apache22 or apache24 (determines the template to choose)
27
+
28
+
29
+
30
+ # -------------------------------------------------------------------------------------------------
31
+ # BOOTSTRAP
32
+ # -------------------------------------------------------------------------------------------------
33
+
34
+ # ##
35
+ # ## Bootstrap (Debug level and source .lib/ and .httpd/ functions)
36
+ # ##
37
+ # shellcheck disable=SC1090,SC1091
38
+ . " /docker-entrypoint.d/bootstrap/bootstrap.sh"
39
+
40
+
41
+
42
+ # -------------------------------------------------------------------------------------------------
43
+ # GENERATE SSL CERTIFICATES?
44
+ # -------------------------------------------------------------------------------------------------
45
+
46
+ # ##
47
+ # ## Generate vhost SSL certificate
48
+ # ##
49
+ if [ " ${VHOST_SSL_TYPE} " != " plain" ]; then
18
50
if [ ! -d " /etc/httpd/cert/mass" ]; then
19
- mkdir -p " /etc/httpd/cert/mass"
51
+ runtime " mkdir -p /etc/httpd/cert/mass"
20
52
fi
21
- _email=" admin@${VHOST_NAME}${VHOST_TLD} "
22
- _domain=" ${VHOST_NAME}${VHOST_TLD} "
23
- _domains=" *.${VHOST_NAME}${VHOST_TLD} "
24
- _out_key=" /etc/httpd/cert/mass/${VHOST_NAME}${VHOST_TLD} .key"
25
- _out_csr=" /etc/httpd/cert/mass/${VHOST_NAME}${VHOST_TLD} .csr"
26
- _out_crt=" /etc/httpd/cert/mass/${VHOST_NAME}${VHOST_TLD} .crt"
27
- if ! cert-gen -v -c DE -s Berlin -l Berlin -o Devilbox -u Devilbox -n " ${_domain} " -e " ${_email} " -a " ${_domains} " " ${CA_KEY} " " ${CA_CRT} " " ${_out_key} " " ${_out_csr} " " ${_out_crt} " ; then
28
- echo " [FAILED] Failed to add SSL certificate for ${VHOST_NAME}${VHOST_TLD} "
53
+ _email=" admin@${VHOST_NAME}${VHOST_TLD_SUFFIX} "
54
+ _domain=" ${VHOST_NAME}${VHOST_TLD_SUFFIX} "
55
+ _domains=" *.${VHOST_NAME}${VHOST_TLD_SUFFIX} "
56
+ _out_key=" /etc/httpd/cert/mass/${VHOST_NAME}${VHOST_TLD_SUFFIX} .key"
57
+ _out_csr=" /etc/httpd/cert/mass/${VHOST_NAME}${VHOST_TLD_SUFFIX} .csr"
58
+ _out_crt=" /etc/httpd/cert/mass/${VHOST_NAME}${VHOST_TLD_SUFFIX} .crt"
59
+ if ! runtime \
60
+ " cert-gen -v -c DE -s Berlin -l Berlin -o Devilbox -u Devilbox -n \" ${_domain} \" -e \" ${_email} \" -a \" ${_domains} \" \" ${CA_KEY_FILE} \" \" ${CA_CRT_FILE} \" \" ${_out_key} \" \" ${_out_csr} \" \" ${_out_crt} \" " \
61
+ " Failed to add SSL certificate for ${VHOST_NAME}${VHOST_TLD_SUFFIX} " ; then
29
62
exit 1
30
63
fi
31
64
fi
32
65
33
- cmd=" vhost-gen -p \" ${VHOST_PATH} \" -n \" ${VHOST_NAME} \" -c /etc/vhost-gen/mass.yml -o \" ${VHOST_TPL} \" -s ${VERBOSE} -m ${GEN_MODE} "
34
- if [ -n " ${VERBOSE} " ]; then
35
- echo " \$ ${cmd} "
66
+
67
+
68
+ # -------------------------------------------------------------------------------------------------
69
+ # BACKEND string
70
+ # -------------------------------------------------------------------------------------------------
71
+
72
+ # ##
73
+ # ## Validate Backend
74
+ # ##
75
+ if [ -n " ${VHOST_BACKEND} " ]; then
76
+ # ##
77
+ # ## Check if BACKEND_REWRITE is set
78
+ # ##
79
+ if [ -n " ${VHOST_BACKEND_REWRITE} " ]; then
80
+ # No need to validate backend string, has been done already in entrypoint
81
+ BACKEND_REWRITE_FILE_NAME=" $( echo " ${VHOST_BACKEND_REWRITE} " | awk -F' :' ' {print $2}' ) "
82
+ BACKEND_REWRITE_FILE_PATH=" ${VHOSTGEN_TEMPLATE_DIR}${BACKEND_REWRITE_FILE_NAME} "
83
+
84
+ # Backend file exists
85
+ if [ -f " ${BACKEND_REWRITE_FILE_PATH} " ]; then
86
+ BACKEND_REWRITE_CONFIG=" $( cat " ${BACKEND_REWRITE_FILE_PATH} " ) "
87
+ log " info" " [${VHOST_NAME}${VHOST_TLD_SUFFIX} ] Backend rewrite found: ${BACKEND_REWRITE_FILE_PATH} "
88
+ log " info" " [${VHOST_NAME}${VHOST_TLD_SUFFIX} ] Backend rewrite config: ${BACKEND_REWRITE_CONFIG} "
89
+ # Rewrite config is invalid
90
+ if ! BACKEND_REWRITE_ERROR=" $( backend_conf_is_valid " ${BACKEND_REWRITE_CONFIG} " ) " ; then
91
+ log " warn" " [${VHOST_NAME}${VHOST_TLD_SUFFIX} ] Backend rewrite config is invalid: ${BACKEND_REWRITE_ERROR} "
92
+ log " warn" " [${VHOST_NAME}${VHOST_TLD_SUFFIX} ] Backend rewrite: skipping"
93
+ else
94
+ # Apply the overwrite
95
+ log " info" " [${VHOST_NAME}${VHOST_TLD_SUFFIX} ] Overwriting MASS_VHOST_BACKEND config"
96
+ VHOST_BACKEND=" ${BACKEND_REWRITE_CONFIG} " # Use config from file
97
+ fi
98
+ fi
99
+ fi
100
+
101
+ # ##
102
+ # ## BACKEND=file:<file>
103
+ # ##
104
+ if echo " ${VHOST_BACKEND} " | grep -E ' ^file:' > /dev/null; then
105
+ # No need to validate backend string, has been done already in entrypoint
106
+ BACKEND_FILE_NAME=" $( echo " ${VHOST_BACKEND} " | awk -F' :' ' {print $2}' ) "
107
+ BACKEND_FILE_PATH=" ${VHOSTGEN_TEMPLATE_DIR}${BACKEND_FILE_NAME} "
108
+ log " info" " [${VHOST_NAME}${VHOST_TLD_SUFFIX} ] Backend config specified via file: ${VHOSTGEN_TEMPLATE_DIR}${BACKEND_FILE_NAME} "
109
+
110
+ # [1/2] Backend file does not exist
111
+ if [ ! -f " ${BACKEND_FILE_PATH} " ]; then
112
+ log " info" " [${VHOST_NAME}${VHOST_TLD_SUFFIX} ] Backend file does not exist: ${VHOSTGEN_TEMPLATE_DIR}${BACKEND_FILE_NAME} "
113
+ log " info" " [${VHOST_NAME}${VHOST_TLD_SUFFIX} ] Backend defaulting to: serve static files only"
114
+ VHOST_BACKEND=" " # Empty the backend
115
+
116
+ # [2/2] Backend exists (need to validate it)
117
+ else
118
+ BACKEND_CONFIG=" $( cat " ${BACKEND_FILE_PATH} " ) "
119
+ log " info" " [${VHOST_NAME}${VHOST_TLD_SUFFIX} ] Backend config file contents: ${BACKEND_CONFIG} "
120
+ if ! BACKEND_ERROR=" $( backend_conf_is_valid " ${BACKEND_CONFIG} " ) " ; then
121
+ log " warn" " [${VHOST_NAME}${VHOST_TLD_SUFFIX} ] Backend config is invalid: ${BACKEND_ERROR} "
122
+ log " warn" " [${VHOST_NAME}${VHOST_TLD_SUFFIX} ] Backend defaulting to: serve static files only"
123
+ VHOST_BACKEND=" " # Empty the backend
124
+ else
125
+ VHOST_BACKEND=" ${BACKEND_CONFIG} " # Use config from file
126
+ fi
127
+ fi
128
+ # ##
129
+ # ## Backend=conf:<type>:<proto>:<host>:<port>
130
+ # ##
131
+ else
132
+ log " info" " [${VHOST_NAME}${VHOST_TLD_SUFFIX} ] Backend config specified via env: ${VHOST_BACKEND} "
133
+ # No need to validate backend string, has been done already in entrypoint
134
+ fi
135
+ else
136
+ log " info" " [${VHOST_NAME}${VHOST_TLD_SUFFIX} ] No Backend specified: Serving static files only"
137
+ fi
138
+
139
+
140
+ # ##
141
+ # ## Evaluate Backend
142
+ # ##
143
+ be_type=" "
144
+ be_prot=" "
145
+ be_host=" "
146
+ be_port=" "
147
+ if [ -n " ${VHOST_BACKEND} " ]; then
148
+ be_type=" $( get_backend_conf_type " ${VHOST_BACKEND} " ) " # phpfpm or rproxy
149
+ be_prot=" $( get_backend_conf_prot " ${VHOST_BACKEND} " ) " # tpc, http, https
150
+ be_host=" $( get_backend_conf_host " ${VHOST_BACKEND} " ) " # <host>
151
+ be_port=" $( get_backend_conf_port " ${VHOST_BACKEND} " ) " # <port>
152
+ if [ " ${be_type} " = " phpfpm" ]; then
153
+ log " info" " [${VHOST_NAME}${VHOST_TLD_SUFFIX} ] Backend PHP-FPM Remote: ${be_prot} ://${be_host} :${be_port} "
154
+ elif [ " ${be_type} " = " rproxy" ]; then
155
+ log " info" " [${VHOST_NAME}${VHOST_TLD_SUFFIX} ] Backend Reverse Proxy: ${be_prot} ://${be_host} :${be_port} "
156
+ fi
157
+ fi
158
+
159
+ INDICES=" index.html, index.htm"
160
+ PHP_FPM_ENABLE=0
161
+ if [ " ${be_type} " = " phpfpm" ]; then
162
+ INDICES=" index.php, index.html, index.htm"
163
+ PHP_FPM_ENABLE=1
36
164
fi
37
165
38
- if ! eval " ${cmd} " ; then
39
- echo " [FAILED] Failed to add vhost for ${VHOST_NAME}${VHOST_TLD} "
40
- exit 1
166
+
167
+
168
+ # -------------------------------------------------------------------------------------------------
169
+ # VHOSTGEN
170
+ # -------------------------------------------------------------------------------------------------
171
+
172
+ VHOSTGEN_CONFIG_NAME=" mass-${VHOST_NAME} .yml"
173
+ VHOSTGEN_CONFIG_PATH=" /etc/vhost-gen/${VHOSTGEN_CONFIG_NAME} "
174
+
175
+ # ##
176
+ # ## Generate vhost-gen config file (not template)
177
+ # ##
178
+ VHOSTGEN_TEMPLATE=" $( \
179
+ generate_vhostgen_conf \
180
+ " ${VHOSTGEN_HTTPD_SERVER} " \
181
+ " /etc/httpd/vhost.d" \
182
+ " ${VHOST_TLD_SUFFIX} " \
183
+ " ${VHOST_DOCROOT_NAME} " \
184
+ " ${INDICES} " \
185
+ " $( to_python_bool " ${HTTP2_ENABLE} " ) " \
186
+ " /etc/httpd/cert/mass" \
187
+ " /etc/httpd/cert/mass" \
188
+ " " \
189
+ " $( to_python_bool " ${DOCKER_LOGS} " ) " \
190
+ " $( to_python_bool " ${PHP_FPM_ENABLE} " ) " \
191
+ " ${be_host} " \
192
+ " ${be_port} " \
193
+ " ${VHOST_BACKEND_TIMEOUT} " \
194
+ " ${VHOST_ALIASES_ALLOW} " \
195
+ " ${VHOST_ALIASES_DENY} " \
196
+ " no" \
197
+ " /httpd-status" \
198
+ ) "
199
+ echo " ${VHOSTGEN_TEMPLATE} " > " ${VHOSTGEN_CONFIG_PATH} "
200
+ log " trace" " ${VHOSTGEN_TEMPLATE} "
201
+
202
+ # ##
203
+ # ## Execute vhost-gen command
204
+ # ##
205
+ if [ " ${be_type} " = " rproxy" ]; then
206
+ if ! runtime \
207
+ " vhost-gen -v -r \" ${be_prot} ://${be_host} :${be_port} \" -l / -n \" ${VHOST_NAME} \" -c \" ${VHOSTGEN_CONFIG_PATH} \" -o \" ${VHOSTGEN_TEMPLATE_DIR} \" -s -m ${VHOST_SSL_TYPE} " \
208
+ " Failed to add vhost for ${VHOST_NAME}${VHOST_TLD_SUFFIX} " ; then
209
+ exit 1
210
+ fi
211
+ else
212
+ if ! runtime \
213
+ " vhost-gen -v -p \" ${VHOST_PATH} \" -n \" ${VHOST_NAME} \" -c \" ${VHOSTGEN_CONFIG_PATH} \" -o \" ${VHOSTGEN_TEMPLATE_DIR} \" -s -m ${VHOST_SSL_TYPE} " \
214
+ " Failed to add vhost for ${VHOST_NAME}${VHOST_TLD_SUFFIX} " ; then
215
+ exit 1
216
+ fi
41
217
fi
218
+ log " trace" " $( grep -v ' ^[[:blank:]]*$' " /etc/httpd/vhost.d/${VHOST_NAME} .conf" ) "
0 commit comments