Skip to content

Instantly share code, notes, and snippets.

@muhqu
Created February 25, 2014 12:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muhqu/76264d73d42edbb75263 to your computer and use it in GitHub Desktop.
Save muhqu/76264d73d42edbb75263 to your computer and use it in GitHub Desktop.
#!/bin/bash
while true; do
echo;
as-describe-auto-scaling-groups --show-long | awk '!/batch-/' | tee all-asg-desc
aws autoscaling describe-auto-scaling-groups > all_asgs.json
cat all_asgs.json | underscore process '
_.filter(map(data.AutoScalingGroups, function (asg) {
var dlc = asg.LaunchConfigurationName,
insNeedReplace = [];
_.each(asg.Instances, function (ins) {
if (ins.LaunchConfigurationName != dlc) {
insNeedReplace.push(ins);
}
})
return {
AutoScalingGroupName: asg.AutoScalingGroupName,
DesiredCapacity: asg.DesiredCapacity,
LaunchConfigurationName: dlc,
InstancesToTerminate: insNeedReplace
};
}), function (asg) {
return asg.InstancesToTerminate.length > 0;
});
'
echo;
date;
cat all-asg-desc | awk 'BEGIN{FS=","}{if ($1 == "AUTO-SCALING-GROUP") { A=$2; C=$3; D=$10; K=0; } if ($1 == "INSTANCE" && $7 != C && K++ < (D>8?4:(D>4?2:1))) print (D>1 ? A"\tterminate-instance\t"$2 : A"\tincrease-capacity\t"(D+1)); }' | tee rolling-tasks
if [ ! -s rolling-tasks ]; then
echo '> no more rolling tasks, yay work done.'
break;
fi
cat rolling-tasks | awk '/increase-capacity/{print $1" --desired-capacity "$3}' | xargs -r -L1 -P4 -t as-update-auto-scaling-group
cat rolling-tasks | awk '/terminate-instance/{print $3" --no-decrement-desired-capacity --force"}' | xargs -r -L1 -P4 -t as-terminate-instance-in-auto-scaling-group
echo '> sleep 2 min...'
sleep 120s;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment