Skip to content

Commit 214c02c

Browse files
committed
Fix issues with templates & makefile and update documentation
1 parent 6280afd commit 214c02c

11 files changed

+180
-108
lines changed

Makefile

+43-26
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,33 @@ ENV=dev
55
SHELL := /bin/bash
66

77
help:
8-
echo "make build | build the SAM project"
9-
echo "make serve | alias for serve-https"
10-
echo "make clean | remove all installed dependencies and build artifacts"
11-
echo "make deps | install all dependencies"
12-
echo "make link | create hard links to allow for hot reloading of a built project"
13-
echo "make secrets | symlink secrets files from ../tfvars"
14-
echo "make start-with-step | run the SAM server locally with step function & download lambdas"
15-
echo "make style | run all style checks"
16-
echo "make test | run all tests"
17-
echo "make cover | run all tests with coverage"
18-
echo "make env ENV=[env] | activate env.\$$ENV.json file (default: dev)"
19-
echo "make deps-node | install node dependencies"
20-
echo "make deps-python | install python dependencies"
21-
echo "make serve-http | run the SAM server locally (HTTP on port 3000)"
22-
echo "make serve-https | run the SAM server locally (HTTPS on port 3002)"
23-
echo "make style-node | run node code style check"
24-
echo "make style-python | run python code style check"
25-
echo "make test-node | run node tests"
26-
echo "make test-python | run python tests"
27-
echo "make cover-node | run node tests with coverage"
28-
echo "make cover-python | run python tests with coverage"
8+
echo "make build | build the SAM project"
9+
echo "make serve | alias for serve-https"
10+
echo "make clean | remove all installed dependencies and build artifacts"
11+
echo "make deps | install all dependencies"
12+
echo "make env.json | create an env.json file for the current user's environment"
13+
echo "make link | create hard links to allow for hot reloading of a built project"
14+
echo "make secrets | symlink secrets files from ../tfvars"
15+
echo "make start-with-step | run the SAM server locally with step function & download lambdas"
16+
echo "make style | run all style checks"
17+
echo "make test | run all tests"
18+
echo "make cover | run all tests with coverage"
19+
echo "make env ENV=[env] | activate env.\$$ENV.json file (default: dev)"
20+
echo "make deps-node | install node dependencies"
21+
echo "make deps-python | install python dependencies"
22+
echo "make samconfig.NAME.yaml" | create a user samconfig file for the specified username"
23+
echo "make build | build the SAM project for deploying"
24+
echo "make deploy | deploy the SAM project to AWS"
25+
echo "make sync | sync the SAM project to AWS for quick development"
26+
echo "make sync-code | sync the SAM project to AWS (code changes only)"
27+
echo "make serve-http | run the SAM server locally (HTTP on port 3000)"
28+
echo "make serve-https | run the SAM server locally (HTTPS on port 3002)"
29+
echo "make style-node | run node code style check"
30+
echo "make style-python | run python code style check"
31+
echo "make test-node | run node tests"
32+
echo "make test-python | run python tests"
33+
echo "make cover-node | run node tests with coverage"
34+
echo "make cover-python | run python tests with coverage"
2935

