Skip to content

Instantly share code, notes, and snippets.

@krohne
Last active January 28, 2019 19:05
Show Gist options
  • Save krohne/79dfc16bf2336057c09f514b699ad4f6 to your computer and use it in GitHub Desktop.
Save krohne/79dfc16bf2336057c09f514b699ad4f6 to your computer and use it in GitHub Desktop.
Update ~/.ssh/config IP addresses for preprod and production
#!/bin/bash
# production|preprod
command -v jq >/dev/null 2>&1 || { echo >&2 "I require jq but it's not installed. Try 'brew install jq' Aborting."; exit 1; }
command -v curl >/dev/null 2>&1 || { echo >&2 "I require curl but it's not installed. Try 'brew install curl' Aborting."; exit 1; }
command -v perl >/dev/null 2>&1 || { echo >&2 "I require perl but it's not installed. Try 'brew install perl' Aborting."; exit 1; }
command -v tr >/dev/null 2>&1 || { echo >&2 "I require tr but it's not installed.' Aborting."; exit 1; }
case $1 in
production|preprod)
platform=$1
;;
*)
echo 'Invalid parameter. Must be production or preprod'
exit 1;
;;
esac
data=$(curl --request GET \
--user $(whoami) \
--cookie-jar ~/.cookiejar \
https://deployment.gocd.routematch.com/go/api/pipelines/provision-$platform/history)
latestPipeline=$(jq '.pipelines[0].counter' <<< $data )
# Must remove the double quotes from the next value
latestStage=$(jq '.pipelines[0].stages[0].counter' <<< $data | tr -d '"' )
# Fetch the log of the latest run. It has the new IP address in it
# extract just the IP address from the log
newip=$(
curl --request GET \
--cookie ~/.cookiejar \
--silent \
--no-buffer \
https://deployment.gocd.routematch.com/go/files/provision-$platform/$latestPipeline/deploy/$latestStage/deploy/cruise-output/console.log \
-H 'Accept: application/vnd.go.cd.v1+json' | \
perl -lne 'm/changed: \[(\d+\.\d+\.\d+\.\d+)\] \=\> \(item\=rabbitmq-data\)/ && print ($1) && exit' )
echo New $platform IP: $newip
# Edit ~/.ssh/config to replace IP address for the named platform (saving a backup, of course)
perl -i.bak -0spe 'BEGIN {$platform=shift; $ip=shift} s/(Host .*?${platform}.*?HostName )(\d{1,3}\.){3}\d{1,3}/$1${ip}/migs' $platform $newip ~/.ssh/config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment