-
-
Save nwalke/dcdb4cf5784e0d704954f863544559b2 to your computer and use it in GitHub Desktop.
This file contains 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
autoscaling_enter_standby() { | |
local instance_id=$1 | |
local asg_name=${2} | |
msg "Checking if this instance has already been moved in the Standby state" | |
local instance_state=$(get_instance_state_asg $instance_id) | |
if [ $? != 0 ]; then | |
msg "Unable to get this instance's lifecycle state." | |
return 1 | |
fi | |
if [ "$instance_state" == "Standby" ]; then | |
msg "Instance is already in Standby; nothing to do." | |
return 0 | |
fi | |
if [ "$instance_state" == "Pending:Wait" ]; then | |
msg "Instance is Pending:Wait; nothing to do." | |
return 0 | |
fi | |
if [ "$HANDLE_PROCS" = "true" ]; then | |
msg "Checking ASG ${asg_name} suspended processes" | |
check_suspended_processes | |
# Suspend troublesome processes while deploying | |
suspend_processes | |
fi | |
msg "Checking to see if Lambda has added item to DynamoDB table" | |
wait_for_dynamodb_record | |
msg "Checking to see if ASG ${asg_name} will let us decrease desired capacity" | |
local min_desired=$($AWS_CLI autoscaling describe-auto-scaling-groups \ | |
--auto-scaling-group-name "${asg_name}" \ | |
--query 'AutoScalingGroups[0].[MinSize, DesiredCapacity]' \ | |
--output text) | |
local min_cap=$(echo $min_desired | awk '{print $1}') | |
local desired_cap=$(echo $min_desired | awk '{print $2}') | |
if [ -z "$min_cap" -o -z "$desired_cap" ]; then | |
msg "Unable to determine minimum and desired capacity for ASG ${asg_name}." | |
msg "Attempting to put this instance into standby regardless." | |
set_flag "asgmindecremented" "false" | |
elif [ $min_cap == $desired_cap -a $min_cap -gt 0 ]; then | |
local new_min=$(($min_cap - 1)) | |
msg "Decrementing ASG ${asg_name}'s minimum size to $new_min" | |
msg $($AWS_CLI autoscaling update-auto-scaling-group \ | |
--auto-scaling-group-name "${asg_name}" \ | |
--min-size $new_min) | |
if [ $? != 0 ]; then | |
msg "Failed to reduce ASG ${asg_name}'s minimum size to $new_min. Cannot put this instance into Standby." | |
return 1 | |
else | |
msg "ASG ${asg_name}'s minimum size has been decremented, creating flag in file $FLAGFILE" | |
# Create a "flag" denote that the ASG min has been decremented | |
set_flag "asgmindecremented" "true" | |
fi | |
else | |
msg "No need to decrement ASG ${asg_name}'s minimum size" | |
set_flag "asgmindecremented" "false" | |
fi | |
msg "Putting instance $instance_id into Standby" | |
$AWS_CLI autoscaling enter-standby \ | |
--instance-ids $instance_id \ | |
--auto-scaling-group-name "${asg_name}" \ | |
--should-decrement-desired-capacity | |
if [ $? != 0 ]; then | |
msg "Failed to put instance $instance_id into Standby for ASG ${asg_name}." | |
return 1 | |
fi | |
msg "Waiting for move to Standby to finish" | |
wait_for_state "autoscaling" $instance_id "Standby" | |
if [ $? != 0 ]; then | |
local wait_timeout=$(($WAITER_INTERVAL * $WAITER_ATTEMPTS)) | |
msg "Instance $instance_id did not make it to standby after $wait_timeout seconds" | |
return 1 | |
fi | |
return 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment