-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks
executable file
·295 lines (258 loc) · 7.29 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
#!/usr/bin/env bash
set -Eeo pipefail
###########################
# Local Config Parameters #
###########################
AWS_DEFAULT_REGION=eu-west-2
export NHS_SERVICE=re-registration-service
IMAGE_REPO_NAME=repo/$NHS_SERVICE
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 set_image_tag() {
if [[ -z "${GO_DEPENDENCY_LABEL_APP}" ]]; then
export IMAGE_TAG=${GO_PIPELINE_LABEL:-$(git rev-parse HEAD | cut -c 1-8)}
else
export IMAGE_TAG=${GO_DEPENDENCY_LABEL_APP}
fi
}
function get_aws_account_id {
AWS_ACCOUNT_ID=$(dojo -c Dojofile-infra "aws sts get-caller-identity | jq -r .Account")
}
function configure_envs {
export AWS_REGION=${AWS_DEFAULT_REGION}
export LOCALSTACK_URL="http://localstack:4566"
}
function configure_sonar_environment_variable {
export SONAR_TOKEN=$(_get_aws_ssm_secret "/repo/dev/output/re-registration-service/sonar_token")
}
function use_fake_aws_creds_for_localstack {
export AWS_ACCESS_KEY_ID=fake-key-id
export AWS_SECRET_ACCESS_KEY=fake-key
}
#######################
# Terraform Functions #
#######################
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
set_image_tag
tf_init
terraform get # modules
if [[ "${operation}" == "create" ]]; then
terraform plan -var task_image_tag=$IMAGE_TAG -var-file=$NHS_ENVIRONMENT.tfvars -out="nhs_deployment.tfplan"
elif [[ "${operation}" == "destroy" ]]; then
terraform plan -var task_image_tag=$IMAGE_TAG -var-file=$NHS_ENVIRONMENT.tfvars -out="nhs_deployment.tfplan" -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 nhs_deployment.tfplan
terraform output -json > tf-out.json
}
############################
# 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`
}
function die_localstack_die {
echo docker processes running: $(docker ps)
DOCKER_CONTAINERS_ON_PORT=$(docker ps --filter publish=4566 -q)
echo docker processes running on expected localstack port: $DOCKER_CONTAINERS_ON_PORT
if [ -z "$DOCKER_CONTAINERS_ON_PORT" ]; then
echo no localstack nonsense going on, cool
else
echo trying to kill localstack...
docker stop $DOCKER_CONTAINERS_ON_PORT
fi
}
###########
## TASKS ##
###########
command="$1"
case "${command}" in
fetch_utils)
fetch_redaction_utils
;;
die_localstack_die)
die_localstack_die
;;
build_docker)
configure_envs
dojo -c Dojofile-itest "./tasks _build"
configure_docker_repository_uri
fetch_redaction_utils
build_docker_image
echo "Pushing the Docker image... $REPOSITORY_URI:$IMAGE_TAG"
docker push $REPOSITORY_URI:$IMAGE_TAG
;;
_build)
rm -rf build/
gradle assemble
;;
build)
dojo "./tasks _build"
;;
_test_unit)
gradle test
;;
test_unit)
dojo "./tasks _test_unit"
;;
_test_integration)
gradle --info integration
;;
test_integration)
configure_envs
use_fake_aws_creds_for_localstack
die_localstack_die
dojo -c Dojofile-itest "./tasks _test_integration"
;;
test_integration_shell)
configure_envs
use_fake_aws_creds_for_localstack
dojo -c Dojofile-itest
;;
_test_coverage)
gradle jacocoTestCoverageVerification
;;
test_coverage)
configure_envs
die_localstack_die
dojo -c Dojofile-itest "./tasks _test_coverage"
;;
_code_quality)
gradle check -x test -x integration
;;
code_quality)
die_localstack_die
dojo -c Dojofile-itest "./tasks _code_quality"
;;
_test_all)
gradle test integration jacocoTestCoverageVerification check
;;
test_all)
configure_envs
use_fake_aws_creds_for_localstack
die_localstack_die
dojo -c Dojofile-itest "./tasks _test_all"
;;
_run_sonar)
./gradlew build sonar --info
;;
run_sonar)
configure_envs
_assume_environment_role $NHS_ENVIRONMENT
configure_sonar_environment_variable
use_fake_aws_creds_for_localstack
die_localstack_die
dojo -c Dojofile-itest "./tasks _run_sonar"
;;
run_local)
configure_envs
./gradlew bootRun
;;
run_localstack_local)
docker-compose -f docker-compose.localstack-local.yaml up -d
;;
_tf)
_assume_environment_role $NHS_ENVIRONMENT
tf_init
bash
;;
tf)
check_env
dojo -c Dojofile-infra "./tasks _tf"
;;
_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}
;;
wait_ecs)
check_env
dojo -c Dojofile-infra "./tasks _wait_ecs"
;;
*)
echo "Invalid command: '${command}'"
exit 1
;;
esac
set +e