-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtasks
executable file
·306 lines (271 loc) · 7.95 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
#!/usr/bin/env bash
set -Eeo pipefail
###########################
# Local Config Parameters #
###########################
AWS_DEFAULT_REGION=eu-west-2
IMAGE_REPO_NAME=deductions/ehr-transfer-service
export NHS_SERVICE=ehr-transfer-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
}
# Do not change the file name as the aws helper scripts depend on it
AWS_HELPERS_FILE="utils/$AWS_HELPERS_VERSION/aws-helpers"
mkdir -p "utils/$AWS_HELPERS_VERSION"
if [[ ! -f $AWS_HELPERS_FILE ]];then
wget --quiet -O $AWS_HELPERS_FILE https://github.com/nhsconnect/prm-deductions-support-infra/releases/download/${AWS_HELPERS_VERSION}/aws-helpers
fi
chmod +x $AWS_HELPERS_FILE
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_local_envs {
export EHR_TRANSFER_SERVICE_MHS_QUEUE_URL_1=tcp://mq-1:61616
export EHR_TRANSFER_SERVICE_MHS_QUEUE_URL_2=tcp://mq-2:61616
export EHR_TRANSFER_SERVICE_TEST_ACTIVE_MQ_HOSTNAME=mq-1
export EHR_TRANSFER_SERVICE_MHS_QUEUE_USERNAME=admin
export EHR_TRANSFER_SERVICE_MHS_QUEUE_PASSWORD=admin
export LOCALSTACK_URL="http://localstack:4566"
export AWS_REGION=${AWS_DEFAULT_REGION}
}
function configure_sonar_environment_variable {
export SONAR_TOKEN=$(_get_aws_ssm_secret "/repo/dev/output/ehr-transfer-service/sonar_token")
}
#######################
# 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="deployment.tfplan"
elif [[ "${operation}" == "destroy" ]]; then
terraform plan -var task_image_tag=$IMAGE_TAG -var-file=$NHS_ENVIRONMENT.tfvars -out="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 deployment.tfplan
}
############################
# 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 ensure_prev_build_images_are_stopped {
echo "docker processes running: $(docker ps)"
MQ_CONTAINER_1=$(docker ps --filter publish=61616 -q)
MQ_CONTAINER_2=$(docker ps --filter publish=61617 -q)
LOCAL_STACK_CONTAINER=$(docker ps --filter publish=4566 -q)
if [ -n "$MQ_CONTAINER_1" ]; then
echo "stopping mq container 1"
docker stop $MQ_CONTAINER_1
fi
if [ -n "$MQ_CONTAINER_2" ]; then
echo "stopping mq container 2"
docker stop $MQ_CONTAINER_2
fi
if [ -n "$LOCAL_STACK_CONTAINER" ]; then
echo "stopping local stack container"
docker stop $LOCAL_STACK_CONTAINER
fi
}
###########
## TASKS ##
###########
command="$1"
case "${command}" in
assemble_docker)
configure_local_envs
dojo "./tasks _assemble"
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
;;
show_image_tag)
set_image_tag
echo $IMAGE_TAG
;;
_build)
rm -rf build/
gradle build
;;
build)
dojo "./tasks _build"
;;
_assemble)
rm -rf build/
gradle assemble
;;
assemble)
dojo "./tasks _assemble"
;;
_test_unit)
gradle test
;;
test_unit)
configure_local_envs
dojo "./tasks _test_unit"
;;
_test_integration)
gradle --info integration
;;
test_integration)
ensure_prev_build_images_are_stopped
configure_local_envs
dojo -c Dojofile-itest "./tasks _test_integration"
;;
test_integration_shell)
configure_local_envs
dojo -c Dojofile-itest
;;
_test_coverage)
gradle jacocoTestCoverageVerification
;;
test_coverage)
ensure_prev_build_images_are_stopped
configure_local_envs
dojo -c Dojofile-itest "./tasks _test_coverage"
;;
fetch_utils)
fetch_redaction_utils
;;
_code_quality)
gradle check -x test -x integration
;;
code_quality)
ensure_prev_build_images_are_stopped
configure_local_envs
dojo -c Dojofile-itest "./tasks _code_quality"
;;
_test_all)
gradle test integration jacocoTestCoverageVerification check
;;
test_all)
configure_local_envs
dojo -c Dojofile-itest "./tasks _test_all"
;;
test_all_shell)
configure_local_envs
dojo -c Dojofile-itest
;;
_run_sonar)
./gradlew build sonar --info
;;
run_sonar)
ensure_prev_build_images_are_stopped
_assume_environment_role $NHS_ENVIRONMENT
configure_local_envs
configure_sonar_environment_variable
dojo -c Dojofile-itest "./tasks _run_sonar"
;;
tf)
check_env
dojo -c Dojofile-infra "bash"
;;
_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"
;;
run_localstack_local)
docker-compose -f docker-compose.localstack-local.yaml up -d
;;
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