-
Notifications
You must be signed in to change notification settings - Fork 827
Expand file tree
/
Copy pathprovision-site.sh
More file actions
397 lines (364 loc) · 15.6 KB
/
Copy pathprovision-site.sh
File metadata and controls
397 lines (364 loc) · 15.6 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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env bash
# @file Core Site Provisioner
# @brief The main site provisioner script.
# @description A script executed as a provisioner that provisions a site. This includes cloning its provisioner template, installing and processing Nginx config files, running setup scripts, etc
# This script takes several arguments:
#
# - The name of the site in the config file
# - The git repository URI for the site provisioner template
# - The git branch to use
# - The location inside the guest to set up the site
# - Wether to skip provisioning for this site
# - The Nginx upstream to use
set -eo pipefail
SITE=$1
SITE_ESCAPED="${SITE//./\\.}"
REPO=$2
BRANCH=$3
VM_DIR=$4
SKIP_PROVISIONING=$5
NGINX_UPSTREAM=$6
VVV_PATH_TO_SITE=${VM_DIR} # used in site templates
VVV_SITE_NAME=${SITE}
VVV_HOSTS=""
SUCCESS=1
. "/srv/provision/provisioners.sh"
# @description Takes 2 values, a key to fetch a value for, and an optional default value
#
# @example
# echo $(get_config_value 'key' 'defaultvalue')
#
# @arg $1 string the name of the custom parameter
# @arg $2 string the default value
#
# @see vvv_get_site_config_value
function get_config_value() {
vvv_get_site_config_value "custom.${1}" "${2}"
}
# @description Retrieves a list of hosts for this site from the config file. Internally this relies on `shyaml get-values-0`
#
# @noargs
# @see get_hosts_list
# @stdout a space separated string of domains, defaulting to `sitename.test` if none are specified
function get_hosts() {
local value=$(shyaml -q get-values-0 "sites.${SITE_ESCAPED}.hosts" < ${VVV_CONFIG} | tr '\0' ' ' | sed 's/ *$//')
echo "${value:-"${VVV_SITE_NAME}.test"}"
}
# @description Retrieves a list of hosts for this site from the config file. Internally this relies on `shyaml get-values`
#
# @noargs
# @see get_hosts
# @stdout a space separated string of domains, defaulting to `sitename.test` if none are specified
function get_hosts_list() {
local value=$(shyaml -q get-values "sites.${SITE_ESCAPED}.hosts" < ${VVV_CONFIG})
echo "${value:-"${VVV_SITE_NAME}.test"}"
}
# @description Retrieves the first host listed for a site.
#
# @noargs
# @see get_hosts
# @see get_hosts_list
# @stdout the first host listed in the config file for this site, defaulting to `sitename.test` if none are specified
function get_primary_host() {
local value=$(shyaml -q get-value "sites.${SITE_ESCAPED}.hosts.0" "${1}" < ${VVV_CONFIG})
echo "${value:-"${VVV_SITE_NAME}.test"}"
}
# @description processes and installs an Nginx config for a site.
# The function performs variable substitutions, and installs the resulting config.
# This includes inserting SSL key references, host names, and paths.
#
# @arg $1 string the name of the site
# @arg $2 string the Nginx config file to process and install
# @internal
function vvv_provision_site_nginx_config() {
local SITE_NAME=$1
local SITE_NGINX_FILE=$2
local DEST_NGINX_FILE=${SITE_NGINX_FILE//\/srv\/www\//}
local DEST_NGINX_FILE=${DEST_NGINX_FILE//\//\-}
local DEST_NGINX_FILE=${DEST_NGINX_FILE/%-vvv-nginx.conf/}
local DEST_NGINX_FILE="vvv-auto-${DEST_NGINX_FILE}-$(md5sum <<< "${SITE_NGINX_FILE}" | cut -c1-32).conf"
VVV_HOSTS=$(get_hosts)
# We allow the replacement of the {vvv_path_to_folder} token with
# whatever you want, allowing flexible placement of the site folder
# while still having an Nginx config which works.
local DIR="$(dirname "$SITE_NGINX_FILE")"
sed "s#{vvv_path_to_folder}#${DIR}#" "$SITE_NGINX_FILE" > "/etc/nginx/custom-sites/${DEST_NGINX_FILE}"
sed -i "s#{vvv_path_to_site}#${VM_DIR}#" "/etc/nginx/custom-sites/${DEST_NGINX_FILE}"
sed -i "s#{vvv_site_name}#${SITE_NAME}#" "/etc/nginx/custom-sites/${DEST_NGINX_FILE}"
sed -i "s#{vvv_hosts}#${VVV_HOSTS}#" "/etc/nginx/custom-sites/${DEST_NGINX_FILE}"
if [ 'php' != "${NGINX_UPSTREAM}" ] && [ ! -f "/etc/nginx/upstreams/${NGINX_UPSTREAM}.conf" ]; then
vvv_warn " * Upstream value '${NGINX_UPSTREAM}' doesn't match a valid upstream. Defaulting to 'php'.${CRESET}"
NGINX_UPSTREAM='php'
fi
sed -i "s#{upstream}#${NGINX_UPSTREAM}#" "/etc/nginx/custom-sites/${DEST_NGINX_FILE}"
if /srv/config/homebin/is_utility_installed core tls-ca; then
sed -i "s#{vvv_tls_cert}#ssl_certificate /srv/certificates/${SITE_NAME}/dev.crt;#" "/etc/nginx/custom-sites/${DEST_NGINX_FILE}"
sed -i "s#{vvv_tls_key}#ssl_certificate_key /srv/certificates/${SITE_NAME}/dev.key;#" "/etc/nginx/custom-sites/${DEST_NGINX_FILE}"
else
sed -i "s#{vvv_tls_cert}#\# TLS cert not included as the core tls-ca is not installed#" "/etc/nginx/custom-sites/${DEST_NGINX_FILE}"
sed -i "s#{vvv_tls_key}#\# TLS key not included as the core tls-ca is not installed#" "/etc/nginx/custom-sites/${DEST_NGINX_FILE}"
fi
# Resolve relative paths since not supported in Nginx root.
while grep -sqE '/[^/][^/]*/\.\.' "/etc/nginx/custom-sites/${DEST_NGINX_FILE}"; do
sed -i 's#/[^/][^/]*/\.\.##g' "/etc/nginx/custom-sites/${DEST_NGINX_FILE}"
done
}
# @description add hosts from a file to VVVs hosts file (the guest, not the host machine)
#
# @arg $1 string a vvv-hosts file to process
# @internal
function vvv_provision_hosts_file() {
local HOSTFILE=$1
while read HOSTFILE; do
while IFS='' read -r line || [ -n "$line" ]; do
if [[ "#" != ${line:0:1} ]]; then
if [[ -z "$(grep -q "^127.0.0.1 ${line}$" /etc/hosts)" ]]; then
echo "127.0.0.1 $line # vvv-auto" >> "/etc/hosts"
echo " - Added ${line} from ${HOSTFILE}"
fi
fi
done < "$HOSTFILE"
done
}
# @description Parse any `vvv-hosts` files located in the site repository for domains to
# be added to the virtual machine's host file so that it is self aware.
#
# @internal
# @noargs
function vvv_process_site_hosts() {
echo " * Adding domains to the virtual machine's /etc/hosts file..."
local hosts=$(get_hosts_list)
if [ ${#hosts[@]} -eq 0 ]; then
echo " * No hosts were found in the VVV config, falling back to vvv-hosts"
if [[ -f "${VM_DIR}/.vvv/vvv-hosts" ]]; then
vvv_success " * Found a .vvv/vvv-hosts file"
vvv_provision_hosts_file "${SITE}" "${VM_DIR}/.vvv/vvv-hosts"
elif [[ -f "${VM_DIR}/provision/vvv-hosts" ]]; then
vvv_success " * Found a provision/vvv-hosts file"
vvv_provision_hosts_file "${SITE}" "${VM_DIR}/provision/vvv-hosts"
elif [[ -f "${VM_DIR}/vvv-hosts" ]]; then
vvv_success " * Found a vvv-hosts file"
vvv_provision_hosts_file "${SITE}" "${VM_DIR}/vvv-hosts"
else
echo " * Searching subfolders 4 levels down for a vvv-hosts file ( this can be skipped by using ./vvv-hosts, .vvv/vvv-hosts, or provision/vvv-hosts"
local HOST_FILES=$(find "${VM_DIR}" -maxdepth 4 -name 'vvv-hosts');
if [[ -z $HOST_FILES ]] ; then
vvv_error " ! Warning: No vvv-hosts file was found, and no hosts were defined in the vvv config, this site may be inaccessible"
else
for HOST_FILE in $HOST_FILES; do
vvv_provision_hosts_file "$HOST_FILE"
done
fi
fi
else
echo " * Adding hosts for the site to the VM hosts file"
for line in $hosts; do
if [[ -z "$(grep -q "^127.0.0.1 $line$" /etc/hosts)" ]]; then
echo "127.0.0.1 ${line} # vvv-auto" >> "/etc/hosts"
echo " - Added ${line} from ${VVV_CONFIG}"
fi
done
fi
}
# @description Clones a site provisioner repository or git repo as specified in the repo: field of a site
#
# @internal
# @noargs
function vvv_provision_site_repo() {
if [[ false != "${REPO}" ]]; then
if [[ -d "${VM_DIR}" ]] && [[ ! -z "$(ls -A "${VM_DIR}")" ]]; then
if [[ -d "${VM_DIR}/.git" ]]; then
echo " * Updating ${SITE} in ${VM_DIR}..."
cd "${VM_DIR}"
noroot git reset "origin/${BRANCH}" --hard -q
noroot git pull origin "${BRANCH}" -q
noroot git checkout "${BRANCH}" -q
else
vvv_error " ! Problem! A site folder for ${SITE} was found at ${VM_DIR} that doesn't use a site template, but a site template is defined in the config file. Either the config file is mistaken, or a previous attempt to provision has failed, VVV will not try to git clone the site template to avoid data destruction, either remove the folder, or fix the config/config.yml entry${CRESET}"
fi
else
# Clone or pull the site repository
vvv_info " * Downloading ${SITE}, git cloning from ${REPO} into ${VM_DIR}"
noroot git clone --recursive --branch "${BRANCH}" "${REPO}" "${VM_DIR}" -q
if [ $? -eq 0 ]; then
vvv_success " * ${SITE} Site Template clone successful"
else
vvv_error " ! Git failed to clone the site template for ${SITE}. It tried to clone the ${BRANCH} of ${REPO} into ${VM_DIR}${CRESET}"
vvv_error " ! VVV won't be able to provision ${SITE} without the template. Check that you have permission to access the repo, and that the filesystem is writable${CRESET}"
exit 1
fi
fi
else
vvv_info " * The site: '${SITE}' does not have a site template, assuming custom provision/vvv-init.sh and provision/vvv-nginx.conf"
if [[ ! -d "${VM_DIR}" ]]; then
vvv_error " ! Error: The '${SITE}' has no folder, VVV does not create the folder for you, or set up the Nginx configs. Use a site template or create the folder and provisioner files, then reprovision VVV"
exit 1
fi
fi
}
# @description Runs a site provisioner script to install and configure a site automatically.
#
# @arg $1 string the provisioner to run
# @arg $2 string the folder containing the provisioner to runn
# @internal
function vvv_run_site_template_script() {
echo " * Found ${1} at ${2}/${1}"
( cd "${2}" && source "${1}" )
if [ $? -eq 0 ]; then
return 0
else
return 1
fi
}
# @description Searches for and executes a site provisioner setup script
#
# @internal
# @noargs
function vvv_provision_site_script() {
# Look for site setup scripts
echo " * Searching for a site template provisioner, vvv-init.sh"
if [[ -f "${VM_DIR}/.vvv/vvv-init.sh" ]]; then
vvv_run_site_template_script "vvv-init.sh" "${VM_DIR}/.vvv"
SUCCESS=$?
elif [[ -f "${VM_DIR}/provision/vvv-init.sh" ]]; then
vvv_run_site_template_script "vvv-init.sh" "${VM_DIR}/provision"
SUCCESS=$?
elif [[ -f "${VM_DIR}/vvv-init.sh" ]]; then
vvv_run_site_template_script "vvv-init.sh" "${VM_DIR}"
SUCCESS=$?
else
vvv_warn " * Warning: A site provisioner was not found at .vvv/vvv-init.conf provision/vvv-init.conf or vvv-init.conf, searching 3 folders down, please be patient..."
local SITE_INIT_SCRIPTS=$(find "${VM_DIR}" -maxdepth 3 -name 'vvv-init.conf');
if [[ -z $SITE_INIT_SCRIPTS ]] ; then
vvv_warn " * Warning: No site provisioner was found, VVV could not perform any scripted setup that might install software for this site"
else
for SITE_INIT_SCRIPT in $SITE_INIT_SCRIPTS; do
local DIR="$(dirname "$SITE_INIT_SCRIPT")"
vvv_run_site_template_script "vvv-init.sh" "${DIR}"
done
fi
fi
}
# @description Searches for and installs a site Nginx configuration.
#
# @internal
# @noargs
function vvv_provision_site_nginx() {
# Look for Nginx vhost files, symlink them into the custom sites dir
if [[ -f "${VM_DIR}/.vvv/vvv-nginx.conf" ]]; then
vvv_provision_site_nginx_config "${SITE}" "${VM_DIR}/.vvv/vvv-nginx.conf"
elif [[ -f "${VM_DIR}/provision/vvv-nginx.conf" ]]; then
vvv_provision_site_nginx_config "${SITE}" "${VM_DIR}/provision/vvv-nginx.conf"
elif [[ -f "${VM_DIR}/vvv-nginx.conf" ]]; then
vvv_provision_site_nginx_config "${SITE}" "${VM_DIR}/vvv-nginx.conf"
else
vvv_warn " * Warning: An nginx config was not found at .vvv/vvv-nginx.conf provision/vvv-nginx.conf or vvv-nginx.conf, searching 3 folders down, please be patient..."
local NGINX_CONFIGS=$(find "${VM_DIR}" -maxdepth 3 -name 'vvv-nginx.conf');
if [[ -z $NGINX_CONFIGS ]] ; then
vvv_error " ! Error: No nginx config was found, VVV will not know how to serve this site"
exit 1
else
for SITE_CONFIG_FILE in $NGINX_CONFIGS; do
vvv_provision_site_nginx_config "${SITE}" "${SITE_CONFIG_FILE}"
done
fi
fi
}
# @description Retrieves a config value for the given site as specified in `config.yml`
#
# @arg $1 string
# @arg $2 string
function vvv_get_site_config_value() {
local value=$(shyaml -q get-value "sites.${SITE_ESCAPED}.${1}" "${2}" < ${VVV_CONFIG})
echo "${value}"
}
# @description Clones a git repository into a sites sub-folder
#
# @arg $1 string the git repository URL to clone
# @arg $2 string the folder to clone into
# @internal
# @see vvv_custom_folder_git
function vvv_clone_site_git_folder() {
local repo="${1}"
local folder="${2}"
vvv_info " * git cloning <b>'${repo}'</b><info> into </info><b>'${VVV_PATH_TO_SITE}/${folder}'</b>"
noroot mkdir -p "${VVV_PATH_TO_SITE}/${folder}"
noroot git clone --recurse-submodules -j2 "${repo}" "${VVV_PATH_TO_SITE}/${folder}"
}
# @description Processes a folder sections git option for a site as specified in `config.yml`
#
# @arg $1 string the folder name to process specified in `config.yml`
# @internal
# @see vvv_clone_site_git_folder
function vvv_custom_folder_git() {
local folder="${1}"
local repo=$(vvv_get_site_config_value "folders.${folder}.git.repo" "?")
local overwrite_on_clone=$(vvv_get_site_config_value "folders.${folder}.git.overwrite_on_clone" "False")
local hard_reset=$(vvv_get_site_config_value "folders.${folder}.git.hard_reset" "False")
local pull=$(vvv_get_site_config_value "folders.${folder}.git.pull" "False")
if [ ! -d "${VVV_PATH_TO_SITE}/${folder}" ]; then
vvv_clone_site_git_folder "${repo}" "${folder}"
else
if [[ $overwrite_on_clone = "True" ]]; then
if [ ! -d "${VVV_PATH_TO_SITE}/${folder}/.git" ]; then
vvv_info " - VVV was asked to clone into a folder that already exists (${folder}), but does not contain a git repo"
vvv_info " - overwrite_on_clone is turned on so VVV will purge with extreme predjudice and clone over the folders grave"
rm -rf "${VVV_PATH_TO_SITE}/${folder}"
vvv_clone_site_git_folder "${repo}" "${folder}"
fi
else
vvv_warn " - Cannot clone into <b>'${folder}'</b><warn>, a folder that is not a git repo already exists. Set overwrite: true to force the folders deletion and a clone will take place"
fi
fi
if [[ $hard_reset = "True" ]]; then
vvv_info " - resetting git checkout and discarding changes in ${folder}"
cd "${VVV_PATH_TO_SITE}/${folder}"
noroot git reset --hard -q
noroot git checkout -q
cd -
fi
if [[ $pull = "True" ]]; then
vvv_info " - runnning git pull in ${folder}"
cd "${VVV_PATH_TO_SITE}/${folder}"
noroot git pull -q
cd -
fi
}
# @description Processes the folders option from the sites `config.yml`
#
# @internal
# @noargs
function vvv_custom_folders() {
if folders=$(shyaml keys -y -q "sites.${SITE_ESCAPED}.folders" < "${VVV_CONFIG}"); then
for folder in $folders
do
if [[ $folder != '...' ]]; then
local gitvcs=$(vvv_get_site_config_value "folders.${folder}.git" "False")
if [[ $gitvcs != "False" ]]; then
vvv_custom_folder_git "${folder}"
fi
fi
done
fi
}
# -------------------------------
if [[ true == "${SKIP_PROVISIONING}" ]]; then
vvv_warn " * Skipping provisioning of <b>${SITE}</b>"
exit 0
fi
vvv_provision_site_repo
if [[ ! -d "${VM_DIR}" ]]; then
vvv_error " ! Error: The <b>${VM_DIR}</b><error> folder does not exist, there is nothing to provision for the <b>'${SITE}'</b><error> site!</error>"
exit 1
fi
vvv_process_site_hosts
vvv_custom_folders
vvv_provision_site_script
vvv_provision_site_nginx
vvv_info " * Reloading Nginx config files"
service nginx reload
if [ "${SUCCESS}" -ne "0" ]; then
vvv_error " ! ${SITE} provisioning had some issues, check the log files as the site may not function correctly."
exit 1
fi
provisioner_success