From 61c93d04dbcc4c1fad3700868a687671831338ef Mon Sep 17 00:00:00 2001 From: Keegan Rankin Date: Tue, 5 Dec 2023 14:07:08 -0800 Subject: [PATCH] Run amanita to pull in templated files --- .cruft.json | 34 ++++++ .gitignore | 3 + drush/README.md | 1 + drush/drush.yml | 29 +++++ drush/sites/self.site.yml | 18 ++++ scripts/build.sh | 36 +++++++ scripts/css.sh | 10 ++ scripts/deploy.sh | 220 ++++++++++++++++++++++++++++++++++++++ scripts/example.template | 12 +++ scripts/nuke.sh | 36 +++++++ scripts/pull.sh | 24 +++-- scripts/show-vars.sh | 14 +++ scripts/sync_to_test.sh | 57 ++++++++++ scripts/update-hook.sh | 22 ++++ scripts/update.sh | 26 +++++ 15 files changed, 533 insertions(+), 9 deletions(-) create mode 100644 .cruft.json create mode 100644 drush/README.md create mode 100644 drush/drush.yml create mode 100644 drush/sites/self.site.yml create mode 100755 scripts/build.sh create mode 100755 scripts/css.sh create mode 100755 scripts/deploy.sh create mode 100644 scripts/example.template create mode 100755 scripts/nuke.sh create mode 100755 scripts/show-vars.sh create mode 100755 scripts/sync_to_test.sh create mode 100755 scripts/update-hook.sh create mode 100755 scripts/update.sh diff --git a/.cruft.json b/.cruft.json new file mode 100644 index 0000000..70ccf14 --- /dev/null +++ b/.cruft.json @@ -0,0 +1,34 @@ +{ + "template": "git@gitlab.com:agaric/python/amanita.git", + "commit": "8244742e86da5516002efbe79542c87b7576ea9b", + "skip": [ + ".gitlab-ci.yml", + "drush/sites/self.site.yml" + ], + "checkout": null, + "context": { + "cookiecutter": { + "project_name": "experienceolympic-com", + "project_slug": "experienceolympic-com", + "composer_source": "drutopia", + "hostname": "experienceolympic.com", + "live_uri": "experienceolympic-live.drutopia.org", + "test_uri": "experienceolympic-test.drutopia.org", + "user": "experienceolympic", + "test_user": "experienceolympic_test", + "live_user": "experienceolympic_live", + "sitedir": "site", + "webdir": "web", + "testuser_homedir": "/home/experienceolympic_test", + "liveuser_homedir": "/home/experienceolympic_live", + "testuser_sitedir": "/home/experienceolympic_test/site", + "liveuser_sitedir": "/home/experienceolympic_live/site", + "testuser_webdir": "/home/experienceolympic_test/site/web", + "liveuser_webdir": "/home/experienceolympic_live/site/web", + "theme_name": "theme", + "sync_method": "ssh", + "_template": "git@gitlab.com:agaric/python/amanita.git" + } + }, + "directory": "drupal_template" +} diff --git a/.gitignore b/.gitignore index 70e658f..9a2bf0e 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,6 @@ sites/simpletest # Ignore database backup directory. /backups/ + +# Ignore build directory. +/build/ diff --git a/drush/README.md b/drush/README.md new file mode 100644 index 0000000..8c85c14 --- /dev/null +++ b/drush/README.md @@ -0,0 +1 @@ +This directory contains commands, configuration and site aliases for Drush. See http://packages.drush.org/ for a directory of Drush commands installable via Composer. diff --git a/drush/drush.yml b/drush/drush.yml new file mode 100644 index 0000000..58cf098 --- /dev/null +++ b/drush/drush.yml @@ -0,0 +1,29 @@ +command: + sql: + dump: + options: + # Omit cache and similar tables (including during a sql:sync). + structure-tables-key: common + # Don't bother pulling migration tables at all. + skip-tables-key: common +sql: + # List of tables whose *data* is skipped by the 'sql-dump' and 'sql-sync' + # commands when the "--structure-tables-key=common" option is provided. + # You may add specific tables to the existing array or add a new element. + structure-tables: + common: + - cache + - 'cache_*' + - history + - 'search_*' + - 'sessions' + - 'watchdog' + # List of tables to be omitted entirely from SQL dumps made by the 'sql-dump' + # and 'sql-sync' commands when the "--skip-tables-key=common" option is + # provided on the command line. This is useful if your database contains + # non-Drupal tables used by some other application or during a migration for + # example. You may add new tables to the existing array or add a new element. + skip-tables: + common: + - 'migration_*' + diff --git a/drush/sites/self.site.yml b/drush/sites/self.site.yml new file mode 100644 index 0000000..6a62e9e --- /dev/null +++ b/drush/sites/self.site.yml @@ -0,0 +1,18 @@ +live: + host: experienceolympic.com + paths: + drush-script: /home/experienceolympic_live/site/vendor/bin/drush + env-vars: + PATH: /home/experienceolympic_live/bin:/usr/bin:/bin + root: /home/experienceolympic_live/site/web + uri: 'experienceolympic-live.drutopia.org' + user: experienceolympic_live +test: + host: experienceolympic.com + paths: + drush-script: /home/experienceolympic_test/site/vendor/bin/drush + env-vars: + PATH: /home/experienceolympic_test/bin:/usr/bin:/bin + root: /home/experienceolympic_test/site/web + uri: 'experienceolympic-test.drutopia.org' + user: experienceolympic_test diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100755 index 0000000..e14ccbe --- /dev/null +++ b/scripts/build.sh @@ -0,0 +1,36 @@ +#!/bin/bash +set -euo pipefail +# ~SCRIPT~ update.sh +# * Pass in a path to drush, or use a default: +# * Runs post-deploy commands to update Drupal + +# ~OPTIONAL~ +# COMPOSER_ARGS: +# Composer command and flags to run +# Defaults to "install --no-dev --no-scripts --optimize-autoload" + +# Load env vars, if supplied +[ $# -eq 1 ] && source "env-${1}" + +# Complain/stop if we don't have needed values +[ -z ${BUILD_ROOT:-} ] && { echo ">> Missing ENV var: BUILD_ROOT"; exit 1 ; } + +# Set defaults +[ -z ${COMPOSER_ARGS:- } ] && COMPOSER_ARGS="install --no-dev --no-scripts --optimize-autoloader" + +# Build dir has to exist first (if only for a moment)... +[ ! -d ${BUILD_ROOT} ] && { mkdir -p ${BUILD_ROOT} || true ; } + +# Normalize paths (e.g. "./build" => "/project/thisone/build)": +BUILD_ROOT="$(cd "${BUILD_ROOT}" && pwd)" +proj_dir="$(cd $(dirname ${0})/../ && pwd)" # Parent of this script folder. + +# Wipe out the build dir, so long as it is somewhere OTHER than the project root. +if [ "${BUILD_ROOT}" != "${proj_dir}" ] ; then + # wipe out existing target, and get latest code. + rm -rf ${BUILD_ROOT} + mkdir ${BUILD_ROOT} + git archive HEAD | tar -x -C ${BUILD_ROOT} + cd ${BUILD_ROOT} + composer ${COMPOSER_ARGS} +fi \ No newline at end of file diff --git a/scripts/css.sh b/scripts/css.sh new file mode 100755 index 0000000..5b9984b --- /dev/null +++ b/scripts/css.sh @@ -0,0 +1,10 @@ +#!/bin/bash +set -euo pipefail +# Confirmed with echo `pwd` that as long as we use via composer it's always in +# /var/www/html (aka the project root). +proj_dir="$(cd $(dirname ${0})/../ && pwd)" # Parent of this script folder. +[ "$proj_dir" != "/var/www/html" ] && { echo "Script running from unexpected path - are you running within ddev, as you should? Try ddev composer css"; exit 1; } +cd /var/www/html/web/themes/custom/theme +# Yarn CSS only +yarn node-sass -r ./src --output ./dist --include-path node_modules --include-path src --output-style compressed --quiet +yarn postcss ./dist/{**,.,*}.css --replace --verbose --env production diff --git a/scripts/deploy.sh b/scripts/deploy.sh new file mode 100755 index 0000000..bcb239e --- /dev/null +++ b/scripts/deploy.sh @@ -0,0 +1,220 @@ +#!/bin/bash +set -euo pipefail +# ~SCRIPT~ deploy.sh +# * Copies a build to the specified target and performs updates. +# * The build should already be prepared elsewhere. +# * Run from project root folder. +# * Will create database dumps (using drush) +# * By default, runs all related drush commands. + +####################################### +# Using deploy.sh +####################################### +# ~REQUIRED~ +# SSH_TARGET: +# Server to push code to/run drush commands on (host aliases work if configured in $HOME/.ssh/config) +# DEPLOY_ENV: +# A label for the environment, used to name backup files, and select settings file +# Defaults to ${CI_ENVIRONMENT_NAME}, if available, which is automatically set by gitlab-ci +# DEPLOY_ROOT: +# Directory name for composer file, containing vendor+config sub-folders + +# ~OPTIONAL~ +# DRUPAL_DIR: +# Defaults to web +# BACKUP_DIR: +# Defaults to ~/backups +# BUILD_ROOT: +# Defaults to ${PWD} +# DRUSH_DISABLE: +# Set to "1" to disable ALL drush commands. +# May be needed if an install failed or is not already present. +# REMOTE_DRUSH: +# Defaults to ${DEPLOY_ROOT}/vendor/bin/drush +# UPDATE_SCRIPT: +# The post-deploy update routine, specified from perspective of the project root. +# This should know how to find drush or can use $1 for it (see update.sh). +# Defaults to scripts/update.sh +# UPDATE_DISABLE: +# Set to "1" to disable only update commands (e.g. drush updb) +# Note: system will be left in maintenance mode! +# drush state:set system.maintenance_mode FALSE +# DEBUG: +# Set to "1" to call show-vars.sh +# WEB_EXCLUDES: +# --exclude .well-known --exclude sites/default/files --exclude sites/default/settings.*.php" + +####################################### +# Load env vars, if supplied +####################################### +[ $# -eq 1 ] && source "env-${1}" + +####################################### +# DEFAULTS +####################################### +_backup_dir="~/backups" +_config_dir="config" # Folder under deploy root with config +_remote_drush="${DEPLOY_ROOT:-}/vendor/bin/drush" # Full remote path to drush used by ssh +_update_script="scripts/update.sh" +_web_dir="web" # Under deploy root, the Drupal public web directory +_web_excludes="--exclude .well-known --exclude sites/default/files --exclude sites/default/settings.*.php" +# Note: `_php_settings_dir` cannot be included here; Default directly in optionals block + +####################################### +# Check for required variables: +####################################### +[ -z "${DEPLOY_ENV:-}" ] && DEPLOY_ENV="${CI_ENVIRONMENT_NAME:-}" +[ -z "${DEPLOY_ENV:-}" ] && { echo 'Missing ENV var: CI_ENVIRONMENT_NAME/DEPLOY_ENV'; exit 1; } +[ -z "${DEPLOY_ROOT:-}" ] && { echo "Missing ENV var: DEPLOY_ROOT"; exit 1; } +[ -z "${SSH_TARGET:-}" ] && { echo "Missing ENV var: SSH_TARGET"; exit 1; } + +####################################### +# Optional variable setup from defaults: +####################################### +[ -z "${BACKUP_DIR:-}" ] && BACKUP_DIR="${_backup_dir}" +[ -z "${BUILD_ROOT:-}" ] && BUILD_ROOT="./" +[ -z "${CONFIG_DIR:-}" ] && CONFIG_DIR="${_config_dir}" # NO trailing slash! +[ -z "${DRUPAL_DIR:-}" ] && DRUPAL_DIR="${_web_dir}" # Name of Drupal public sub-folder +[ -z "${REMOTE_DRUSH:-}" ] && REMOTE_DRUSH="${_remote_drush}" # Full path to remote drush +[ -z "${UPDATE_SCRIPT:-}" ] && UPDATE_SCRIPT="${_update_script}" +[ -z "${WEB_EXCLUDES:-}" ] && WEB_EXCLUDES="${_web_excludes}" +[ -z "${DRUSH_DISABLE:-}" ] && DRUSH_DISABLE="0" +[ -z "${UPDATE_DISABLE:-}" ] && UPDATE_DISABLE="0" +[ -z "${PHP_SETTINGS_DIR:-}" ] && PHP_SETTINGS_DIR="${DRUPAL_DIR}/sites/default" + +# Be sure to not overwrite any server-only .user.ini +if [ ! -f ${BUILD_ROOT}/${DRUPAL_DIR}/.user.ini ]; then + WEB_EXCLUDES="--exclude .user.ini ${WEB_EXCLUDES}" +fi + +if [ "${DEBUG:-0}" == "1" ]; then + #./scripts/show-vars.sh # per-env + echo "[DEBUG] IN-USE SETTINGS:" + echo "SSH_TARGET=$SSH_TARGET" + echo "DEPLOY_ENV=${DEPLOY_ENV}" + echo "DEPLOY_ROOT=${DEPLOY_ROOT}" + echo "BUILD_ROOT=${BUILD_ROOT}" + echo "CONFIG_DIR=${CONFIG_DIR}" + echo "DRUPAL_DIR=${DRUPAL_DIR}" + echo "REMOTE_DRUSH=${REMOTE_DRUSH}" + echo "BACKUP_DIR=${BACKUP_DIR}" + echo "DRUSH_DISABLE=${DRUSH_DISABLE}" + echo "UPDATE_SCRIPT=${UPDATE_SCRIPT}" + echo "UPDATE_DISABLE=${UPDATE_DISABLE}" + echo "WEB_EXCLUDES=${WEB_EXCLUDES}" + exit 0 +fi + +####################################### +# Functions +####################################### +function drush_enabled() { + if [ "${DRUSH_DISABLE}" == "1" ] + then + return 1 #1 is false (so, disabled) + else + return 0 #0 is true (enabled) because...bash + fi +} + +####################################### +# Check drush/site status +####################################### +echo "Starting deployment to ${DEPLOY_ENV}" +drush_enabled || echo "Note: Drush commands are disabled. Commands marked [D] will not execute." +echo "Verify connectivity (print user & working directory)" +ssh ${SSH_TARGET} "whoami; pwd" +echo "[D] Verify drush functionality (cr, status bootstrap)" +drush_enabled && ssh ${SSH_TARGET} "${REMOTE_DRUSH} cr" +# Expected good result: "Drupal bootstrap : Successful", otherwise empty (but drush returns 0) +ssh_cmd="${REMOTE_DRUSH} status --fields=bootstrap" +drush_enabled && { [[ $(ssh ${SSH_TARGET} "${ssh_cmd}") = *Successful* ]] || { echo ">> Drush failed bootstrap!"; exit 1; } ; } + +####################################### +# Enable maintenance mode +####################################### +# echo "[D] Set maintenance mode ON" +# drush_enabled && ssh ${SSH_TARGET} "${REMOTE_DRUSH} state:set system.maintenance_mode TRUE" + +####################################### +# Perform a database backup +####################################### +backup_file="${DEPLOY_ENV}_$(date +%Y%m%dT%H%M%S).sql.gz" +ssh_cmd="[ ! -d ${BACKUP_DIR} ] && mkdir -p ${BACKUP_DIR} || true" +echo "Using backup dir '${BACKUP_DIR}' (created as needed)" +ssh "${SSH_TARGET}" "$ssh_cmd" +ssh_cmd="${REMOTE_DRUSH} sql-dump | gzip > ${BACKUP_DIR}/$backup_file" +echo "[D] Creating backup file ${backup_file}" +drush_enabled && ssh "${SSH_TARGET}" "${ssh_cmd}" + +####################################### +# DEPLOY CODE: +####################################### +# Check for sub-dirs... +ssh_cmd="[ ! -d ${DEPLOY_ROOT}/${CONFIG_DIR} ] && mkdir -p ${DEPLOY_ROOT}/${CONFIG_DIR} || true" +echo "Ensure '${CONFIG_DIR}' exists" +ssh "${SSH_TARGET}" "$ssh_cmd" +ssh_cmd="[ ! -d ${DEPLOY_ROOT}/${DRUPAL_DIR}/sites/default ] && mkdir -p ${DEPLOY_ROOT}/${DRUPAL_DIR}/sites/default || true" +echo "Ensure '${DRUPAL_DIR}/sites/default exists'" +ssh "${SSH_TARGET}" "$ssh_cmd" + +# Disable write protect... +echo "Disabling write protection on settings folder and files." +ssh_cmd="chmod ug+w ${DEPLOY_ROOT}/${DRUPAL_DIR}/sites/default/" +ssh ${SSH_TARGET} ${ssh_cmd} +ssh_cmd="find ${DEPLOY_ROOT}/${DRUPAL_DIR}/sites/default/ -maxdepth 1 -type f -name 'settings*php' -exec chmod ug+w '{}' \;" +ssh ${SSH_TARGET} ${ssh_cmd} +# Rsync's +printf "Pushing latest codebase: config…" +rsync -rz --delete ${BUILD_ROOT}/${CONFIG_DIR}/ ${SSH_TARGET}:${DEPLOY_ROOT}/config +printf "\b ✓, ${DRUPAL_DIR}…" +rsync -rz --delete ${WEB_EXCLUDES} ${BUILD_ROOT}/${DRUPAL_DIR}/ ${SSH_TARGET}:${DEPLOY_ROOT}/${DRUPAL_DIR} +printf "\b ✓, vendor…" +rsync -rz --delete --links ${BUILD_ROOT}/vendor ${SSH_TARGET}:${DEPLOY_ROOT}/ +printf "\b ✓, scripts…" +[ -d ./scripts ] && rsync -rz --delete ${BUILD_ROOT}/scripts ${SSH_TARGET}:${DEPLOY_ROOT}/ +printf "\b ✓, composer…" +scp -q composer.json ${SSH_TARGET}:${DEPLOY_ROOT}/composer.json +printf "\b ✓, settings…" +scp -q ${BUILD_ROOT}/${PHP_SETTINGS_DIR}/settings.${DEPLOY_ENV}.php ${SSH_TARGET}:${DEPLOY_ROOT}/${DRUPAL_DIR}/sites/default/settings.local.php +printf "\b ✓\nFile sync complete!\n" +# Re-protect settings +echo "Enabling write protection on settings folder and files." +ssh_cmd="[ -d ${DEPLOY_ROOT}/${DRUPAL_DIR}/sites/default ] && chmod ug-w ${DEPLOY_ROOT}/${DRUPAL_DIR}/sites/default/" +ssh ${SSH_TARGET} ${ssh_cmd} +ssh_cmd="find ${DEPLOY_ROOT}/${DRUPAL_DIR}/sites/default/ -maxdepth 1 -type f -name 'settings*php' -exec chmod ug-w '{}' \;" +ssh ${SSH_TARGET} ${ssh_cmd} + +####################################### +# [P|re]load database +####################################### +if [ ! -z "${PRELOAD_DB_FILE:-}" -a "${DEPLOY_ENV}" != "live" -a "${DEPLOY_ENV}" != "prod" ]; then + ssh_cmd="${REMOTE_DRUSH} sql-drop -y" + echo "[D] Dropping existing database" + drush_enabled && ssh "${SSH_TARGET}" "${ssh_cmd}" + if drush_enabled; then + ssh_cmd="${REMOTE_DRUSH} sqlc < ${PRELOAD_DB_FILE}" + echo "[D] Loading database from ${PRELOAD_DB_FILE}" + ssh "${SSH_TARGET}" "${ssh_cmd}" + else + echo "Fallback: Load database file using mysql (requires .my.cnf)" + ssh_cmd="mysql < ${PRELOAD_DB_FILE}" + echo "Loading database from ${PRELOAD_DB_FILE}" + ssh "${SSH_TARGET}" "${ssh_cmd}" + fi +fi + +####################################### +# RUN DRUPAL TASKS (via UPDATE_SCRIPT): +####################################### +if drush_enabled && [ "${UPDATE_DISABLE}" == "0" -a -f "${BUILD_ROOT}/${UPDATE_SCRIPT}" ]; then + echo ">> Calling ${UPDATE_SCRIPT} to perform updates..." + ssh ${SSH_TARGET} "${DEPLOY_ROOT}/${UPDATE_SCRIPT} ${REMOTE_DRUSH}" +# echo ">> [D] Set maintenance mode OFF" +# ssh ${SSH_TARGET} ${REMOTE_DRUSH} state:set system.maintenance_mode FALSE +else + echo ">> [D] Skipping follow-up drush (cr, updb, cim, etc)." +# echo ">> Maintenence mode on. To disable: ${drush} state:set system.maintenance_mode FALSE" +fi + +echo "Deployment to ${DEPLOY_ENV} completed!" diff --git a/scripts/example.template b/scripts/example.template new file mode 100644 index 0000000..5645672 --- /dev/null +++ b/scripts/example.template @@ -0,0 +1,12 @@ + "${DB_NAME}", + 'username' => "${DB_USER}", + 'password' => "${DB_PASSWORD}", + 'host' => "127.0.0.1", + 'driver' => "mysql", + 'port' => 3306, + 'prefix' => "", +); +$settings['hash_salt'] = '${SALT}'; +$settings['trusted_host_patterns'] = [${TRUSTED_HOSTS}]; diff --git a/scripts/nuke.sh b/scripts/nuke.sh new file mode 100755 index 0000000..7fbc26b --- /dev/null +++ b/scripts/nuke.sh @@ -0,0 +1,36 @@ +# Delete Drupal core and any contrib directories. +rm -rf web/core +rm -rf web/modules/contrib +rm -rf web/themes/contrib +rm -rf web/profiles/contrib +rm -rf drush/Commands/contrib + +# Delete Drupal root files. +rm -f web/.csslintrc +rm -f web/.editorconfig +rm -f web/.eslintignore +rm -f web/.eslintrc.json +rm -f web/.gitattributes +rm -f web/.gitignore +rm -f web/.ht.router.php +rm -f web/autoload.php +rm -f web/example.gitignore +rm -f web/index.php +rm -f web/INSTALL.txt +rm -f web/robots.txt +rm -f web/README.txt +rm -f web/update.php +rm -f web/web.config + +# Delete any Drupal scaffold files. +rm -f .editorconfig +rm -f .gitattributes + +# Delete any third party libraries. +# rm -rf web/libraries + +# Delete the vendor directory. +rm -rf vendor + +# Delete any build folders. +rm -rf build diff --git a/scripts/pull.sh b/scripts/pull.sh index dbe9b90..135b7d7 100755 --- a/scripts/pull.sh +++ b/scripts/pull.sh @@ -1,17 +1,23 @@ #!/bin/bash -set -e +set -euo pipefail +# ~SCRIPT~ update.sh +# * Pass in a path to drush, or use a default: +# * Runs post-deploy commands to update Drupal + # Confirmed with echo `pwd` that as long as we use via composer it's always in # /var/www/html (aka the project root). proj_dir="$(cd $(dirname ${0})/../ && pwd)" # Parent of this script folder. [ "$proj_dir" != "/var/www/html" ] && { echo "Script running from unexpected path - are you running within ddev, as you should?"; exit 1; } -backup_file="$(date +%Y%m%dT%H%M%S)_pre_pull.sql" +backup_file="$(date +%Y%m%dT%H%M%S)_pre_pull.sql.gz" [ ! -d /var/www/html/backups ] && mkdir /var/www/html/backups || true -echo "Backing up current db to backups/${backup_file}..." -drush sql-dump > /var/www/html/backups/${backup_file} -echo "Dropping local db..." +echo ">> Backing up current db to backups/${backup_file}..." +drush sql-dump --gzip > /var/www/html/backups/${backup_file} +echo ">> Dropping local db..." drush -y sql-drop -echo "Pulling db from live into local..." -drush -y sql-sync @live @self -echo "Pulling files from live into local..." -drush -y rsync @live:%files @self:%files +echo ">> Pulling db from live into local..." +drush -y sql-sync --structure-tables-key=common @live @self +echo ">> Pulling files from live into local..." +drush -y rsync --exclude-paths=css:js:php @live:%files @self:%files +echo ">> Local cache reload..." +drush cr echo "Done" diff --git a/scripts/show-vars.sh b/scripts/show-vars.sh new file mode 100755 index 0000000..adc52aa --- /dev/null +++ b/scripts/show-vars.sh @@ -0,0 +1,14 @@ +#!/bin/sh +echo "[DEBUG] ENV SETTINGS (Required):" +echo "SSH_TARGET=$SSH_TARGET" +echo "DEPLOY_ENV=${DEPLOY_ENV}" +echo "CI_ENVIRONMENT_NAME=$CI_ENVIRONMENT_NAME" +echo "DEPLOY_ROOT=${DEPLOY_ROOT}" +echo "[DEBUG] ENV SETTINGS (Optional):" +echo "DRUPAL_ROOT=${DRUPAL_ROOT}" +echo "BACKUP_ROOT=${BACKUP_ROOT}" +echo "BUILD_ROOT=${BUILD_ROOT}" +echo "DRUSH_DISABLE=${DRUSH_DISABLE}" +echo "UPDATE_SCRIPT=${UPDATE_SCRIPT}" +echo "UPDATE_DISABLE=${UPDATE_DISABLE}" +echo "DEBUG=${DEBUG}" \ No newline at end of file diff --git a/scripts/sync_to_test.sh b/scripts/sync_to_test.sh new file mode 100755 index 0000000..ffc9ab1 --- /dev/null +++ b/scripts/sync_to_test.sh @@ -0,0 +1,57 @@ +#!/bin/bash +set -euo pipefail + +source_site_root="/home/experienceolympic_live/site/web" +source_drush="/home/experienceolympic_live/site/vendor/bin/drush -r ${source_site_root}" +source_files="${source_site_root}/sites/default/files/" + +# Requires an .ssh/config entry for test-site. May be to localhost, and must not require a password for private key. +target_site=test-site +target_site_root="/home/experienceolympic_test/site/web" +target_drush="/home/experienceolympic_test/site/vendor/bin/drush -r ${target_site_root}" +target_files="${target_site_root}/sites/default/files/" +# Use ssh to target when target_site is set, otherwise use eval to just run in the local context +target_cmd="${target_site:+ssh ${target_site}}" +target_cmd="${target_cmd:-eval}" + +sync_date=$(date +%Y%m%d_%H%M%S%Z) + +# Test local drush/file paths/ssh connection/remote drush +[ -d ${source_files} ] || (echo "source_files folder does not exist"; exit 1) +${source_drush} status > /dev/null || (echo "Cannot execute local drush"; exit 1) +target_cmd "[ -d ${target_files} ]" || (echo "${target_cmd} failed, or ${target_files} folder does not exist at target. If using ssh, have you set up the key?"; exit 1) +target_cmd "${target_drush} status > /dev/null" || (echo "${target_cmd} ${target_drush} failed - does the site exist"; exit 1) + +echo ">> All checks succeeded!" +echo ">> Syncing this site to ${target_site:-${target_site_root}} (using file date: ${sync_date})..." + +# Dump destination DB (extra backup) +echo ">> Dumping destination database..." +target_cmd "${target_drush} sql-dump | gzip > ~/backups/${sync_date}-paranoia.sql.gz" +echo ">> Dumping source database..." +${source_drush} sql-dump | gzip > ~/backups/${sync_date}-sync.sql.gz + + +# Send copy +echo ">> Copying source db to target..." +'scp' ~/backups/${sync_date}-sync.sql.gz ${target_site}:backups/ + + +# Drop DB and reload from source +echo ">> Reloading target database..." +target_cmd "${target_drush} sql-drop -y" + +target_cmd "gunzip ~/backups/${sync_date}-sync.sql.gz" +target_cmd "${target_drush} sqlc < ~/backups/${sync_date}-sync.sql" + + +# Copy files +echo ">> Sending new files..." +rsync -rlptvz --exclude=php --exclude=js --exclude=css --exclude=styles --exclude=twig ${source_files} ${target_site:+${target_site}:}${target_files} + +echo ">> Sync to ${target_site:-${target_site_root}} complete - running cache rebuild..." + +# CR forever! +target_cmd "${target_drush} cr" + +echo ">> Target ${target_site:-${target_site_root}} is ready!" diff --git a/scripts/update-hook.sh b/scripts/update-hook.sh new file mode 100755 index 0000000..a8a49fa --- /dev/null +++ b/scripts/update-hook.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +set -euo pipefail +# ~SCRIPT~ update-hook.sh +# * Pass in a path to drush, or use a default: +# * Runs drush deploy command to update Drupal + +# https://stackoverflow.com/questions/59895/how-do-i-get-the-directory-where-a-bash-script-is-located-from-within-the-script +DIR="$( dirname -- "${BASH_SOURCE[0]}"; )"; # Get the directory name +SCRIPT_DIR="$( realpath -e -- "$DIR"; )"; # Resolve its full path if need be + +drush=${*-${SCRIPT_DIR}/../vendor/bin/drush} +echo "Using drush=${drush}" + +_oldpath=$PATH +export PATH=$HOME/bin:/usr/local/bin:/usr/bin:/bin + +echo ">> [D] Running drush cache:rebuild" +${drush} cache:rebuild +echo ">> [D] Running drush deploy" +${drush} deploy -y +export PATH=$_oldpath +exit 0 diff --git a/scripts/update.sh b/scripts/update.sh new file mode 100755 index 0000000..5299c93 --- /dev/null +++ b/scripts/update.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +set -euo pipefail +# ~SCRIPT~ update.sh +# * Pass in a path to drush, or use a default: +# * Runs post-deploy commands to update Drupal + +# https://stackoverflow.com/questions/59895/how-do-i-get-the-directory-where-a-bash-script-is-located-from-within-the-script +DIR="$( dirname -- "${BASH_SOURCE[0]}"; )"; # Get the directory name +SCRIPT_DIR="$( realpath -e -- "$DIR"; )"; # Resolve its full path if need be + +drush=${*-${SCRIPT_DIR}/../vendor/bin/drush} +echo "Using drush=${drush}" + +_oldpath=$PATH +export PATH=$HOME/bin:/usr/local/bin:/usr/bin:/bin + +echo ">> [D] Running drush cr" +${drush} cr +echo ">> [D] Running drush updb -y" +${drush} updb -y +echo ">> [D] Running drush cim" +${drush} cim -y +echo ">> [D] Running drush cr (again)" +${drush} cr +export PATH=$_oldpath +exit 0