3036
.aws-sam/build.toml: ./template.yaml api/package-lock.json api/src/package-lock.json chat/dependencies/requirements.txt chat/src/requirements.txt
3137
sed -Ei.orig 's/"dependencies"/"devDependencies"/' api/src/package.json
@@ -42,17 +48,18 @@ help:
4248
done
4349
mv api/src/package.json.orig api/src/package.json
4450
mv api/src/package-lock.json.orig api/src/package-lock.json
45-
deps-node:
51+
deps-api:
4652
cd api/src ;\
4753
npm list >/dev/null 2>&1 ;\
4854
src_deps=$$? ;\
4955
cd .. ;\
5056
npm list >/dev/null 2>&1 ;\
5157
dev_deps=$$? ;\
5258
test $$src_deps -eq 0 -a $$dev_deps -eq 0 || npm ci
53-
59+
deps-av-download:
5460
cd av-download/lambdas ;\
5561
npm list >/dev/null 2>&1 || npm ci
62+
deps-node: deps-api deps-av-download
5663
cover-node: deps-node
5764
cd api && npm run test:coverage
5865
style-node: deps-node
@@ -87,12 +94,20 @@ serve: serve-https
8794
start-with-step: deps-node env.json
8895
sam local start-lambda -t av-download/template.yaml --host 0.0.0.0 --port 3005 --env-vars $$PWD/env.json --log-file lambda.log & \
8996
echo $$! > .sam-pids ;\
97+
sg open all 3005 ;\
9098
sam local start-api -t api/template.yaml --env-vars $$PWD/env.json --host 0.0.0.0 --port 3002 --log-file dc-api.log \
9199
--ssl-cert-file $$HOME/.dev_cert/dev.rdc.cert.pem --ssl-key-file $$HOME/.dev_cert/dev.rdc.key.pem & \
92100
echo $$! >> .sam-pids ;\
93101
docker run --rm -p 8083:8083 -e LAMBDA_ENDPOINT=http://172.17.0.1:3005/ amazon/aws-stepfunctions-local ;\
102+
echo -n "Shutting down..." ;\
103+
sg close all 3005 ;\
94104
kill $$(cat .sam-pids) ;\
95-
rm -f .sam-pids
105+
rm -f .sam-pids ;\
106+
echo ""
107+
state-machine:
108+
export TEMPLATE_DIR=$$(mktemp -d); \
109+
yq -o=json '.Resources.avDownloadStateMachine.Properties.Definition' av-download/template.yaml > $$TEMPLATE_DIR/av_download.json; \
110+
aws stepfunctions create-state-machine --endpoint http://localhost:8083 --definition file://$$TEMPLATE_DIR/av_download.json --name "hlsStitcherStepFunction" --role-arn arn:aws:iam::012345678901:role/DummyRole
96111
deps: deps-node deps-python
97112
style: style-node style-python
98113
test: test-node test-python
@@ -104,10 +119,12 @@ samconfig.%.yaml:
104119
deploy: build samconfig.$(DEV_PREFIX).yaml
105120
sam deploy --config-file samconfig.$(DEV_PREFIX).yaml --stack-name dc-api-$(DEV_PREFIX)
106121
sync: samconfig.$(DEV_PREFIX).yaml
107-
sam sync --config-file samconfig.$(DEV_PREFIX).yaml --stack-name dc-api-$(DEV_PREFIX) --watch
122+
sam sync --config-file samconfig.$(DEV_PREFIX).yaml --stack-name dc-api-$(DEV_PREFIX) --watch $(ARGS)
123+
sync-code: ARGS=--code
124+
sync-code: sync
108125
secrets:
109126
ln -s ../tfvars/dc-api/*.yaml .
110127
clean:
111-
rm -rf .aws-sam api/node_modules api/src/node_modules python/**/__pycache__ python/.coverage python/.ruff_cache
128+
rm -rf .aws-sam api/.aws-sam chat/.aws-sam av-download/.aws-sam api/node_modules api/src/node_modules chat/**/__pycache__ chat/.coverage chat/.ruff_cache
112129
reset:
113130
for f in $$(find . -maxdepth 2 -name '*.orig'); do mv $$f $${f%%.orig}; done

README.md

+12-18
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The `env.json` file contains environment variable values for the lambda function
1212
make env.json
1313
```
1414

15-
If the file already exists, it will not be overwritten, so make sure to `rm` it if you would like to re-create it.
15+
If the file already exists, it will not be overwritten unless you include `-B` in the make command.
1616

1717
## Running the API locally
1818

@@ -79,21 +79,11 @@ Access the app in a browser at: https://USER_PREFIX.dev.rdc.library.northwestern
7979
# From the repo root
8080
cd dc-api-v2
8181

82-
# Make sure you've done an `npm install` recently to update any packages in the `lambdas` directory
83-
npm install
84-
85-
# Open port 3005 (if needed)
86-
sg open all 3005
87-
88-
# Login as the staging-admin user
89-
export AWS_PROFILE=staging-admin
90-
aws sso login
91-
9282
# Start the API + step function and associated lambdas
9383
make start-with-step
9484

9585
# Open a second terminal and create the state machine
96-
aws stepfunctions create-state-machine --endpoint http://localhost:8083 --definition file://state_machines/av_download.json --name "hlsStitcherStepFunction" --role-arn arn:aws:iam::012345678901:role/DummyRole
86+
make state-machine
9787
```
9888

9989
## Deploying a development branch
@@ -107,6 +97,9 @@ There are two ways to deploy a development branch: `make deploy` and `make sync`
10797

10898
Either way, the resulting stack will be accessible at `https://dcapi-USER_PREFIX.rdc-staging.library.northwestern.edu`.
10999

100+
An existing `sync` stack can be reused by running `make sync` again, or by running `make sync-code` to only
101+
sync code changes (no infrastructure/template changes).
102+
110103
### `samconfig.*.yaml`
111104

