Skip to content

Commit a671f06

Browse files
committed
Integrated all changes from aws-samples/aws-codedeploy-samples#45
1 parent 6c4a762 commit a671f06

File tree

3 files changed

+82
-57
lines changed

3 files changed

+82
-57
lines changed

scripts/common_functions.sh

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
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

5356
AWS_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
5861
set_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.
7072
get_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.
154154
remove_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.
375385
reset_waiter_timeout() {
376386
local elb=$1
377387
local state_name=$2
@@ -528,30 +538,11 @@ validate_elb() {
528538
get_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

scripts/deregister_from_elb.sh

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,27 @@ if [ $? == 0 -a -n "${asg}" ]; then
4949
fi
5050
fi
5151

52-
msg "Instance is not part of an ASG, continuing..."
52+
msg "Instance is not part of an ASG, trying with ELB..."
5353

54-
msg "Checking that user set at least one load balancer"
55-
if test -z "$ELB_LIST"; then
56-
error_exit "Must have at least one load balancer to deregister from"
54+
if [ -z "$ELB_LIST" ]; then
55+
error_exit "ELB_LIST is empty. Must have at least one load balancer to deregister from, or \"_all_\", \"_any_\" values."
56+
elif [ "${ELB_LIST}" = "_all_" ]; then
57+
msg "Automatically finding all the ELBs that this instance is registered to..."
58+
get_elb_list $INSTANCE_ID
59+
if [ $? != 0 ]; then
60+
error_exit "Couldn't find any. Must have at least one load balancer to deregister from."
61+
fi
62+
set_flag "ELBs" "$ELB_LIST"
63+
elif [ "${ELB_LIST}" = "_any_" ]; then
64+
msg "Automatically finding all the ELBs that this instance is registered to..."
65+
get_elb_list $INSTANCE_ID
66+
if [ $? != 0 ]; then
67+
msg "Couldn't find any, but ELB_LIST=any so finishing successfully without deregistering."
68+
set_flag "ELBs" ""
69+
finish_msg
70+
exit 0
71+
fi
72+
set_flag "ELBs" "$ELB_LIST"
5773
fi
5874

5975
# Loop through all LBs the user set, and attempt to deregister this instance from them.
@@ -73,7 +89,7 @@ for elb in $ELB_LIST; do
7389
fi
7490
done
7591

76-
# Wait for all Deregistrations to finish
92+
# Wait for all deregistrations to finish
7793
msg "Waiting for instance to de-register from its load balancers"
7894
for elb in $ELB_LIST; do
7995
wait_for_state "elb" $INSTANCE_ID "OutOfService" $elb

scripts/register_with_elb.sh

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,27 @@ if [ $? == 0 -a -n "${asg}" ]; then
4949
fi
5050
fi
5151

52-
msg "Instance is not part of an ASG, continuing..."
52+
msg "Instance is not part of an ASG, continuing with ELB"
5353

54-
msg "Checking that user set at least one load balancer"
55-
if test -z "$ELB_LIST"; then
56-
error_exit "Must have at least one load balancer to register to"
54+
if [ -z "$ELB_LIST" ]; then
55+
error_exit "ELB_LIST is empty. Must have at least one load balancer to register to, or \"_all_\", \"_any_\" values."
56+
elif [ "${ELB_LIST}" = "_all_" ]; then
57+
msg "Finding all the ELBs that this instance was previously registered to"
58+
if ! ELB_LIST=$(get_flag "ELBs"); then
59+
error_exit "$FLAGFILE doesn't exist or is unreadble"
60+
elif [ -z $ELB_LIST ]; then
61+
error_exit "Couldn't find any. Must have at least one load balancer to register to."
62+
fi
63+
elif [ "${ELB_LIST}" = "_any_" ]; then
64+
msg "Finding all the ELBs that this instance was previously registered to"
65+
if ! ELB_LIST=$(get_flag "ELBs"); then
66+
error_exit "$FLAGFILE doesn't exist or is unreadble"
67+
elif [ -z $ELB_LIST ]; then
68+
msg "Couldn't find any, but ELB_LIST=any so finishing successfully without registering."
69+
remove_flagfile
70+
finish_msg
71+
exit 0
72+
fi
5773
fi
5874

5975
# Loop through all LBs the user set, and attempt to register this instance to them.
@@ -73,7 +89,7 @@ for elb in $ELB_LIST; do
7389
fi
7490
done
7591

76-
# Wait for all Registrations to finish
92+
# Wait for all registrations to finish
7793
msg "Waiting for instance to register to its load balancers"
7894
for elb in $ELB_LIST; do
7995
wait_for_state "elb" $INSTANCE_ID "InService" $elb
@@ -82,4 +98,6 @@ for elb in $ELB_LIST; do
8298
fi
8399
done
84100

101+
remove_flagfile
102+
85103
finish_msg

0 commit comments

Comments
 (0)