Created
March 16, 2016 09:20
-
-
Save seventhskye/a8a3e00bb0d3bcab91a0 to your computer and use it in GitHub Desktop.
A library of bash functions to use for Amazon Web Services CloudFormation deployments.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Abort with exit and message | |
# Used for testing | |
function abort() | |
{ | |
echo " *** ERROR $@" | |
exit 1 | |
} | |
# Delete the stack | |
# Used for testing and cleaning | |
function cleanup() | |
{ | |
echo " --> Cleaning up" | |
echo " --> Deleting CloudFormation stack" | |
aws cloudformation delete-stack --stack-name $STACK_NAME | |
echo "Finished at $(date -u +%Y-%m-%dT%H:%M:%S%z)" | |
} | |
# Configure the awscli | |
function configure_awscli() | |
{ | |
# Configures the aws-cli, requires an access key is and secret | |
aws configure --profile $AWS_DEFAULT_PROFILE | |
} | |
# Create the stack in Cloudformation | |
# - not generic, yet. | |
function create_stack() | |
{ | |
echo " --> Creating Cloudformation stack" | |
# Creates a CloudFormation stack for the ECS cluster | |
aws cloudformation create-stack \ | |
--stack-name $STACK_NAME \ | |
--template-body file://cloudformation.json \ | |
--capabilities CAPABILITY_IAM \ | |
--notification-arns "$CF_NOTIFICATION_TOPIC_ARN" \ | |
--parameters $PARAMETERS > /dev/null | |
} | |
# Source the environment.variables | |
# See environment.variables.example | |
function source_envs() | |
{ | |
export VERSION=`cat VERSION` | |
export IPADDRESS=`curl -s http://whatismijnip.nl |cut -d " " -f 5` | |
# Source Environment variables if present | |
if [ -e "environment.variables" ]; then | |
echo " --> Sourcing environment variables" | |
source environment.variables | |
fi | |
} | |
# Test for aws and cURL | |
function test_prerequisites() | |
{ | |
echo " --> Testing for awscli" | |
which aws > /dev/null 2>&1 | |
echo " --> Testing for cURL" | |
which curl > /dev/null 2>&1 | |
} | |
# Function to query Amazon for the CF stack progress | |
# Exits when complete or delete/delete-failed | |
function watch_stack_progress() | |
{ | |
echo " --> Watching stack progress, please wait..." | |
STACK_STATUS=CREATE_IN_PROGRESS | |
SLEEP=60 | |
n=0 | |
while [ "$STACK_STATUS" == "CREATE_IN_PROGRESS" ]; do | |
STACK_STATUS=`aws cloudformation describe-stacks --stack-name $STACK_NAME --query Stacks[*].StackStatus --output text` | |
n=$[n+1] | |
echo " --> Stack status after ${n}m: $STACK_STATUS" | |
if [[ ( "$STACK_STATUS" == "CREATE_COMPLETE" ) || ( "$STACK_STATUS" == "DELETE_COMPLETE" ) || ( "$STACK_STATUS" == "DELETE_FAILED" ) ]]; then | |
break | |
fi | |
sleep $SLEEP | |
done | |
} | |
# Test for endpoint | |
# Requires the $HOST and $DNS_DOMAIN variables to be set | |
function test_endpoint() | |
{ | |
HOSTNAME=$HOST.$DNS_DOMAIN | |
PROTOCOL=${PROTOCOL:-https} | |
URL_PATH=${URL_PATH:-""} | |
URL=$PROTOCOL://${HOSTNAME}${URL_PATH} | |
echo " --> Testing for endpoint" | |
# Number of requests | |
TIMEOUT=30 | |
# Time to wait between requests | |
SLEEP=60 | |
# Initial HTTP code, should not be 200 | |
HTTP_CODE="500" | |
while [ $TIMEOUT -gt 0 -a $HTTP_CODE -ne 200 ]; do | |
TIMEOUT=$[$TIMEOUT-1] | |
HTTP_CODE=`curl -o /dev/null -k --silent --head --write-out '%{http_code}' $URL` | |
if [ $HTTP_CODE -eq 200 ]; then | |
break | |
fi | |
echo " --> GET $URL http_code:$HTTP_CODE ($[TIMEOUT-i] remaining) " | |
sleep $SLEEP | |
done | |
if [ $HTTP_CODE -eq 200 ]; then | |
echo " --> Connection to endpoint was successful" | |
exit 0 | |
else | |
echo " --> All requests to endpoint failed, aborting" | |
exit 1 | |
fi | |
} | |
# Initialise by sourcing environment variables | |
source_envs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment