-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdelete_stacks.sh
More file actions
executable file
·85 lines (68 loc) · 3.51 KB
/
delete_stacks.sh
File metadata and controls
executable file
·85 lines (68 loc) · 3.51 KB
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
#!/usr/bin/env bash
# generic script for removing cloudformation stacks and old CNAME records where the pull request is closed
# set the repo name to be the name of the repo this is running in
REPO_NAME=eps-prescription-status-update-api
# this should be a regex used in jq command that parses the output from aws cloudformation list-stacks and just captures stacks we are interested in
CAPTURE_REGEX="^psu-(cdk-)?pr-([0-9]+)(-sandbox|-stateful)?$"
# TODO: no longer needed?
# this should be a regex that is used to get the pull request id from the cloud formation stack name
# this is used in a replace command to replace the stack name so what is left is just the pull request id
#PULL_REQUEST_STACK_REGEX=psu-pr-
CNAME_QUERY=psu-pr-
main() {
delete_cloudformation_stacks
delete_cname_records
}
delete_cloudformation_stacks() {
echo "checking cloudformation stacks"
echo
ACTIVE_STACKS=$(aws cloudformation list-stacks | jq -r --arg CAPTURE_REGEX "${CAPTURE_REGEX}" '.StackSummaries[] | select ( .StackStatus != "DELETE_COMPLETE" ) | select( .StackName | capture($CAPTURE_REGEX) ) | .StackName ')
mapfile -t ACTIVE_STACKS_ARRAY <<< "$ACTIVE_STACKS"
for i in "${ACTIVE_STACKS_ARRAY[@]}"
do
echo "Checking if stack $i has open pull request"
# Extract PR number from stack names like psu-pr-123, psu-pr-123-sandbox,
# psu-cdk-pr-123, or psu-cdk-pr-123-stateful
PULL_REQUEST=$(echo "${i}" | sed 's/.*-pr-//' | sed 's/-.*//')
echo "Checking pull request id ${PULL_REQUEST}"
URL="https://api.github.com/repos/NHSDigital/${REPO_NAME}/pulls/${PULL_REQUEST}"
RESPONSE=$(curl "${URL}" 2>/dev/null)
STATE=$(echo "${RESPONSE}" | jq -r .state)
if [ "$STATE" == "closed" ]; then
echo "** going to delete stack $i as state is ${STATE} **"
aws cloudformation delete-stack --stack-name "${i}"
echo "** Sleeping for 60 seconds to avoid 429 on delete stack **"
sleep 60
else
echo "not going to delete stack $i as state is ${STATE}"
fi
done
}
delete_cname_records() {
HOSTED_ZONE_ID=$(aws route53 list-hosted-zones-by-name --dns-name dev.eps.national.nhs.uk. | jq -r ".HostedZones[0] | .Id")
CNAME_RECORDS=$(aws route53 list-resource-record-sets --hosted-zone-id "${HOSTED_ZONE_ID}" \
--query "ResourceRecordSets[?Type == 'CNAME' && contains(Name, '${CNAME_QUERY}')]" \
| jq -r " .[] | .Name")
mapfile -t CNAME_RECORDS_ARRAY <<< "$CNAME_RECORDS"
for i in "${CNAME_RECORDS_ARRAY[@]}"
do
echo "Checking if CNAME record $i has open pull request"
PULL_REQUEST=$(echo "$i" | grep -Po '(?<=-pr-)\d+')
echo "Checking pull request id ${PULL_REQUEST}"
URL="https://api.github.com/repos/NHSDigital/${REPO_NAME}/pulls/${PULL_REQUEST}"
RESPONSE=$(curl --url "${URL}" --header "Authorization: Bearer ${GITHUB_TOKEN}" 2>/dev/null)
STATE=$(echo "${RESPONSE}" | jq -r .state)
if [ "$STATE" == "closed" ]; then
echo "** going to delete CNAME record $i as state is ${STATE} **"
record_set=$(aws route53 list-resource-record-sets --hosted-zone-id "${HOSTED_ZONE_ID}" \
--query "ResourceRecordSets[?Name == '$i']" --output json | jq .[0])
jq -n --argjson record_set "${record_set}" \
'{Changes: [{Action: "DELETE", ResourceRecordSet: $record_set}]}' > /tmp/payload.json
aws route53 change-resource-record-sets --hosted-zone-id "${HOSTED_ZONE_ID}" --change-batch file:///tmp/payload.json
echo "CNAME record $i deleted"
else
echo "not going to delete CNAME record $i as state is ${STATE} **"
fi
done
}
main