-
-
Save JeanMichaelRosario/e94650d823f2b5eb2587080a7b69036a to your computer and use it in GitHub Desktop.
DeployHQ+AWS Autoscalers
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 | |
# Set these variables | |
USER_NAME="[email protected]" | |
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. | |
# IF YOU DON'T HAVE SERVERS GROUPS USE THIS | |
DHQ_REV_RESOURCE="projects/$DHQ_API_PROJ/servers/$DHQ_SERVER_GROUP" | |
# BUT IF YOU HAVED, USE THIS | |
# 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" | |
echo 'Getting files from S3' | |
aws s3 cp s3://nzxt/apin/env $DEPLOY_PATH/.env | |
# 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": "'$DEPLOY_PATH'", | |
"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 > "$DEPLOY_PATH"/server.json` | |
DHQ_NEWSERVER_UUID=`cat "$DEPLOY_PATH"/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}'` | |
echo "REVISION: '$REVISION'" | |
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` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment