-
Notifications
You must be signed in to change notification settings - Fork 848
/
Copy pathprovision-site.sh
649 lines (585 loc) · 24.9 KB
/
provision-site.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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#!/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%/}" # No trailing strings
SKIP_PROVISIONING=$5
NGINX_UPSTREAM=$6
VVV_PATH_TO_SITE=${VM_DIR} # used in site templates
VVV_SITE_NAME=${SITE}
VVV_HOSTS=""
SUCCESS=0
DEFAULTPHP="8.2"
VVV_CONFIG=/vagrant/config.yml
. "/srv/provision/provisioners.sh"
# @description Retrieves a config value for the given site as specified in `config.yml`
#
# @arg $1 string the config value to fetch
# @arg $2 string the default value
function vvv_get_site_config_value() {
local value
value=$(shyaml -q get-value "sites.${SITE_ESCAPED}.${1}" "${2}" < ${VVV_CONFIG})
echo "${value}"
}
function vvv_get_site_php_version() {
SITE_PHP=$(vvv_get_site_config_value 'php' "${DEFAULTPHP}")
# remove whitespace
SITE_PHP=$(echo -n "${SITE_PHP}" | xargs | tr -d '\n' | tr -d '\r')
# Handle when php:8 instead of 8.0 or if it's parsed as a number
if [[ ${#SITE_PHP} -eq 1 ]]; then
SITE_PHP="${SITE_PHP}.0"
fi
echo -n "${SITE_PHP}"
}
vvv_validate_site_php_version() {
local SITE_PHP
SITE_PHP=$(vvv_get_site_php_version)
if [[ ${#SITE_PHP} -gt 3 ]]; then
vvv_warn " ! Warning: PHP version defined is using a wrong format: '${SITE_PHP}' with length '${#SITE_PHP}'"
vvv_warn " If you are trying to use a more specific version of PHP such as 7.4.1 or 7.4.0 you"
vvv_warn " need to be less specific and use 7.4"
vvv_warn " ! https://varyingvagrantvagrants.org/docs/en-US/adding-a-new-site/changing-php-version/"
fi
if [[ ! -e "/usr/bin/php${SITE_PHP}" ]]; then
vvv_warn " ! Warning: The chosen PHP version doesn't exist in this environment: '${SITE_PHP}' looking for '/usr/bin/php${SITE_PHP}'"
vvv_warn " ! Did you forget to install it via config/config.yml? Add it to the extensions section as documented in the"
vvv_warn " ! changing PHP versions age on the VVV site, and re-provision:"
vvv_warn " ! https://varyingvagrantvagrants.org/docs/en-US/adding-a-new-site/changing-php-version/"
fi
}
# @description sets a sites PHP version as the global version, or to the VVV default if none is specified
#
# @internal
# @noargs
function vvv_apply_site_php_cli_version() {
vvv_validate_site_php_version
SITE_PHP=$(vvv_get_site_php_version)
echo " * Setting the default PHP CLI version ( ${SITE_PHP} ) for this site"
update-alternatives --set php "/usr/bin/php${SITE_PHP}" &> /dev/null
update-alternatives --set phar "/usr/bin/phar${SITE_PHP}" &> /dev/null
update-alternatives --set phar.phar "/usr/bin/phar.phar${SITE_PHP}" &> /dev/null
update-alternatives --set phpize "/usr/bin/phpize${SITE_PHP}" &> /dev/null
update-alternatives --set php-config "/usr/bin/php-config${SITE_PHP}" &> /dev/null
}
# @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
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
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
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\//}
DEST_NGINX_FILE=${DEST_NGINX_FILE//\//\-}
DEST_NGINX_FILE=${DEST_NGINX_FILE/%-vvv-nginx.conf/}
DEST_NGINX_FILE="vvv-auto-${DEST_NGINX_FILE}-$(md5sum <<< "${SITE_NGINX_FILE}" | cut -c1-32).conf"
VVV_HOSTS=$(get_hosts)
local TMPFILE
TMPFILE=$(mktemp /tmp/vvv-site-XXXXX)
cat "${SITE_NGINX_FILE}" >> "${TMPFILE}"
vvv_info " * VVV is adding an Nginx config from ${SITE_NGINX_FILE}"
# 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
DIR="$(dirname "${SITE_NGINX_FILE}")"
local NCONFIG
NCONFIG=$(vvv_search_replace_in_file "${SITE_NGINX_FILE}" "{vvv_path_to_folder}" "${DIR}")
NCONFIG=$(vvv_search_replace "${NCONFIG}" "{vvv_path_to_site}" "${VM_DIR}")
NCONFIG=$(vvv_search_replace "${NCONFIG}" "{vvv_site_name}" "${SITE_NAME}")
NCONFIG=$(vvv_search_replace "${NCONFIG}" "{vvv_hosts}" "${VVV_HOSTS}")
# if php: is configured, set the upstream to match
SITE_PHP=$(vvv_get_site_php_version)
if [ "${DEFAULTPHP}" != "${SITE_PHP}" ]; then
NGINX_UPSTREAM="php${SITE_PHP}"
NGINX_UPSTREAM=$(echo "$NGINX_UPSTREAM" | tr --delete .)
fi
# check if the nginx upstream value has been set and is valid
if [ 'php' != "${NGINX_UPSTREAM}" ] && [ ! -f "/etc/nginx/upstreams/${NGINX_UPSTREAM}.conf" ]; then
vvv_error " * Upstream value '${NGINX_UPSTREAM}' doesn't match a valid upstream. Defaulting to 'php'.${CRESET}"
NGINX_UPSTREAM='php'
fi
NCONFIG=$(vvv_search_replace "${NCONFIG}" "{upstream}" "${NGINX_UPSTREAM}")
if [ -f "/srv/certificates/${SITE_NAME}/dev.crt" ]; then
NCONFIG=$(vvv_search_replace "${NCONFIG}" "{vvv_tls_cert}" "ssl_certificate \"/srv/certificates/${SITE_NAME}/dev.crt\";")
NCONFIG=$(vvv_search_replace "${NCONFIG}" "{vvv_tls_key}" "ssl_certificate_key \"/srv/certificates/${SITE_NAME}/dev.key\";")
else
NCONFIG=$(vvv_search_replace "${NCONFIG}" "{vvv_tls_cert}" "# TLS cert not included as the certificate file is not present")
NCONFIG=$(vvv_search_replace "${NCONFIG}" "{vvv_tls_key}" "# TLS key not included as the certificate file is not present")
fi
# Resolve relative paths since not supported in Nginx root.
while [[ $NCONFIG =~ /[^/][^/]*/\.\. ]]; do
NCONFIG=${NCONFIG//\/[^\/][^\/]*\/\.\./}
done
# "/etc/nginx/custom-sites/${DEST_NGINX_FILE}"
local DEST_NGINX_FILE
DEST_NGINX_FILE=${SITE_NGINX_FILE//\/srv\/www\//}
DEST_NGINX_FILE=${DEST_NGINX_FILE//\//\-}
DEST_NGINX_FILE=${DEST_NGINX_FILE//-srv-provision-core-nginx-config-/\-}
DEST_NGINX_FILE=${DEST_NGINX_FILE//-provision/} # remove the provision folder name
DEST_NGINX_FILE=${DEST_NGINX_FILE//-.vvv/} # remove the .vvv folder name
#local DEST_NGINX_FILE=${DEST_NGINX_FILE//\-\-/\-}
DEST_NGINX_FILE=${DEST_NGINX_FILE/%-vvv-nginx.conf/}
DEST_NGINX_FILE="vvv-${DEST_NGINX_FILE}-$(md5sum <<< "${SITE_NGINX_FILE}" | cut -c1-8).conf"
echo "${NCONFIG}" > "${TMPFILE}"
if ! vvv_maybe_install_nginx_config "${TMPFILE}" "${DEST_NGINX_FILE}" "sites"; then
vvv_warn " ! This sites nginx config had problems, it may not load. Look at the above errors to diagnose the problem"
vvv_info " ! VVV will now continue with provisioning so that other sites have an opportunity to run"
fi
vvv_success " * Installed ${DEST_NGINX_FILE}"
rm -f "${TMPFILE}"
}
# @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 -r 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 "::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
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
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 "::1 ${line} # vvv-auto" >> "/etc/hosts"
echo " - Added ${line} from ${VVV_CONFIG}"
fi
done
fi
}
# @description Clones the site template and creates the initial folder.
#
# @internal
# @noargs
function vvv_provision_fresh_site_template_git_repo() {
vvv_info " * Downloading ${SITE} provisioner, git cloning from ${REPO} into ${VM_DIR}"
if noroot git clone --recursive --branch "${BRANCH}" "${REPO}" "${VM_DIR}"; then
vvv_success " * ${SITE} provisioner 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
}
# @description Fetches remote git updates for a site template and does a hard reset.
#
# @internal
# @noargs
function vvv_provision_fetch_reset_site_template_git_repo() {
cd "${VM_DIR}"
echo " * Checking that site provisioner remote origin is still: ${REPO}"
local CURRENTORIGIN
CURRENTORIGIN=$(noroot git remote get-url origin)
if [[ "${CURRENTORIGIN}" != "${REPO}" ]]; then
vvv_error " ! The site config said to use <b>${REPO}</b>"
vvv_error " ! But the origin remote is actually <b>${CURRENTORIGIN}</b>"
vvv_error " ! Remove the unknown origin remote and re-add it."
vvv_error ""
vvv_error " ! You can do this by running these commands inside the VM:"
vvv_error " "
vvv_error " cd ${VM_DIR}"
vvv_error " git remote remove origin"
vvv_error " git remote add origin ${REPO}"
vvv_error " exit"
vvv_error " "
vvv_error " ! You can get inside the VM using <b>vagrant ssh</b>"
vvv_error " "
SUCCESS=1
return 1
fi
echo " * Updating ${SITE} provisioner repo in ${VM_DIR} (${REPO}, ${BRANCH})"
echo " * Any local changes not present on the server will be discarded in favor of the remote branch"
local SKIP_GIT_UPDATE
SKIP_GIT_UPDATE=$(vvv_get_site_config_value "skip_site_provisioner_update" "test")
if [[ $SKIP_GIT_UPDATE == "True" ]]; then
vvv_warn " ! Did not pull down latest site provisioner, config file indicates it should be skipped."
return
fi
echo " * Fetching origin ${BRANCH}"
noroot git fetch origin "${BRANCH}"
echo " * performing a hard reset on origin/${BRANCH}"
noroot git reset "origin/${BRANCH}" --hard
echo " * Updating provisioner repo complete"
}
# @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
vvv_info " * Pulling down the ${BRANCH} branch of ${REPO}"
if [[ -d "${VM_DIR}" ]] && [[ ! -z "$(ls -A "${VM_DIR}")" ]]; then
if [[ -d "${VM_DIR}/.git" ]]; then
vvv_provision_fetch_reset_site_template_git_repo
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_provision_fresh_site_template_git_repo
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}"
if source "${1}"; then
vvv_info " * sourcing of ${1} reported success"
return 0
else
vvv_error " ! sourcing of ${1} reported failure with an error code of ${?}"
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.sh provision/vvv-init.sh or vvv-init.sh, searching 3 folders down, please be patient..."
local SITE_INIT_SCRIPTS
SITE_INIT_SCRIPTS=$(find "${VM_DIR}" -maxdepth 3 -name 'vvv-init.sh');
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
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 " * VVV searched and did not find an Nginx config in these locations:"
vvv_warn " - ${VM_DIR}/.vvv/vvv-nginx.conf"
vvv_warn " - ${VM_DIR}/provision/vvv-nginx.conf"
vvv_warn " - ${VM_DIR}/vvv-nginx.conf"
vvv_warn " * VVV will search 3 folders down to find an Nginx config, please be patient..."
local NGINX_CONFIGS
NGINX_CONFIGS=$(find "${VM_DIR}" -maxdepth 3 -name 'vvv-nginx.conf');
if [[ -z $NGINX_CONFIGS ]] ; then
vvv_warn " * VVV did not found an Nginx config file, it will use a fallback config file."
noroot mkdir -p "${VM_DIR}/log"
vvv_provision_site_nginx_config "${SITE}" "/srv/provision/core/nginx/config/site-fallback.conf"
else
vvv_warn " * VVV found Nginx config files in subfolders, move these files to the expected locations to avoid these warnings."
for SITE_CONFIG_FILE in $NGINX_CONFIGS; do
vvv_provision_site_nginx_config "${SITE}" "${SITE_CONFIG_FILE}"
done
fi
fi
}
# @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 composer option for a site as specified in `config.yml`
#
# @arg $1 string the folder name to process specified in `config.yml`
function vvv_custom_folder_composer() {
local folder="${1}"
if keys=$(shyaml keys -y -q "sites.${SITE_ESCAPED}.folders.${folder}.composer" < "${VVV_CONFIG}"); then
for key in $keys; do
cd "${folder}"
local value
value=$(vvv_get_site_config_value "folders.${folder}.composer.${key}" "")
if [[ "install" == "${key}" ]]; then
if [[ "True" == "${value}" ]]; then
vvv_info " * Running composer install in ${folder}"
noroot composer install
fi
elif [[ "update" == "${key}" ]]; then
if [[ "True" == "${value}" ]]; then
vvv_info " * Running composer update in ${folder}"
noroot composer update
fi
elif [[ "create-project" == "${key}" ]]; then
vvv_info " * Running composer create-project ${value} in ${folder}"
noroot composer create-project "${value}" .
else
vvv_warn " * Unknown key in Composer section: <b>${key}</b><warn> for </warn><b>${folder}</b>"
fi
cd -
done
fi
}
# @description Processes a folder sections npm option for a site as specified in `config.yml`
#
# @arg $1 string the folder name to process specified in `config.yml`
function vvv_custom_folder_npm() {
local folder="${1}"
if keys=$(shyaml keys -y -q "sites.${SITE_ESCAPED}.folders.${folder}.npm" < "${VVV_CONFIG}"); then
for key in $keys; do
cd "${folder}"
local value
value=$(vvv_get_site_config_value "folders.${folder}.npm.${key}" "")
if [[ "install" == "${key}" ]]; then
if [[ "True" == "${value}" ]]; then
vvv_info " * Running npm install in ${folder}"
noroot npm install
fi
elif [[ "update" == "${key}" ]]; then
if [[ "True" == "${value}" ]]; then
vvv_info " * Running npm update in ${folder}"
noroot npm update
fi
elif [[ "run" == "${key}" ]]; then
vvv_info " * Running npm run ${value} in ${folder}"
noroot npm run "${value}"
else
vvv_warn " * Unknown key in NPM section: <b>${key}</b><warn> for </warn><b>${folder}</b>"
fi
cd -
done
fi
}
# @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
local repo
local overwrite_on_clone
local hard_reset
local pull
folder="${1}"
repo=$(vvv_get_site_config_value "folders.${folder}.git.repo" "?")
overwrite_on_clone=$(vvv_get_site_config_value "folders.${folder}.git.overwrite_on_clone" "False")
hard_reset=$(vvv_get_site_config_value "folders.${folder}.git.hard_reset" "False")
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
if keys=$(shyaml keys -y -q "sites.${SITE_ESCAPED}.folders.${folder}" < "${VVV_CONFIG}"); then
for key in $keys; do
if [[ "${key}" == "git" ]]; then
vvv_custom_folder_git "${folder}"
elif [[ "${key}" == "composer" ]]; then
vvv_custom_folder_composer "${folder}"
elif [[ "${key}" == "npm" ]]; then
vvv_custom_folder_npm "${folder}"
else
vvv_warn " * Unknown folders sub-parameter <b>${key}<b><warn> ignoring"
fi
done
fi
fi
done
fi
}
# -------------------------------
source /srv/config/homebin/vvv_restore_php_default
vvv_apply_site_php_cli_version
if [[ true == "${SKIP_PROVISIONING}" ]]; then
vvv_warn " * Skipping provisioning of <b>${SITE}</b>"
exit 0
fi
# Ensure npm is available
if ! command -v nvm &> /dev/null; then
if [ -f /home/vagrant/.nvm/nvm.sh ]; then
source /home/vagrant/.nvm/nvm.sh
fi
fi
nvm use default
vvv_provision_site_repo
if [[ ! -d "${VM_DIR}" ]]; then
vvv_error " "
vvv_error " "
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>"
vvv_error " ! It is not enough to declare a site, if you do not specify a provisioner repo/site template then you have to create the folder and fill it yourself."
vvv_error " ! At a very minimum, VVV needs an Nginx config so it knows how to serve the website"
vvv_error " "
vvv_error " "
exit 1
fi
vvv_process_site_hosts
vvv_provision_site_script
vvv_custom_folders
vvv_provision_site_nginx
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
/srv/config/homebin/vvv_restore_php_default
provisioner_success