Skip to content

Commit 99aca27

Browse files
committed
start of sync
1 parent 64b8b23 commit 99aca27

File tree

5 files changed

+116
-11
lines changed

5 files changed

+116
-11
lines changed

.github/scripts/fix_cdk_json.sh

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ fix_string_key() {
1515
jq \
1616
--arg key_value "${KEY_VALUE}" \
1717
--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
18+
'. += {($key_name): $key_value}' "$OUTPUT_FILE_NAME" > "${TEMP_FILE}"
19+
mv "${TEMP_FILE}" "$OUTPUT_FILE_NAME"
2020
}
2121

2222
# helper function to set boolean and number values (without quotes)
@@ -31,10 +31,19 @@ fix_boolean_number_key() {
3131
jq \
3232
--argjson key_value "${KEY_VALUE}" \
3333
--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
34+
'. += {($key_name): $key_value}' "$OUTPUT_FILE_NAME" > "${TEMP_FILE}"
35+
mv "${TEMP_FILE}" "$OUTPUT_FILE_NAME"
3636
}
3737

38+
39+
OUTPUT_FILE_NAME=$1
40+
if [ -z "${OUTPUT_FILE_NAME}" ]; then
41+
echo "OUTPUT_FILE_NAME value is unset or set to the empty string"
42+
exit 1
43+
fi
44+
echo "{}" > "$OUTPUT_FILE_NAME"
45+
TEMP_FILE=$(mktemp)
46+
3847
CFN_DRIFT_DETECTION_GROUP="epsam"
3948
IS_PULL_REQUEST="false"
4049
if [[ "$STACK_NAME" =~ -pr-[0-9]+$ ]]; then
@@ -43,7 +52,6 @@ if [[ "$STACK_NAME" =~ -pr-[0-9]+$ ]]; then
4352
fi
4453

4554
# go through all the key values we need to set
46-
fix_string_key accountId "${ACCOUNT_ID}"
4755
fix_string_key stackName "${STACK_NAME}"
4856
fix_string_key versionNumber "${VERSION_NUMBER}"
4957
fix_string_key commitId "${COMMIT_ID}"

.github/workflows/cdk_release_code.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ jobs:
107107

108108
- name: fix cdk.json for deployment
109109
run: |
110-
./.github/scripts/fix_cdk_json.sh
110+
./.github/scripts/fix_cdk_json.sh .build/epsam_app.json
111111
env:
112112
ACCOUNT_ID: "${{ env.ACCOUNT_ID }}"
113113
STACK_NAME: "${{ inputs.STACK_NAME }}"
@@ -128,6 +128,7 @@ jobs:
128128
-e AWS_REGION="eu-west-2" \
129129
-e SHOW_DIFF="true" \
130130
-e DEPLOY_CODE="false" \
131+
-e CONFIG_FILE_NAME="epsam_app.json" \
131132
-e CDK_APP_PATH="packages/cdk/bin/EpsAssistMeApp.ts" \
132133
cdk-utils-build-repo:latest
133134
@@ -142,6 +143,7 @@ jobs:
142143
-e AWS_REGION="eu-west-2" \
143144
-e SHOW_DIFF="false" \
144145
-e DEPLOY_CODE="true" \
146+
-e CONFIG_FILE_NAME="epsam_app.json" \
145147
-e CDK_APP_PATH="packages/cdk/bin/EpsAssistMeApp.ts" \
146148
cdk-utils-build-repo:latest
147149
shell: bash

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ vendor
3030
.npmrc
3131
cdk.out
3232
.build
33+
.requirements_slackBotFunction
34+
.requirements_syncKnowledgeBaseFunction
35+
.local_config/

packages/cdk/bin/EpsAssistMeApp.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@ import {App, Aspects, Tags} from "aws-cdk-lib"
33
import {AwsSolutionsChecks} from "cdk-nag"
44
import {EpsAssistMeStack} from "../stacks/EpsAssistMeStack"
55
import {applyCfnGuardSuppressions} from "./utils/appUtils"
6+
import fs from "fs"
67

7-
const app = new App()
8+
// read the config in
9+
const configFileName = process.env["CONFIG_FILE_NAME"]
10+
if (configFileName === undefined) {
11+
throw new Error("Can not read config file")
12+
}
13+
14+
const configDetails = JSON.parse(fs.readFileSync(configFileName, "utf-8"))
15+
16+
// create the app using the config details
17+
const app = new App({context: configDetails})
818

919
/* Required Context:
1020
- accountId
@@ -13,7 +23,6 @@ const app = new App()
1323
- commit
1424
*/
1525

16-
const accountId = app.node.tryGetContext("accountId")
1726
const stackName = app.node.tryGetContext("stackName")
1827
const version = app.node.tryGetContext("versionNumber")
1928
const commit = app.node.tryGetContext("commitId")
@@ -22,16 +31,14 @@ const cfnDriftDetectionGroup = app.node.tryGetContext("cfnDriftDetectionGroup")
2231
Aspects.of(app).add(new AwsSolutionsChecks({verbose: true}))
2332

