-
Notifications
You must be signed in to change notification settings - Fork 17
/
ccd
executable file
·711 lines (646 loc) · 20.9 KB
/
ccd
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
#!/bin/bash
TAGS_FILE=".tags.env"
WORKSPACE_ROOT=".workspace"
# Build related
GRADLE_ASSEMBLE_CMD="./gradlew assemble"
function project_config() {
project=$1
config=$2
case $project in
operational-reports|reports-runner)
name=operational
repository="git@github.com:hmcts/hmc-operational-reports-runner.git"
envPrefix="HMC_OPERATIONAL_REPORTS"
buildCommand=$GRADLE_ASSEMBLE_CMD
;;
cft-hearing-service|hearing-service)
name=hearings
repository="git@github.com:hmcts/hmc-cft-hearing-service.git"
envPrefix="HMC_HEARING_SERVICE"
buildCommand=$GRADLE_ASSEMBLE_CMD
;;
outbound-adapter)
name=outbound
repository="git@github.com:hmcts/hmc-hmi-outbound-adapter.git"
envPrefix="HMC_OUTBOUND_ADAPTER"
buildCommand=$GRADLE_ASSEMBLE_CMD
;;
inbound-adapter)
name=inbound
repository="git@github.com:hmcts/hmc-hmi-inbound-adapter.git"
envPrefix="HMC_INBOUND_ADAPTER"
buildCommand=$GRADLE_ASSEMBLE_CMD
;;
ccd-data-store-api|data-store-api|data-store|datastore|data-store)
name=ccd-data-store-api
repository="git@github.com:hmcts/ccd-data-store-api.git"
envPrefix="CCD_DATA_STORE_API"
buildCommand=$GRADLE_ASSEMBLE_CMD
;;
ccd-case-document-am-api|case-document-api|document-api|case-document)
name=ccd-ccd-case-document-am-api
repository="git@github.com:hmcts/ccd-case-document-am-api.git"
envPrefix="CCD_CASE_DOCUMENT_AM_API"
buildCommand=$GRADLE_ASSEMBLE_CMD
;;
ts-translation-service|translation-service)
name=ts-translation-service
repository="git@github.com:hmcts/ts-translation-service.git"
envPrefix="TS_TRANSLATION_SERVICE"
buildCommand=$GRADLE_ASSEMBLE_CMD
;;
ccd-definition-store-api|definition-store-api|definition-store|def-store|defstore)
name=ccd-definition-store-api
repository="git@github.com:hmcts/ccd-definition-store-api.git"
envPrefix="CCD_DEFINITION_STORE_API"
buildCommand=$GRADLE_ASSEMBLE_CMD
;;
ccd-user-profile-api|user-profile-api|user-profile|userprofile)
name=ccd-user-profile-api
repository="git@github.com:hmcts/ccd-user-profile-api.git"
envPrefix="CCD_USER_PROFILE_API"
buildCommand=$GRADLE_ASSEMBLE_CMD
;;
ccd-api-gateway|api-gateway|gateway)
name=ccd-api-gateway
repository="git@github.com:hmcts/ccd-api-gateway.git"
envPrefix="CCD_API_GATEWAY"
;;
ccd-admin-web|admin-web|admin-web)
name=ccd-admin-web
repository="git@github.com:hmcts/ccd-admin-web.git"
envPrefix="CCD_ADMIN_WEB"
;;
dm-store|document-store)
name=dm-store
repository="git@github.com:hmcts/document-management-store-app.git"
envPrefix="DM_STORE"
buildCommand="./gradlew installDist assemble"
;;
ccd-test-stubs-service|test-stubs-service)
name=ccd-test-stubs-service
repository="git@github.com:hmcts/ccd-test-stubs-service.git"
envPrefix="CCD_TEST_STUBS_SERVICE"
buildCommand=$GRADLE_ASSEMBLE_CMD
;;
xui-manage-cases)
name=xui-manage-cases
repository="git@github.com:hmcts/rpx-xui-webapp.git"
envPrefix="XUI_MANAGE_CASES"
;;
ccd-message-publisher|message-publisher)
name=ccd-message-publisher
repository="git@github.com:hmcts/ccd-message-publisher.git"
envPrefix="CCD_MESSAGE_PUBLISHER"
buildCommand=$GRADLE_ASSEMBLE_CMD
;;
ccd-case-disposer|case-disposer)
name=ccd-case-disposer
repository="git@github.com:hmcts/ccd-case-disposer.git"
envPrefix="CCD_CASE_DISPOSER"
buildCommand=$GRADLE_ASSEMBLE_CMD
;;
ccd-next-hearing-date-updater|next-hearing-date-updater)
name=ccd-next-hearing-date-updater
repository="git@github.com:hmcts/ccd-next-hearing-date-updater.git"
envPrefix="CCD_NEXT_HEARING_DATE_UPDATER"
buildCommand=$GRADLE_ASSEMBLE_CMD
;;
am-role-assignment-service|role-assignment-service|ras)
name=am-role-assignment-service
repository="git@github.com:hmcts/am-role-assignment-service.git"
envPrefix="AM_ROLE_ASSIGNMENT_SERVICE"
buildCommand=$GRADLE_ASSEMBLE_CMD
;;
ccd-definition-designer-api|definition-designer-api|definition-designer|def-design|designer)
name=ccd-definition-designer-api
repository="git@github.com:hmcts/ccd-definition-designer-api.git"
envPrefix="CCD_DEFINITION_DESIGNER_API"
buildCommand=$GRADLE_ASSEMBLE_CMD
;;
*)
echo "Project must be one of: ccd-data-store-api, ccd-definition-store-api, ccd-user-profile-api, ccd-api-gateway, xui-manage-cases, ccd-message-publisher, ccd-case-disposer, ccd-admin-web, dm-store, ccd-definition-designer-api, ccd-case-document-am-api, ts-translation-service, ccd-next-hearing-date-updater, hearing-service, operational-reports, outbound-adapter, inbound-adapter"
exit 1 ;;
esac
case $config in
name)
echo $name
;;
tagEnv)
echo ${envPrefix}_TAG
;;
useLocalEnv)
echo ${envPrefix}_USE_LOCAL
;;
repository)
echo $repository
;;
buildCommand)
echo $buildCommand
;;
*)
echo "Config is one of name, repository, tagEnv, useLocalEnv or buildCommand. '$config' is unknown"
exit 1
;;
esac
}
function tag_unset() {
project=$1
tagEnv=$(project_config $project tagEnv)
useLocalEnv=$(project_config $project useLocalEnv)
touch $TAGS_FILE
sed -i '' "/$tagEnv/d" $TAGS_FILE
sed -i '' "/${useLocalEnv}/d" $TAGS_FILE
}
function tag_set() {
project=$1
branch=$2
tagEnv=$(project_config $project tagEnv)
useLocalEnv=$(project_config $project useLocalEnv)
tagName=$(tag_name $project $branch)
touch $TAGS_FILE
sed -i '' "/$tagEnv/d" $TAGS_FILE
echo "export $tagEnv=$tagName" >> $TAGS_FILE
echo "export ${useLocalEnv}=''" >> $TAGS_FILE
}
function has_active_tags() {
if [ $(has_tagfile) -eq 0 ]; then
echo 0
else
egrep -q "_TAG=" $TAGS_FILE
if [ $? -eq 0 ]; then
echo 1
else
echo 0
fi
fi
}
function active_tags() {
egrep "_TAG=" $TAGS_FILE | sed -E 's/^export +(.*)_TAG=.*/\1/'
}
function active_tag_hash() {
tag=$1
egrep "${tag}_TAG=" $TAGS_FILE | sed -E 's/.*[-]([a-f0-9]+)$/\1/'
}
function active_tag_branch() {
tag=$1
egrep "${tag}_TAG=" $TAGS_FILE | sed -E 's/.*=(.+)[-][a-f0-9]+$/\1/'
}
function tag_friendly_name() {
tag=$1
echo $1 | tr "[:upper:]_" "[:lower:]-"
}
function workspace_dir() {
project=$1
name=$(project_config $project name)
workspace="$WORKSPACE_ROOT/$name"
echo $workspace
}
function clean_name() {
name=$1
clean=$(echo $name | sed 's#/#-#')
echo $clean
}
function git_clone() {
project=$1
branch=$2
local_repository=$3
if [ -n "$branch" ]; then
branch_option="--branch $branch"
fi
workspace=$(workspace_dir $project)
repository=$(project_config $project repository)
rm -rf $workspace
mkdir -p $workspace
if [ "$local_repository" ]; then
echo "Cloning from local repository $local_repository"
repository=$local_repository
fi
result=$(git clone --depth=20 --no-tags $branch_option $repository $workspace 2>&1 || exit $?)
exitcode=$?
echo "$result"
echo $result | egrep -q "Remote branch .* not found in upstream origin"
no_branch=$?
if [ $no_branch -eq 0 ]; then
echo
echo "$project does not contain the branch '$branch'"
echo
echo " $(basename $0) branches $project"
echo
echo "To list available branches in '$project'"
echo
exit $exitcode
fi
}
function available_branches() {
project=$1
workspace=$(workspace_dir $project)
git_clone $project
(cd $workspace; git branch -vr --sort refname --no-merged) | egrep -v 'HEAD|master|demo' | sed 's#origin/##'
}
git_hash() {
project=$1
workspace=$(workspace_dir $project)
gitHash=$(cd $workspace && git rev-parse HEAD | cut -c -7)
echo $gitHash
}
function tag_name() {
project=$1
branch=$2
gitHash=$(git_hash $project)
cleanBranch=$(clean_name $branch)
branch_and_hash=${cleanBranch}-${gitHash}
echo $branch_and_hash
}
function project_build() {
project=$1
workspace=$(workspace_dir $project)
buildCommand=$(project_config $project buildCommand)
# Build if required
if [ -n "$buildCommand" ]; then
(cd $workspace && eval $buildCommand || exit $?)
fi
}
function docker_build() {
project=$1
branch=$2
buildProject=$project
workspace=$(workspace_dir $project)
tagName=$(tag_name $project $branch)
# ccd-api-gateway image from azure coming as ccd-api-gateway-web, to match and fix for local branch build
if [[ $project == "ccd-api-gateway" && $branch != "master" ]]; then
buildProject="$buildProject-web"
fi
imageName=${buildProject#"ccd-"}
if [[ $project == "xui-manage-cases" ]]; then
(cd $workspace && docker build . -t xui/webapp:$tagName || exit $?)
fi
if [[ $project == "am-role-assignment-service" ]]; then
(cd $workspace && docker build . -t am/role-assignment-service:$tagName || exit $?)
fi
if [[ $project == "ts-translation-service" ]]; then
(cd $workspace && docker build . -t ts/translation-service:$tagName || exit $?)
fi
if [[ $project == "ccd-next-hearing-date-updater" ]]; then
(cd $workspace && docker build . -t ccd/next-hearing-date-updater:$tagName || exit $?)
fi
if [[ $project == "outbound-adapter" ]]; then
(cd $workspace && docker build . -t hmc/hmi-outbound-adapter:$tagName || exit $?)
fi
if [[ $project == "inbound-adapter" ]]; then
(cd $workspace && docker build . -t hmc/hmi-inbound-adapter:$tagName || exit $?)
fi
if [[ $project == "cft-hearing-service" || $project == "hearing-service" ]]; then
(cd $workspace && docker build . -t hmc/cft-hearing-service:$tagName || exit $?)
fi
if [[ $project == "operational-reports" || $project == "reports-runner" ]]; then
(cd $workspace && docker build . -t hmc/operational-reports-runner:$tagName || exit $?)
fi
if [[ $project != "xui-manage-cases" && $project != "am-role-assignment-service"
&& $project != "ts-translation-service" && $project != "ccd-next-hearing-date-updater"
&& $project != "hearing-service" && $project != "cft-hearing-service"
&& $project != "operational-reports" && $project != "reports-runner"
&& $project != "outbound-adapter" && $project != "inbound-adapter" ]]; then
(cd $workspace && docker build . -t ccd/$imageName:$tagName || exit $?)
fi
}
function available_compose_files() {
find compose -name "*.yml" | xargs -I % basename % .yml
}
function default_compose_files() {
cat compose/defaults.conf
}
function has_tagfile() {
if [ -f $TAGS_FILE ]; then
echo 1
else
echo 0
fi
}
function has_active_compose_files() {
if [ $(has_tagfile) -eq 0 ]; then
echo 0
else
egrep -q "^CCD_ENABLED_COMPOSE_FILES=" $TAGS_FILE
if [ $? -eq 0 ]; then
echo 1
else
echo 0
fi
fi
}
function active_compose_files() {
if [ $(has_active_compose_files) -eq 1 ]; then
source $TAGS_FILE
echo $CCD_ENABLED_COMPOSE_FILES
else
echo $(default_compose_files)
fi
}
function create_compose_file_options() {
for compose_file in $(active_compose_files)
do
compose_file_options="$compose_file_options -f compose/${compose_file}.yml"
done
echo $compose_file_options
}
function status_tags() {
if [ $(has_active_tags) -eq 1 ]; then
echo "Current overrides:"
for tag in $(active_tags)
do
echo "$(tag_friendly_name $tag) branch:$(active_tag_branch $tag) hash:$(active_tag_hash $tag)"
done
echo -e "\n"
else
echo -e "No overrides, all using master\n"
fi
}
function status_compose_files() {
if [ $(has_active_compose_files) -eq 1 ]; then
echo "Active compose files:"
echo -e "$(active_compose_files)\n"
else
echo "Using default compose files:"
echo -e "$(default_compose_files)\n"
fi
}
function usage() {
echo "Usage: $(basename $0) <command> [options]"
echo
echo "Commands:"
echo " set <project> <branch> [file://local_repository_path] - override and build project"
echo " unset <project...> - remove project override(s)"
echo " branches <project...> - list available branches"
echo " status - list current overrides and their status against upstream"
echo " update <project...> - update project override to match upstream branch; and build"
echo " enable <project>|defaults|show - enable a compose file"
echo " disable <project> - disable a compose file"
echo " compose [<docker-compose command> [options]] - wrap docker compose for your configuration"
echo
exit 1
}
function usage_set() {
if [ "$1" == "-h" ] || ([ $# -ne 2 ] && [ $# -ne 3 ]); then
echo "Overrides project to use the supplied branch, and build the project."
echo "To use a local repository provide its path (file://..) as a third parameter"
echo "Usage: $(basename $0) set <project> <branch> [file://local_repository_path]"
echo
exit 1
fi
}
function usage_unset() {
if [ $# -lt 1 -o "$1" == "-h" ]; then
echo "Remove branch overrides for the given projects."
echo "Usage: $(basename $0) unset <project...>"
echo
exit 1
fi
}
function usage_branches() {
if [ $# -lt 1 -o "$1" == "-h" ]; then
echo "List available branches for the given projects."
echo "Usage: $(basename $0) branches <project...>"
echo
exit 1
fi
}
function usage_enable() {
if [ $# -eq 1 -a "$1" == "-h" ]; then
echo "Enable projects that will be run by 'compose'"
echo "Usage:"
echo " $(basename $0) enable <project> - enable 'project'"
echo " $(basename $0) enable defaults - reset the enabled projects to the defaults"
echo " $(basename $0) enable show - currently enabled projects"
echo
exit 1
fi
}
function usage_disable() {
if [ $# -ne 1 -o "$1" == "-h" ]; then
echo "Disable a projects from being run by 'compose'"
echo "Usage: $(basename $0) disable <project>"
echo
exit 1
fi
}
function usage_compose() {
if [ $# -eq 1 -a "$1" == "-h" ]; then
echo "Use docker-compose to run the configured overrides and enabled projects"
echo "Usage: $(basename $0) compose [<docker-compose command> [options]]"
echo
exit 1
fi
}
function notimplemented() {
echo "Not Implemented yet"
exit 1
}
function ccd_set() {
usage_set $*
project=$1
branch=$2
if [ "$branch" == "master" ]; then
tag_unset $project
return
fi
project_config $project repository
git_clone $*
project_build $project
docker_build $project $branch
tag_set $project $branch
}
function ccd_unset() {
usage_unset $*
projects=$*
for project in $projects
do
tag_unset $project
done
}
function ccd_branches() {
usage_branches $*
projects=$*
for project in $projects
do
echo "Branches in '$project'"
available_branches $project
echo
done
}
function ccd_init() {
echo "creating docker network 'ccd-network'"
docker network create ccd-network && echo "'ccd-network' created"
}
function ccd_status() {
echo "Status:"
echo "======="
echo
status_compose_files
echo
status_tags
echo
}
function ccd_update() {
notimplemented
}
function ccd_enable_show() {
if [ $(has_active_compose_files) -eq 1 ]; then
echo "Currently active compose files:"
for compose_file in $(active_compose_files)
do
echo $compose_file
done | sort
echo
fi
echo "Default compose files:"
for compose_file in $(default_compose_files)
do
echo $compose_file
done | sort
}
function ccd_enable_defaults() {
if [ $(has_active_compose_files) -eq 1 ]; then
sed -i '' "/CCD_ENABLED_COMPOSE_FILES/d" $TAGS_FILE
fi
}
function ccd_enable_compose_file() {
for compose_file in $*
do
if [ ! -f compose/${compose_file}.yml ]; then
(>&2 echo "No such compose file. Choose one of:"
echo $(available_compose_files))
else
touch $TAGS_FILE
source $TAGS_FILE
sed -i '' "/CCD_ENABLED_COMPOSE_FILES/d" $TAGS_FILE
existing=0
for enabled_file in $CCD_ENABLED_COMPOSE_FILES
do
if [ "$compose_file" == "$enabled_file" ]; then
existing=1
fi
done
if [ $existing -eq 0 ]; then
echo "CCD_ENABLED_COMPOSE_FILES=\"$CCD_ENABLED_COMPOSE_FILES $compose_file\"" >> $TAGS_FILE
else
echo "CCD_ENABLED_COMPOSE_FILES=\"$CCD_ENABLED_COMPOSE_FILES\"" >> $TAGS_FILE
fi
fi
done
}
function ccd_enable() {
usage_enable $*
subcommand=$1
case $subcommand in
show)
ccd_enable_show
;;
default|defaults)
ccd_enable_defaults
;;
*)
ccd_enable_compose_file $*
;;
esac
}
function ccd_disable() {
if [ $(has_active_compose_files) -eq 0 ]; then
(>&2 echo "No enabled compose files, so we can't remove this. Currently using defaults:"
echo $(default_compose_files))
else
for compose_file in $*
do
touch $TAGS_FILE
source $TAGS_FILE
sed -i '' "/CCD_ENABLED_COMPOSE_FILES/d" $TAGS_FILE
existing=0
for enabled_file in $CCD_ENABLED_COMPOSE_FILES
do
#if enabled_file is not in the list to be disabled and it's not in $new_enabled yet
if [[ ! " $* " =~ .*\ $enabled_file\ .* ]] && [[ ! " $new_enabled " =~ .*\ $enabled_file\ .* ]]; then
new_enabled="$new_enabled $enabled_file"
else
existing=1
fi
done
if [ $existing -eq 0 ]; then
(>&2 echo "Cannot disable \"$compose_file\", as it is not active. Currently active:"
echo $(active_compose_files))
fi
if [ "$new_enabled" != "" ]; then
echo "CCD_ENABLED_COMPOSE_FILES=\"$new_enabled\"" >> $TAGS_FILE
fi
done
fi
}
function ccd_compose() {
print_warn_on_compose_down_if_idam_enabled $*
usage_compose $*
status_compose_files
status_tags
options="$@"
if [ $(has_tagfile) -eq 1 ]; then
source $TAGS_FILE
fi
compose_file_options=$(create_compose_file_options)
echo "Docker compose:"
echo -e "docker-compose $compose_file_options $options\n"
docker-compose $compose_file_options $options
}
function print_warn_on_compose_down_if_idam_enabled {
if [[ $(active_compose_files) == *"sidam"* ]] && [[ $1 = "down" ]]; then
read -p "WARNING - 'down' will remove Idam data. Use 'stop' to preserve data. Or if you intended to remove only CCD containers, disable Idam
compose files first. Do you want to continue?" yn
case $yn in
[Yy]* ) ;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
fi
}
function ccd_login() {
#docker logout hmctspublic.azurecr.io &>/dev/null
az acr login --name hmctspublic --subscription DCD-CNP-Prod
az acr login --name hmctsprivate --subscription DCD-CNP-Prod
}
if [ $# -lt 1 ]; then
usage
fi
command=$1
shift
case $command in
init)
ccd_init
;;
set)
ccd_set $*
;;
unset)
ccd_unset $*
;;
branches)
ccd_branches $*
;;
status)
ccd_status
;;
update)
ccd_update
;;
enable)
ccd_enable $*
;;
disable)
ccd_disable $*
;;
compose)
ccd_compose "$@"
;;
login)
ccd_login
;;
*)
usage
;;
esac