#use of "hash" chosen as it is a shell builtin and will add programs to hash table, possibly speeding execution. Use of type also considered - open to suggestions.
hash$prerequisite&> /dev/null
if[[$?==1]]#has exits with exit status of 70, executable was not found
thenecho"In order to use $app_name, the executable \"$prerequisite\" must be installed." 1>&2;exit70
fi
done
}
return_as_initial_maxsize()
{
if[[$max_size_change -eq 1]]
thenecho"$asg_group_name had its max-size increased temporarily by 1 to a max-size of $asg_temporary_max_size. $app_name will now return the max-size of $asg_group_name to its original max-size of $asg_initial_max_size."
echo"$asg_group_name had its desired-capacity increased temporarily by 1 to a desired-capacity of $asg_temporary_desired_capacity. $app_name will now return the desired-capacity of $asg_group_name to its original desired-capacity of $asg_initial_desired_capacity."
#max_size_change is used as a "flag" to determine if the max-size of an Auto Scaling Group was changed
max_size_change="0"
inservice_time_allowed=300
inservice_polling_time=10
delimiter="%"
#calls prerequisitecheck function to ensure that all executables required for script execution are available
prerequisitecheck
#handles options processing
whilegetopts :a:t:r:i: opt
do
case$opt in
a)asg_group_name="$OPTARG";;
t)elb_timeout="$OPTARG";;
r)region="$OPTARG";;
i)inservice_time_allowed="$OPTARG";;
*)echo"Error with Options Input. Cause of failure is most likely that an unsupported parameter was passed or a parameter was passed without a corresponding option." 1>&2;exit64;;
esac
done
#validate elb_timeout is number
##code to be written
if[[ -z $asg_group_name]]
thenecho"You did not specify an Auto Scaling Group name. In order to use $app_name you must specify an Auto Scaling Group name using -a <autoscalingroupname>." 1>&2;exit64
#validate - the pipeline of echo -e "$asg_result" | grep -c "AutoScalingGroupARN" must only return one group found - in the case below - more than one group has been found
thenecho"More than one Auto Scaling Group found. As more than one Auto Scaling Group has been found, $app_name does not know which Auto Scaling Group should have Instances terminated." 1>&2;exit64
thenecho"No Auto Scaling Group was found. Because no Auto Scaling Group has been found, $app_name does not know which Auto Scaling Group should have Instances terminated." 1>&2;exit64
#confirms that certain Auto Scaling processes are not suspended. For certain processes, the "Suspending Processing" state prevents the termination of Auto Scaling Group instances and thus prevents aws-ha-release from running properly.
thenecho"Scaling Process $process for the Auto Scaling Group $asg_group_name is currently suspended. $app_name will now exit as Scaling Processes ${necessary_processes[@]} are required for $app_name to run properly." 1>&2;exit77
#if the desired-capacity of an Auto Scaling Group group is greater than or equal to the max-size of an Auto Scaling Group, the max-size must be increased by 1 to cycle instances while maintaining desired-capacity. This is particularly true of groups of 1 instance (where we'd be removing all instances if we cycled).
thenecho"$asg_group_name has a max-size of $asg_initial_max_size. In order to recycle instances max-size will be temporarily increased by 1 to max-size $asg_temporary_max_size."
#increase groups desired capacity to allow for instance recycling without decreasing available instances below initial capacity
echo"$asg_group_name is currently at $asg_initial_desired_capacity desired-capacity. $app_name will increase desired-capacity by 1 to desired-capacity $asg_temporary_desired_capacity."
thenecho"During the last $inservice_time_allowed seconds the InService capacity of the $asg_group_name Auto Scaling Group did not meet the Auto Scaling Group's desired capacity of $asg_temporary_desired_capacity." 1>&2
echo"Because we can't be sure that instances created by this script are healthy, settings that were changed are being left as is. Settings that were changed:"
echo$inservice_instance_count"Instances are InService status. $asg_temporary_desired_capacity Instances are required to terminate the next instance. $inservice_time_taken seconds have elapsed while waiting for an Instance to reach InService status."
#sleep for "elb_timeout" seconds so that the instance can complete all processing before being terminated
sleep $elb_timeout
#terminates a pre-existing instance within the autoscaling group
echo"Instance $instance_selected will now be terminated. By terminating this Instance, the actual capacity will be decreased to 1 under desired-capacity."