-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
420 lines (339 loc) · 12.5 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
412
413
414
415
416
417
418
419
420
# Internationally Educated Nurses
# Default Environments
-include ./.env
export $(shell sed 's/=.*//' ./.env)
# Project
export PROJECT := ien
# Runtime and application Environments specific variable
export ENV_NAME ?= dev
export POSTGRES_USERNAME ?= freshworks
export CHES_CLIENT_ID ?= IEN_SERVICE_CLIENT
# Integration testing variables
export TEST_POSTGRES_HOST ?= localhost
export TEST_POSTGRES_USERNAME ?= freshworks
export TEST_POSTGRES_PASSWORD ?= password
export TEST_POSTGRES_DATABASE ?= ien_test
export TEST_POSTGRES_PORT ?= 5433
# Git
export COMMIT_SHA:=$(shell git rev-parse --short=7 HEAD)
export LAST_COMMIT_MESSAGE:=$(shell git log -1 --oneline --decorate=full --no-color --format="%h, %cn, %f, %D" | sed 's/->/:/')
# TF Token
export TFCTK:=$(shell cat ~/.terraform.d/credentials.tfrc.json | jq -r '.credentials."app.terraform.io".token')
# FE Env Vars
export NEXT_PUBLIC_API_URL ?= /api/v1
export NEXT_PUBLIC_AUTH_REALM ?= moh_applications
export NEXT_PUBLIC_AUTH_CLIENTID ?= IEN
# BE Env Vars
export AUTH_URL:=$(AUTH_URL)
export AUTH_REALM:=$(AUTH_REALM)
# Docker container names
LOCAL_API_CONTAINER_NAME = $(PROJECT)_api
# AWS Environments variables
export AWS_REGION ?= ca-central-1
NAMESPACE = $(PROJECT)-$(ENV_NAME)
APP_SRC_BUCKET = $(NAMESPACE)-app
API_SRC_BUCKET = $(NAMESPACE)-api
DOCS_BUCKET = $(NAMESPACE)-docs
REPORTS_BUCKET = $(NAMESPACE)-reports
# Terraform variables
TERRAFORM_DIR = terraform
export BOOTSTRAP_ENV=terraform/bootstrap
export KC_ADMIN=kcadmin
export KC_ADMIN_PASSWORD=password
ifeq ($(ENV_NAME), prod)
export DOMAIN=ien.gov.bc.ca
export NEXT_PUBLIC_AUTH_URL=https://common-logon.hlth.gov.bc.ca/auth
export BASTION_INSTANCE_ID = $(BASTION_INSTANCE_ID_PROD)
export DB_HOST = $(DB_HOST_PROD)
export BASTION_INSTANCE_ID = $(BASTION_INSTANCE_ID_PROD)
endif
ifeq ($(ENV_NAME), dev)
export DOMAIN=dev.ien.freshworks.club
export NEXT_PUBLIC_AUTH_URL=https://common-logon-dev.hlth.gov.bc.ca/auth
export BASTION_INSTANCE_ID=$(BASTION_INSTANCE_ID_DEV)
export DB_HOST=$(DB_HOST_DEV)
endif
ifeq ($(ENV_NAME), test)
export DOMAIN=test.ien.freshworks.club
export NEXT_PUBLIC_AUTH_URL=https://common-logon-test.hlth.gov.bc.ca/auth
export BASTION_INSTANCE_ID=$(BASTION_INSTANCE_ID_TEST)
export DB_HOST=$(DB_HOST_TEST)
endif
export MAIL_FROM = IENDoNotReply@$(DOMAIN)
define TFVARS_DATA
target_env = "$(ENV_NAME)"
project_code = "$(PROJECT)"
api_artifact = "build/api.zip"
app_sources = "build/app"
app_sources_bucket = "$(APP_SRC_BUCKET)"
api_sources_bucket = "$(API_SRC_BUCKET)"
docs_bucket = "$(DOCS_BUCKET)"
reports_bucket = "$(REPORTS_BUCKET)"
domain = "$(DOMAIN)"
db_username = "$(POSTGRES_USERNAME)"
ches_client_id = "$(CHES_CLIENT_ID)"
mail_from = "$(MAIL_FROM)"
build_id = "$(COMMIT_SHA)"
build_info = "$(LAST_COMMIT_MESSAGE)"
target_aws_account_id = "${AWS_ACCOUNT_ID}"
endef
export TFVARS_DATA
# Terraform cloud backend config variables
# LZ2
LZ2_PROJECT = uux0vy
# Terraform Cloud backend config variables
define TF_BACKEND_CFG
bucket = "terraform-remote-state-${LZ2_PROJECT}-${ENV_NAME}"
key = ".terraform/terraform.tfstate"
dynamodb_table ="terraform-remote-state-lock-${LZ2_PROJECT}"
endef
export TF_BACKEND_CFG
.PHONY: start-local print-env start-local-db bootstrap bootstrap-terraform
# ===================================
# Aliases
# ===================================
bootstrap-terraform: print-env bootstrap
build-terraform-artifact: clean-yarn print-env pre-build build-api
# ===================================
# Local Development
# ===================================
build-artifact-local: build-terraform-artifact
@yarn
clean-yarn:
@rm -rf node_modules
@yarn
print-env:
@echo "\n**** ENVIRONMENTS ****\n"
@echo "\nProject: $(PROJECT)"
@echo "\nNODE_ENV: $(NODE_ENV)"
@echo "\nNAMESPACE=$(NAMESPACE)"
@echo
@echo ./$(TERRAFORM_DIR)/.auto.tfvars:
@echo "$$TFVARS_DATA"
@echo
@echo ./$(TERRAFORM_DIR)/backend.hcl:
@echo "$$TF_BACKEND_CFG"
@echo "\n*********************\n"
watch: print-env start-local-db
@echo "++\n***** Running api + web in local Node server\n++"
@yarn
@yarn watch
start-local: print-env start-local-db
@echo "++\n***** Running api + web in local Node server\n++"
@yarn
@yarn start:local
start-local-db:
@echo "++\n***** Starting local database\n++"
@docker compose up -d db
@echo "++\n*****"
stop-local-db:
@echo "++\n***** Stopping local database\n++"
@docker stop "$(PROJECT)_db"
@echo "++\n*****"
docker-down:
@echo "++\n***** Stopping Docker containers\n++"
@docker compose down
@echo "++\n*****"
docker-build:
@echo "++\n***** Running docker compose\n++"
@docker compose build
@echo "++\n*****"
docker-run:
@echo "++\n***** Running docker compose\n++"
@yarn
@docker compose up --build
@echo "++\n*****"
api-unit-test:
@echo "++\n***** Running API unit tests\n++"
@yarn workspace @ien/api build
@yarn workspace @ien/api test
@echo "++\n*****"
web-unit-test:
@echo "++\n***** Running WEB unit tests\n++"
@yarn workspace @ien/web test
@echo "++\n*****"
start-test-env:
@docker compose -f docker-compose.test.yaml up --build -d
stop-test-env:
@docker compose -f docker-compose.test.yaml down
start-test-db:
@docker compose -f docker-compose.test.yaml up --build -d test-db
stop-test-db:
@docker compose -f docker-compose.test.yaml down test-db
start-keycloak:
docker compose -f ./docker-compose.test.yaml up -d keycloak
stop-keycloak:
docker compose -f ./docker-compose.test.yaml down keycloak
format:
@yarn format:write
api-integration-test:
@make start-test-db
@echo "++\n***** Running API integration tests\n++"
@yarn workspace @ien/api build
@NODE_ENV=test yarn workspace @ien/api test:e2e
@echo "++\n*****"
@make stop-test-db
run-seed:
@make start-test-env
@scripts/seed-test-data.sh
@echo "++\n*****"
run-test-apps:
@make start-test-env
@scripts/seed-test-data.sh &
NODE_ENV=test yarn watch
@echo "++\n*****"
test-e2e:
@make start-test-env
@echo "++\n***** Running Web integration tests\n++"
@yarn build
@NODE_ENV=test yarn test:e2e
@make stop-test-db
@echo "++\n*****"
cypress:
@yarn workspace @ien/web cypress
open-cypress:
@yarn workspace @ien/web open:cypress
test-pa11y:
@make start-test-env
@yarn build
@echo "++\n***** Running front end accessibility tests\n++"
@NODE_ENV=test yarn test:pa11y
@make stop-test-db
@echo "++\n*****"
debug-pa11y:
@echo "++\n***** Running front end accessibility tests\n++"
@yarn workspace @ien/accessibility debug
@echo "++\n*****"
generate-accessibility-results:
@echo "++\n***** Generating Github Comment from Test Results\n++"
@yarn workspace @ien/accessibility generate-accessibility-results
@echo "++\n*****"
sync-master:
@yarn workspace @ien/api syncdata master
sync-applicant:
@yarn workspace @ien/api syncdata applicant
# ===================================
# Build application stack
# ===================================
pre-build:
@echo "++\n***** Pre-build Clean Build Artifact\n++"
@rm -rf ./terraform/build || true
@mkdir -p ./terraform/build
@echo "++\n*****"
build-api:
@echo "++\n***** Building API for AWS\n++"
@echo 'Building api package... \n'
@yarn workspace @ien/api build
@echo 'Updating prod dependencies...\n'
@yarn workspaces focus @ien/api --production
@echo 'Deleting existing build dir...\n'
@rm -rf ./.build || true
@echo 'Creating build dir...\n'
@mkdir -p .build/api
@echo 'Copy Node modules....\n' && cp -r node_modules .build/api
@echo 'Unlink local packages...\n' && rm -rf .build/api/node_modules/@ien/*
@echo 'Hardlink local packages...\n'
@cp -r ./packages/* .build/api/node_modules/@ien/
@echo 'Copy api ...\n' && cp -r apps/api/dist/* .build/api
@echo 'Copy api/ormconfig ...\n' && cp -r apps/api/dist/ormconfig.js .build/api
@echo 'Creating Zip ...\n' && cd .build && zip -r api.zip ./api && cd ..
@echo 'Copying to terraform build location...\n'
@cp ./.build/api.zip ./terraform/build/api.zip
@echo 'Done!\n'
@echo "++\n****"
build-web:
@echo "++\n***** Building Web for AWS\n++"
@yarn workspace @ien/web build
@mv ./apps/web/out ./terraform/build/app
@echo "++\n*****"
build-common:
@echo "++\n***** Building Common\n++"
@yarn workspace @ien/common build
@echo "++\n*****"
# ===================================
# Terraform commands
# ===================================
write-config-tf:
@echo "$$TFVARS_DATA" > $(TERRAFORM_DIR)/.auto.tfvars
@echo "$$TF_BACKEND_CFG" > $(TERRAFORM_DIR)/backend.hcl
init: write-config-tf
# Initializing the terraform environment
@terraform -chdir=$(TERRAFORM_DIR) init -input=false \
-reconfigure \
-backend-config=backend.hcl \
-upgrade
plan: init
# Creating all AWS infrastructure.
@terraform -chdir=$(TERRAFORM_DIR) plan -no-color
apply: init
# Creating all AWS infrastructure.
@terraform -chdir=$(TERRAFORM_DIR) apply -auto-approve -input=false
destroy: init
@terraform -chdir=$(TERRAFORM_DIR) destroy
runs:
./terraform/scripts/runs.sh $(TFCTK) $(ENV_NAME)
# ===================================
# AWS Deployments
# ===================================
sync-app:
aws s3 sync ./terraform/build/app s3://$(APP_SRC_BUCKET) --delete
upload-api-zip:
aws s3 cp ./terraform/build/api.zip s3://$(API_SRC_BUCKET)/api-lambda-s3 --region $(AWS_REGION)
deploy-app:
aws --region $(AWS_REGION) cloudfront create-invalidation --distribution-id $(CLOUDFRONT_ID) --paths "/*"
# Full redirection to /dev/null is required to not leak env variables
deploy-api:
aws lambda update-function-code --function-name ien-$(ENV_NAME)-api --s3-bucket $(API_SRC_BUCKET) --s3-key "api-lambda-s3" --region $(AWS_REGION) > /dev/null
aws lambda update-function-code --function-name ien-$(ENV_NAME)-syncdata --s3-bucket $(API_SRC_BUCKET) --s3-key "api-lambda-s3" --region $(AWS_REGION) > /dev/null
aws lambda update-function-code --function-name ien-$(ENV_NAME)-notifylambda --s3-bucket $(API_SRC_BUCKET) --s3-key "api-lambda-s3" --region $(AWS_REGION) > /dev/null
aws lambda update-function-code --function-name ien-$(ENV_NAME)-cache-reports --s3-bucket $(API_SRC_BUCKET) --s3-key "api-lambda-s3" --region $(AWS_REGION) > /dev/null
aws lambda update-function-code --function-name ien-$(ENV_NAME)-s3-upload-reports --s3-bucket $(API_SRC_BUCKET) --s3-key "api-lambda-s3" --region $(AWS_REGION) > /dev/null
aws lambda update-function-code --function-name ien-$(ENV_NAME)-end-of-journey-lambda --s3-bucket $(API_SRC_BUCKET) --s3-key "api-lambda-s3" --region $(AWS_REGION) > /dev/null
deploy-all: sync-app upload-api-zip deploy-api
@echo "Deploying Webapp and API"
backup-db:
@echo "Creating a database snapshot"
aws rds create-db-cluster-snapshot --db-cluster-identifier ien-$(ENV_NAME)-db --db-cluster-snapshot-identifier ien-$(ENV_NAME)-db-snapshot-$(COMMIT_SHA)
# ===================================
# Tag Based Deployments
# ===================================
pre-tag:
@./scripts/check_rebase.sh
tag-dev:
@git tag -fa dev -m "Deploy dev: $(git rev-parse --abbrev-ref HEAD)"
@git push --force origin refs/tags/dev:refs/tags/dev
tag-test:
@git tag -fa test -m "Deploy test: $(git rev-parse --abbrev-ref HEAD)"
@git push --force origin refs/tags/test:refs/tags/test
tag-prod:
ifndef version
@echo "++\n***** ERROR: version not set.\n++"
@exit 1
else
@git tag -fa $(version) -m "IEN release version: $(version)"
@git push --force origin refs/tags/$(version):refs/tags/$(version)
@git tag -fa prod -m "Deploy prod: $(version)"
@git push --force origin refs/tags/prod:refs/tags/prod
endif
tag-sec:
@git tag -fa security -m "security scans: $(git rev-parse --abbrev-ref HEAD)"
@git push --force origin refs/tags/security:refs/tags/security
# Typeorm Migrations
migration-generate:
@docker exec $(LOCAL_API_CONTAINER_NAME) yarn workspace @ien/api typeorm migration:generate -n $(name)
migration-revert:
@docker exec $(LOCAL_API_CONTAINER_NAME) yarn workspace @ien/api typeorm migration:revert
# ===================================
# DB Tunneling
# ===================================
open-db-tunnel:
# Needs exported credentials for a matching LZ2 space
@echo "Running for ENV_NAME=$(ENV_NAME)\n"
@echo "Host Instance Id: $(BASTION_INSTANCE_ID) | $(BASTION_INSTANCE_ID_DEV) | $(DOMAIN)\n"
@echo "DB HOST URL: $(DB_HOST)\n"
# Checking you have the SSM plugin for the AWS cli installed
session-manager-plugin
rm ssh-keypair ssh-keypair.pub || true
ssh-keygen -t rsa -f ssh-keypair -N ''
aws ec2-instance-connect send-ssh-public-key --instance-id $(BASTION_INSTANCE_ID) --availability-zone ca-central-1b --instance-os-user ssm-user --ssh-public-key file://ssh-keypair.pub
ssh -i ssh-keypair ssm-user@$(BASTION_INSTANCE_ID) -L 5454:$(DB_HOST):5432 -o ProxyCommand="aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"