-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Destroy via Cloudformation and the CLI. This is more forceful but does a better job of cleaning up and is also not bound to the version of code that was deployed. * just adding some comments * Hide warning when the tagset doesnt exist Co-authored-by: Mike Dial <mdial@collabralink.com>
- Loading branch information
Showing
1 changed file
with
65 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |