|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# generic script for removing cloudformation stacks and proxygen deployed apis where the pull request is closed |
| 4 | + |
| 5 | +# set the repo name to be the name of the repo this is running in |
| 6 | +REPO_NAME=eps-assist-me |
| 7 | + |
| 8 | +# 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 |
| 9 | +CAPTURE_REGEX="^epsam-pr-(\\d+)(-sandbox)?$" |
| 10 | + |
| 11 | +# this should be a regex that is used to get the pull request id from the cloud formation stack name |
| 12 | +# this is used in a replace command to replace the stack name so what is left is just the pull request id |
| 13 | +PULL_REQUEST_STACK_REGEX=epsam-pr- |
| 14 | + |
| 15 | +CNAME_QUERY=epsam-pr- |
| 16 | + |
| 17 | +main() { |
| 18 | + delete_cloudformation_stacks |
| 19 | + delete_cname_records |
| 20 | +} |
| 21 | + |
| 22 | +delete_cloudformation_stacks() { |
| 23 | + echo "checking cloudformation stacks" |
| 24 | + echo |
| 25 | + ACTIVE_STACKS=$(aws cloudformation list-stacks | jq -r --arg CAPTURE_REGEX "${CAPTURE_REGEX}" '.StackSummaries[] | select ( .StackStatus != "DELETE_COMPLETE" ) | select( .StackName | capture($CAPTURE_REGEX) ) | .StackName ') |
| 26 | + |
| 27 | + mapfile -t ACTIVE_STACKS_ARRAY <<< "$ACTIVE_STACKS" |
| 28 | + |
| 29 | + for i in "${ACTIVE_STACKS_ARRAY[@]}" |
| 30 | + do |
| 31 | + echo "Checking if stack $i has open pull request" |
| 32 | + PULL_REQUEST=${i//${PULL_REQUEST_STACK_REGEX}/} |
| 33 | + PULL_REQUEST=${PULL_REQUEST//-sandbox/} |
| 34 | + echo "Checking pull request id ${PULL_REQUEST}" |
| 35 | + URL="https://api.github.com/repos/NHSDigital/${REPO_NAME}/pulls/${PULL_REQUEST}" |
| 36 | + RESPONSE=$(curl "${URL}" 2>/dev/null) |
| 37 | + STATE=$(echo "${RESPONSE}" | jq -r .state) |
| 38 | + if [ "$STATE" == "closed" ]; then |
| 39 | + echo "** going to delete stack $i as state is ${STATE} **" |
| 40 | + aws cloudformation delete-stack --stack-name "${i}" |
| 41 | + echo "** Sleeping for 60 seconds to avoid 429 on delete stack **" |
| 42 | + sleep 60 |
| 43 | + else |
| 44 | + echo "not going to delete stack $i as state is ${STATE}" |
| 45 | + fi |
| 46 | + done |
| 47 | +} |
| 48 | + |
| 49 | +delete_cname_records() { |
| 50 | + HOSTED_ZONE_ID=$(aws route53 list-hosted-zones-by-name --dns-name dev.eps.national.nhs.uk. | jq -r ".HostedZones[0] | .Id") |
| 51 | + CNAME_RECORDS=$(aws route53 list-resource-record-sets --hosted-zone-id "${HOSTED_ZONE_ID}" \ |
| 52 | + --query "ResourceRecordSets[?Type == 'CNAME' && contains(Name, '${CNAME_QUERY}')]" \ |
| 53 | + | jq -r " .[] | .Name") |
| 54 | + |
| 55 | + mapfile -t CNAME_RECORDS_ARRAY <<< "$CNAME_RECORDS" |
| 56 | + |
| 57 | + for i in "${CNAME_RECORDS_ARRAY[@]}" |
| 58 | + do |
| 59 | + echo "Checking if CNAME record $i has open pull request" |
| 60 | + |
| 61 | + PULL_REQUEST=$(echo "$i" | grep -Po '(?<=-pr-)\d+') |
| 62 | + echo "Checking pull request id ${PULL_REQUEST}" |
| 63 | + URL="https://api.github.com/repos/NHSDigital/${REPO_NAME}/pulls/${PULL_REQUEST}" |
| 64 | + RESPONSE=$(curl --url "${URL}" --header "Authorization: Bearer ${GITHUB_TOKEN}" 2>/dev/null) |
| 65 | + STATE=$(echo "${RESPONSE}" | jq -r .state) |
| 66 | + if [ "$STATE" == "closed" ]; then |
| 67 | + echo "** going to delete CNAME record $i as state is ${STATE} **" |
| 68 | + record_set=$(aws route53 list-resource-record-sets --hosted-zone-id "${HOSTED_ZONE_ID}" \ |
| 69 | + --query "ResourceRecordSets[?Name == '$i']" --output json | jq .[0]) |
| 70 | + |
| 71 | + jq -n --argjson record_set "${record_set}" \ |
| 72 | + '{Changes: [{Action: "DELETE", ResourceRecordSet: $record_set}]}' > /tmp/payload.json |
| 73 | + |
| 74 | + aws route53 change-resource-record-sets --hosted-zone-id "${HOSTED_ZONE_ID}" --change-batch file:///tmp/payload.json |
| 75 | + |
| 76 | + echo "CNAME record $i deleted" |
| 77 | + else |
| 78 | + echo "not going to delete CNAME record $i as state is ${STATE} **" |
| 79 | + fi |
| 80 | + done |
| 81 | +} |
| 82 | + |
| 83 | +main |
0 commit comments