Skip to content

Instantly share code, notes, and snippets.

@markf3lton
Last active July 22, 2020 12:27
Show Gist options
  • Save markf3lton/08ceb539b4cc1141bfde6314594c6be1 to your computer and use it in GitHub Desktop.
Save markf3lton/08ceb539b4cc1141bfde6314594c6be1 to your computer and use it in GitHub Desktop.
A script to generate ACSF aliases via the Site Factory API

For best results adapt this script use with aliases you have tested, e.g. the Acquia aliases... see: https://gist.github.com/markf3lton/0f446eb0d953bb2eb12a8f10bdaabcef#file-acsf-to-local-md

Or write a similar script that implements aliases in the format of your choice. For example if using Lando the Acquia drush aliases can be used within the virtual machine to sync sites from ACSF to your local environment, see step 6 here: https://gist.github.com/markf3lton/66c5cce6ccaaa69d8b0525ba16d1bd99#file-lando-md

The nice thing about this script is it is very simple, so it's a good starter-script. Many variations have been implemented by customers. ACSF are a popular topic in the BLT issue queue, for example: acquia/blt#3932


#!/bin/bash

# API keys
USER=$1
KEY=$2

# Variables.
SERVER=${3:-production}
SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
API_LIMIT=200

# URL for Production Cloud API
if [ "$SERVER" == production ] ; then
  ENVIRONMENT=PROD
  API_ENDPOINT=https://www.civiccms.acsitefactory.com/api/v1/sites
  ALIAS_FILE=civiccms.aliases.drushrc.php
  DRUPAL_ROOT=/var/www/html/civiccms.01live/docroot
  REMOTE_HOST=civiccms01live.ssh.enterprise-g1.acquia-sites.com
  REMOTE_USER=civiccms.01live
elif [ "$SERVER" == test ] ; then
  ENVIRONMENT=TEST
  API_ENDPOINT=https://www.test-civiccms.acsitefactory.com/api/v1/sites
  ALIAS_FILE=civiccms-test.aliases.drushrc.php
  DRUPAL_ROOT=/var/www/html/civiccms01test/docroot
  REMOTE_HOST=civiccms01test.ssh.enterprise-g1.acquia-sites.com
  REMOTE_USER=civiccms.01test
else
  ENVIRONMENT=DEV
  API_ENDPOINT=https://www.dev-civiccms.acsitefactory.com/api/v1/sites
  ALIAS_FILE=civiccms-dev.aliases.drushrc.php
  DRUPAL_ROOT=/var/www/html/acquiatam1.01dev/docroot
  REMOTE_HOST=civiccms01dev.ssh.enterprise-g1.acquia-sites.com
  REMOTE_USER=civiccms.01dev
fi

# Drush Aliases File.
END_FILE=${SCRIPT_DIR}/${ALIAS_FILE}

# Variables.
SITE_IDS=()
SITE_ARRAY=()

# Colors
red='\033[0;31m'
green='\033[0;32m'
orange='\033[0;33m'
NC='\033[0m'

# Starting file for Drush aliases.
START_PHP="<?php "
TEMPLATE="\$aliases['aliasname'] = array(
  'uri' => 'aliasname',
  'root' => '$DRUPAL_ROOT',
  'remote-host' => '$REMOTE_HOST',
  'remote-user' => '$REMOTE_USER',
  'ssh-options' => '-F /dev/null',
  'path-aliases' => array(
    '%drush-script' => 'drush8',
    '%dump-dir' => '/mnt/tmp/',
  )
);"

# Initial alias file bits.
echo $START_PHP > $END_FILE

function create_aliases {
    OUTPUT_FILE=$1
    PAGE=$2
    INDEX=0

    # Create array of site IDs.
    echo -e "${orange}Page #${PAGE}${NC}"
    SITE_IDS=()
    SITE_IDS=$(cat ${OUTPUT_FILE} | jq '.sites[].id')
    SITE_IDS=(${SITE_IDS})

    SITE_ARRAY=()
    SITE_ARRAY=$(cat ${OUTPUT_FILE}  | jq -r '.sites[].domain' | tr " " "\n")

    for ACSF_DOMAIN in $SITE_ARRAY ; do
        # Site ID.
        ID=${SITE_IDS[${INDEX}]}

        # Debug.
        echo -e " - @${ACSF_DOMAIN} [id:${ID}]"

        # Perform a per site API query to get the domains for each site.
        SITE_OUTPUT="$(curl -s -L -u "$USER":$KEY ${API_ENDPOINT}/${ID})"
        SITE_MESSAGE=$(echo $SITE_OUTPUT | grep 'Unprocessable Entity')
        if [ $? -eq 0 ] ; then
          # Invalid site.
          echo -e " -- ${red}Invalid site state, skipping.${NC}"
        else
          echo "$TEMPLATE" | sed "s/aliasname/${ACSF_DOMAIN}/g" >> $END_FILE
          SITE_DOMAINS=$(echo $SITE_OUTPUT | jq --raw-output '.domains[]')

          # Find the best domain (e.g. the one with www.).
          BEST_DOMAIN=${ACSF_DOMAIN}
          for SITE_DOMAIN in ${SITE_DOMAINS} ; do
              # www takes precedence.
              if [[ ${SITE_DOMAIN} == *"www."* ]] ; then
                  BEST_DOMAIN=${SITE_DOMAIN}
                  break
              elif [[ ${SITE_DOMAIN} == *".com"* ]] ; then
                  BEST_DOMAIN=${SITE_DOMAIN}
              fi
          done

          # We did find a better domain to use on production.
          if [[ ${BEST_DOMAIN} != ${ACSF_DOMAIN} ]] ; then
              echo -e " -- @${BEST_DOMAIN}"
              echo "$TEMPLATE" | sed "s/aliasname/${BEST_DOMAIN}/g" >> $END_FILE
          fi
        fi

        # Index++.
        INDEX=$((${INDEX} + 1))
    done

}

DATE=`date +%Y-%m-%d`
echo "ACSF site alias creation (${ENVIRONMENT})"
echo -e "Loading sites from ${API_ENDPOINT}\n"

OUTPUT="$(curl -s -L -u "$USER":$KEY ${API_ENDPOINT}?limit=${API_LIMIT}\&page=1)"
echo "${OUTPUT}" > /tmp/civiccms-aliases.txt
if [[ ${OUTPUT} == *"Permission denied"* ]] ; then
    echo -e "${red}API query failed, ensure you are coming from the correct IP${NC}\n"
    exit 1
fi
COUNT=$(echo $OUTPUT | jq -r '.count')
echo -e "${orange}Number of Sites [$COUNT]${NC}"
create_aliases /tmp/civiccms-aliases.txt 1


# OUTPUT="$(curl -s -L -u "$USER":$KEY ${API_ENDPOINT}?limit=${API_LIMIT}\&page=2)"
# echo "${OUTPUT}" > /tmp/civiccms-aliases2.txt
# create_aliases /tmp/civiccms-aliases2.txt


echo -e "${green}Aliases file \"${ALIAS_FILE}\" finished writing${NC}"
echo "" >> $END_FILE
# Copy File to .drush directory so we can start running drush commands
rm -f ~/.drush/$END_FILE
cp -f $END_FILE ~/.drush/

@markf3lton
Copy link
Author

markf3lton commented Jul 21, 2020

To use the script,

chmod +x my-script.sh

then

./my-script.sh UserName API-KEY

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment