-
Notifications
You must be signed in to change notification settings - Fork 5
/
tasks
executable file
·418 lines (377 loc) · 10.1 KB
/
tasks
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
#!/bin/bash
set -Eeo pipefail
###########################
# Local Config Parameters #
###########################
AWS_DEFAULT_REGION=eu-west-2
IMAGE_REPO_NAME=deductions/gp2gp-messenger
export NHS_SERVICE=gp2gp-messenger
AWS_HELPERS_VERSION=0.2.27
echo "AWS helper scripts version: $AWS_HELPERS_VERSION"
###########################
# Shared utils #
###########################
function download_util() {
local UTIL_VERSION=$1
local UTIL_FILENAME=$2
local UTIL_FILEPATH="utils/$UTIL_VERSION/$UTIL_FILENAME"
mkdir -p "utils/$UTIL_VERSION"
if [[ ! -f $UTIL_FILEPATH ]];then
wget --quiet -O $UTIL_FILEPATH https://github.com/nhsconnect/prm-deductions-support-infra/releases/download/${UTIL_VERSION}/${UTIL_FILENAME}
fi
chmod +x $UTIL_FILEPATH
echo "$UTIL_FILEPATH"
}
function fetch_redaction_utils() {
download_util $AWS_HELPERS_VERSION run-with-redaction.sh
download_util $AWS_HELPERS_VERSION redactor
}
AWS_HELPERS_FILE=$(download_util $AWS_HELPERS_VERSION aws-helpers)
source $AWS_HELPERS_FILE
####################################
# Instance (Environment) Variables #
####################################
function check_env {
if [[ -z "${NHS_ENVIRONMENT}" ]]; then
echo "Must set NHS_ENVIRONMENT"
exit 1
fi
}
function check_nhs_service {
if [[ -z "${NHS_SERVICE}" ]]; then
echo "Must set NHS_SERVICE"
exit 1
fi
}
function configure_domain_infix {
if [[ "${NHS_ENVIRONMENT}" == "prod" ]]; then
export DOMAIN_INFIX="prod"
else
export DOMAIN_INFIX="${NHS_ENVIRONMENT}.non-prod"
fi
}
function configure_local_envs {
export E2E_TEST_AUTHORIZATION_KEYS_FOR_GP2GP_MESSENGER=auth-key-2
export REPOSITORY_URI=$IMAGE_REPO_NAME
configure_service_url
}
function configure_domain_infix {
if [[ "${NHS_ENVIRONMENT}" == "prod" ]]; then
export DOMAIN_INFIX="prod"
else
export DOMAIN_INFIX="${NHS_ENVIRONMENT}.non-prod"
fi
}
function configure_service_url {
configure_domain_infix
if [[ -z "${NHS_ENVIRONMENT}" ]]; then
export SERVICE_URL="http://${NHS_SERVICE}:3000"
else
export SERVICE_URL="https://${NHS_SERVICE}.${DOMAIN_INFIX}.patient-deductions.nhs.uk"
fi
}
function set_image_tag() {
export IMAGE_TAG=$(git rev-parse HEAD | cut -c 1-8)
}
function get_aws_account_id {
AWS_ACCOUNT_ID=$(dojo -c Dojofile-infra "aws sts get-caller-identity | jq -r .Account")
}
function configure_aws_service_e2e_test_auth_keys_for_gp2gp_messenger {
parameter_name="/repo/${NHS_ENVIRONMENT}/user-input/api-keys/${NHS_SERVICE}/e2e-test"
export E2E_TEST_AUTHORIZATION_KEYS_FOR_GP2GP_MESSENGER=$(_get_aws_ssm_secret ${parameter_name})
}
#######################
# Terraform Functions #
#######################
function configure_tf_plan_filename {
certs=$1
if [[ "${certs}" == "true" ]]; then
export TF_PLAN_FILENAME="certs_deployment.tfplan"
else
export TF_PLAN_FILENAME="deployment.tfplan"
fi
}
function tf_init {
check_env
cd terraform
terraform init -reconfigure \
-backend-config key="${NHS_SERVICE}-${NHS_ENVIRONMENT}/terraform.tfstate" \
-backend-config bucket="prm-deductions-${NHS_ENVIRONMENT}-terraform-state" \
-backend-config dynamodb_table="prm-deductions-${NHS_ENVIRONMENT}-terraform-table" \
-backend-config region=${AWS_DEFAULT_REGION}
}
function tf_plan {
operation=$1
certs=$2
set_image_tag
TARGET=""
configure_tf_plan_filename $certs
if [[ "${certs}" == "true" ]]; then
TARGET="-target=aws_acm_certificate.gp2gp-messenger-cert"
fi
tf_init
terraform get # modules
if [[ "${operation}" == "create" ]]; then
terraform plan -var task_image_tag=$IMAGE_TAG -var-file=$NHS_ENVIRONMENT.tfvars $TARGET -out="${TF_PLAN_FILENAME}"
elif [[ "${operation}" == "destroy" ]]; then
terraform plan -var task_image_tag=$IMAGE_TAG -var-file=$NHS_ENVIRONMENT.tfvars -out="${TF_PLAN_FILENAME}" -destroy
else
echo "Unknown operation (should be create or destroy), got: ${operation}"
exit 1
fi
}
function tf_apply {
tf_init
terraform get # modules
terraform apply deployment.tfplan
}
function tf_apply_certs {
tf_init
terraform get # modules
terraform apply certs_deployment.tfplan
}
####################
# Script Functions #
####################
function generate_authorization_keys {
check_env
secret_id="/repo/${NHS_ENVIRONMENT}/user-input/gp2gp-messenger-authorization-keys"
value=$(openssl rand -base64 24 | tr -d "/@\'+")
set +e
aws ssm get-parameter --region $AWS_DEFAULT_REGION --name "$secret_id" | jq -r ".Parameter.Value" > /dev/null
if [[ $? == 0 ]]; then
echo "Authorization keys already exist"
else
set -e
echo "Authorization keys do not exist. Creating..."
aws ssm put-parameter \
--region $AWS_DEFAULT_REGION \
--name "$secret_id" \
--type SecureString \
--overwrite \
--value "$value"
fi
}
############################
# Docker Related Functions #
############################
function docker_login {
echo Logging in to Amazon ECR...
eval $(dojo -c Dojofile-infra "aws ecr get-login --no-include-email --region $AWS_DEFAULT_REGION")
}
function configure_docker_repository_uri {
docker_login
get_aws_account_id
export REPOSITORY_URI=$AWS_ACCOUNT_ID.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/$IMAGE_REPO_NAME
}
function build_docker_image {
echo Build started on `date`
set_image_tag
echo Building the Docker image...
docker build --build-arg UTILS_VERSION=$AWS_HELPERS_VERSION -t $REPOSITORY_URI:latest -t $REPOSITORY_URI:$IMAGE_TAG .
echo Build completed on `date`
}
###########
## TASKS ##
###########
command="$1"
case "${command}" in
_dep)
npm install
npm run check-audit
;;
dep)
dojo "./tasks _dep"
;;
_fix_dep)
npm install
npm run resolve-audit
;;
fix_dep)
dojo "./tasks _fix_dep"
;;
_list_outdated)
npm install
npm outdated > outdated-dependencies.txt
;;
list_outdated)
dojo "./tasks _list_outdated"
;;
update_package_json)
dojo "npx npm-check-updates -u"
;;
_build)
rm -rf build/
npm install
npm run build
;;
build)
dojo "./tasks _build"
;;
_test_lint)
npm install
npm run lint
;;
test_lint)
dojo "./tasks _test_lint"
;;
_test_unit)
npm install
npm run test:unit
;;
test_unit)
dojo "./tasks _test_unit"
;;
_test_integration)
npm install
npm run test:integration
;;
test_integration)
configure_local_envs
dojo -c Dojofile-itest "./tasks _test_integration"
;;
test_integration_shell)
configure_local_envs
dojo -c Dojofile-itest
;;
sanity_check)
dojo "./tasks _sanity_check"
;;
_sanity_check)
configure_domain_infix
check_env
nslookup "gp2gp-messenger.${DOMAIN_INFIX}.patient-deductions.nhs.uk"
curl -i --fail "https://gp2gp-messenger.${DOMAIN_INFIX}.patient-deductions.nhs.uk/health"
;;
_test_functional)
npm install
if [[ "${NHS_ENVIRONMENT}" == "dev" ]]; then
npm run test:functional:opentest
else
npm run test:functional
fi
;;
test_functional)
check_env
_assume_environment_role $NHS_ENVIRONMENT
configure_aws_service_e2e_test_auth_keys_for_gp2gp_messenger
configure_service_url
dojo "./tasks _test_functional"
;;
_test_e2e)
npm install
npm run test:e2e
;;
test_e2e)
check_env
_assume_environment_role $NHS_ENVIRONMENT
configure_aws_service_e2e_test_auth_keys_for_gp2gp_messenger
configure_service_url
dojo "./tasks _test_e2e"
;;
_test_coverage)
npm install
npm run test:coverage
;;
test_coverage)
configure_local_envs
dojo -c Dojofile-itest "./tasks _test_coverage"
;;
fetch_utils)
fetch_redaction_utils
;;
build_docker_local)
configure_local_envs
fetch_redaction_utils
build_docker_image
;;
build_docker)
configure_docker_repository_uri
fetch_redaction_utils
build_docker_image
echo "Pushing the Docker image... $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG"
docker push $REPOSITORY_URI:$IMAGE_TAG
;;
_test_docker)
npm install
npm run test:docker
;;
test_docker)
configure_docker_repository_uri
set_image_tag
configure_service_url
dojo -c Dojofile-dtest "./tasks _test_docker"
;;
test_docker_local)
configure_local_envs
set_image_tag
dojo -c Dojofile-dtest "./tasks _test_docker"
;;
_generate_authorization_keys)
generate_authorization_keys
;;
generate_authorization_keys)
dojo -c Dojofile-infra "./tasks _generate_authorization_keys"
;;
_tf)
check_env
tf_init
bash
;;
tf)
dojo -c Dojofile-infra "./tasks _tf"
;;
_tf_plan_certs)
_assume_environment_role $NHS_ENVIRONMENT
tf_plan "$2" true
;;
tf_plan_certs)
check_env
dojo -c Dojofile-infra "./tasks _tf_plan_certs $2"
;;
_tf_apply_certs)
_assume_environment_role $NHS_ENVIRONMENT
tf_apply_certs
;;
tf_apply_certs)
check_env
dojo -c Dojofile-infra "./tasks _tf_apply_certs"
;;
_tf_plan)
_assume_environment_role $NHS_ENVIRONMENT
tf_plan "$2"
;;
tf_plan)
check_env
dojo -c Dojofile-infra "./tasks _tf_plan $2"
;;
_tf_apply)
_assume_environment_role $NHS_ENVIRONMENT
tf_apply
;;
tf_apply)
check_env
dojo -c Dojofile-infra "./tasks _tf_apply"
;;
promote_docker_image)
check_env
set_image_tag
promote_docker_image "$IMAGE_REPO_NAME:$IMAGE_TAG" "$NHS_ENVIRONMENT"
;;
_wait_ecs)
_assume_environment_role $NHS_ENVIRONMENT
aws ecs wait services-stable \
--region $AWS_DEFAULT_REGION \
--cluster $NHS_ENVIRONMENT-${NHS_SERVICE}-ecs-cluster \
--service $NHS_ENVIRONMENT-${NHS_SERVICE}-service
;;
wait_ecs)
check_env
dojo -c Dojofile-infra "./tasks _wait_ecs"
;;
*)
echo "Invalid command: '${command}'"
exit 1
;;
esac
set +e