-
Notifications
You must be signed in to change notification settings - Fork 5
/
build-deploy-docker-compose.sh
executable file
·1694 lines (1481 loc) · 81.5 KB
/
build-deploy-docker-compose.sh
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
set +x
# get the buildname from the pod, $HOSTNAME contains this in the running pod, so we can use this
# set it to something usable here
export LAGOON_BUILD_NAME=$HOSTNAME
BUILD_WARNING_COUNT=0
function cronScheduleMoreOftenThan30Minutes() {
#takes a unexpanded cron schedule, returns 0 if it's more often that 30 minutes
MINUTE=$(echo $1 | (read -a ARRAY; echo ${ARRAY[0]}) )
if [[ $MINUTE =~ ^(M|H|\*)\/([0-5]?[0-9])$ ]]; then
# Match found for M/xx, H/xx or */xx
# Check if xx is smaller than 30, which means this cronjob runs more often than every 30 minutes.
STEP=${BASH_REMATCH[2]}
if [ $STEP -lt 30 ]; then
return 0
else
return 1
fi
elif [[ $MINUTE =~ ^\*$ ]]; then
# We are running every minute
return 0
else
# all other cases are more often than 30 minutes
return 1
fi
}
function contains() {
[[ $1 =~ (^|[[:space:]])$2($|[[:space:]]) ]] && return 0 || return 1
}
# featureFlag searches for feature flag variables in the following locations
# and order:
#
# 1. The cluster-force feature flag, prefixed with LAGOON_FEATURE_FLAG_FORCE_,
# as a build pod environment variable. This is set via a flag on the
# build-deploy controller. This overrides the other variables and allows
# policy enforcement at the cluster level.
#
# 2. The regular feature flag, prefixed with LAGOON_FEATURE_FLAG_, in the
# Lagoon environment global scoped env-vars. This allows policy control at
# the environment level.
#
# 3. The regular feature flag, prefixed with LAGOON_FEATURE_FLAG_, in the
# Lagoon project global scoped env-vars. This allows policy control at the
# project level.
#
# 4. The cluster-default feature flag, prefixed with
# LAGOON_FEATURE_FLAG_DEFAULT_, as a build pod environment variable. This is
# set via a flag on the build-deploy controller. This allows default policy
# to be set at the cluster level, but maintains the ability to selectively
# override at the project or environment level.
#
# The value of the first variable found is printed to stdout. If the variable
# is not found, print an empty string. Additional arguments are ignored.
function featureFlag() {
# check for argument
[ "$1" ] || return
local forceFlagVar defaultFlagVar flagVar
# check build pod environment for the force policy first
forceFlagVar="LAGOON_FEATURE_FLAG_FORCE_$1"
[ "${!forceFlagVar}" ] && echo "${!forceFlagVar}" && return
flagVar="LAGOON_FEATURE_FLAG_$1"
# check Lagoon environment variables
flagValue=$(jq -r '.[] | select(.scope == "global" and .name == "'"$flagVar"'") | .value' <<<"$LAGOON_ENVIRONMENT_VARIABLES")
[ "$flagValue" ] && echo "$flagValue" && return
# check Lagoon project variables
flagValue=$(jq -r '.[] | select(.scope == "global" and .name == "'"$flagVar"'") | .value' <<<"$LAGOON_PROJECT_VARIABLES")
[ "$flagValue" ] && echo "$flagValue" && return
# fall back to the default, if set.
defaultFlagVar="LAGOON_FEATURE_FLAG_DEFAULT_$1"
echo "${!defaultFlagVar}"
}
function projectEnvironmentVariableCheck() {
# check for argument
[ "$1" ] || return
local flagVar
flagVar="$1"
# check Lagoon environment variables
flagValue=$(jq -r '.[] | select(.name == "'"$flagVar"'") | .value' <<<"$LAGOON_ENVIRONMENT_VARIABLES")
[ "$flagValue" ] && echo "$flagValue" && return
# check Lagoon project variables
flagValue=$(jq -r '.[] | select(.name == "'"$flagVar"'") | .value' <<<"$LAGOON_PROJECT_VARIABLES")
[ "$flagValue" ] && echo "$flagValue" && return
echo "$2"
}
SCC_CHECK=$(kubectl -n ${NAMESPACE} get pod ${LAGOON_BUILD_NAME} -o json | jq -r '.metadata.annotations."openshift.io/scc" // false')
function beginBuildStep() {
[ "$1" ] || return #Buildstep start
[ "$2" ] || return #buildstep
echo -e "##############################################\nBEGIN ${1}\n##############################################"
# patch the buildpod with the buildstep
if [ "${SCC_CHECK}" == false ]; then
kubectl patch -n ${NAMESPACE} pod ${LAGOON_BUILD_NAME} \
-p "{\"metadata\":{\"labels\":{\"lagoon.sh/buildStep\":\"${2}\"}}}" &> /dev/null
# tiny sleep to allow patch to complete before logs roll again
sleep 0.5s
fi
}
function patchBuildStep() {
[ "$1" ] || return #total start time
[ "$2" ] || return #step start time
[ "$3" ] || return #previous step end time
[ "$4" ] || return #namespace
[ "$5" ] || return #buildstep
[ "$6" ] || return #buildstep
[ "$7" ] || return #has warnings
totalStartTime=$(date -d "${1}" +%s)
startTime=$(date -d "${2}" +%s)
endTime=$(date -d "${3}" +%s)
timeZone=$(date +"%Z")
diffSeconds="$(($endTime-$startTime))"
diffTime=$(date -d @${diffSeconds} +"%H:%M:%S" -u)
diffTotalSeconds="$(($endTime-$totalStartTime))"
diffTotalTime=$(date -d @${diffTotalSeconds} +"%H:%M:%S" -u)
hasWarnings=""
if [ "${7}" == "true" ]; then
hasWarnings=" WithWarnings"
fi
echo -e "##############################################\nSTEP ${6}: Completed at ${3} (${timeZone}) Duration ${diffTime} Elapsed ${diffTotalTime}${hasWarnings}\n##############################################"
}
##############################################
### PREPARATION
##############################################
buildStartTime="$(date +"%Y-%m-%d %H:%M:%S")"
beginBuildStep "Initial Environment Setup" "initialSetup"
echo "STEP: Preparation started ${buildStartTime}"
##############################################
### PUSH the latest .lagoon.yml into lagoon-yaml configmap as a pre-deploy field
##############################################
# set the imagecache registry if it is provided
IMAGECACHE_REGISTRY=""
if [ ! -z "$(featureFlag IMAGECACHE_REGISTRY)" ]; then
IMAGECACHE_REGISTRY=$(featureFlag IMAGECACHE_REGISTRY)
# add trailing slash if it is missing
length=${#IMAGECACHE_REGISTRY}
last_char=${IMAGECACHE_REGISTRY:length-1:1}
[[ $last_char != "/" ]] && IMAGECACHE_REGISTRY="$IMAGECACHE_REGISTRY/"; :
fi
# Load path of docker-compose that should be used
DOCKER_COMPOSE_YAML=($(cat .lagoon.yml | shyaml get-value docker-compose-yaml))
echo "Updating lagoon-yaml configmap with a pre-deploy version of the .lagoon.yml file"
if kubectl -n ${NAMESPACE} get configmap lagoon-yaml &> /dev/null; then
# replace it
# if the environment has already been deployed with an existing configmap that had the file in the key `.lagoon.yml`
# just nuke the entire configmap and replace it with our new key and file
LAGOON_YML_CM=$(kubectl -n ${NAMESPACE} get configmap lagoon-yaml -o json)
if [ "$(echo ${LAGOON_YML_CM} | jq -r '.data.".lagoon.yml" // false')" == "false" ]; then
# if the key doesn't exist, then just update the pre-deploy yaml only
kubectl -n ${NAMESPACE} get configmap lagoon-yaml -o json | jq --arg add "`cat .lagoon.yml`" '.data."pre-deploy" = $add' | kubectl apply -f -
else
# if the key does exist, then nuke it and put the new key
kubectl -n ${NAMESPACE} create configmap lagoon-yaml --from-file=pre-deploy=.lagoon.yml -o yaml --dry-run=client | kubectl replace -f -
fi
else
# create it
kubectl -n ${NAMESPACE} create configmap lagoon-yaml --from-file=pre-deploy=.lagoon.yml
fi
echo "Updating docker-compose-yaml configmap with a pre-deploy version of the docker-compose.yml file"
if kubectl -n ${NAMESPACE} get configmap docker-compose-yaml &> /dev/null; then
# replace it
# if the environment has already been deployed with an existing configmap that had the file in the key `docker-compose.yml`
# just nuke the entire configmap and replace it with our new key and file
LAGOON_YML_CM=$(kubectl -n ${NAMESPACE} get configmap docker-compose-yaml -o json)
if [ "$(echo ${LAGOON_YML_CM} | jq -r '.data."docker-compose.yml" // false')" == "false" ]; then
# if the key doesn't exist, then just update the pre-deploy yaml only
kubectl -n ${NAMESPACE} get configmap docker-compose-yaml -o json | jq --arg add "`cat ${DOCKER_COMPOSE_YAML}`" '.data."pre-deploy" = $add' | kubectl apply -f -
else
# if the key does exist, then nuke it and put the new key
kubectl -n ${NAMESPACE} create configmap docker-compose-yaml --from-file=pre-deploy=${DOCKER_COMPOSE_YAML} -o yaml --dry-run=client | kubectl replace -f -
fi
else
# create it
kubectl -n ${NAMESPACE} create configmap docker-compose-yaml --from-file=pre-deploy=${DOCKER_COMPOSE_YAML}
fi
set +ex
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${buildStartTime}" "${currentStepEnd}" "${NAMESPACE}" "initialSetup" "Initial Environment Setup" "false"
previousStepEnd=${currentStepEnd}
beginBuildStep "Docker Compose Validation" "dockerComposeValidation"
DOCKER_COMPOSE_WARNING_COUNT=0
##############################################
### RUN docker compose config check against the provided docker-compose file
### use the `build-validate` built in validater to run over the provided docker-compose file
##############################################
dccOutput=$(bash -c 'build-deploy-tool validate docker-compose --docker-compose '${DOCKER_COMPOSE_YAML}'; exit $?' 2>&1)
dccExit=$?
if [ "${dccExit}" != "0" ]; then
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "dockerComposeValidationError" "Docker Compose Validation" "false"
previousStepEnd=${currentStepEnd}
echo "
##############################################
Warning!
There are issues with your docker compose file that lagoon uses that should be fixed.
You can run docker compose config locally to check that your docker-compose file is valid.
##############################################
"
echo ${dccOutput}
echo "
##############################################"
exit 1
fi
## validate the docker-compose in a way to eventually phase out forked library by displaying warnings
dccOutput=$(bash -c 'build-deploy-tool validate docker-compose --ignore-non-string-key-errors=false --ignore-missing-env-files=false --docker-compose '${DOCKER_COMPOSE_YAML}'; exit $?' 2>&1)
dccExit=$?
if [ "${dccExit}" != "0" ]; then
((++BUILD_WARNING_COUNT))
((++DOCKER_COMPOSE_WARNING_COUNT))
echo "
##############################################
Warning!
There are issues with your docker compose file that lagoon uses that should be fixed.
You can run docker compose config locally to check that your docker-compose file is valid.
"
if [[ "${dccOutput}" =~ "no such file or directory" ]]; then
echo "> an env_file is defined in your docker-compose file, but no matching file found."
fi
if [[ "${dccOutput}" =~ "Non-string key" ]]; then
echo "> an invalid string key was detected in your docker-compose file."
fi
echo ERR: ${dccOutput}
echo ""
fi
dccOutput=$(bash -c 'build-deploy-tool validate docker-compose-with-errors --docker-compose '${DOCKER_COMPOSE_YAML}'; exit $?' 2>&1)
dccExit2=$?
if [ "${dccExit2}" != "0" ]; then
((++DOCKER_COMPOSE_WARNING_COUNT))
if [ "${dccExit}" == "0" ]; then
((++BUILD_WARNING_COUNT))
echo "
##############################################
Warning!
There are issues with your docker compose file that lagoon uses that should be fixed.
You can run docker compose config locally to check that your docker-compose file is valid.
"
fi
echo "> There are yaml validation errors in your docker-compose file that should be corrected."
echo ERR: ${dccOutput}
echo ""
fi
if [[ "$DOCKER_COMPOSE_WARNING_COUNT" -gt 0 ]]; then
echo "Read the docs for more on errors displayed here https://docs.lagoon.sh/lagoon-build-errors
"
echo "##############################################"
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "dockerComposeValidationWarning" "Docker Compose Validation" "true"
previousStepEnd=${currentStepEnd}
else
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "dockerComposeValidation" "Docker Compose Validation" "false"
previousStepEnd=${currentStepEnd}
fi
beginBuildStep ".lagoon.yml Validation" "lagoonYmlValidation"
##############################################
### RUN lagoon-yml validation against the final data which may have overrides
### from .lagoon.override.yml file or LAGOON_YAML_OVERRIDE environment variable
##############################################
lyvOutput=$(bash -c 'build-deploy-tool validate lagoon-yml; exit $?' 2>&1)
lyvExit=$?
if [ "${lyvExit}" != "0" ]; then
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "lagoonYmlValidationError" ".lagoon.yml Validation" "false"
previousStepEnd=${currentStepEnd}
echo "
##############################################
Warning!
There are issues with your .lagoon.yml file that must be fixed.
Refer to the .lagoon.yml docs for the correct syntax
https://docs.lagoon.sh/using-lagoon-the-basics/lagoon-yml/
##############################################
"
echo "${lyvOutput}"
echo "
##############################################"
exit 1
fi
set -ex
set +x
# Validate .lagoon.yml only, no overrides. lagoon-linter still has checks that
# aren't in build-deploy-tool.
if ! lagoon-linter; then
echo "https://docs.lagoon.sh/lagoon/using-lagoon-the-basics/lagoon-yml#restrictions describes some possible reasons for this build failure."
echo "If you require assistance to fix this error, please contact support."
exit 1
else
echo "lagoon-linter found no issues with the .lagoon.yml file"
fi
##################
# build deploy-tool can collect this value now from the lagoon.yml file
# this means further use of `LAGOON_GIT_SHA` can eventually be
# completely handled with build-deploy-tool wherever this value could be consumed
# this logic can then just be replaced entirely with a single export so that the build-deploy-tool
# will know what the value is, and performs the switch based on what the lagoon.yml provides
# this is retained for now until the remaining functionality that uses it is moved to the build-deploy-tool
#
# export LAGOON_GIT_SHA=`git rev-parse HEAD`
#
INJECT_GIT_SHA=$(cat .lagoon.yml | shyaml get-value environment_variables.git_sha false)
if [ "$INJECT_GIT_SHA" == "true" ]
then
# export this so the build-deploy-tool can read it
export LAGOON_GIT_SHA=`git rev-parse HEAD`
else
export LAGOON_GIT_SHA="0000000000000000000000000000000000000000"
fi
##################
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "lagoonYmlValidation" ".lagoon.yml Validation" "false"
previousStepEnd=${currentStepEnd}
beginBuildStep "Configure Variables" "configuringVariables"
DEPLOY_TYPE=$(cat .lagoon.yml | shyaml get-value environments.${BRANCH//./\\.}.deploy-type default)
# Load all Services that are defined
COMPOSE_SERVICES=($(cat $DOCKER_COMPOSE_YAML | shyaml keys services))
##############################################
### CACHE IMAGE LIST GENERATION
##############################################
# get a list of the images in the deployments for seeing image cache if required
export LAGOON_CACHE_BUILD_ARGS=$(kubectl -n ${NAMESPACE} get deployments -o yaml -l 'lagoon.sh/service' \
| yq -o json e '.items[].spec.template.spec.containers[].image | capture("^(?P<image>.+\/.+\/.+\/(?P<name>.+)\@.*)$")' \
| jq -sMrc)
# Figure out which services should we handle
SERVICE_TYPES=()
IMAGES=()
NATIVE_CRONJOB_CLEANUP_ARRAY=()
declare -A MAP_DEPLOYMENT_SERVICETYPE_TO_IMAGENAME
declare -A MAP_SERVICE_TYPE_TO_COMPOSE_SERVICE
declare -A MAP_SERVICE_NAME_TO_IMAGENAME
declare -A MAP_SERVICE_NAME_TO_SERVICEBROKER_CLASS
declare -A MAP_SERVICE_NAME_TO_SERVICEBROKER_PLAN
declare -A MAP_SERVICE_NAME_TO_DBAAS_ENVIRONMENT
# this array stores the images that will need to be pulled from an external registry (private, dockerhub)
declare -A IMAGES_PULL
# this array stores the built images
declare -A IMAGES_BUILD
# this array stores the image names that will be pushed (registry/project/environment/service:tag)
declare -A IMAGES_PUSH
# this array stores the images from the source environment that will be pulled from
declare -A IMAGES_PROMOTE
# this array stores the hashes of the built images
declare -A IMAGE_HASHES
HELM_ARGUMENTS=()
. /kubectl-build-deploy/scripts/kubectl-get-cluster-capabilities.sh
for CAPABILITIES in "${CAPABILITIES[@]}"; do
HELM_ARGUMENTS+=(-a "${CAPABILITIES}")
done
# Allow the servicetype be overridden by the lagoon API
# This accepts colon separated values like so `SERVICE_NAME:SERVICE_TYPE_OVERRIDE`, and multiple overrides
# separated by commas
# Example 1: mariadb:mariadb-dbaas < tells any docker-compose services named mariadb to use the mariadb-dbaas service type
# Example 2: mariadb:mariadb-dbaas,nginx:nginx-persistent
if [ ! -z "$LAGOON_PROJECT_VARIABLES" ]; then
LAGOON_SERVICE_TYPES=($(echo $LAGOON_PROJECT_VARIABLES | jq -r '.[] | select(.name == "LAGOON_SERVICE_TYPES") | "\(.value)"'))
fi
if [ ! -z "$LAGOON_ENVIRONMENT_VARIABLES" ]; then
TEMP_LAGOON_SERVICE_TYPES=($(echo $LAGOON_ENVIRONMENT_VARIABLES | jq -r '.[] | select(.name == "LAGOON_SERVICE_TYPES") | "\(.value)"'))
if [ ! -z $TEMP_LAGOON_SERVICE_TYPES ]; then
LAGOON_SERVICE_TYPES=$TEMP_LAGOON_SERVICE_TYPES
fi
fi
# Allow the dbaas environment type to be overridden by the lagoon API
# This accepts colon separated values like so `SERVICE_NAME:DBAAS_ENVIRONMENT_TYPE`, and multiple overrides
# separated by commas
# Example 1: mariadb:production < tells any docker-compose services named mariadb to use the production dbaas environment type
# Example 2: mariadb:production,mariadb-test:development
if [ ! -z "$LAGOON_PROJECT_VARIABLES" ]; then
LAGOON_DBAAS_ENVIRONMENT_TYPES=($(echo $LAGOON_PROJECT_VARIABLES | jq -r '.[] | select(.name == "LAGOON_DBAAS_ENVIRONMENT_TYPES") | "\(.value)"'))
fi
if [ ! -z "$LAGOON_ENVIRONMENT_VARIABLES" ]; then
TEMP_LAGOON_DBAAS_ENVIRONMENT_TYPES=($(echo $LAGOON_ENVIRONMENT_VARIABLES | jq -r '.[] | select(.name == "LAGOON_DBAAS_ENVIRONMENT_TYPES") | "\(.value)"'))
if [ ! -z $TEMP_LAGOON_DBAAS_ENVIRONMENT_TYPES ]; then
LAGOON_DBAAS_ENVIRONMENT_TYPES=$TEMP_LAGOON_DBAAS_ENVIRONMENT_TYPES
fi
fi
# loop through created DBAAS templates
DBAAS=($(build-deploy-tool identify dbaas))
for COMPOSE_SERVICE in "${COMPOSE_SERVICES[@]}"
do
# The name of the service can be overridden, if not we use the actual servicename
SERVICE_NAME=$(cat $DOCKER_COMPOSE_YAML | shyaml get-value services.$COMPOSE_SERVICE.labels.lagoon\\.name default)
if [ "$SERVICE_NAME" == "default" ]; then
SERVICE_NAME=$COMPOSE_SERVICE
fi
# Load the servicetype. If it's "none" we will not care about this service at all
SERVICE_TYPE=$(cat $DOCKER_COMPOSE_YAML | shyaml get-value services.$COMPOSE_SERVICE.labels.lagoon\\.type custom)
# Allow the servicetype to be overriden by environment in .lagoon.yml
ENVIRONMENT_SERVICE_TYPE_OVERRIDE=$(cat .lagoon.yml | shyaml get-value environments.${BRANCH//./\\.}.types.$SERVICE_NAME false)
if [ ! $ENVIRONMENT_SERVICE_TYPE_OVERRIDE == "false" ]; then
SERVICE_TYPE=$ENVIRONMENT_SERVICE_TYPE_OVERRIDE
fi
if [ ! -z "$LAGOON_SERVICE_TYPES" ]; then
IFS=',' read -ra LAGOON_SERVICE_TYPES_SPLIT <<< "$LAGOON_SERVICE_TYPES"
for LAGOON_SERVICE_TYPE in "${LAGOON_SERVICE_TYPES_SPLIT[@]}"
do
IFS=':' read -ra LAGOON_SERVICE_TYPE_SPLIT <<< "$LAGOON_SERVICE_TYPE"
if [ "${LAGOON_SERVICE_TYPE_SPLIT[0]}" == "$SERVICE_NAME" ]; then
SERVICE_TYPE=${LAGOON_SERVICE_TYPE_SPLIT[1]}
fi
done
fi
# Previous versions of Lagoon used "python-ckandatapusher", this should be mapped to "python"
if [[ "$SERVICE_TYPE" == "python-ckandatapusher" ]]; then
SERVICE_TYPE="python"
fi
if [[ "${CAPABILITIES[@]}" =~ "backup.appuio.ch/v1alpha1/PreBackupPod" ]]; then
if [[ "$SERVICE_TYPE" == "opensearch" ]] || [[ "$SERVICE_TYPE" == "elasticsearch" ]]; then
if kubectl -n ${NAMESPACE} get prebackuppods.backup.appuio.ch "${SERVICE_NAME}-prebackuppod" &> /dev/null; then
kubectl -n ${NAMESPACE} delete prebackuppods.backup.appuio.ch "${SERVICE_NAME}-prebackuppod"
fi
fi
fi
if [ "$SERVICE_TYPE" == "none" ]; then
continue
fi
# For DeploymentConfigs with multiple Services inside (like nginx-php), we allow to define the service type of within the
# deploymentconfig via lagoon.deployment.servicetype. If this is not set we use the Compose Service Name
DEPLOYMENT_SERVICETYPE=$(cat $DOCKER_COMPOSE_YAML | shyaml get-value services.$COMPOSE_SERVICE.labels.lagoon\\.deployment\\.servicetype default)
if [ "$DEPLOYMENT_SERVICETYPE" == "default" ]; then
DEPLOYMENT_SERVICETYPE=$COMPOSE_SERVICE
fi
# The ImageName is the same as the Name of the Docker Compose ServiceName
IMAGE_NAME=$COMPOSE_SERVICE
for DBAAS_ENTRY in "${DBAAS[@]}"
do
IFS=':' read -ra DBAAS_ENTRY_SPLIT <<< "$DBAAS_ENTRY"
DBAAS_SERVICE_NAME=${DBAAS_ENTRY_SPLIT[0]}
DBAAS_SERVICE_TYPE=${DBAAS_ENTRY_SPLIT[1]}
if [ "$DBAAS_SERVICE_NAME" == "$SERVICE_NAME" ]; then
if [ "$SERVICE_TYPE" == "mariadb" ]; then
SERVICE_TYPE=$DBAAS_SERVICE_TYPE
fi
if [ "$SERVICE_TYPE" == "postgres" ]; then
SERVICE_TYPE=$DBAAS_SERVICE_TYPE
fi
if [ "$SERVICE_TYPE" == "mongo" ]; then
SERVICE_TYPE=$DBAAS_SERVICE_TYPE
fi
fi
done
# Do not handle images for shared services
if [[ "$SERVICE_TYPE" != "mariadb-dbaas" ]] &&
[[ "$SERVICE_TYPE" != "mariadb-shared" ]] &&
[[ "$SERVICE_TYPE" != "postgres-shared" ]] &&
[[ "$SERVICE_TYPE" != "postgres-dbaas" ]] &&
[[ "$SERVICE_TYPE" != "mongodb-dbaas" ]] &&
[[ "$SERVICE_TYPE" != "mongodb-shared" ]]; then
# Generate list of images to build
IMAGES+=("${IMAGE_NAME}")
fi
# Map Deployment ServiceType to the ImageName
MAP_DEPLOYMENT_SERVICETYPE_TO_IMAGENAME["${SERVICE_NAME}:${DEPLOYMENT_SERVICETYPE}"]="${IMAGE_NAME}"
# Create an array with all Service Names and Types if it does not exist yet
if [[ ! " ${SERVICE_TYPES[@]} " =~ " ${SERVICE_NAME}:${SERVICE_TYPE} " ]]; then
SERVICE_TYPES+=("${SERVICE_NAME}:${SERVICE_TYPE}")
fi
# ServiceName and Type to Original Service Name Mapping, but only once per Service name and Type,
# as we have original services that appear twice (like in the case of nginx-php)
if [[ ! "${MAP_SERVICE_TYPE_TO_COMPOSE_SERVICE["${SERVICE_NAME}:${SERVICE_TYPE}"]+isset}" ]]; then
MAP_SERVICE_TYPE_TO_COMPOSE_SERVICE["${SERVICE_NAME}:${SERVICE_TYPE}"]="${COMPOSE_SERVICE}"
fi
# ServiceName to ImageName mapping, but only once as we have original services that appear twice (like in the case of nginx-php)
# these will be handled via MAP_DEPLOYMENT_SERVICETYPE_TO_IMAGENAME
if [[ ! "${MAP_SERVICE_NAME_TO_IMAGENAME["${SERVICE_NAME}"]+isset}" ]]; then
MAP_SERVICE_NAME_TO_IMAGENAME["${SERVICE_NAME}"]="${IMAGE_NAME}"
fi
done
# Get the pre-rollout and post-rollout vars
if [ ! -z "$LAGOON_PROJECT_VARIABLES" ]; then
LAGOON_PREROLLOUT_DISABLED=($(echo $LAGOON_PROJECT_VARIABLES | jq -r '.[] | select(.name == "LAGOON_PREROLLOUT_DISABLED") | "\(.value)"'))
LAGOON_POSTROLLOUT_DISABLED=($(echo $LAGOON_PROJECT_VARIABLES | jq -r '.[] | select(.name == "LAGOON_POSTROLLOUT_DISABLED") | "\(.value)"'))
fi
if [ ! -z "$LAGOON_ENVIRONMENT_VARIABLES" ]; then
TEMP_LAGOON_PREROLLOUT_DISABLED=($(echo $LAGOON_ENVIRONMENT_VARIABLES | jq -r '.[] | select(.name == "LAGOON_PREROLLOUT_DISABLED") | "\(.value)"'))
TEMP_LAGOON_POSTROLLOUT_DISABLED=($(echo $LAGOON_ENVIRONMENT_VARIABLES | jq -r '.[] | select(.name == "LAGOON_POSTROLLOUT_DISABLED") | "\(.value)"'))
if [ ! -z $TEMP_LAGOON_PREROLLOUT_DISABLED ]; then
LAGOON_PREROLLOUT_DISABLED=$TEMP_LAGOON_PREROLLOUT_DISABLED
fi
if [ ! -z $TEMP_LAGOON_POSTROLLOUT_DISABLED ]; then
LAGOON_POSTROLLOUT_DISABLED=$TEMP_LAGOON_POSTROLLOUT_DISABLED
fi
fi
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${buildStartTime}" "${currentStepEnd}" "${NAMESPACE}" "configureVars" "Configure Variables" "false"
previousStepEnd=${currentStepEnd}
beginBuildStep "Container Registry Login" "registryLogin"
# seed all the push images for use later on, push images relate to images that may not be built by this build
# but are required from somewhere else like a promote environment or from another registry
ENVIRONMENT_IMAGE_BUILD_DATA=$(build-deploy-tool identify image-builds)
# log in to the provided registry if details are provided
if [ ! -z ${INTERNAL_REGISTRY_URL} ] ; then
echo "Logging in to Lagoon main registry"
if [ ! -z ${INTERNAL_REGISTRY_USERNAME} ] && [ ! -z ${INTERNAL_REGISTRY_PASSWORD} ] ; then
echo "docker login -u '${INTERNAL_REGISTRY_USERNAME}' -p '${INTERNAL_REGISTRY_PASSWORD}' ${INTERNAL_REGISTRY_URL}" | /bin/bash
# create lagoon-internal-registry-secret if it does not exist yet
# TODO: remove this, the secret is created by the remote-controller, builds only need to log in to it now
# if ! kubectl -n ${NAMESPACE} get secret lagoon-internal-registry-secret &> /dev/null; then
# kubectl create secret docker-registry lagoon-internal-registry-secret --docker-server=${INTERNAL_REGISTRY_URL} --docker-username=${INTERNAL_REGISTRY_USERNAME} --docker-password=${INTERNAL_REGISTRY_PASSWORD} --dry-run -o yaml | kubectl apply -f -
# fi
echo "Set internal registry secrets for token ${INTERNAL_REGISTRY_USERNAME} in ${REGISTRY}"
fi
fi
# log in to any container registries before building or pulling images
for PCR in $(echo "$ENVIRONMENT_IMAGE_BUILD_DATA" | jq -c '.containerRegistries[]? | @base64')
do
PRIVATE_CONTAINER_REGISTRY=$(echo "${PCR}" | jq -rc '@base64d')
PRIVATE_CONTAINER_REGISTRY_URL=$(echo "$PRIVATE_CONTAINER_REGISTRY" | jq -r '.url // false')
PRIVATE_CONTAINER_REGISTRY_CREDENTIAL_USERNAME=$(echo "$PRIVATE_CONTAINER_REGISTRY" | jq -r '.username // false')
PRIVATE_CONTAINER_REGISTRY_USERNAME_SOURCE=$(echo "$PRIVATE_CONTAINER_REGISTRY" | jq -r '.usernameSource // false')
PRIVATE_REGISTRY_CREDENTIAL=$(echo "$PRIVATE_CONTAINER_REGISTRY" | jq -r '.password // false')
PRIVATE_REGISTRY_CREDENTIAL_SOURCE=$(echo "$PRIVATE_CONTAINER_REGISTRY" | jq -r '.passwordSource // false')
if [ $PRIVATE_CONTAINER_REGISTRY_URL != "false" ]; then
echo "Attempting to log in to $PRIVATE_CONTAINER_REGISTRY_URL with user $PRIVATE_CONTAINER_REGISTRY_CREDENTIAL_USERNAME from $PRIVATE_CONTAINER_REGISTRY_USERNAME_SOURCE"
echo "Using password sourced from $PRIVATE_REGISTRY_CREDENTIAL_SOURCE"
docker login --username $PRIVATE_CONTAINER_REGISTRY_CREDENTIAL_USERNAME --password $PRIVATE_REGISTRY_CREDENTIAL $PRIVATE_CONTAINER_REGISTRY_URL
else
echo "Attempting to log in to docker hub with user $PRIVATE_CONTAINER_REGISTRY_CREDENTIAL_USERNAME from $PRIVATE_CONTAINER_REGISTRY_USERNAME_SOURCE"
echo "Using password sourced from $PRIVATE_REGISTRY_CREDENTIAL_SOURCE"
docker login --username $PRIVATE_CONTAINER_REGISTRY_CREDENTIAL_USERNAME --password $PRIVATE_REGISTRY_CREDENTIAL
fi
done
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${buildStartTime}" "${currentStepEnd}" "${NAMESPACE}" "registryLogin" "Container Registry Login" "false"
previousStepEnd=${currentStepEnd}
beginBuildStep "Image Builds" "buildingImages"
##############################################
### BUILD IMAGES
##############################################
for IMAGE_BUILD_DATA in $(echo "$ENVIRONMENT_IMAGE_BUILD_DATA" | jq -c '.images[]')
do
SERVICE_NAME=$(echo "$IMAGE_BUILD_DATA" | jq -r '.name // false')
# add the image name to the array of images to push. this is consumed later in the build process
IMAGES_PUSH["${SERVICE_NAME}"]="$(echo "$IMAGE_BUILD_DATA" | jq -r '.imageBuild.buildImage')"
if [ "$BUILD_TYPE" == "promote" ]; then
# add the image name to the array of images to promote from. this is consumed later in the build process
IMAGES_PROMOTE["${SERVICE_NAME}"]="$(echo "$IMAGE_BUILD_DATA" | jq -r '.imageBuild.promoteImage')"
fi
done
# we only need to build images for pullrequests and branches
if [[ "$BUILD_TYPE" == "pullrequest" || "$BUILD_TYPE" == "branch" ]]; then
# use the build-deploy-tool to seed the image build information
BUILD_ARGS=() # build args are now calculated in the build-deploy tool in the generator step
# this loop extracts the build arguments from the response from the build deploy tools previous identify image-builds call
for IMAGE_BUILD_ARGUMENTS in $(echo "$ENVIRONMENT_IMAGE_BUILD_DATA" | jq -r '.buildArguments | to_entries[] | @base64'); do
BUILD_ARG_NAME=$(echo "$IMAGE_BUILD_ARGUMENTS" | jq -Rr '@base64d | fromjson | .key')
BUILD_ARG_VALUE=$(echo "$IMAGE_BUILD_ARGUMENTS" | jq -Rr '@base64d | fromjson | .value')
BUILD_ARGS+=(--build-arg ${BUILD_ARG_NAME}="${BUILD_ARG_VALUE}")
done
# now we loop through the images in the build data and determine if they need to be pulled or build
for IMAGE_BUILD_DATA in $(echo "$ENVIRONMENT_IMAGE_BUILD_DATA" | jq -c '.images[]')
do
SERVICE_NAME=$(echo "$IMAGE_BUILD_DATA" | jq -r '.name // false')
DOCKERFILE=$(echo "$IMAGE_BUILD_DATA" | jq -r '.imageBuild.dockerFile // false')
# if there is no dockerfile, then this image needs to be pulled from somewhere else
if [ $DOCKERFILE == "false" ]; then
PULL_IMAGE=$(echo "$IMAGE_BUILD_DATA" | jq -r '.imageBuild.pullImage // false')
if [ "$PULL_IMAGE" != "false" ]; then
IMAGES_PULL["${SERVICE_NAME}"]="${PULL_IMAGE}"
fi
else
# otherwise extract build information from the image build data payload
# this is a temporary image name to use for the build, it is based on the namespace and service, this can probably be deprecated and the images could just be
# built with the name they are meant to be. only 1 build can run at a time within a namespace
# the temporary name would clash here as well if there were multiple builds (it could use the `imageBuild.buildImage` value)
TEMPORARY_IMAGE_NAME=$(echo "$IMAGE_BUILD_DATA" | jq -r '.imageBuild.temporaryImage // false')
# the context for this image build, the original source for this value is from the `docker-compose file`
BUILD_CONTEXT=$(echo "$IMAGE_BUILD_DATA" | jq -r '.imageBuild.context // ""')
# the build target for this image build, the original source for this value is from the `docker-compose file`
BUILD_TARGET=$(echo "$IMAGE_BUILD_DATA" | jq -r '.imageBuild.target // false')
# determine if buildkit should be used for this build
DOCKER_BUILDKIT=0
if [ "$(echo "${ENVIRONMENT_IMAGE_BUILD_DATA}" | jq -r '.buildKit // false')" == "true" ]; then
DOCKER_BUILDKIT=1
echo "Using BuildKit for $DOCKERFILE";
fi
# now do the actual image build
if [ $BUILD_TARGET == "false" ]; then
echo "Building ${BUILD_CONTEXT}/${DOCKERFILE}"
DOCKER_BUILDKIT=$DOCKER_BUILDKIT docker build --network=host "${BUILD_ARGS[@]}" -t $TEMPORARY_IMAGE_NAME -f $BUILD_CONTEXT/$DOCKERFILE $BUILD_CONTEXT
else
echo "Building target ${BUILD_TARGET} for ${BUILD_CONTEXT}/${DOCKERFILE}"
DOCKER_BUILDKIT=$DOCKER_BUILDKIT docker build --network=host "${BUILD_ARGS[@]}" -t $TEMPORARY_IMAGE_NAME -f $BUILD_CONTEXT/$DOCKERFILE --target $BUILD_TARGET $BUILD_CONTEXT
fi
# Keep a list of the images we have built, as we need to push them to the registry later
IMAGES_BUILD["${SERVICE_NAME}"]="${TEMPORARY_IMAGE_NAME}"
# adding the build image to the list of arguments passed into the next image builds
SERVICE_NAME_UPPERCASE=$(echo "$SERVICE_NAME" | tr '[:lower:]' '[:upper:]')
fi
done
fi
# print information about built image sizes
function printBytes {
local -i bytes=$1;
echo "$(( (bytes + 1000000)/1000000 ))MB"
}
if [[ "${IMAGES_BUILD[@]}" ]]; then
echo "##############################################"
echo "Built image sizes:"
echo "##############################################"
fi
for IMAGE_NAME in "${!IMAGES_BUILD[@]}"
do
TEMPORARY_IMAGE_NAME="${IMAGES_BUILD[${IMAGE_NAME}]}"
echo -e "Image ${TEMPORARY_IMAGE_NAME}\t\t$(printBytes $(docker inspect ${TEMPORARY_IMAGE_NAME} | jq -r '.[0].Size'))"
done
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "imageBuildComplete" "Image Builds" "false"
previousStepEnd=${currentStepEnd}
beginBuildStep "Pre-Rollout Tasks" "runningPreRolloutTasks"
##############################################
### RUN PRE-ROLLOUT tasks defined in .lagoon.yml
##############################################
if [ "${LAGOON_PREROLLOUT_DISABLED}" != "true" ]; then
build-deploy-tool tasks pre-rollout
else
echo "pre-rollout tasks are currently disabled LAGOON_PREROLLOUT_DISABLED is set to true"
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "preRolloutsCompleted" "Pre-Rollout Tasks" "false"
fi
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
previousStepEnd=${currentStepEnd}
beginBuildStep "Service Configuration Phase 1" "serviceConfigurationPhase1"
##############################################
### CONFIGURE SERVICES, AUTOGENERATED ROUTES AND DBAAS CONFIG
##############################################
YAML_FOLDER="/kubectl-build-deploy/lagoon/services-routes"
mkdir -p $YAML_FOLDER
# BC for routes.insecure, which is now called routes.autogenerate.insecure
BC_ROUTES_AUTOGENERATE_INSECURE=$(cat .lagoon.yml | shyaml get-value routes.insecure false)
if [ ! $BC_ROUTES_AUTOGENERATE_INSECURE == "false" ]; then
echo "=== routes.insecure is now defined in routes.autogenerate.insecure, pleae update your .lagoon.yml file"
# update the .lagoon.yml with the new location for build-deploy-tool to read
yq3 write -i -- .lagoon.yml 'routes.autogenerate.insecure' $BC_ROUTES_AUTOGENERATE_INSECURE
fi
touch /kubectl-build-deploy/values.yaml
yq3 write -i -- /kubectl-build-deploy/values.yaml 'project' $PROJECT
yq3 write -i -- /kubectl-build-deploy/values.yaml 'environment' $ENVIRONMENT
yq3 write -i -- /kubectl-build-deploy/values.yaml 'environmentType' $ENVIRONMENT_TYPE
yq3 write -i -- /kubectl-build-deploy/values.yaml 'namespace' $NAMESPACE
yq3 write -i -- /kubectl-build-deploy/values.yaml 'gitSha' $LAGOON_GIT_SHA
yq3 write -i -- /kubectl-build-deploy/values.yaml 'buildType' $BUILD_TYPE
yq3 write -i -- /kubectl-build-deploy/values.yaml 'kubernetes' $KUBERNETES
yq3 write -i -- /kubectl-build-deploy/values.yaml 'lagoonVersion' $LAGOON_VERSION
# check for ROOTLESS_WORKLOAD feature flag, disabled by default
if [ "${SCC_CHECK}" != "false" ]; then
# openshift permissions are different, this is to set the fsgroup to the supplemental group from the openshift annotations
# this applies it to all deployments in this environment because we don't isolate by service type its applied to all
OPENSHIFT_SUPPLEMENTAL_GROUP=$(kubectl get namespace ${NAMESPACE} -o json | jq -r '.metadata.annotations."openshift.io/sa.scc.supplemental-groups"' | cut -c -10)
echo "Setting openshift fsGroup to ${OPENSHIFT_SUPPLEMENTAL_GROUP}"
yq3 write -i -- /kubectl-build-deploy/values.yaml 'podSecurityContext.fsGroup' $OPENSHIFT_SUPPLEMENTAL_GROUP
fi
echo -e "\
LAGOON_PROJECT=${PROJECT}\n\
LAGOON_ENVIRONMENT=${ENVIRONMENT}\n\
LAGOON_ENVIRONMENT_TYPE=${ENVIRONMENT_TYPE}\n\
LAGOON_GIT_SHA=${LAGOON_GIT_SHA}\n\
LAGOON_KUBERNETES=${KUBERNETES}\n\
" >> /kubectl-build-deploy/values.env
# DEPRECATED: will be removed with Lagoon 3.0.0
# LAGOON_GIT_SAFE_BRANCH is pointing to the enviornment name, therefore also is filled if this environment
# is created by a PR or Promote workflow. This technically wrong, therefore will be removed
echo -e "\
LAGOON_GIT_SAFE_BRANCH=${ENVIRONMENT}\n\
" >> /kubectl-build-deploy/values.env
if [ "$BUILD_TYPE" == "branch" ]; then
yq3 write -i -- /kubectl-build-deploy/values.yaml 'branch' $BRANCH
echo -e "\
LAGOON_GIT_BRANCH=${BRANCH}\n\
" >> /kubectl-build-deploy/values.env
fi
if [ "$BUILD_TYPE" == "pullrequest" ]; then
yq3 write -i -- /kubectl-build-deploy/values.yaml 'prHeadBranch' "$PR_HEAD_BRANCH"
yq3 write -i -- /kubectl-build-deploy/values.yaml 'prBaseBranch' "$PR_BASE_BRANCH"
yq3 write -i -- /kubectl-build-deploy/values.yaml 'prTitle' "$PR_TITLE"
yq3 write -i -- /kubectl-build-deploy/values.yaml 'prNumber' "$PR_NUMBER"
echo -e "\
LAGOON_PR_HEAD_BRANCH=${PR_HEAD_BRANCH}\n\
LAGOON_PR_BASE_BRANCH=${PR_BASE_BRANCH}\n\
LAGOON_PR_TITLE=${PR_TITLE}\n\
LAGOON_PR_NUMBER=${PR_NUMBER}\n\
" >> /kubectl-build-deploy/values.env
fi
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "serviceConfigurationComplete" "Service Configuration Phase 1" "false"
previousStepEnd=${currentStepEnd}
beginBuildStep "Service Configuration Phase 2" "serviceConfigurationPhase2"
##############################################
### CUSTOM FASTLY API SECRETS .lagoon.yml
##############################################
# if a customer is using their own fastly configuration, then they can define their api token and platform tls configuration ID in the .lagoon.yml file
# this will get created as a `kind: Secret` in kubernetes so that created ingresses will be able to use this secret to talk to the fastly api.
#
# in this example, the customer needs to add a build envvar called `FASTLY_API_TOKEN` and then populates the .lagoon.yml file with something like this
#
# fastly:
# api-secrets:
# - name: customer
# apiTokenVariableName: FASTLY_API_TOKEN
# platformTLSConfiguration: A1bcEdFgH12eD242Sds
#
# then the build process will attempt to check the lagoon variables for one called `FASTLY_API_TOKEN` and will use the value of this variable when creating the
# `kind: Secret` in kubernetes
#
# support for multiple api-secrets is possible in the instance that a customer uses 2 separate services in different accounts in the one project
## any fastly api secrets will be prefixed with this, so that we always add this to whatever the customer provides
FASTLY_API_SECRET_PREFIX="fastly-api-"
FASTLY_API_SECRETS_COUNTER=0
FASTLY_API_SECRETS=()
if [ -n "$(cat .lagoon.yml | shyaml keys fastly.api-secrets.$FASTLY_API_SECRETS_COUNTER 2> /dev/null)" ]; then
while [ -n "$(cat .lagoon.yml | shyaml get-value fastly.api-secrets.$FASTLY_API_SECRETS_COUNTER 2> /dev/null)" ]; do
FASTLY_API_SECRET_NAME=$FASTLY_API_SECRET_PREFIX$(cat .lagoon.yml | shyaml get-value fastly.api-secrets.$FASTLY_API_SECRETS_COUNTER.name 2> /dev/null)
if [ -z "$FASTLY_API_SECRET_NAME" ]; then
echo -e "A fastly api secret was defined in the .lagoon.yml file, but no name could be found the .lagoon.yml\n\nPlease check if the name has been set correctly."
exit 1
fi
FASTLY_API_TOKEN_VALUE=$(cat .lagoon.yml | shyaml get-value fastly.api-secrets.$FASTLY_API_SECRETS_COUNTER.apiTokenVariableName false)
if [[ $FASTLY_API_TOKEN_VALUE == "false" ]]; then
echo "No 'apiTokenVariableName' defined for fastly secret $FASTLY_API_SECRET_NAME"; exit 1;
fi
# if we have everything we need, we can proceed to logging in
if [ $FASTLY_API_TOKEN_VALUE != "false" ]; then
FASTLY_API_TOKEN=""
# check if we have a password defined anywhere in the api first
if [ ! -z "$LAGOON_PROJECT_VARIABLES" ]; then
FASTLY_API_TOKEN=($(echo $LAGOON_PROJECT_VARIABLES | jq -r '.[] | select(.scope == "build" and .name == "'$FASTLY_API_TOKEN_VALUE'") | "\(.value)"'))
fi
if [ ! -z "$LAGOON_ENVIRONMENT_VARIABLES" ]; then
TEMP_FASTLY_API_TOKEN=($(echo $LAGOON_ENVIRONMENT_VARIABLES | jq -r '.[] | select(.scope == "build" and .name == "'$FASTLY_API_TOKEN_VALUE'") | "\(.value)"'))
if [ ! -z "$TEMP_FASTLY_API_TOKEN" ]; then
FASTLY_API_TOKEN=$TEMP_FASTLY_API_TOKEN
fi
fi
if [ -z "$FASTLY_API_TOKEN" ]; then
echo -e "A fastly api secret was defined in the .lagoon.yml file, but no token could be found in the Lagoon API matching the variable name provided\n\nPlease check if the token has been set correctly."
exit 1
fi
fi
FASTLY_API_PLATFORMTLS_CONFIGURATION=$(cat .lagoon.yml | shyaml get-value fastly.api-secrets.$FASTLY_API_SECRETS_COUNTER.platformTLSConfiguration "")
if [ -z "$FASTLY_API_PLATFORMTLS_CONFIGURATION" ]; then
echo -e "A fastly api secret was defined in the .lagoon.yml file, but no platform tls configuration id could be found in the .lagoon.yml\n\nPlease check if the platform tls configuration id has been set correctly."
exit 1
fi
# run the script to create the secrets
. /kubectl-build-deploy/scripts/exec-fastly-api-secrets.sh
set +x
let FASTLY_API_SECRETS_COUNTER=FASTLY_API_SECRETS_COUNTER+1
done
fi
# FASTLY API SECRETS FROM LAGOON API VARIABLE
# Allow for defining fastly api secrets using lagoon api variables
# This accepts colon separated values like so `SECRET_NAME:FASTLY_API_TOKEN:FASTLY_PLATFORMTLS_CONFIGURATION_ID`, and multiple overrides
# separated by commas
# Example 1: examplecom:x1s8asfafasf7ssf:fa23rsdgsdgas
# ^^^ will create a kubernetes secret called `$FASTLY_API_SECRET_PREFIX-examplecom` with 2 data fields (one for api token, the other for platform tls id)
# populated with `x1s8asfafasf7ssf` and `fa23rsdgsdgas` for whichever field it should be
# and the name will get created with the prefix defined in `FASTLY_API_SECRET_PREFIX`
# Example 2: examplecom:x1s8asfafasf7ssf:fa23rsdgsdgas,example2com:fa23rsdgsdgas:x1s8asfafasf7ssf,example3com:fa23rsdgsdgas:x1s8asfafasf7ssf:example3com
if [ ! -z "$LAGOON_PROJECT_VARIABLES" ]; then
LAGOON_FASTLY_API_SECRETS=($(echo $LAGOON_PROJECT_VARIABLES | jq -r '.[] | select(.name == "LAGOON_FASTLY_API_SECRETS") | "\(.value)"'))
fi
if [ ! -z "$LAGOON_ENVIRONMENT_VARIABLES" ]; then
TEMP_LAGOON_FASTLY_API_SECRETS=($(echo $LAGOON_ENVIRONMENT_VARIABLES | jq -r '.[] | select(.name == "LAGOON_FASTLY_API_SECRETS") | "\(.value)"'))
if [ ! -z $TEMP_LAGOON_FASTLY_API_SECRETS ]; then
LAGOON_FASTLY_API_SECRETS=$TEMP_LAGOON_FASTLY_API_SECRETS
fi
fi
if [ ! -z "$LAGOON_FASTLY_API_SECRETS" ]; then
IFS=',' read -ra LAGOON_FASTLY_API_SECRETS_SPLIT <<< "$LAGOON_FASTLY_API_SECRETS"
for LAGOON_FASTLY_API_SECRETS_DATA in "${LAGOON_FASTLY_API_SECRETS_SPLIT[@]}"
do
IFS=':' read -ra LAGOON_FASTLY_API_SECRET_SPLIT <<< "$LAGOON_FASTLY_API_SECRETS_DATA"
if [ -z "${LAGOON_FASTLY_API_SECRET_SPLIT[0]}" ] || [ -z "${LAGOON_FASTLY_API_SECRET_SPLIT[1]}" ] || [ -z "${LAGOON_FASTLY_API_SECRET_SPLIT[2]}" ]; then
echo -e "An override was defined in the lagoon API with LAGOON_FASTLY_API_SECRETS but was not structured correctly, the format should be NAME:FASTLY_API_TOKEN:FASTLY_PLATFORMTLS_CONFIGURATION_ID and comma separated for multiples"
exit 1
fi
# the fastly api secret name will be created with the prefix that is defined above
FASTLY_API_SECRET_NAME=$FASTLY_API_SECRET_PREFIX${LAGOON_FASTLY_API_SECRET_SPLIT[0]}
FASTLY_API_TOKEN=${LAGOON_FASTLY_API_SECRET_SPLIT[1]}
FASTLY_API_PLATFORMTLS_CONFIGURATION=${LAGOON_FASTLY_API_SECRET_SPLIT[2]}
# run the script to create the secrets
. /kubectl-build-deploy/scripts/exec-fastly-api-secrets.sh
set +x
done
fi
# FASTLY SERVICE ID PER INGRESS OVERRIDE FROM LAGOON API VARIABLE
# Allow the fastly serviceid for specific ingress to be overridden by the lagoon API
# This accepts colon separated values like so `INGRESS_DOMAIN:FASTLY_SERVICE_ID:WATCH_STATUS:SECRET_NAME(OPTIONAL)`, and multiple overrides
# separated by commas
# Example 1: www.example.com:x1s8asfafasf7ssf:true
# ^^^ tells the ingress creation to use the service id x1s8asfafasf7ssf for ingress www.example.com, with the watch status of true
# Example 2: www.example.com:x1s8asfafasf7ssf:true,www.not-example.com:fa23rsdgsdgas:false
# ^^^ same as above, but also tells the ingress creation to use the service id fa23rsdgsdgas for ingress www.not-example.com, with the watch status of false
# Example 3: www.example.com:x1s8asfafasf7ssf:true:examplecom
# ^^^ tells the ingress creation to use the service id x1s8asfafasf7ssf for ingress www.example.com, with the watch status of true
# but it will also be annotated to be told to use the secret named `examplecom` that could be defined elsewhere
if [ ! -z "$LAGOON_PROJECT_VARIABLES" ]; then
LAGOON_FASTLY_SERVICE_IDS=($(echo $LAGOON_PROJECT_VARIABLES | jq -r '.[] | select(.name == "LAGOON_FASTLY_SERVICE_IDS") | "\(.value)"'))
fi
if [ ! -z "$LAGOON_ENVIRONMENT_VARIABLES" ]; then
TEMP_LAGOON_FASTLY_SERVICE_IDS=($(echo $LAGOON_ENVIRONMENT_VARIABLES | jq -r '.[] | select(.name == "LAGOON_FASTLY_SERVICE_IDS") | "\(.value)"'))
if [ ! -z $TEMP_LAGOON_FASTLY_SERVICE_IDS ]; then
LAGOON_FASTLY_SERVICE_IDS=$TEMP_LAGOON_FASTLY_SERVICE_IDS
fi
fi
##############################################
### CREATE SERVICES, AUTOGENERATED ROUTES AND DBAAS CONFIG
##############################################
# start custom routes disabled
AUTOGEN_ROUTES_DISABLED=false
if [ ! -z "$LAGOON_PROJECT_VARIABLES" ]; then
AUTOGEN_ROUTES_DISABLED=($(echo $LAGOON_PROJECT_VARIABLES | jq -r '.[] | select(.scope == "build") | select(.name == "LAGOON_AUTOGEN_ROUTES_DISABLED") | "\(.value)"'))
fi
if [ ! -z "$LAGOON_ENVIRONMENT_VARIABLES" ]; then
TEMP_AUTOGEN_ROUTES_DISABLED=($(echo $LAGOON_ENVIRONMENT_VARIABLES | jq -r '.[] | select(.scope == "build") | select(.name == "LAGOON_AUTOGEN_ROUTES_DISABLED") | "\(.value)"'))
if [ ! -z $TEMP_AUTOGEN_ROUTES_DISABLED ]; then
AUTOGEN_ROUTES_DISABLED=$TEMP_AUTOGEN_ROUTES_DISABLED
fi
fi
# generate the autogenerated ingress
if [ ! "$AUTOGEN_ROUTES_DISABLED" == true ]; then
LAGOON_AUTOGEN_YAML_FOLDER="/kubectl-build-deploy/lagoon/autogen-routes"
mkdir -p $LAGOON_AUTOGEN_YAML_FOLDER
build-deploy-tool template autogenerated-ingress --saved-templates-path ${LAGOON_AUTOGEN_YAML_FOLDER}
# apply autogenerated ingress
if [ -n "$(ls -A $LAGOON_AUTOGEN_YAML_FOLDER/ 2>/dev/null)" ]; then
find $LAGOON_AUTOGEN_YAML_FOLDER -type f -exec cat {} \;
kubectl apply -n ${NAMESPACE} -f $LAGOON_AUTOGEN_YAML_FOLDER/
fi
else
echo ">> Autogenerated ingress templates disabled for this build"
# end custom route
fi
# identify any autognerated resources based on their resource name
AUTOGEN_INGRESS=$(build-deploy-tool identify created-ingress | jq -r '.autogenerated[]')
AUTOGEN_ROUTES=$(kubectl -n ${NAMESPACE} get ingress --no-headers -l "lagoon.sh/autogenerated=true" | cut -d " " -f 1 | xargs)
MATCHED_AUTOGEN=false
DELETE_AUTOGEN=()
for AR in $AUTOGEN_ROUTES; do
for AI in $AUTOGEN_INGRESS; do
if [ "${AR}" == "${AI}" ]; then
MATCHED_AUTOGEN=true
continue
fi
done
if [ "${MATCHED_AUTOGEN}" != "true" ]; then
DELETE_AUTOGEN+=($AR)
fi
MATCHED_AUTOGEN=false
done
for DA in ${!DELETE_AUTOGEN[@]}; do
# delete any autogenerated ingress in the namespace as they are disabled
if kubectl -n ${NAMESPACE} get ingress ${DELETE_AUTOGEN[$DA]} &> /dev/null; then
echo ">> Removing autogenerated ingress for ${DELETE_AUTOGEN[$DA]} because it was disabled"
kubectl -n ${NAMESPACE} delete ingress ${DELETE_AUTOGEN[$DA]}
fi
done
for SERVICE_TYPES_ENTRY in "${SERVICE_TYPES[@]}"
do
echo "=== BEGIN route processing for service ${SERVICE_TYPES_ENTRY} ==="
IFS=':' read -ra SERVICE_TYPES_ENTRY_SPLIT <<< "$SERVICE_TYPES_ENTRY"
TEMPLATE_PARAMETERS=()
SERVICE_NAME=${SERVICE_TYPES_ENTRY_SPLIT[0]}
SERVICE_TYPE=${SERVICE_TYPES_ENTRY_SPLIT[1]}
touch /kubectl-build-deploy/${SERVICE_NAME}-values.yaml
done
# generate the dbaas templates if any
LAGOON_DBAAS_YAML_FOLDER="/kubectl-build-deploy/lagoon/dbaas"
mkdir -p $LAGOON_DBAAS_YAML_FOLDER
build-deploy-tool template dbaas --saved-templates-path ${LAGOON_DBAAS_YAML_FOLDER}
# apply dbaas
if [ -n "$(ls -A $LAGOON_DBAAS_YAML_FOLDER/ 2>/dev/null)" ]; then
find $LAGOON_DBAAS_YAML_FOLDER -type f -exec cat {} \;
kubectl apply -n ${NAMESPACE} -f $LAGOON_DBAAS_YAML_FOLDER/
fi
set +x
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "serviceConfiguration2Complete" "Service Configuration Phase 2" "false"
previousStepEnd=${currentStepEnd}
beginBuildStep "Route/Ingress Configuration" "configuringRoutes"
TEMPLATE_PARAMETERS=()
##############################################
### CUSTOM ROUTES
##############################################
# Run the route generation process
# start custom routes disabled
CUSTOM_ROUTES_DISABLED=false
if [ ! -z "$LAGOON_PROJECT_VARIABLES" ]; then