Skip to content

Instantly share code, notes, and snippets.

@snipe
Last active September 11, 2019 15:06
Show Gist options
  • Save snipe/5349692 to your computer and use it in GitHub Desktop.
Save snipe/5349692 to your computer and use it in GitHub Desktop.
DeployHQ+AWS Autoscalers
#!/bin/bash
# Set these variables
USER_NAME="you@example.com"
API_KEY="XXXXXXXX_YOUR_DHQ_API_KEY_XXXXXXXXXXXXX"
START_REV='SOME_ARBITRARY_GIT_REVISION_HASH_TO_START_FROM'
DHQ_API_PROJ="your-project-shortname"
DHQ_BASE_URL="https://yoursite.deployhq.com/"
DHQ_SERVER_GROUP="YOUR_SERVER_GROUP_UUID"
DHQ_SERVER_USERNAME="your-server-username"
# That's it - you're done configuring.
# These are just some DHQ API endpoint settings we need to
# reference a few times. You don't need to edit anything here.
DHQ_REV_RESOURCE="projects/$DHQ_API_PROJ/server_groups/$DHQ_SERVER_GROUP"
DHQ_NEWSERVER_RESOURCE="projects/$DHQ_API_PROJ/servers"
DHQ_DEPLOY_RESOURCE="projects/$DHQ_API_PROJ/deployments"
HEADER_CONTENT_TYPE="Content-Type: application/json"
HEADER_ACCEPT="Accept: application/json"
# Get the DNS name of this server that was just provisioned
echo 'Calling AWS for DNS name.....'
DNS_NAME=`curl -s http://169.254.169.254/latest/meta-data/public-hostname`
echo $DNS_NAME
# Get the latest revision number from DeployHQ
echo 'Calling DHQ for latest revision:'$DHQ_BASE_URL$DHQ_REV_RESOURCE
curl -s GET -u $USER_NAME:$API_KEY $DHQ_BASE_URL$DHQ_REV_RESOURCE > /tmp/servergroup.json
REVISION=`cat /tmp/servergroup.json | sed -e 's/[{}]/''/g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | grep -m 1 '"last_revision":' | sed 's/:/ /1' | awk -F" " '{ print $2 i}'`
echo 'Update to revision: '$REVISION
#exit
# We just use the date to add onto the server name in DHQ, so we can quickly
# see when it was added to DHQ
NOW=$(date +"%m-%d-%Y-%H:%M")
NEW_SERVER_JSON='{
"server" : {
"name": "Auto-'$DNS_NAME'-'$NOW'",
"server_path": "/var/www/html",
"port": 22,
"username": "'$DHQ_SERVER_USERNAME'",
"use_ssh_keys": true,
"server_group_identifier": "'$DHQ_SERVER_GROUP'",
"hostname": "'$DNS_NAME'",
"protocol_type": "ssh"
}
}'
# Get create newly identified server at DeployHQ
echo 'Create a new server in our desired server group with the hostname we learned above: '$DHQ_BASE_URL$DHQ_NEWSERVER_RESOURCE
CREATE=`curl -X POST -d "$NEW_SERVER_JSON" -H "$HEADER_ACCEPT" -H "$HEADER_CONTENT_TYPE" -u $USER_NAME:$API_KEY $DHQ_BASE_URL$DHQ_NEWSERVER_RESOURCE > server.json`
DHQ_NEWSERVER_UUID=`cat server.json | sed -e 's/[{}]/''/g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | grep '"identifier":' | sed 's/:/ /1' | awk -F" " '{ print $2 i}'`
DEPLOY_JSON='{"deployment" : {
"parent_identifier" : '$DHQ_NEWSERVER_UUID',
"start_revision" : "'$START_REV'",
"end_revision" : '$REVISION',
"mode" : "queue",
"copy_config_files" : 1,
"email_notify" : 1
}}'
echo 'Deploy!: '$DHQ_BASE_URL$DHQ_DEPLOY_RESOURCE
echo $DEPLOY_JSON
CREATE=`curl -X POST -d "$DEPLOY_JSON" -H "$HEADER_ACCEPT" -H "$HEADER_CONTENT_TYPE" -u $USER_NAME:$API_KEY $DHQ_BASE_URL$DHQ_DEPLOY_RESOURCE`
@kaioken
Copy link

kaioken commented Aug 12, 2017

@snipe thanks for this code ;)

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