diff --git a/destroy.sh b/destroy.sh index 80daa1db..3bcd57f5 100755 --- a/destroy.sh +++ b/destroy.sh @@ -1,40 +1,70 @@ #!/bin/bash +set -e + +if [[ $1 == "" ]] ; then + echo 'ERROR: You must pass a stage to destroy. Ex. sh destroy.sh my-stage-name' + exit 1 +fi +stage=$1 +echo "\nCollecting information on stage $stage before attempting a destroy... This can take a minute or two..." + +# Find buckets associated with stage +# Unfortunately, I can't get all buckets and all associaged tags in a single CLI call (that I know of) +# So this can be pretty slow, depending on how many buckets exist in the account +# We get all bucket names, then find associated tags for each one-by-one +bucketList=(`aws s3api list-buckets --output text --query 'Buckets[*].Name'` ) +filteredBucketList=() +set +e +for i in "${bucketList[@]}" +do + stage_tag=`aws s3api get-bucket-tagging --bucket $i --output text --query 'TagSet[?Key==\`STAGE\`].Value' 2>/dev/null` + if [ "$stage_tag" == "$stage" ]; then + filteredBucketList+=($i) + fi +done set -e -stage=${1:-dev} - -# Clean up existing, non-empty buckets -pushd services -ui_bucket_name=`./output.sh ui S3BucketName $stage` -aws s3 rm s3://$ui_bucket_name --recursive -uploads_bucket_name=`./output.sh uploads AttachmentsBucketName $stage` -aws s3 rm s3://$uploads_bucket_name --recursive -popd - -services=( - 'database' - 'uploads' - 'uploads-scan' - 'app-api' - 'elasticsearch-auth' - 'elasticsearch' - 'stream-functions' - 'ui-auth' - 'ui' - # Running remove on ui-src would delete the s3 bucket and cause remove on ui to fail. - # We empty the bucket near the top of this file, and allow ui to delete it - # 'ui-src' -) - -deploy() { - service=$1 - pushd services/$service - yarn install - serverless remove --stage $stage - popd -} - -for (( idx=${#services[@]}-1 ; idx>=0 ; idx-- )) ; do - deploy ${services[idx]} +# Find cloudformation stacks associated with stage +filteredStackList=(`aws cloudformation describe-stacks | jq -r ".Stacks[] | select(.Tags[] | select(.Key==\"STAGE\") | select(.Value==\"$stage\")) | .StackName"`) + + +echo """ +******************************************************************************** +- Check the following carefully - +******************************************************************************** +""" + +echo "The following buckets will be emptied" +printf '%s\n' "${filteredBucketList[@]}" + +echo "\nThe following stacks will be destroyed:" +printf '%s\n' "${filteredStackList[@]}" + +echo """ +******************************************************************************** +- Scroll up and check carefully - +******************************************************************************** +""" + +read -p "Do you wish to continue? Re-enter the stage name to continue: " -r +echo +if [[ ! $REPLY == "$stage" ]] +then + echo "Stage name not re-entered. Doing nothing and exiting." + exit 1 +fi + + +for i in "${filteredBucketList[@]}" +do + echo $i + aws s3 rm s3://$i/ --recursive +done + + +for i in "${filteredStackList[@]}" +do + echo $i + aws cloudformation delete-stack --stack-name $i done