Skip to content

Instantly share code, notes, and snippets.

@lmakarov
Created August 23, 2018 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmakarov/f283dc4fe9eceb5864de9dbcfbe65a9e to your computer and use it in GitHub Desktop.
Save lmakarov/f283dc4fe9eceb5864de9dbcfbe65a9e to your computer and use it in GitHub Desktop.
Docksal custom command example
#!/usr/bin/env bash
## Initialize stack and site (full reset)
##
## Usage: fin init
set -e # Abort if anything fails
set -x # Echo commands
#-------------------------- Helper functions --------------------------------
# Console colors
red='\033[0;31m'
green='\033[0;32m'
green_bg='\033[42m'
yellow='\033[1;33m'
NC='\033[0m'
echo-red () { echo -e "${red}$1${NC}"; }
echo-green () { echo -e "${green}$1${NC}"; }
echo-green-bg () { echo -e "${green_bg}$1${NC}"; }
echo-yellow () { echo -e "${yellow}$1${NC}"; }
#-------------------------- Execution --------------------------------
# Stack initialization/reset
echo -e "${green_bg} Step 1 ${NC}${green} Initializing stack...${NC}"
fin reset -f
echo "Waiting 10s for MySQL to initialize...";
sleep 10
echo -e "${green_bg} Step 2 ${NC}${green} Initializing site...${NC}"
fin site-init
echo -en "${green_bg} DONE! ${NC} "
echo -e "Open ${yellow}http://${VIRTUAL_HOST}${NC} in your browser to verify the setup."
#-------------------------- END: Execution --------------------------------
#!/usr/bin/env bash
#: exec_target = cli
## Initialize/reinstall site
##
## Usage: fin site-init
set -e # Abort if anything fails
#set -x # Echo commands
#-------------------------- Settings --------------------------------
# PROJECT_ROOT and DOCROOT are set as env variables in cli
DOCROOT_PATH="${PROJECT_ROOT}/${DOCROOT}"
SITEDIR_PATH="${DOCROOT_PATH}/sites/${SITE_DIRECTORY}"
# SITE_KEY is set in docksal.env
SOURCE_ALIAS="@emmis.prod.${SITE_KEY}"
#-------------------------- END: Settings --------------------------------
#-------------------------- Helper functions --------------------------------
# Copy a settings file.
# Skips if the destination file already exists.
# @param $1 source file
# @param $2 destination file
copy_settings_file()
{
local source="$1"
local dest="$2"
if [[ ! -f $dest ]]; then
echo "Copying ${dest}..."
cp $source $dest
else
echo "${dest} already in place."
fi
}
#-------------------------- END: Helper functions --------------------------------
#-------------------------- Functions --------------------------------
# Initialize local settings files
init_settings ()
{
# Copy from settings templates
copy_settings_file "${SITEDIR_PATH}/example.settings.local.php" "${SITEDIR_PATH}/settings.local.php"
copy_settings_file "${PROJECT_ROOT}/tests/behat/behat.yml.dist" "${PROJECT_ROOT}/tests/behat/behat.yml"
}
# Set file/folder permissions
file_permissions ()
{
mkdir -p "${SITEDIR_PATH}/files"
mkdir -p "${SITEDIR_PATH}/files/private"
chmod -R 777 "${SITEDIR_PATH}/files"
chmod -R 777 "${SITEDIR_PATH}/files/private"
}
# Selectively sync files
# File sync needs a working local Drupal instance to resolve %files (file directory path)
sync_files ()
{
cd $DOCROOT_PATH
# CSS and JS injector files
drush -y rsync ${SOURCE_ALIAS}:%files/css_injector @docksal:%files/css_injector --delete || true
drush -y rsync ${SOURCE_ALIAS}:%files/js_injector @docksal:%files/js_injector --delete || true
# GTM files
drush -y rsync ${SOURCE_ALIAS}:%files/google_tag @docksal:%files/google_tag --delete || true
}
# Local adjustments
local_settings ()
{
cd $DOCROOT_PATH
drush @docksal en ${MODULES_ENABLED} -y
drush @docksal dis ${MODULES_DISABLED} -y
}
#-------------------------- END: Functions --------------------------------
#-------------------------- Execution --------------------------------
# Project initialization steps
echo "Initializing local settings files..."
file_permissions
echo "Resetting files and private files directory permissions..."
init_settings
# Get a databased dump
# Note: Calling a custom command within cli has to be done this way
echo "Downloading database backup..."
time ${PROJECT_ROOT}/.docksal/commands/db-backup-get
echo "Importing database backup..."
time ${PROJECT_ROOT}/.docksal/commands/db-backup-import
echo "Running database updates..."
${PROJECT_ROOT}/.docksal/commands/db-update @docksal
# This should happen after the DB is ready
echo "Syncing files..."
sync_files
# Some modules (like Google Tag Manager) store files in a subdirectory of the public files directory.
# This directory is created via `hook_requirements` during module install or at runtime (when `admin/reports/status`
# page is loaded). Since we are using `stage_file_proxy`, the files directory is never synced.
# That's why those module specific subfolders would never be created, unless the status page is triggered.
# This triggers runtime requirements check for all modules.
drush @docksal status-report
echo "Applying local dev settings..."
local_settings
#-------------------------- END: Execution --------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment