-
Notifications
You must be signed in to change notification settings - Fork 17
/
Makefile
293 lines (238 loc) · 9.04 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
SOURCES := \
thin_egress_app/var/otel-config/collector.yaml \
thin_egress_app/app.py \
thin_egress_app/tea_bumper.py \
thin_egress_app/update_lambda.py
HTML_TEMPLATES := $(wildcard templates/*.html)
MD_TEMPLATES := $(wildcard templates/*.md)
TERRAFORM := $(wildcard terraform/*)
REQUIREMENTS_IN := $(wildcard requirements/*.in)
REQUIREMENTS_TXT := $(REQUIREMENTS_IN:.in=.txt)
# Output directory
DIR := dist
EMPTY := $(DIR)/empty
# Temporary artifacts
DIST_SOURCES := $(SOURCES:thin_egress_app/%=$(DIR)/code/%)
DIST_MD_RESOURCES := $(MD_TEMPLATES:%.md=$(DIR)/code/%.html)
DIST_HTML_RESOURCES := $(HTML_TEMPLATES:%=$(DIR)/code/%)
DIST_RESOURCES := $(DIST_HTML_RESOURCES) $(DIST_MD_RESOURCES)
DIST_TERRAFORM := $(TERRAFORM:terraform/%=$(DIR)/terraform/%)
BUCKET_MAP_OBJECT_KEY := DEFAULT
DATE := $(shell date -u "+%b %d %Y, %T %Z")
DATE_SHORT := $(shell date -u "+%Y%m%dT%H%M%S")
BUILD_ID := $(shell git rev-parse --short HEAD 2>/dev/null || echo "SNAPSHOT")
DOCKER := docker
# On Linux we need to do a bit of userid finagling so that the output files
# end up being owned by us and not by root. On Mac this works out of the box.
DOCKER_USER_ARG := --user "$(shell id -u):$(shell id -g)"
DOCKER_COMMAND = $(DOCKER) run --rm $(DOCKER_USER_ARG) -v "$$PWD":/var/task $(DOCKER_ARGS) --entrypoint ""
PYTHON := python3
BUILD_VENV := $(DIR)/.venv
#####################
# Deployment Config #
#####################
# A tag to distinguish between artifacts of the same name. The tag should be
# different for each build.
S3_ARTIFACT_TAG = $(DATE_SHORT)
# Include custom configuration
Makefile.config:
@echo
@echo "It looks like you are building TEA for the first time."
@echo "Please review the configuration in '$@' and run Make again."
@echo
@cp Makefile.config.example $@
@exit 1
include Makefile.config
ifdef DOCKER_COMMAND
DOCKER_DEPENDENCY_BUILDER = $(DOCKER_COMMAND) tea-dependency-builder
endif
.DEFAULT_GOAL := all
.PHONY: all
all: build ;
##############################
# Local building/development #
##############################
# Build everything
.PHONY: build
build: tea-dependency-builder \
$(DIR)/thin-egress-app-code.zip \
$(DIR)/thin-egress-app-dependencies.zip \
$(DIR)/thin-egress-app.yaml \
$(DIR)/thin-egress-app-terraform.zip
# Build individual components
.PHONY: dependencies
dependencies: $(DIR)/thin-egress-app-dependencies.zip
@echo "Built dependency layer for version ${BUILD_ID}"
.PHONY: code
code: $(DIR)/thin-egress-app-code.zip
@echo "Built code for version ${BUILD_ID}"
.PHONY: yaml
yaml: $(DIR)/thin-egress-app.yaml
@echo "Built CloudFormation template for version ${BUILD_ID}"
.PHONY: terraform
terraform: $(DIR)/thin-egress-app-terraform.zip
@echo "Built Terraform zip file for version ${BUILD_ID}"
.PHONY: clean
clean:
rm -rf $(DIR)
$(BUILD_VENV): requirements/requirements-make.txt
rm -rf $(BUILD_VENV)
$(PYTHON) -m venv $(BUILD_VENV)
$(BUILD_VENV)/bin/pip --cache-dir $(DIR)/.pip-cache/ install -r requirements/requirements-make.txt
$(DIR)/thin-egress-app-dependencies.zip: requirements/requirements.txt $(REQUIREMENTS_DEPS)
rm -rf $(DIR)/python
@mkdir -p $(DIR)/python
$(DOCKER_DEPENDENCY_BUILDER) build/dependency_builder.sh "$(DIR)/thin-egress-app-dependencies.zip" "$(DIR)"
.SECONDARY: $(DIST_MD_RESOURCES)
$(DIST_MD_RESOURCES): $(DIR)/code/%.html: %.md $(BUILD_VENV)
@mkdir -p $(@D)
$(BUILD_VENV)/bin/python scripts/render_md.py $< --output $@
.SECONDARY: $(DIST_RESOURCES)
$(DIST_HTML_RESOURCES): $(DIR)/code/%: %
@mkdir -p $(@D)
cp $< $@
.SECONDARY: $(DIST_SOURCES)
$(DIST_SOURCES): $(DIR)/code/%: thin_egress_app/%
@mkdir -p $(@D)
cp $< $@
$(PYTHON) scripts/sed.py -i $@ "<BUILD_ID>" "${BUILD_ID}"
$(DIR)/thin-egress-app-code.zip: $(DIST_SOURCES) $(DIST_RESOURCES)
@mkdir -p $(DIR)/code
cd $(DIR)/code && zip -r ../thin-egress-app-code.zip .
$(DIR)/bucket-map.yaml:
cp config/bucket-map-template.yaml $@
$(DIR)/thin-egress-app.yaml: cloudformation/thin-egress-app.yaml.j2 $(BUILD_VENV)
@mkdir -p $(DIR)
$(BUILD_VENV)/bin/python scripts/render_cf.py \
cloudformation/thin-egress-app.yaml.j2 \
--output $(DIR)/thin-egress-app.yaml \
--code-bucket "$(CF_DEFAULT_CODE_BUCKET)" \
--dependency-archive-key "$(CF_DEFAULT_DEPENDENCY_ARCHIVE_KEY)" \
--code-archive-key "$(CF_DEFAULT_CODE_ARCHIVE_KEY)" \
--build-version "$(CF_BUILD_VERSION)" \
--description "$(CF_DESCRIPTION)"
.SECONDARY: $(DIST_TERRAFORM)
$(DIST_TERRAFORM): $(DIR)/%: %
@mkdir -p $(@D)
cp $< $@
$(DIR)/thin-egress-app-terraform.zip: \
$(DIR)/thin-egress-app-code.zip \
$(DIR)/thin-egress-app-dependencies.zip \
$(DIR)/thin-egress-app.yaml \
$(DIST_TERRAFORM)
@mkdir -p $(DIR)/terraform
cp $(DIR)/thin-egress-app-code.zip $(DIR)/terraform/lambda.zip
cp $(DIR)/thin-egress-app-dependencies.zip $(DIR)/terraform/dependencylayer.zip
cp $(DIR)/thin-egress-app.yaml $(DIR)/terraform/thin-egress-app.yaml
cd $(DIR)/terraform && zip ../thin-egress-app-terraform.zip \
*.tf \
thin-egress-app.yaml \
lambda.zip \
dependencylayer.zip
##############
# Deployment #
##############
# Empty targets so we don't re-deploy stuff that is unchanged. Technically they
# might not be empty, but their purpose is the same.
# https://www.gnu.org/software/make/manual/html_node/Empty-Targets.html
$(EMPTY)/.deploy-dependencies: $(DIR)/thin-egress-app-dependencies.zip
@echo "Deploying dependencies"
$(AWS) s3 cp --profile=$(AWS_PROFILE) $< \
s3://$(CODE_BUCKET)/$(CODE_PREFIX)dependencies-$(S3_ARTIFACT_TAG).zip
@mkdir -p $(EMPTY)
@echo $(S3_ARTIFACT_TAG) > $@
$(EMPTY)/.deploy-code: $(DIR)/thin-egress-app-code.zip
@echo "Deploying code"
$(AWS) s3 cp --profile=$(AWS_PROFILE) \
$(DIR)/thin-egress-app-code.zip \
s3://$(CODE_BUCKET)/$(CODE_PREFIX)code-$(S3_ARTIFACT_TAG).zip
@mkdir -p $(EMPTY)
@echo $(S3_ARTIFACT_TAG) > $@
$(EMPTY)/.deploy-bucket-map: $(DIR)/bucket-map.yaml
@echo "Deploying bucket map"
$(AWS) s3 cp --profile=$(AWS_PROFILE) $< \
s3://$(CONFIG_BUCKET)/$(CONFIG_PREFIX)bucket-map-$(S3_ARTIFACT_TAG).yaml
@mkdir -p $(EMPTY)
@echo $(S3_ARTIFACT_TAG) > $@
# Optionally upload a bucket map if the user hasn't specified one
BUCKET_MAP_REQUIREMENT :=
ifeq ($(BUCKET_MAP_OBJECT_KEY), DEFAULT)
BUCKET_MAP_REQUIREMENT := $(EMPTY)/.deploy-bucket-map
BUCKET_MAP_OBJECT_KEY = $(CONFIG_PREFIX)bucket-map-`cat $(EMPTY)/.deploy-bucket-map`.yaml
endif
$(EMPTY)/.deploy-stack: $(DIR)/thin-egress-app.yaml $(EMPTY)/.deploy-dependencies $(EMPTY)/.deploy-code $(BUCKET_MAP_REQUIREMENT)
@echo "Deploying stack '$(STACK_NAME)'"
$(AWS) cloudformation deploy \
--profile=$(AWS_PROFILE) \
--stack-name $(STACK_NAME) \
--template-file $(DIR)/thin-egress-app.yaml \
--capabilities CAPABILITY_NAMED_IAM \
--parameter-overrides \
LambdaCodeS3Key="$(CODE_PREFIX)code-`cat $(EMPTY)/.deploy-code`.zip" \
LambdaCodeDependencyArchive="$(CODE_PREFIX)dependencies-`cat $(EMPTY)/.deploy-dependencies`.zip" \
BucketMapFile="$(BUCKET_MAP_OBJECT_KEY)" \
URSAuthCredsSecretName=$(URS_CREDS_SECRET_NAME) \
AuthBaseUrl=$(URS_URL) \
ConfigBucket=$(CONFIG_BUCKET) \
LambdaCodeS3Bucket=$(CODE_BUCKET) \
PermissionsBoundaryName=$(PERMISSION_BOUNDARY_NAME) \
BucketnamePrefix=$(BUCKETNAME_PREFIX) \
DownloadRoleArn="" \
DownloadRoleInRegionArn="" \
HtmlTemplateDir= \
StageName=API \
Loglevel=DEBUG \
Logtype=$(LOG_TYPE) \
Maturity=DEV \
PrivateVPC=$(PRIVATE_VPC) \
VPCSecurityGroupIDs=$(VPC_SECURITY_GROUP_IDS) \
VPCSubnetIDs=$(VPC_SUBNET_IDS) \
EnableApiGatewayLogToCloudWatch="False" \
EnableS3CredentialsEndpoint="True" \
DomainName=$(DOMAIN_NAME-"") \
DomainCertArn=$(DOMAIN_CERT_ARN-"") \
CookieDomain=$(COOKIE_DOMAIN-"") \
LambdaTimeout=$(LAMBDA_TIMEOUT) \
LambdaMemory=$(LAMBDA_MEMORY) \
JwtAlgo=$(JWTALGO) \
JwtKeySecretName=$(JWT_KEY_SECRET_NAME) \
UseReverseBucketMap="False" \
UseCorsCookieDomain="False"
@mkdir -p $(EMPTY)
@touch $@
# Deploy everything
.PHONY: deploy
deploy: deploy-code deploy-dependencies deploy-stack
# Deploy individual components
.PHONY: deploy-code
deploy-code: $(EMPTY)/.deploy-code
.PHONY: deploy-dependencies
deploy-dependencies: $(EMPTY)/.deploy-dependencies
.PHONY: deploy-bucket-map
deploy-bucket-map: $(EMPTY)/.deploy-bucket-map
.PHONY: deploy-stack
deploy-stack: $(EMPTY)/.deploy-stack
# Remove the empty target files so that aws commands will be run again
.PHONY: cleandeploy
cleandeploy:
rm -r $(EMPTY)
###############
# Development #
###############
.PHONY: tea-dependency-builder
tea-dependency-builder: build/tea-dependency-builder.Dockerfile
$(DOCKER) build -f build/tea-dependency-builder.Dockerfile -t tea-dependency-builder ./build
@mkdir -p $(EMPTY)
@touch $@
requirements/requirements.txt: requirements/requirements.in requirements/constraints.txt
requirements/requirements-dev.txt: \
requirements/requirements-dev.in \
requirements/constraints.txt \
requirements/requirements.txt
requirements/%.txt: requirements/%.in
$(DOCKER_DEPENDENCY_BUILDER) pip-compile -q -U --cache-dir /var/task/$(DIR)/.pip-cache/ $<
.PHONY: lock
lock: $(REQUIREMENTS_TXT)
.PHONY: test
test:
pytest --cov=thin_egress_app --cov-report=term-missing --cov-branch tests