Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add/nginx config install testing #2605

Merged
merged 10 commits into from
May 11, 2022
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ permalink: /docs/en-US/changelog/

### Enhancements

* Improved provisioning output
* VVV will now attempt to test Nginx configs on installation and recover ( #2604 )

### Bug Fixes

* WP CLI package update failures now fail gracefully instead of stopping a provision ( #2601 )
* Fixed an edge case updating NVM via git ( #2604 )

## 3.9.1 ( 2022 April 13th )

Expand Down
17 changes: 6 additions & 11 deletions provision/core/env/provision.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,52 +67,45 @@ function cleanup_terminal_splash() {
# @description Sets up the VVV users bash profile, and configuration files
# @noargs
function profile_setup() {
vvv_info " * Setting ownership of files in /home/vagrant to vagrant"
chown -R vagrant:vagrant /home/vagrant/

# Copy custom dotfiles and bin file for the vagrant user from local
vvv_info " * Copying /srv/provision/core/env/homedir/.bash_profile to /home/vagrant/.bash_profile"
rm -f "/home/vagrant/.bash_profile"
noroot cp -f "/srv/provision/core/env/homedir/.bash_profile" "/home/vagrant/.bash_profile"

vvv_info " * Copying /srv/provision/core/env/homedir/.bash_aliases to /home/vagrant/.bash_aliases"
rm -f "/home/vagrant/.bash_aliases"
noroot cp -f "/srv/provision/core/env/homedir/.bash_aliases" "/home/vagrant/.bash_aliases"

vvv_info " * Copying /srv/provision/core/env/homedir/.bash_aliases to ${HOME}/.bash_aliases"
rm -f "${HOME}/.bash_aliases"
cp -f "/srv/provision/core/env/homedir/.bash_aliases" "${HOME}/.bash_aliases"

vvv_info " * Copying /srv/provision/core/env/homedir/.vimrc to /home/vagrant/.vimrc"
rm -f "/home/vagrant/.vimrc"
noroot cp -f "/srv/provision/core/env/homedir/.vimrc" "/home/vagrant/.vimrc"

if [[ ! -d "/home/vagrant/.subversion" ]]; then
noroot mkdir -p "/home/vagrant/.subversion"
fi

vvv_info " * Copying /srv/provision/core/env/homedir/.subversion/subversion-servers to /home/vagrant/.subversion/servers"
rm -f /home/vagrant/.subversion/servers
noroot cp "/srv/provision/core/env/homedir/.subversion/subversion-servers" "/home/vagrant/.subversion/servers"

vvv_info " * Copying /srv/provision/core/env/homedir/.subversion/subversion-config to /home/vagrant/.subversion/config"
rm -f /home/vagrant/.subversion/config
noroot cp "/srv/provision/core/env/homedir/.subversion/subversion-config" "/home/vagrant/.subversion/config"

# If a bash_prompt file exists in the VVV config/ directory, copy to the VM.
if [[ -f "/srv/config/bash_prompt" ]]; then
vvv_info " * Copying /srv/config/bash_prompt to /home/vagrant/.bash_prompt"
rm -f /home/vagrant/.bash_prompt
noroot cp "/srv/config/bash_prompt" "/home/vagrant/.bash_prompt"
fi

if [ -d "/etc/ssh" ]; then
vvv_info " * Copying /srv/provision/core/env/ssh/ssh_known_hosts to /etc/ssh/ssh_known_hosts"
cp -f /srv/provision/core/env/ssh/ssh_known_hosts /etc/ssh/ssh_known_hosts
vvv_info " * Copying /srv/provision/core/env/ssh/sshd_config to /etc/ssh/sshd_config"
cp -f /srv/provision/core/env/ssh/sshd_config /etc/ssh/sshd_config
vvv_info " * Reloading SSH Daemon"
service ssh reload
if ! sudo service ssh reload; then
vvv_error " ! SSH daemon failed to reload"
return 1
fi
fi
}

Expand All @@ -126,4 +119,6 @@ function vvv_init_profile() {
profile_setup
}

export -f vvv_init_profile;

vvv_add_hook init vvv_init_profile 0
13 changes: 7 additions & 6 deletions provision/core/node-nvm/provision.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ set -eo pipefail

export NVM_DIR="/home/vagrant/.nvm"

function nvm_setup() {
function vvv_nvm_setup() {
vvv_info " * Checking for NVM"

if [[ -d "${NVM_DIR}" && -f "${NVM_DIR}/nvm.sh" ]]
Expand All @@ -35,11 +35,11 @@ function nvm_setup() {
vvv_info " - Installing NVM via git"
noroot git clone https://github.com/nvm-sh/nvm.git "${NVM_DIR}"
cd "${NVM_DIR}"
noroot git checkout $(git describe --abbrev=0 --tags --match "v[0-9]*" $(git rev-list --tags --max-count=1))
noroot git checkout $(noroot git describe --abbrev=0 --tags --match "v[0-9]*" $(noroot git rev-list --tags --max-count=1))
cd -
vvv_info " - Loading nvm"
[ -s "${NVM_DIR}/nvm.sh" ] && . "${NVM_DIR}/nvm.sh"
vvv_info " - nvm loaded"
vvv_info " - NVM loaded"

vvv_success " ✓ NVM installed"

Expand All @@ -55,13 +55,14 @@ function nvm_setup() {

vvv_info " - Installing Node 14 via nvm"
nvm install 14

nvm use 14

vvv_info " - Ensuring vagrant user owns its own nvm folder"
chown -R vagrant:vagrant /home/vagrant/.nvm/

vvv_success " - NVM setup completed"
vvv_success " NVM setup completed"
}

vvv_add_hook after_packages nvm_setup
export -f vvv_nvm_setup;

vvv_add_hook after_packages vvv_nvm_setup
4 changes: 2 additions & 2 deletions provision/core/wp-cli/provision.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ function wp_cli_setup() {
if [ "${VVV_DOCKER}" != 1 ]; then
vvv_info " * [WP-CLI]: Updating packages"
if noroot wp package update; then
vvv_info " * [WP-CLI]: Package updates completed"
vvv_info " [WP-CLI]: Package updates completed"
else
vvv_warn " ! [WP-CLI]: Package update did not complete, wp package update exited with a bad error code ${?}"
fi
fi

vvv_success " * [WP-CLI]: WP CLI setup completed"
vvv_success " [WP-CLI]: WP CLI setup completed"
}
export -f wp_cli_setup

Expand Down
42 changes: 40 additions & 2 deletions provision/provision-helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ vvv_package_install() {

return 0
}
export -f vvv_package_install
export -f vvv_package_install;

# @description checks if an apt package is installed, returns 0 if installed, 1 if not
# @arg $1 string the package to check for
Expand Down Expand Up @@ -587,4 +587,42 @@ vvv_apt_package_remove() {

return 0
}
export -f vvv_apt_package_remove
export -f vvv_apt_package_remove;

# @description Installs an Nginx config file and reload Nginx.
# If Nginx fails to load after doing this it will print an
# error and attempt to undo the change.
#
# @arg $1 string the path and filename of the nginx config that needs to be installed
# @arg $2 string the file name of the config when installed
# @arg $3 the type of config, valid values as sites and utilities
#
# @example
# vvv_maybe_install_nginx_config /tmp/nginx-site-config.conf vvv-site-mysite.conf sites
function vvv_maybe_install_nginx_config() {
SOURCE_FILE="${1}"
TARGET_NAME="${2}"
TARGET="${3}"
TARGET_DIR="/etc/nginx/custom-${3}/"
TARGET_FILE="${TARGET_DIR}${TARGET_NAME}"
if [ -f "${TARGET_FILE}" ]; then
sudo rm -f "${TARGET_FILE}"
fi

sudo mkdir -p "${TARGET_DIR}"

sudo cp -f "${SOURCE_FILE}" "${TARGET_FILE}"

if ! sudo nginx -t; then
vvv_error " ! Installing an Nginx config failed! VVV tried to install ${TARGET_NAME} into ${TARGET} from ${SOURCE_FILE} but a syntax test with sudo nginx -t failed!"
vvv_error " ! VVV is now deleting the config to avoid further breakage"
sudo rm -f "${TARGET_FILE}"
return 1
fi

sudo service nginx reload

return 0
}

export -f vvv_maybe_install_nginx_config;
47 changes: 28 additions & 19 deletions provision/provision-site.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,41 +84,53 @@ function get_primary_host() {
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)
local 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="$(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}"
local DIR="$(dirname "${SITE_NGINX_FILE}")"
sed "s#{vvv_path_to_folder}#${DIR}#" "${SITE_NGINX_FILE}" > "${TMPFILE}"
sed -i "s#{vvv_path_to_site}#${VM_DIR}#" "${TMPFILE}"
sed -i "s#{vvv_site_name}#${SITE_NAME}#" "${TMPFILE}"
sed -i "s#{vvv_hosts}#${VVV_HOSTS}#" "${TMPFILE}"

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
sed -i "s#{upstream}#${NGINX_UPSTREAM}#" "/etc/nginx/custom-sites/${DEST_NGINX_FILE}"
sed -i "s#{upstream}#${NGINX_UPSTREAM}#" "${TMPFILE}"

if [ -f "/srv/certificates/${SITE_NAME}/dev.crt" ]; 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}"
sed -i "s#{vvv_tls_cert}#ssl_certificate \"/srv/certificates/${SITE_NAME}/dev.crt\";#" "${TMPFILE}"
sed -i "s#{vvv_tls_key}#ssl_certificate_key \"/srv/certificates/${SITE_NAME}/dev.key\";#" "${TMPFILE}"
else
sed -i "s#{vvv_tls_cert}#\# TLS cert not included as the certificate file is not present#" "/etc/nginx/custom-sites/${DEST_NGINX_FILE}"
sed -i "s#{vvv_tls_key}#\# TLS key not included as the certificate file is not present#" "/etc/nginx/custom-sites/${DEST_NGINX_FILE}"
sed -i "s#{vvv_tls_cert}#\# TLS cert not included as the certificate file is not present#" "${TMPFILE}"
sed -i "s#{vvv_tls_key}#\# TLS key not included as the certificate file is not present#" "${TMPFILE}"
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}"
while grep -sqE '/[^/][^/]*/\.\.' "${TMPFILE}"; do
sed -i 's#/[^/][^/]*/\.\.##g' "${TMPFILE}"
done

# "/etc/nginx/custom-sites/${DEST_NGINX_FILE}"
local DEST_NGINX_FILE=${SITE_NGINX_FILE//\/srv\/www\//}
local DEST_NGINX_FILE=${DEST_NGINX_FILE//\//\-}
local DEST_NGINX_FILE=${DEST_NGINX_FILE//-provision/} # remove the provision folder name
local DEST_NGINX_FILE=${DEST_NGINX_FILE//-.vvv/} # remove the .vvv folder name
local DEST_NGINX_FILE=${DEST_NGINX_FILE/%-vvv-nginx.conf/}
local DEST_NGINX_FILE="vvv-${DEST_NGINX_FILE}-$(md5sum <<< "${SITE_NGINX_FILE}" | cut -c1-8).conf"

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
rm -f "${TMPFILE}"
}

# @description add hosts from a file to VVVs hosts file (the guest, not the host machine)
Expand Down Expand Up @@ -496,9 +508,6 @@ vvv_provision_site_script
vvv_custom_folders
vvv_provision_site_nginx

vvv_info " * Reloading Nginx"
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
Expand Down