2433
Tags.of(app).add("cdkApp", "EpsAssistMe")
25-
Tags.of(app).add("accountId", accountId)
2634
Tags.of(app).add("stackName", stackName)
2735
Tags.of(app).add("version", version)
2836
Tags.of(app).add("commit", commit)
2937
Tags.of(app).add("cfnDriftDetectionGroup", cfnDriftDetectionGroup)
3038

3139
const EpsAssistMe = new EpsAssistMeStack(app, "EpsAssistMeStack", {
3240
env: {
33-
region: "eu-west-2",
34-
account: accountId
41+
region: "eu-west-2"
3542
},
3643
stackName: stackName,
3744
version: version,

scripts/run_sync.sh

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
5+
echo "Script directory: $CURRENT_DIR"
6+
7+
mkdir -p .local_config
8+
FIX_SCRIPT="${CURRENT_DIR}/../.github/scripts/fix_cdk_json.sh"
9+
EPSAM_CONFIG=".local_config/epsam_app.config.json"
10+
EPSAM_LOG=".local_config/epsam_app.log"
11+
12+
if [ -z "${PULL_REQUEST_ID}" ]; then
13+
echo "What is the pull request id?"
14+
read -r PULL_REQUEST_ID
15+
else
16+
read -r -p "Getting exports for pull request id ${PULL_REQUEST_ID}. Is this correct?" yn
17+
case $yn in
18+
[Yy]* ) ;;
19+
[Nn]* ) exit;;
20+
* ) exit;;
21+
esac
22+
fi
23+
24+
STACK_NAME=epsam-pr-$PULL_REQUEST_ID
25+
26+
echo "Getting exports from stack ${STACK_NAME}"
27+
28+
CF_LONDON_EXPORTS=$(aws cloudformation list-exports --region eu-west-2 --output json)
29+
30+
31+
# vars needed for cdk
32+
33+
COMMIT_ID=$(echo "$CF_LONDON_EXPORTS" | \
34+
jq \
35+
--arg EXPORT_NAME "${STACK_NAME}:local:COMMIT-ID" \
36+
-r '.Exports[] | select(.Name == $EXPORT_NAME) | .Value')
37+
VERSION_NUMBER=$(echo "$CF_LONDON_EXPORTS" | \
38+
jq \
39+
--arg EXPORT_NAME "${STACK_NAME}:local:VERSION-NUMBER" \
40+
-r '.Exports[] | select(.Name == $EXPORT_NAME) | .Value')
41+
SLACK_BOT_TOKEN=$(echo "$CF_LONDON_EXPORTS" | \
42+
jq \
43+
--arg EXPORT_NAME "${STACK_NAME}:local:slackBotToken" \
44+
-r '.Exports[] | select(.Name == $EXPORT_NAME) | .Value')
45+
SLACK_SIGNING_SECRET=$(echo "$CF_LONDON_EXPORTS" | \
46+
jq \
47+
--arg EXPORT_NAME "${STACK_NAME}:local:slackSigningSecret" \
48+
-r '.Exports[] | select(.Name == $EXPORT_NAME) | .Value')
49+
LOG_RETENTION_IN_DAYS=30
50+
LOG_LEVEL=debug
51+
52+
# export all the vars so they can be picked up by external programs
53+
export STACK_NAME
54+
export COMMIT_ID
55+
export VERSION_NUMBER
56+
export SLACK_BOT_TOKEN
57+
export SLACK_SIGNING_SECRET
58+
export LOG_RETENTION_IN_DAYS
59+
export LOG_LEVEL
60+
61+
62+
echo "Generating config for ${EPSAM_CONFIG}"
63+
"$FIX_SCRIPT" "$EPSAM_CONFIG"
64+
65+
echo "Installing dependencies locally"
66+
poetry export --without-hashes --format=requirements.txt --with slackBotFunction > .requirements_slackBotFunction
67+
poetry export --without-hashes --format=requirements.txt --with syncKnowledgeBaseFunction > .requirements_syncKnowledgeBaseFunction
68+
pip3 install -r .requirements_slackBotFunction -t packages/slackBotFunction
69+
pip3 install -r .requirements_syncKnowledgeBaseFunction -t packages/syncKnowledgeBaseFunction
70+
71+
sync_epsam_app() {
72+
echo "Starting sync epsam CDK app"
73+
echo "Stateful CDK app log file at ${EPSAM_LOG}"
74+
CONFIG_FILE_NAME="${EPSAM_CONFIG}" npx cdk deploy \
75+
--app "npx ts-node --prefer-ts-exts packages/cdk/bin/EpsAssistMeApp.ts" \
76+
--watch \
77+
--all \
78+
--ci true \
79+
--require-approval never \
80+
--output .local_config/epsam_app.out/ \
81+
> $EPSAM_LOG 2>&1
82+
}
83+
84+
85+
(trap 'kill 0' SIGINT; sync_epsam_app)

0 commit comments

Comments
 (0)