112105
Both methods involve a `samconfig.USER_PREFIX.yaml` file. This file, with default values, can be created by
@@ -116,19 +109,20 @@ running (for example):
116109
make samconfig.mbk.yaml
117110
```
118111

119-
This will create a configuration to deploy the API, AV Download, and Chat stacks. To deploy a different combination of
120-
features, specify them using the `WITH` option:
112+
This will create a configuration to stand up the default stacks in both `deploy` mode (API, AV Download, and Chat) and
113+
`sync` mode (Chat only). To deploy a different combination of features, specify them using the `WITH` option:
121114

122115
```shell
123116
make samconfig.mbk.yaml WITH=API,DOCS
124117
```
125118

126-
Available features are: `API`, `AV_DOWNLOAD`, `CHAT`, and `DOCS`.
119+
Available features are: `API`, `AV_DOWNLOAD`, `CHAT`, and `DOCS`.
120+
121+
⚠️ Be **very** careful including the API in `sync` mode as every change within `/api` will take a long time to deploy.
127122

128-
As with the `env.json` file, `make` will not overwrite the file if it already exists. You will need to `rm` it
129-
manually if you'd like to create a new one.
123+
As with the `env.json` file, `make` will not overwrite an existing file unless you include `-B`.
130124

131-
### Tearing down a development branch
125+
### Tearing down a development stack
132126

133127
```shell
134128
sam delete --stack-name dc-api-USER_PREFIX

api/src/environment.js

-28
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,10 @@
11
const fs = require("fs");
22
const jwt = require("jsonwebtoken");
33
const path = require("path");
4-
const {
5-
GetSecretValueCommand,
6-
SecretsManagerClient,
7-
} = require("@aws-sdk/client-secrets-manager");
84
const PackageInfo = JSON.parse(
95
fs.readFileSync(path.join(__dirname, "package.json"))
106
);
117

12-
const { SECRETS_PATH } = process.env;
13-
const SecretIds = {
14-
index: `${SECRETS_PATH}/infrastructure/index`,
15-
meadow: "config/meadow",
16-
};
17-
let Initialized = false;
18-
let Secrets = {};
19-
20-
async function initialize() {
21-
if (Initialized) return;
22-
23-
const client = new SecretsManagerClient();
24-
for (const source in SecretIds) {
25-
const SecretId = SecretIds[source];
26-
console.debug("loading", SecretId, "from", source);
27-
const cmd = new GetSecretValueCommand({ SecretId });
28-
const { SecretString } = await client.send(cmd);
29-
Secrets[source] = JSON.parse(SecretString);
30-
}
31-
Initialized = true;
32-
return Secrets;
33-
}
34-
358
function apiToken() {
369
const token = {
3710
displayName: ["Digital Collection API v2"],
@@ -92,7 +65,6 @@ module.exports = {
9265
dcApiEndpoint,
9366
dcUrl,
9467
devTeamNetIds,
95-
initialize,
9668
openSearchEndpoint,
9769
prefix,
9870
region,

api/src/handlers/get-file-set-download.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ exports.handler = wrap(async (event, context) => {
6969
async function loadSecrets(client) {
7070
if (Secrets) return Secrets;
7171

72-
const { SECRETS_PATH } = process.env;
72+
const SECRETS_PATH =
73+
process.env?.API_CONFIG_PREFIX || process.env.SECRETS_PATH;
7374
const SecretId = `${SECRETS_PATH}/config/av-download`;
7475
try {
7576
const cmd = new GetSecretValueCommand({ SecretId });

api/src/handlers/middleware.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ const debug = require("debug")("api.middleware");
1919
const Honeybadger = require("../honeybadger-setup");
2020
const { StatusCodes } = require("http-status-codes");
2121
const { SECRETS_PATH } = process.env;
22+
const API_CONFIG_PREFIX = process.env?.API_CONFIG_PREFIX || SECRETS_PATH;
2223
const SecretPaths = [
23-
`${SECRETS_PATH}/config/dcapi`,
24+
`${API_CONFIG_PREFIX}/config/dcapi`,
2425
`${SECRETS_PATH}/infrastructure/index`,
2526
`${SECRETS_PATH}/infrastructure/nusso`,
2627
];
2728

2829
const wrap = function (handler) {
2930
return async (event, context) => {
31+
console.log("getSearch event", JSON.stringify(event));
3032
await _initializeEnvironment();
3133

3234
let response;

0 commit comments

Comments
 (0)