-
Notifications
You must be signed in to change notification settings - Fork 35
/
Makefile
210 lines (166 loc) · 7.67 KB
/
Makefile
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
SHELL := /bin/bash
.PHONY : deploy deploy-containers pre-deploy setup test test-cov test-acceptance test-acceptance-cov test-no-state-machine test-no-state-machine-cov test-unit test-unit-cov
# The name of the virtualenv directory to use
VENV ?= venv
pre-deploy:
ifndef TEMP_BUCKET
$(error TEMP_BUCKET is undefined)
endif
ifndef ADMIN_EMAIL
$(error ADMIN_EMAIL is undefined)
endif
pre-run:
ifndef ROLE_NAME
$(error ROLE_NAME is undefined)
endif
build-frontend:
npm run build --workspace frontend
deploy:
make pre-deploy
make deploy-artefacts
make deploy-cfn
make setup-frontend-local-dev
deploy-vpc:
aws cloudformation create-stack --template-body file://templates/vpc.yaml --stack-name S3F2-VPC
deploy-cfn:
aws cloudformation package --template-file templates/template.yaml --s3-bucket $(TEMP_BUCKET) --output-template-file packaged.yaml
aws cloudformation deploy --template-file ./packaged.yaml --stack-name S3F2 --capabilities CAPABILITY_IAM CAPABILITY_AUTO_EXPAND \
--parameter-overrides CreateCloudFrontDistribution=true EnableContainerInsights=true AdminEmail=$(ADMIN_EMAIL) \
AccessControlAllowOriginOverride=* PreBuiltArtefactsBucketOverride=$(TEMP_BUCKET) KMSKeyArns=$(KMS_KEYARNS)
deploy-artefacts:
$(eval VERSION := $(shell $(MAKE) -s version))
make package-artefacts
aws s3 cp build.zip s3://$(TEMP_BUCKET)/amazon-s3-find-and-forget/$(VERSION)/build.zip
.PHONY: format-cfn
format-cfn:
$(eval VERSION := $(shell $(MAKE) -s version))
TEMP_FILE="$$(mktemp)" ; \
sed -e '3s/.*/Description: Amazon S3 Find and Forget \(uksb-1q2j8beb0\) \(version:$(VERSION)\) \(tag:main\)/' templates/template.yaml > "$$TEMP_FILE" ; \
mv "$$TEMP_FILE" templates/template.yaml
git add templates/template.yaml
.PHONY: format-docs
format-docs:
npx prettier ./*.md ./docs/*.md --write
git add *.md
git add docs/*.md
.PHONY: format-js
format-js:
npx prettier ./frontend/src/**/*.js --write
git add frontend/src/
.PHONY: format-python
format-python: | $(VENV)
for src in \
tests/ \
backend/ecs_tasks/ \
backend/lambdas/ \
backend/lambda_layers/boto_utils/python/boto_utils.py \
backend/lambda_layers/decorators/python/decorators.py \
; do \
$(VENV)/bin/black "$$src" \
; done
generate-api-docs:
TEMP_FILE="$$(mktemp)" ; \
$(VENV)/bin/yq -y .Resources.Api.Properties.DefinitionBody ./templates/api.yaml > "$$TEMP_FILE" ; \
npx openapi-generator generate -i "$$TEMP_FILE" -g markdown -t ./docs/templates/ -o docs/api
git add docs/api
.PHONY: generate-pip-requirements
generate-pip-requirements: $(patsubst %.in,%.txt,$(shell find . -type f -name requirements.in))
.PHONY: lint-cfn
lint-cfn:
$(VENV)/bin/cfn-lint -i W3002 templates/*
package:
make package-artefacts
zip -r packaged.zip \
backend/lambda_layers \
backend/lambdas \
build.zip \
cfn-publish.config \
templates \
-x '**/__pycache*' '*settings.js' @
package-artefacts:
make build-frontend
zip -r build.zip \
backend/ecs_tasks/ \
backend/lambda_layers/boto_utils/ \
frontend/build \
-x '**/__pycache*' '*settings.js' @
redeploy-containers:
$(eval ACCOUNT_ID := $(shell aws sts get-caller-identity --query Account --output text))
$(eval API_URL := $(shell aws cloudformation describe-stacks --stack-name S3F2 --query 'Stacks[0].Outputs[?OutputKey==`ApiUrl`].OutputValue' --output text))
$(eval REGION := $(shell echo $(API_URL) | cut -d'.' -f3))
$(eval ECR_REPOSITORY := $(shell aws cloudformation describe-stacks --stack-name S3F2 --query 'Stacks[0].Outputs[?OutputKey==`ECRRepository`].OutputValue' --output text))
$(eval REPOSITORY_URI := $(shell aws ecr describe-repositories --repository-names $(ECR_REPOSITORY) --query 'repositories[0].repositoryUri' --output text))
$(shell aws ecr get-login --no-include-email --region $(REGION))
docker build --platform linux/amd64 -t $(ECR_REPOSITORY) -f backend/ecs_tasks/delete_files/Dockerfile .
docker tag $(ECR_REPOSITORY):latest $(REPOSITORY_URI):latest
docker push $(REPOSITORY_URI):latest
redeploy-frontend:
$(eval WEBUI_BUCKET := $(shell aws cloudformation describe-stacks --stack-name S3F2 --query 'Stacks[0].Outputs[?OutputKey==`WebUIBucket`].OutputValue' --output text))
make build-frontend
cd frontend/build && aws s3 cp --recursive . s3://$(WEBUI_BUCKET) --exclude *settings.js
run-local-container:
make pre-run
./docker_run_with_creds.sh
setup: | $(VENV) lambda-layer-deps
(! [[ -d .git ]] || $(VENV)/bin/pre-commit install)
npm i
gem install cfn-nag
# virtualenv setup
.PHONY: $(VENV)
$(VENV): $(VENV)/pip-sync.sentinel
$(VENV)/pip-sync.sentinel: requirements.txt | $(VENV)/bin/pip-sync
$(VENV)/bin/pip-sync $<
touch $@
$(VENV)/bin/activate:
test -d $(VENV) || virtualenv $(VENV)
$(VENV)/bin/pip-compile $(VENV)/bin/pip-sync: $(VENV)/bin/activate
$(VENV)/bin/pip install pip-tools
# Lambda layers
.PHONY: lambda-layer-deps
lambda-layer-deps: \
backend/lambda_layers/aws_sdk/requirements-installed.sentinel \
backend/lambda_layers/cr_helper/requirements-installed.sentinel \
backend/lambda_layers/decorators/requirements-installed.sentinel \
;
backend/lambda_layers/%/requirements-installed.sentinel: backend/lambda_layers/%/requirements.txt | $(VENV)
@# pip-sync only works with virtualenv, so we can't use it here.
$(VENV)/bin/pip install -r $< -t $(subst requirements-installed.sentinel,python,$@)
touch $@
setup-frontend-local-dev:
$(eval WEBUI_URL := $(shell aws cloudformation describe-stacks --stack-name S3F2 --query 'Stacks[0].Outputs[?OutputKey==`WebUIUrl`].OutputValue' --output text))
$(eval WEBUI_BUCKET := $(shell aws cloudformation describe-stacks --stack-name S3F2 --query 'Stacks[0].Outputs[?OutputKey==`WebUIBucket`].OutputValue' --output text))
$(if $(filter none, $(WEBUI_URL)), @echo "WebUI not deployed.", aws s3 cp s3://$(WEBUI_BUCKET)/settings.js frontend/public/settings.js)
setup-predeploy:
virtualenv venv
source venv/bin/activate && pip install cfn-flip==1.2.2
start-frontend-local:
npm start --workspace frontend
start-frontend-remote:
$(eval WEBUI_URL := $(shell aws cloudformation describe-stacks --stack-name S3F2 --query 'Stacks[0].Outputs[?OutputKey==`WebUIUrl`].OutputValue' --output text))
$(if $(filter none, $(WEBUI_URL)), @echo "WebUI not deployed.", open $(WEBUI_URL))
tests/acceptance/data/basic.json.gz:
gzip -k tests/acceptance/data/basic.json
test-cfn:
cfn_nag templates/*.yaml --blacklist-path ci/cfn_nag_blacklist.yaml
test-frontend:
npm t --workspace frontend
test-unit: | $(VENV)
$(VENV)/bin/pytest -m unit --log-cli-level info --cov=backend.lambdas --cov=decorators --cov=boto_utils --cov=backend.ecs_tasks --cov-report term-missing
test-ci: | $(VENV)
$(VENV)/bin/pytest -m unit --log-cli-level info --cov=backend.lambdas --cov=decorators --cov=boto_utils --cov=backend.ecs_tasks --cov-report xml
test-acceptance-cognito: | $(VENV) tests/acceptance/data/basic.json.gz
$(VENV)/bin/pytest -m acceptance_cognito --log-cli-level info
test-acceptance-iam: | $(VENV) tests/acceptance/data/basic.json.gz
$(VENV)/bin/pytest -m acceptance_iam --log-cli-level info
test-no-state-machine: | $(VENV)
$(VENV)/bin/pytest -m "not state_machine" --log-cli-level info --cov=backend.lambdas --cov=boto_utils --cov=decorators --cov=backend.ecs_tasks
test: | $(VENV)
make test-cfn
$(VENV)/bin/pytest --log-cli-level info --cov=backend.lambdas --cov=decorators --cov=boto_utils --cov=backend.ecs_tasks
make test-frontend
version:
@echo $(shell $(VENV)/bin/cfn-flip templates/template.yaml | $(VENV)/bin/python -c 'import sys, json; print(json.load(sys.stdin)["Mappings"]["Solution"]["Constants"]["Version"])')
%/requirements.txt: %/requirements.in | $(VENV)/bin/pip-compile
$(VENV)/bin/pip-compile -q -o $@ $<
requirements.txt: requirements.in $(shell awk '/^-r / { print $$2 }' requirements.in) | $(VENV)/bin/pip-compile
$(VENV)/bin/pip-compile -q -o $@ $<