#!/usr/bin/env bash THISDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "${THISDIR}/common.sh" delete_integ=0 # Helper functions usage() { cat >&2 <&2 usage exit 2 ;; esac shift done # Required arguments required_arg "--cluster" "${CLUSTER}" } delete_stack() { local stack_name="${1:?}" log INFO "Deleting Cloudformation stack '${stack_name}'" if ! aws cloudformation delete-stack \ --stack-name "${stack_name}"; then log ERROR "Failed to delete '${stack_name}'" return fi log INFO "Waiting for Cloudformation stack '${stack_name}' to be deleted" if ! aws cloudformation wait stack-delete-complete \ --stack-name "${stack_name}"; then log ERROR "Failed to wait for ${stack_name} to delete" aws cloudformation describe-stack-events \ --stack-name "${stack_name}" return fi log INFO "Cloudformation stack '${stack_name}' deleted!" } delete_services() { local cluster="${1:?}" log INFO "Deleting services running on cluster '${cluster}'" if ! services=$(aws ecs list-services \ --cluster ecs-updater-integ-cluster \ --query 'serviceArns[]' \ --output text); then log ERROR "Failed to list services in cluster '${cluster}'" return fi for service in ${services}; do log INFO "Deleting service '${service}'" if ! aws ecs delete-service \ --cluster "${cluster}" \ --service "${service}" \ --force >/dev/null; then log ERROR "Failed to delete service '${service}'" fi done } stop_tasks() { local cluster="${1:?}" log INFO "Stopping tasks running on cluster '${cluster}'" if ! tasks=$(aws ecs list-tasks \ --cluster ecs-updater-integ-cluster \ --query 'taskArns[]' \ --output text); then log ERROR "Failed to list tasks in cluster '${cluster}'" return fi for task in ${tasks}; do log INFO "Stopping task '${task}'" if ! aws ecs stop-task \ --cluster "${cluster}" \ --task "${task}" >/dev/null; then log ERROR "Failed to stop task '${task}'" fi done } terminate_instances() { local cluster="${1:?}" log INFO "Extracting auto-scaling group name from '${cluster}' stack" if ! output=$(aws cloudformation describe-stacks \ --stack-name "${cluster}" \ --output json \ --query 'Stacks[].Outputs[]'); then log ERROR "Failed to get outputs from '${cluster}' stack" return fi if ! auto_scaling_group=$(echo "${output}" | jq --raw-output '.[] | select(.OutputKey == "AutoScalingGroupName") | .OutputValue'); then log ERROR "Failed to extract auto scaling group name from '${cluster}' stack outputs" return fi log INFO "Describing auto-scaling group '${auto_scaling_group}' to get instance ids" if ! instance_ids=$(aws autoscaling describe-auto-scaling-groups \ --auto-scaling-group-name "${auto_scaling_group}" \ --query "AutoScalingGroups[].Instances[].InstanceId" \ --output text); then log ERROR "Failed to get instance ids from auto scaling group '${auto_scaling_group}'" return fi log INFO "Instances '${instance_ids}' found" log INFO "Setting auto scaling group desired count to zero" if ! aws autoscaling update-auto-scaling-group \ --auto-scaling-group-name "${auto_scaling_group}" \ --desired-capacity 0 \ --min-size 0; then log ERROR "Failed to change auto scaling group '${auto_scaling_group}' desired count to 0" return fi for inst_id in ${instance_ids}; do log INFO "Waiting for instance '${inst_id}' to terminate" if ! aws ec2 wait instance-terminated \ --instance-ids "${inst_id}"; then log ERROR "Failed to terminate instance '${inst_id}'" fi done } # Initial setup and checks parse_args "${@}" delete_stack "${UPDATER_STACK_PREFIX}${CLUSTER}" terminate_instances "${CLUSTER}" delete_services "${CLUSTER}" stop_tasks "${CLUSTER}" delete_stack "${CLUSTER}" if [[ "${delete_integ}" -eq 1 ]]; then delete_stack "${INTEG_STACK_NAME}" fi