Skip to content

Instantly share code, notes, and snippets.

@fribibb
Last active November 2, 2017 00:37
Show Gist options
  • Save fribibb/0662096e88800d01014bfc01c820f75a to your computer and use it in GitHub Desktop.
Save fribibb/0662096e88800d01014bfc01c820f75a to your computer and use it in GitHub Desktop.
#!/bin/bash
#eg: bash ~/govcms-alias-creator-4000.sh "Tim" abc123abc123
# API keys
USER=$1
KEY=$2
# Variables.
SERVER=${3:-production}
SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# URL for Production Cloud API.
if [ "$SERVER" == production ] ; then
API_ENDPOINT=https://www.govcms.acsitefactory.com/api/v1/sites
API_LIMIT=100
ALIAS_FILE=govcms.aliases.drushrc.php
DRUPAL_ROOT=/var/www/html/govcms.01live/docroot
REMOTE_HOST=web-256.enterprise-g1.hosting.acquia.com
REMOTE_USER=govcms.01live
else
API_ENDPOINT=https://www.test-govcms.acsitefactory.com/api/v1/sites
API_LIMIT=100
ALIAS_FILE=test-govcms.aliases.drushrc.php
DRUPAL_ROOT=/var/www/html/govcms.01test/docroot
REMOTE_HOST=staging-258.enterprise-g1.hosting.acquia.com
REMOTE_USER=govcms.01test
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' => 'drush6',
'%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}]"
echo "$TEMPLATE" | sed "s/aliasname/${ACSF_DOMAIN}/g" >> $END_FILE
# 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_DOMAINS=$(echo $SITE_OUTPUT | jq --raw-output '.domains[]')
# Find the best domain (e.g. the one with www. or .gov.au).
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
# Index++.
INDEX=$((${INDEX} + 1))
done
}
DATE=`date +%Y-%m-%d`
echo "ACSF site alias creation"
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/govcms.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/govcms.txt 1
OUTPUT="$(curl -s -L -u "$USER":$KEY ${API_ENDPOINT}?limit=${API_LIMIT}\&page=2)"
echo "${OUTPUT}" > /tmp/govcms2.txt
create_aliases /tmp/govcms2.txt 2
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/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment