-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdestroy.sh
executable file
·114 lines (93 loc) · 4.57 KB
/
destroy.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/bin/bash
# This is a simple bash script for the BBB Application Infrastructure deployment.
# It basically glues together the parts running in loose coupeling during the deployment and helps to speed things up which
# otherwise would have to be noted down and put into the command line.
# This can be migrated into real orchestration / automation toolsets if needed (e.g. Ansible, Puppet or Terraform)
# created by David Surey - [email protected]
# Disclaimber: NOT FOR PRODUCTION USE - Only for demo and testing purposes
ERROR_COUNT=0;
if [[ $# -lt 2 ]] ; then
echo 'arguments missing, please the aws profile string (-p) and the deployment Stack Name (-s)'
exit 1
fi
while getopts ":p:s:" opt; do
case $opt in
p) BBBPROFILE="$OPTARG"
;;
s) BBBSTACK="$OPTARG"
;;
\?) echo "Invalid option -$OPTARG" >&2
;;
esac
done
if ! [ -x "$(command -v aws)" ]; then
echo 'Error: aws cli is not installed.' >&2
exit 1
fi
echo "using AWS Profile $BBBPROFILE"
echo "##################################################"
# Destroy the BBB infrastructure.
echo "Delete the BBB Environment"
echo "##################################################"
aws cloudformation delete-stack --profile=$BBBPROFILE --stack-name $BBBSTACK
aws cloudformation wait stack-delete-complete --profile=$BBBPROFILE --stack-name $BBBSTACK
echo "##################################################"
echo "Deletion finished"
# Destroy Bucket and ECR
echo "deleting the Prerequisites stacks"
echo "##################################################"
BBBPREPSTACK="${BBBSTACK}-Sources"
SOURCE=`aws cloudformation describe-stacks --profile=$BBBPROFILE --query "Stacks[0].Outputs[0].OutputValue" --stack-name $BBBPREPSTACK`
SOURCE=`echo "${SOURCE//\"}"`
echo "##################################################"
echo "Truncate the S3 Bucket"
echo "##################################################"
#aws s3 rm --profile=$BBBPROFILE s3://$SOURCE --recursive
aws s3api --profile=$BBBPROFILE list-object-versions \
--bucket $SOURCE \
--query "Versions[].Key" \
--output json | jq 'unique' | jq -r '.[]' | while read key; do
echo "deleting versions of $key"
aws s3api --profile=$BBBPROFILE list-object-versions \
--bucket $SOURCE \
--prefix $key \
--query "Versions[].VersionId" \
--output json | jq 'unique' | jq -r '.[]' | while read version; do
echo "deleting $version"
aws s3api --profile=$BBBPROFILE delete-object \
--bucket $SOURCE \
--key $key \
--version-id $version
done
done
aws cloudformation delete-stack --stack-name $BBBPREPSTACK --profile=$BBBPROFILE
aws cloudformation wait stack-delete-complete --profile=$BBBPROFILE --stack-name $BBBPREPSTACK
ENVIRONMENTTYPE=$(jq -r ".Parameters.BBBEnvironmentType" bbb-on-aws-param.json)
if [ "$ENVIRONMENTTYPE" == 'scalable' ]
then
BBBECRStack="${BBBSTACK}-registry"
GREENLIGHTREGISTRY=`aws cloudformation describe-stacks --profile=$BBBPROFILE --query "Stacks[0].Outputs[0].OutputValue" --stack-name $BBBECRStack`
GREENLIGHTREGISTRY=`echo "${GREENLIGHTREGISTRY//\"}"`
SCALEILITEREGISTRY=`aws cloudformation describe-stacks --profile=$BBBPROFILE --query "Stacks[0].Outputs[1].OutputValue" --stack-name $BBBECRStack`
SCALEILITEREGISTRY=`echo "${SCALEILITEREGISTRY//\"}"`
echo $GREENLIGHTREGISTRY
echo $SCALEILITEREGISTRY
echo "##################################################"
echo "Truncate and delete the ECR Repositories"
echo "##################################################"
IMAGESGREENLIGHT=$(aws --profile $BBBPROFILE ecr describe-images --repository-name $GREENLIGHTREGISTRY --output json | jq '.[]' | jq '.[]' | jq "select (.imagePushedAt > 0)" | jq -r '.imageDigest')
for IMAGE in ${IMAGESGREENLIGHT[*]}; do
echo "Deleting $IMAGE"
aws ecr --profile $BBBPROFILE batch-delete-image --repository-name $GREENLIGHTREGISTRY --image-ids imageDigest=$IMAGE
done
IMAGESSCALEILITE=$(aws --profile $BBBPROFILE ecr describe-images --repository-name $SCALEILITEREGISTRY --output json | jq '.[]' | jq '.[]' | jq "select (.imagePushedAt > 0)" | jq -r '.imageDigest')
for IMAGE in ${IMAGESSCALEILITE[*]}; do
echo "Deleting $IMAGE"
aws ecr --profile $BBBPROFILE batch-delete-image --repository-name $SCALEILITEREGISTRY --image-ids imageDigest=$IMAGE
done
aws cloudformation delete-stack --profile=$BBBPROFILE --stack-name $BBBECRStack
aws cloudformation wait stack-delete-complete --profile=$BBBPROFILE --stack-name $BBBECRStack
fi
echo "##################################################"
echo "Deletion done"
exit 0