Skip to content

Instantly share code, notes, and snippets.

@ericvanjohnson
Forked from snipe/dqh+aws
Created April 10, 2013 01:39
Show Gist options
  • Save ericvanjohnson/5351051 to your computer and use it in GitHub Desktop.
Save ericvanjohnson/5351051 to your computer and use it in GitHub Desktop.
#!/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/repository/latest_revision"
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
REVISION=`curl -s GET -H $HEADER_ACCEPT -H $HEADER_CONTENT_TYPE -u $USER_NAME:$API_KEY $DHQ_BASE_URL$DHQ_REV_RESOURCE`
echo $REVISION
# 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-MYSERVER-Prod-Web-'$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
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`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment