forked from BloomTech-Labs/mission-control-be
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
411 lines (364 loc) · 26.4 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
SHELL := bash
.SHELLFLAGS := -eu -o pipefail -c
# =================================================================
# = Utility targets ===============================================
# =================================================================
NO_COLOR := \x1b[0m
OK_COLOR := \x1b[32;01m
ERROR_COLOR := \x1b[31;01m
WARN_COLOR := \x1b[33;01m
# =================================================================
# Allows a target to require environment variables to exist
# Example that will only run 'mytarget' when the environment variable named 'SERVER' has been set:
# mytarget: env-SERVER another-dependency
# =================================================================
env-%:
@if [ "${${*}}" = "" ]; then \
echo "Required environment variable $* not set"; \
echo; \
exit 1; \
fi
clean:
@echo
@echo Cleaning up
@rm -rf apollo/dist apollo/node_modules apollo/src/generated apollo/schema/generated
init: clean
@echo
@echo Initializing
@cd apollo && npm install -production && npm prune
# =================================================================
# = Prisma targets ================================================
# =================================================================
prisma-generate:
@export $$(cat .env | xargs) && \
echo && \
echo Generating Prisma schema && \
cd prisma && prisma generate
local-prisma-deploy:
@export $$(cat .env | xargs) && \
echo && \
echo Deploying Prisma schema && \
cd prisma && prisma deploy
local-prisma-reseed:
@export $$(cat .env | xargs) && \
echo && \
echo Deploying Prisma schema && \
cd prisma && \
prisma reset --force && \
prisma seed
local-prisma-token:
@export $$(cat .env | xargs) && \
echo && \
echo Generating Prisma token && \
cd prisma && prisma token
# =================================================================
# = Apollo targets ================================================
# =================================================================
apollo-docker-build: prisma-generate
@export $$(cat .env | xargs) && \
printf "$(OK_COLOR)" && \
printf "\n%s\n" "======================================================================================" && \
printf "%s\n" "= Building Apollo container image: $${APOLLO_CONTAINER_IMAGE}" && \
printf "%s\n" "======================================================================================" && \
printf "$(NO_COLOR)" && \
cd apollo && docker build --no-cache -t $${APOLLO_CONTAINER_IMAGE} .
apollo-push: apollo-docker-build
@export $$(cat .env | xargs) && \
printf "$(OK_COLOR)" && \
printf "\n%s\n" "======================================================================================" && \
printf "%s\n" "= Pushing Apollo container image: $${APOLLO_CONTAINER_IMAGE}" && \
printf "%s\n" "======================================================================================" && \
printf "$(NO_COLOR)" && \
cd apollo && docker push $${APOLLO_CONTAINER_IMAGE}
apollo-token:
@export $$(cat .env | xargs) && \
printf "$(OK_COLOR)" && \
printf "\n%s\n" "======================================================================================" && \
printf "%s\n" "= Grabbing token from: $${OAUTH_TOKEN_ENDPOINT}" && \
printf "%s\n" "======================================================================================" && \
printf "$(NO_COLOR)" && \
curl --request POST \
--url $${OAUTH_TOKEN_ENDPOINT}/v1/token \
--header 'content-type: application/x-www-form-urlencoded' \
--data 'grant_type=client_credentials&scope=groups' \
-u $${TEST_OAUTH_CLIENT_ID}:$${TEST_OAUTH_CLIENT_SECRET}
# =================================================================
# = AWS targets ===================================================
# =================================================================
# =================================================================
# Show a banner before running targets for the whole application
# =================================================================
aws-app-banner: env-APPLICATION_NAME
@printf "$(WARN_COLOR)"
@printf "%s\n" "======================================================================================"
@printf "%s\n" "= Attention!!"
@printf "%s\n" "= The following actions will be performed against this application:"
@printf "%s\n" "= Application: $(APPLICATION_NAME)"
@printf "%s\n" "= Parameters: aws.$(APPLICATION_NAME)"
@printf "%s\n" "======================================================================================"
@printf "$(NO_COLOR)"
@( read -p "Are you sure you want to continue? [y/N]: " sure && case "$$sure" in [yY]) true;; *) false;; esac )
# =================================================================
# Provisions IAM resources for the application
# =================================================================
aws-deploy-app-iam: aws-app-banner
@export STACK_NAME=$(APPLICATION_NAME)-iam && \
export STACK_PARAMETERS="$$(cat aws.$(APPLICATION_NAME) | tr '\n' ' ')" && \
printf "$(OK_COLOR)" && \
printf "\n%s\n" "======================================================================================" && \
printf "%s\n" "= Deploying CloudFormation stack $${STACK_NAME}" && \
printf "%s" "======================================================================================" && \
printf "$(NO_COLOR)" && \
aws cloudformation deploy \
--no-fail-on-empty-changeset \
--template-file aws/app-iam.cf.yaml \
--stack-name $${STACK_NAME} \
--capabilities CAPABILITY_IAM \
--parameter-overrides $${STACK_PARAMETERS} \
--tags poweredby=prismatopia application=$(APPLICATION_NAME)
# =================================================================
# Deploys the application specific network resources to AWS
# =================================================================
aws-deploy-app-network: aws-app-banner
@export STACK_NAME=$(APPLICATION_NAME)-network && \
export STACK_PARAMETERS="$$(cat aws.$(APPLICATION_NAME) | tr '\n' ' ')" && \
printf "$(OK_COLOR)" && \
printf "\n%s\n" "======================================================================================" && \
printf "%s\n" "= Deploying CloudFormation stack $${STACK_NAME}" && \
printf "$(WARN_COLOR)" && \
printf "%s\n" "= Note: This will create a hosted zone for your domain. You may need to stop here and" && \
printf "%s\n" "= update your domain registrar with the name servers for this hosted zone." && \
printf "$(OK_COLOR)" && \
printf "%s" "======================================================================================" && \
printf "$(NO_COLOR)" && \
aws cloudformation deploy \
--no-fail-on-empty-changeset \
--template-file aws/app-network.cf.yaml \
--stack-name $${STACK_NAME} \
--capabilities CAPABILITY_IAM \
--parameter-overrides $${STACK_PARAMETERS} \
--tags poweredby=prismatopia application=$(APPLICATION_NAME)
# =================================================================
# Show a banner before running targets for a specific environment
# =================================================================
aws-env-banner: env-APPLICATION_NAME env-ENVIRONMENT_NAME
@printf "$(WARN_COLOR)"
@printf "%s\n" "======================================================================================"
@printf "%s\n" "= Attention!!"
@printf "%s\n" "= This command is going to be executed in the following AWS environment:"
@printf "%s\n" "= Application: $(APPLICATION_NAME)"
@printf "%s\n" "= Environment: $(ENVIRONMENT_NAME)"
@printf "%s\n" "= Parameters: aws.$(APPLICATION_NAME)"
@printf "%s\n" "= aws.$(APPLICATION_NAME).$(ENVIRONMENT_NAME)"
@printf "%s\n" "======================================================================================"
@printf "$(NO_COLOR)"
@( read -p "Are you sure you want to continue? [y/N]: " sure && case "$$sure" in [yY]) true;; *) false;; esac )
# ===========================================================================
# Provision DNS resources for the environment
# ===========================================================================
aws-deploy-env-dns: aws-env-banner
@export STACK_NAME=$(APPLICATION_NAME)-$(ENVIRONMENT_NAME)-dns && \
export STACK_PARAMETERS="$$(cat aws.$(APPLICATION_NAME) aws.$(APPLICATION_NAME).$(ENVIRONMENT_NAME) | tr '\n' ' ')" && \
printf "$(OK_COLOR)" && \
printf "\n%s\n" "======================================================================================" && \
printf "%s\n" "= Deploying CloudFormation stack $${STACK_NAME}" && \
printf "%s" "======================================================================================" && \
printf "$(NO_COLOR)" && \
aws cloudformation deploy \
--no-fail-on-empty-changeset \
--template-file aws/env-dns.cf.yaml \
--stack-name $${STACK_NAME} \
--parameter-overrides $${STACK_PARAMETERS} \
--tags poweredby=prismatopia application=$(APPLICATION_NAME) environment=$(ENVIRONMENT_NAME)
# ===========================================================================
# Provision SSL certificate for the environmnet
# ===========================================================================
aws-deploy-env-certificate: aws-env-banner
@export STACK_NAME=$(APPLICATION_NAME)-$(ENVIRONMENT_NAME)-certificate && \
export STACK_PARAMETERS="$$(cat aws.$(APPLICATION_NAME) aws.$(APPLICATION_NAME).$(ENVIRONMENT_NAME) | tr '\n' ' ')" && \
printf "$(OK_COLOR)" && \
printf "\n%s\n" "======================================================================================" && \
printf "%s\n" "= Deploying CloudFormation stack $${STACK_NAME}" && \
printf "$(WARN_COLOR)" && \
printf "%s\n" "= Note: You need to verify the certificate deployed by this step in the AWS console" && \
printf "%s\n" "= before you continue." && \
printf "%s\n" "= TODO: https://github.com/binxio/cfn-certificate-provider" && \
printf "$(OK_COLOR)" && \
printf "%s" "======================================================================================" && \
printf "$(NO_COLOR)" && \
aws cloudformation deploy \
--no-fail-on-empty-changeset \
--template-file aws/env-certificate.cf.yaml \
--stack-name $${STACK_NAME} \
--parameter-overrides $${STACK_PARAMETERS} \
--tags poweredby=prismatopia application=$(APPLICATION_NAME) environment=$(ENVIRONMENT_NAME)
# ===========================================================================
# Provision network resources for the environment
# ===========================================================================
aws-deploy-env-network: aws-env-banner
@export STACK_NAME=$(APPLICATION_NAME)-$(ENVIRONMENT_NAME)-network && \
export STACK_PARAMETERS="$$(cat aws.$(APPLICATION_NAME) aws.$(APPLICATION_NAME).$(ENVIRONMENT_NAME) | tr '\n' ' ')" && \
printf "$(OK_COLOR)" && \
printf "\n%s\n" "======================================================================================" && \
printf "%s\n" "= Deploying CloudFormation stack $${STACK_NAME}" && \
printf "%s" "======================================================================================" && \
printf "$(NO_COLOR)" && \
aws cloudformation deploy \
--no-fail-on-empty-changeset \
--template-file aws/env-network.cf.yaml \
--stack-name $${STACK_NAME} \
--parameter-overrides $${STACK_PARAMETERS} \
--tags poweredby=prismatopia application=$(APPLICATION_NAME) environment=$(ENVIRONMENT_NAME)
# ===========================================================================
# Provision database resources for the environment
# ===========================================================================
aws-deploy-env-db: aws-env-banner
@export STACK_NAME=$(APPLICATION_NAME)-$(ENVIRONMENT_NAME)-db && \
export STACK_PARAMETERS="$$(cat aws.$(APPLICATION_NAME) aws.$(APPLICATION_NAME).$(ENVIRONMENT_NAME) | tr '\n' ' ')" && \
printf "$(OK_COLOR)" && \
printf "\n%s\n" "======================================================================================" && \
printf "%s\n" "= Deploying CloudFormation stack $${STACK_NAME}" && \
printf "%s" "======================================================================================" && \
printf "$(NO_COLOR)" && \
aws cloudformation deploy \
--no-fail-on-empty-changeset \
--template-file aws/env-db.cf.yaml \
--stack-name $${STACK_NAME} \
--parameter-overrides $${STACK_PARAMETERS} \
--tags poweredby=prismatopia application=$(APPLICATION_NAME) environment=$(ENVIRONMENT_NAME)
# ===========================================================================
# Provisions the Prisma service for the environment
# ===========================================================================
aws-deploy-env-prisma: aws-env-banner
@export STACK_NAME=$(APPLICATION_NAME)-$(ENVIRONMENT_NAME)-prisma && \
export STACK_PARAMETERS="$$(cat aws.$(APPLICATION_NAME) aws.$(APPLICATION_NAME).$(ENVIRONMENT_NAME) | tr '\n' ' ')" && \
printf "$(OK_COLOR)" && \
printf "\n%s\n" "======================================================================================" && \
printf "%s\n" "= Deploying CloudFormation stack $${STACK_NAME}" && \
printf "%s" "======================================================================================" && \
printf "$(NO_COLOR)" && \
aws cloudformation deploy \
--no-fail-on-empty-changeset \
--template-file aws/env-prisma.cf.yaml \
--stack-name $${STACK_NAME} \
--parameter-overrides $${STACK_PARAMETERS} \
--tags poweredby=prismatopia application=$(APPLICATION_NAME) environment=$(ENVIRONMENT_NAME)
# ===========================================================================
# Provisions the Apollo service for the environment
# ===========================================================================
aws-deploy-env-apollo: aws-env-banner
@export STACK_NAME=$(APPLICATION_NAME)-$(ENVIRONMENT_NAME)-apollo && \
export STACK_PARAMETERS="$$(cat aws.$(APPLICATION_NAME) aws.$(APPLICATION_NAME).$(ENVIRONMENT_NAME) | tr '\n' ' ')" && \
printf "$(OK_COLOR)" && \
printf "\n%s\n" "======================================================================================" && \
printf "%s\n" "= Deploying CloudFormation stack $${STACK_NAME}" && \
printf "%s" "======================================================================================" && \
printf "$(NO_COLOR)" && \
aws cloudformation deploy \
--no-fail-on-empty-changeset \
--template-file aws/env-apollo.cf.yaml \
--stack-name $${STACK_NAME} \
--parameter-overrides $${STACK_PARAMETERS} \
--tags poweredby=prismatopia application=$(APPLICATION_NAME) environment=$(ENVIRONMENT_NAME)
# ===========================================================================
# Deploys all of the application level AWS resources in the proper order
# ===========================================================================
aws-deploy-app: aws-deploy-app-iam aws-deploy-app-network
@echo
@echo ======================================================================================
@echo Finished deploying all application level AWS resources
@echo ======================================================================================
# ===========================================================================
# Deploys all of the environment level AWS resources in the proper order
# ===========================================================================
aws-deploy-env: aws-deploy-env-dns aws-deploy-env-certificate aws-deploy-env-network aws-deploy-env-db aws-deploy-env-prisma aws-deploy-env-apollo
@echo
@echo ======================================================================================
@echo Finished deploying all environment level AWS resources
@echo ======================================================================================
# ===========================================================================
# Retrieves the Prisma secret for the AWS deployed Prisma management API
# ===========================================================================
PRISMA_MANAGEMENT_API_SECRET_ARN_EXPORT := mission-control-$(ENVIRONMENT_NAME)-PrismaManagementAPISecret
PRISMA_MANAGEMENT_API_SECRET_ARN := $$(aws cloudformation list-exports --query "Exports[?Name=='$(PRISMA_MANAGEMENT_API_SECRET_ARN_EXPORT)'].Value" --output text)
PRISMA_MANAGEMENT_API_SECRET := $$(aws secretsmanager get-secret-value --secret-id $(PRISMA_MANAGEMENT_API_SECRET_ARN) --query 'SecretString' --output text)
aws-prisma-management-secret: aws-env-banner
@echo PRISMA_MANAGEMENT_API_SECRET: $(PRISMA_MANAGEMENT_API_SECRET)
# ===========================================================================
# Retrieves the Prisma secret for the AWS deployed service
# ===========================================================================
PRISMA_SERVICE_API_SECRET_ARN_EXPORT := mission-control-$(ENVIRONMENT_NAME)-PrismaServiceAPISecret
PRISMA_SERVICE_API_SECRET_ARN := $$(aws cloudformation list-exports --query "Exports[?Name=='$(PRISMA_SERVICE_API_SECRET_ARN_EXPORT)'].Value" --output text)
PRISMA_SERVICE_API_SECRET := $$(aws secretsmanager get-secret-value --secret-id $(PRISMA_SERVICE_API_SECRET_ARN) --query 'SecretString' --output text)
aws-prisma-service-secret: aws-env-banner
@echo PRISMA_SERVICE_API_SECRET: $(PRISMA_SERVICE_API_SECRET)
# ===========================================================================
# Gets a token for connecting to the AWS Prisma API
# ===========================================================================
aws-prisma-token: aws-env-banner
@export $$(cat aws.$(APPLICATION_NAME) | xargs) && \
export $$(cat aws.$(APPLICATION_NAME).$(ENVIRONMENT_NAME) | xargs) && \
export PRISMA_MANAGEMENT_API_SECRET="$(PRISMA_MANAGEMENT_API_SECRET)" && \
export PRISMA_SECRET="$(PRISMA_SERVICE_API_SECRET)" && \
printf "$(OK_COLOR)" && \
printf "\n%s\n" "======================================================================================" && \
printf "%s\n" "= Getting Prisma API token from $${PRISMA_ENDPOINT}" && \
printf "%s" "======================================================================================" && \
printf "$(NO_COLOR)\n" && \
printf "%s\n" "PRISMA_MANAGEMENT_API_SECRET: $${PRISMA_MANAGEMENT_API_SECRET}" && \
printf "%s\n" "PRISMA_SECRET: $${PRISMA_SECRET}" && \
cd prisma && prisma token
# ===========================================================================
# Runs Prisma deploy against the AWS environment
# ===========================================================================
aws-prisma-deploy: aws-env-banner
@export $$(cat aws.$(APPLICATION_NAME) | xargs) && \
export $$(cat aws.$(APPLICATION_NAME).$(ENVIRONMENT_NAME) | xargs) && \
export PRISMA_MANAGEMENT_API_SECRET="$(PRISMA_MANAGEMENT_API_SECRET)" && \
export PRISMA_SECRET="$(PRISMA_SERVICE_API_SECRET)" && \
printf "$(OK_COLOR)" && \
printf "\n%s\n" "======================================================================================" && \
printf "%s\n" "= Deploying Prisma datamodel to $${PRISMA_ENDPOINT}" && \
printf "%s" "======================================================================================" && \
printf "$(NO_COLOR)\n" && \
printf "%s\n" "PRISMA_MANAGEMENT_API_SECRET: $${PRISMA_MANAGEMENT_API_SECRET}" && \
printf "%s\n" "PRISMA_SECRET: $${PRISMA_SECRET}" && \
cd prisma && prisma deploy
# ===========================================================================
# Runs Prisma seed against the AWS environment
# ===========================================================================
aws-prisma-reseed: aws-env-banner
@export $$(cat aws.$(APPLICATION_NAME) | xargs) && \
export $$(cat aws.$(APPLICATION_NAME).$(ENVIRONMENT_NAME) | xargs) && \
export PRISMA_MANAGEMENT_API_SECRET="$(PRISMA_MANAGEMENT_API_SECRET)" && \
export PRISMA_SECRET="$(PRISMA_SERVICE_API_SECRET)" && \
printf "$(OK_COLOR)" && \
printf "\n%s\n" "======================================================================================" && \
printf "%s\n" "= Seeding $${PRISMA_ENDPOINT}" && \
printf "%s" "======================================================================================" && \
printf "$(NO_COLOR)\n" && \
printf "%s\n" "PRISMA_MANAGEMENT_API_SECRET: $${PRISMA_MANAGEMENT_API_SECRET}" && \
printf "%s\n" "PRISMA_SECRET: $${PRISMA_SECRET}" && \
cd prisma && prisma delete --force && prisma deploy --force
# =================================================================
# Force an update of the Prisma service in AWS
# =================================================================
PRISMA_SERVICE_ARN_EXPORT := $(APPLICATION_NAME)-$(ENVIRONMENT_NAME)-PrismaServiceArn
PRISMA_SERVICE_ARN := $$(aws cloudformation list-exports --query 'Exports[?Name==`$(PRISMA_SERVICE_ARN_EXPORT)`].Value' --output text)
aws-prisma-update-service: aws-env-banner
@export PRISMA_SERVICE_ARN=$(PRISMA_SERVICE_ARN) && \
echo PRISMA_SERVICE_ARN: $${PRISMA_SERVICE_ARN} && \
aws ecs update-service --cluster $(APPLICATION_NAME)-$(ENVIRONMENT_NAME) --service "$${PRISMA_SERVICE_ARN}" --force-new-deployment
# =================================================================
# Force an update of the Apollo service in AWS
# =================================================================
APOLLO_SERVICE_ARN_EXPORT := $(APPLICATION_NAME)-$(ENVIRONMENT_NAME)-ApolloServiceArn
APOLLO_SERVICE_ARN := $$(aws cloudformation list-exports --query 'Exports[?Name==`$(APOLLO_SERVICE_ARN_EXPORT)`].Value' --output text)
aws-apollo-update-service: aws-env-banner
@printf "$(OK_COLOR)" && \
printf "\n%s\n" "======================================================================================" && \
printf "%s\n" "= Updating the Apollo service" && \
printf "%s" "======================================================================================" && \
printf "$(NO_COLOR)" && \
export APOLLO_SERVICE_ARN=$(APOLLO_SERVICE_ARN) && \
aws ecs update-service --cluster $(APPLICATION_NAME)-$(ENVIRONMENT_NAME) --service "$${APOLLO_SERVICE_ARN}" --force-new-deployment