1414# permissions and limitations under the License.
1515
1616# ELB_LIST defines which Elastic Load Balancers this instance should be part of.
17- # The elements in ELB_LIST should be seperated by space.
18- ELB_LIST=" "
17+ # The elements in ELB_LIST should be separated by space. Safe default is "".
18+ # Set to "_all_" to automatically find all load balancers the instance is registered to.
19+ # Set to "_any_" will work as "_all_" but will not fail if instance is not attached to
20+ # any ASG or ELB, giving flexibility.
21+ ELB_LIST=" _any_"
1922
2023# Under normal circumstances, you shouldn't need to change anything below this line.
2124# -----------------------------------------------------------------------------
@@ -52,30 +55,25 @@ get_instance_region() {
5255
5356AWS_CLI=" aws --region $( get_instance_region) "
5457
55- # Usage: set_flag <flag>
58+ # Usage: set_flag <flag> <value>
5659#
57- # Writes <flag> to FLAGFILE
60+ # Writes <flag>=<value> to FLAGFILE
5861set_flag () {
59- if echo " $1 =true" >> $FLAGFILE ; then
60- # msg "$1 flag written to $FLAGFILE"
62+ if echo " $1 =$2 " >> $FLAGFILE ; then
6163 return 0
6264 else
63- error_exit " $1 flag NOT written to $FLAGFILE "
65+ error_exit " Unable to write flag \" $1 = $2 \" to $FLAGFILE "
6466 fi
6567}
6668
6769# Usage: get_flag <flag>
6870#
69- # Checks if <flag> is true in FLAGFILE
71+ # Checks for <flag> in FLAGFILE. Echoes it's value and returns 0 on success or non-zero if it fails to read the file.
7072get_flag () {
71- if [ -e $FLAGFILE ]; then
72- if grep " $1 =true" $FLAGFILE > /dev/null; then
73- # msg "$1 flag found in $FLAGFILE"
74- return 0
75- else
76- # msg "$1 flag NOT found in $FLAGFILE"
77- return 1
78- fi
73+ if [ -r $FLAGFILE ]; then
74+ local result=$( awk -F= -v flag=" $1 " ' {if ( $1 == flag ) {print $2}}' $FLAGFILE )
75+ echo " ${result} "
76+ return 0
7977 else
8078 # FLAGFILE doesn't exist
8179 return 1
@@ -100,13 +98,13 @@ check_suspended_processes() {
10098 msg " This processes were suspended on the ASG before starting: ${suspended[*]} "
10199 fi
102100
103- # If "Launch" process is suspended abort because we will not be able to recover from StandBy
101+ # If "Launch" process is suspended abort because we will not be able to recover from StandBy. Note the "[[ ... =~" bashism.
104102 if [[ " ${suspended[@]} " =~ " Launch" ]]; then
105103 error_exit " 'Launch' process of AutoScaling is suspended which will not allow us to recover the instance from StandBy. Aborting."
106104 fi
107105
108106 for process in ${suspended[@]} ; do
109- set_flag $process
107+ set_flag " $process " " true "
110108 done
111109}
112110
@@ -134,7 +132,9 @@ resume_processes() {
134132 local -a to_resume
135133
136134 for p in ${processes[@]} ; do
137- if ! get_flag " $p " ; then
135+ if ! local tmp_flag_value=$( get_flag " $p " ) ; then
136+ error_exit " $FLAGFILE doesn't exist or is unreadable"
137+ elif [ ! " $tmp_flag_value " = " true" ] ; then
138138 to_resume=(" ${to_resume[@]} " " $p " )
139139 fi
140140 done
@@ -150,12 +150,13 @@ resume_processes() {
150150
151151# Usage: remove_flagfile
152152#
153- # Removes FLAGFILE
153+ # Removes FLAGFILE. Returns non-zero if failure.
154154remove_flagfile () {
155- if rm -f $FLAGFILE ; then
155+ if rm $FLAGFILE ; then
156156 msg " Successfully removed flagfile $FLAGFILE "
157+ return 0
157158 else
158- error_exit " Failed to remove flagfile $FLAGFILE ."
159+ msg " WARNING: Failed to remove flagfile $FLAGFILE ."
159160 fi
160161}
161162
@@ -166,7 +167,7 @@ finish_msg() {
166167 msg " Finished $( basename $0 ) at $( /bin/date " +%F %T" ) "
167168
168169 end_sec=$( /bin/date +%s.%N)
169- elapsed_seconds=$( echo " $end_sec - $start_sec " | /usr/bin/bc )
170+ elapsed_seconds=$( echo " $end_sec " " $start_sec " | awk ' { print $1 - $2 } ' )
170171
171172 msg " Elapsed time: $elapsed_seconds "
172173}
@@ -241,6 +242,7 @@ autoscaling_enter_standby() {
241242 if [ -z " $min_cap " -o -z " $desired_cap " ]; then
242243 msg " Unable to determine minimum and desired capacity for ASG ${asg_name} ."
243244 msg " Attempting to put this instance into standby regardless."
245+ set_flag " asgmindecremented" " false"
244246 elif [ $min_cap == $desired_cap -a $min_cap -gt 0 ]; then
245247 local new_min=$(( $min_cap - 1 ))
246248 msg " Decrementing ASG ${asg_name} 's minimum size to $new_min "
@@ -253,8 +255,11 @@ autoscaling_enter_standby() {
253255 else
254256 msg " ASG ${asg_name} 's minimum size has been decremented, creating flag in file $FLAGFILE "
255257 # Create a "flag" denote that the ASG min has been decremented
256- set_flag asgmindecremented
258+ set_flag " asgmindecremented" " true "
257259 fi
260+ else
261+ msg " No need to decrement ASG ${asg_name} 's minimum size"
262+ set_flag " asgmindecremented" " false"
258263 fi
259264
260265 msg " Putting instance $instance_id into Standby"
@@ -320,7 +325,9 @@ autoscaling_exit_standby() {
320325 return 1
321326 fi
322327
323- if get_flag asgmindecremented; then
328+ if ! local tmp_flag_value=$( get_flag " asgmindecremented" ) ; then
329+ error_exit " $FLAGFILE doesn't exist or is unreadable"
330+ elif [ " $tmp_flag_value " = " true" ]; then
324331 local min_desired=$( $AWS_CLI autoscaling describe-auto-scaling-groups \
325332 --auto-scaling-group-name " ${asg_name} " \
326333 --query ' AutoScalingGroups[0].[MinSize, DesiredCapacity]' \
@@ -372,6 +379,9 @@ get_instance_state_asg() {
372379 fi
373380}
374381
382+ # Usage: reset_waiter_timeout <ELB name> <state name>
383+ #
384+ # Resets the timeout value to account for the ELB timeout and also connection draining.
375385reset_waiter_timeout () {
376386 local elb=$1
377387 local state_name=$2
@@ -528,30 +538,11 @@ validate_elb() {
528538get_elb_list () {
529539 local instance_id=$1
530540
531- local asg_name=$( $AWS_CLI autoscaling describe-auto-scaling-instances \
532- --instance-ids $instance_id \
533- --query AutoScalingInstances[* ].AutoScalingGroupName \
534- --output text | sed -e $' s/\t / /g' )
535541 local elb_list=" "
536542
537- if [ -z " ${asg_name} " ]; then
538- msg " Instance is not part of an ASG. Looking up from ELB."
539- local all_balancers=$( $AWS_CLI elb describe-load-balancers \
540- --query LoadBalancerDescriptions[* ].LoadBalancerName \
541- --output text | sed -e $' s/\t / /g' )
542- for elb in $all_balancers ; do
543- local instance_health
544- instance_health=$( get_instance_health_elb $instance_id $elb )
545- if [ $? == 0 ]; then
546- elb_list=" $elb_list $elb "
547- fi
548- done
549- else
550- elb_list=$( $AWS_CLI autoscaling describe-auto-scaling-groups \
551- --auto-scaling-group-names " ${asg_name} " \
552- --query AutoScalingGroups[* ].LoadBalancerNames \
553- --output text | sed -e $' s/\t / /g' )
554- fi
543+ elb_list=$( $AWS_CLI elb describe-load-balancers \
544+ --query $' LoadBalancerDescriptions[].[join(`,`,Instances[?InstanceId==`' $instance_id ' `].InstanceId),LoadBalancerName]' \
545+ --output text | grep $instance_id | awk ' {ORS=" ";print $2}' )
555546
556547 if [ -z " $elb_list " ]; then
557548 return 1
0 commit comments