Last active
December 3, 2019 05:03
-
-
Save jobwat/f21263f45c03ef1e7d7c to your computer and use it in GitHub Desktop.
update .ssh/config with CloudFormation stack IPs
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 | |
aws_credentials_file=${HOME}/.aws/credentials | |
script_name=`basename $0` | |
error(){ | |
echo "[${script_name}] Error: $@. Exiting." | |
exit 1 | |
} | |
# set case insensitive for following regexp | |
shopt -s nocasematch | |
environment=$1 | |
[[ ! "${environment}" =~ (DEV|QA|UAT|PREPROD|PROD) ]] && error "ENV <QA|UAT|PREPROD|PROD> should be passed as first parameter." | |
[[ -z "${project}" ]] && error "'project' environment variable must be set." | |
#echo "env=${environment} proj=${project}" | |
case $environment in | |
dev|qa|uat) | |
aws_account="dev-account" | |
;; | |
test) | |
aws_account="test-account" | |
;; | |
preprod|prod) | |
aws_account="prod-acount" | |
;; | |
*) | |
error "ENV '${environment}' must be part of these: QA|UAT|PREPROD|PROD." | |
;; | |
esac | |
matched_account_name=`cat $aws_credentials_file | grep "^\[.*${aws_account}" | head -1 | sed -E 's/\[(.*)\]/\1/'` | |
[[ -z "${matched_account_name}" ]] && error "AWS account '${aws_account}' not existing in ${aws_credentials_file}." | |
echo ${matched_account_name} | |
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 | |
[[ -z $BASH_SOURCE ]] && export BASH_SOURCE=$0 | |
script_name=`basename $BASH_SOURCE` | |
script_path=`dirname $BASH_SOURCE` | |
ssh_config_file=$HOME/.ssh/config | |
error(){ | |
echo "[${script_name}] Error: $@" | |
exit 1 | |
} | |
[[ -z "${ENVIRONMENT}" ]] && error "Environment variable 'ENVIRONMENT' should be set to one of these <QA|UAT|PREPROD|PROD>." | |
aws_account=`${script_path}/guess_account.sh $ENVIRONMENT` | |
[[ -z "${aws_account}" ]] && error "<aws account name> missing" | |
aws_cmd(){ | |
AWS_COMMAND=$@ | |
aws --profile ${aws_account} --region ap-southeast-2 $AWS_COMMAND | |
} | |
STACK_NAME=$1 | |
[[ -z "${STACK_NAME}" ]] && error "STACK_NAME parameter missing!! | |
Available stacks: | |
`aws_cmd cloudformation list-stacks --stack-status-filter CREATE_COMPLETE | jq -r '.StackSummaries[].StackName' | sed -E 's/^/- /'` | |
" | |
#echo "Fetch autoscaling group..." && aws_cmd cloudformation list-stack-resources --stack-name $STACK_NAME > stack_resources | |
AUTOSCALING_GROUPS=`aws_cmd cloudformation list-stack-resources --stack-name $STACK_NAME | jq '.StackResourceSummaries | map( select(.["ResourceType"] == "AWS::AutoScaling::AutoScalingGroup") )[].PhysicalResourceId'` | |
for AUTOSCALING_GROUP in $AUTOSCALING_GROUPS; do | |
echo "AUTOSCALING_GROUP=$AUTOSCALING_GROUP" | |
aws_cmd ec2 describe-instances --filters "Name=tag:aws:autoscaling:groupName,Values=$AUTOSCALING_GROUP" "Name=instance-state-name,Values=running" | jq -r '.Reservations[].Instances[] | .Tags as $tags | .NetworkInterfaces[0].PrivateIpAddresses[0].PrivateIpAddress as $ip | ($tags|map(select(.Key=="Name"))|.[].Value) as $app | ($tags|map(select(.Key=="Environment"))|.[].Value) as $env | .Placement.AvailabilityZone as $az | [$app, $env, $az, $ip ] | join(":")' | while read instance; do | |
app=`echo $instance | cut -f1 -d ':'` | |
az=`echo $instance | cut -f3 -d ':'` | |
env=`echo $instance | cut -f2 -d ':'` | |
az_count_name=`echo $instance | cut -f3 -d ':' | sed 's/-//g'` | |
ip=`echo $instance | cut -f4 -d ':'` | |
eval `echo ${az_count_name}=$[${az_count_name=0} + 1]` | |
name="${app}_${env}_${az}_${!az_count_name}" | |
if grep "$name" $ssh_config_file >/dev/null 2>&1; then | |
echo "Updating $name to $ip" | |
perl -0777 -pi -e "s/(Host.*${name}.*\n.*Hostname).*/\1 $ip/i" $ssh_config_file | |
else | |
echo "ERROR: $name not found in $ssh_config_file" | |
fi | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment