Skip to content

Instantly share code, notes, and snippets.

@tsaavik
Created May 30, 2023 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsaavik/fbad15ac31b9d524e1ccbdb18a93e244 to your computer and use it in GitHub Desktop.
Save tsaavik/fbad15ac31b9d524e1ccbdb18a93e244 to your computer and use it in GitHub Desktop.
AWS CESI Script
#!/bin/bash
#
# This script grabs the Name, Ip and environment tags from aws.
# It then grabs a cesi conf header file and appends (using heredocs).
config="/opt/cesi/defaults/cesi.conf.toml"
tmpconf="/tmp/cesi.conf.toml"
# Create the header in our new copy of conf file
cp -f "${config}.head" ${tmpconf}
# fetch name, ip, staging from ec2 and stuff into config using heredoc
while read -r ip name environment; do
cat << EOF >> ${tmpconf}
[[nodes]]
name = "${name} - ${ip}"
environment = "${environment}"
username = ""
password = ""
host = "${ip}"
port = "9001"
EOF
done < <(aws ec2 describe-instances \
--query 'Reservations[*].Instances[*].{Instance:InstanceId,Instance:PrivateIpAddress,environment:Tags[?Key==`environment`]|[0].Val
ue,Name:Tags[?Key==`Name`]|[0].Value}' \
--output text |sort -k 2)
#Any changes? Then copy new conf into place and restart cesi
if ! diff ${tmpconf} ${config} ;then
echo "Changes detected, overwriting cesi conf and signaling config reload"
cp ${tmpconf} ${config}
pkill -HUP -f cesi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment