Skip to content

Commit 162f2fc

Browse files
committed
Add github scripts
1 parent 26ba5f8 commit 162f2fc

File tree

5 files changed

+164
-0
lines changed

5 files changed

+164
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
3+
cat <<EOF > payload.json
4+
{
5+
"currentTag": "$CURRENT_DEPLOYED_TAG",
6+
"targetTag": "$DEV_TAG",
7+
"repoName": "eps-assist-me",
8+
"targetEnvironment": "$ENV",
9+
"productName": "EPS Assist Me",
10+
"releaseNotesPageId": "$PAGE_ID",
11+
"releaseNotesPageTitle": "Current EPSAM release notes - $ENV"
12+
}
13+
EOF
14+
cat payload.json
15+
16+
function_arn=$(aws cloudformation list-exports --query "Exports[?Name=='release-notes:CreateReleaseNotesLambdaArn'].Value" --output text)
17+
aws lambda invoke --function-name "${function_arn}" --cli-binary-format raw-in-base64-out --payload file://payload.json out.txt
18+
cat out.txt

.github/scripts/delete_stacks.sh

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

.github/scripts/fix_cdk_json.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
# script used to set context key values in cdk.json pre deployment from environment variables
5+
6+
# helper function to set string values
7+
fix_string_key() {
8+
KEY_NAME=$1
9+
KEY_VALUE=$2
10+
if [ -z "${KEY_VALUE}" ]; then
11+
echo "${KEY_NAME} value is unset or set to the empty string"
12+
exit 1
13+
fi
14+
echo "Setting ${KEY_NAME}"
15+
jq \
16+
--arg key_value "${KEY_VALUE}" \
17+
--arg key_name "${KEY_NAME}" \
18+
'.context += {($key_name): $key_value}' .build/cdk.json > .build/cdk.new.json
19+
mv .build/cdk.new.json .build/cdk.json
20+
}
21+
22+
# helper function to set boolean and number values (without quotes)
23+
fix_boolean_number_key() {
24+
KEY_NAME=$1
25+
KEY_VALUE=$2
26+
if [ -z "${KEY_VALUE}" ]; then
27+
echo "${KEY_NAME} value is unset or set to the empty string"
28+
exit 1
29+
fi
30+
echo "Setting ${KEY_NAME}"
31+
jq \
32+
--argjson key_value "${KEY_VALUE}" \
33+
--arg key_name "${KEY_NAME}" \
34+
'.context += {($key_name): $key_value}' .build/cdk.json > .build/cdk.new.json
35+
mv .build/cdk.new.json .build/cdk.json
36+
}
37+
38+
# get some values from AWS
39+
TRUSTSTORE_BUCKET_ARN=$(aws cloudformation describe-stacks --stack-name account-resources --query "Stacks[0].Outputs[?OutputKey=='TrustStoreBucket'].OutputValue" --output text)
40+
TRUSTSTORE_BUCKET_NAME=$(echo "${TRUSTSTORE_BUCKET_ARN}" | cut -d ":" -f 6)
41+
TRUSTSTORE_VERSION=$(aws s3api list-object-versions --bucket "${TRUSTSTORE_BUCKET_NAME}" --prefix "${TRUSTSTORE_FILE}" --query 'Versions[?IsLatest].[VersionId]' --output text)
42+
43+
# go through all the key values we need to set
44+
fix_string_key accountId "${ACCOUNT_ID}"
45+
fix_string_key stackName "${STACK_NAME}"
46+
fix_string_key versionNumber "${VERSION_NUMBER}"
47+
fix_string_key commitId "${COMMIT_ID}"
48+
fix_string_key logRetentionInDays "${LOG_RETENTION_IN_DAYS}"
49+
fix_string_key logLevel "${LOG_LEVEL}"
50+
fix_string_key targetSpineServer "${TARGET_SPINE_SERVER}"
51+
fix_boolean_number_key enableMutualTls "${ENABLE_MUTUAL_TLS}"
52+
fix_string_key trustStoreFile "${TRUSTSTORE_FILE}"
53+
fix_string_key trustStoreVersion "${TRUSTSTORE_VERSION}"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
dev_tag=$(aws cloudformation describe-stacks --stack-name cpt --query "Stacks[0].Tags[?Key=='version'].Value" --output text)
4+
5+
echo "DEV_TAG=${dev_tag}" >> "$GITHUB_ENV"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
current_deployed_tag=$(aws cloudformation describe-stacks --stack-name epsam --query "Stacks[0].Tags[?Key=='version'].Value" --output text)
4+
5+
echo "CURRENT_DEPLOYED_TAG=${current_deployed_tag}" >> "$GITHUB_ENV"

0 commit comments

Comments
 (0)