forked from rydnr/set-square
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·1417 lines (1261 loc) · 45 KB
/
build.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 dry-wit
# Copyright 2014-today Automated Computing Machinery S.L.
# Distributed under the terms of the GNU General Public License v3
# DW.import command;
# fun: main
# api: public
# txt: Main logic. Gets called by dry-wit.
# txt: Returns 0/TRUE always, but may exit due to errors.
# use: main
function main() {
local _repo;
local _parents;
local _buildRepo;
local _oldIFS="${IFS}";
resolve_base_image;
IFS="${DWIFS}";
for _repo in ${REPOSITORIES}; do
IFS="${_oldIFS}";
loadRepoEnvironmentVariables "${_repo}";
evalEnvVars;
retrieveNamespace;
local _namespace="${RESULT}";
_buildRepo=${FALSE};
if force_mode_enabled; then
_buildRepo=${TRUE};
elif ! repo_exists "${_repo}" "${TAG}"; then
_buildRepo=${TRUE};
else
logInfo -n "Not building ${_namespace}/${_repo}:${TAG} since it's already built";
logInfoResult SUCCESS "skipped";
fi
if isTrue ${_buildRepo}; then
find_parents "${_repo}"
_parents="${RESULT}"
IFS="${DWIFS}";
for _parent in ${_parents}; do
IFS="${_oldIFS}";
build_repo_if_defined_locally "${_parent}";
done
IFS="${_oldIFS}";
build_repo "${_repo}";
fi
overwrite_latest_tag "${_namespace}" "${_repo}" "${TAG}";
annotate_successful_build "${_repo}" "${TAG}";
if registry_push_enabled || registry_tag_enabled; then
registry_tag "${_repo}" "${TAG}";
if overwrite_latest_enabled; then
registry_tag "${_repo}" "latest";
fi
fi
if registry_push_enabled; then
registry_push "${_repo}" "${TAG}";
if overwrite_latest_enabled; then
registry_push "${_repo}" "latest";
fi
fi
done
IFS="${_oldIFS}";
cleanup_containers;
cleanup_images;
}
# fun: retrieveNamespace
# api: public
# txt: Retrieves the namespace.
# txt: Returns 0/TRUE always.
# use: if retrieveNamespace "bla"; then echo "Namespace for bla"; fi
function retrieveNamespace() {
local _flavor="";
if ! isEmpty "${SETSQUARE_FLAVOR}"; then
_flavor="-${SETSQUARE_FLAVOR}";
fi
export RESULT="${NAMESPACE}${_flavor}";
return ${TRUE};
}
# fun: repo_exists repo tag
# api: public
# txt: Does "${NAMESPACE}/${REPO}:${TAG}" exist?
# opt: repo: The repository.
# opt: tag: The tag.
# txt: Returns 0/TRUE if it exists; 1/FALSE otherwise.
# use: if repo_exists "myImage" "latest"; then echo "Repo exists"; fi
function repo_exists() {
local _repo="${1}";
checkNotEmpty repo "${_repo}" 1;
local _tag="${2}";
checkNotEmpty "tag" "${_tag}" 2;
local _aux;
local -i _rescode;
if evalEnvVar "${_tag}"; then
_aux="${RESULT}";
if isNotEmpty "${_aux}"; then
_tag="${_aux}";
fi
fi
retrieveNamespace;
local _namespace="${RESULT}";
local _images=$(${DOCKER} images "${_namespace}/${_repo}")
local _matches=$(echo "${_images}" | grep -- "${_tag}");
if isEmpty "${_matches}"; then
_rescode=${FALSE};
else
_rescode=${TRUE};
fi
return ${_rescode};
}
# fun: build_repo_if_defined_locally repo
# txt: Builds the image if it's defined locally.
# opt: repo: The repository.
# txt: Returns 0/TRUE always.
# use: build_repo_if_defined_locally "myImage:latest";
function build_repo_if_defined_locally() {
local _repo="${1}";
checkNotEmpty repo "${_repo}" 1;
local _name="${_repo%:*}";
local _tag="${_repo#*:}";
retrieveNamespace;
local _namespace;
if ! isEmpty "${_name}" && \
[[ -d ${_name} ]] && \
! repo_exists "${_name#${_namespace}/}" "${_tag}"; then
build_repo "${_name}"
fi
}
# fun: reduce_image_size namespace repo currentTag tag
# api: public
# txt: Squashes the image with docker-squash [1]
# txt: [1] https://github.com/jwilder/docker-squash
# opt: namespace: The namespace.
# opt: repo: The repo name.
# opt: currentTag: The current tag.
# opt: tag: The new tag for the squashed image.
# use: reduce_image_size "namespace" "myimage" "201508-raw" "201508"
function reduce_image_size() {
local _namespace="${1}";
checkNotEmpty namespace "${_namespace}" 1;
local _repo="${2}";
checkNotEmpty repo "${_repo}" 2;
local _currentTag="${3}";
checkNotEmpty currentTag "${_currentTag}" 3;
local _tag="${4}";
checkNotEmpty tag "${_tag}" 4;
checkReq docker-squash DOCKER_SQUASH_NOT_INSTALLED;
logInfo -n "Squashing ${_image} as ${_namespace}/${_repo}:${_tag}"
${DOCKER} save "${_namespace}/${_repo}:${_currentTag}" | sudo docker-squash -t "${_namespace}/${_repo}:${_tag}" | ${DOCKER} load
if isTrue $?; then
logInfoResult SUCCESS "done"
else
logInfoResult FAILURE "failed"
exitWithErrorCode ERROR_REDUCING_IMAGE "${_namespace}/${_repo}:${_currentTag}";
fi
}
# fun: process_file
# api: public
# txt: Processes given file.
# opt: file: The input file.
# opt: output: The output file.
# opt: repoFolder: The repo folder.
# opt: templateFolder: The template folder.
# opt: repo: The image.
# opt: rootImage: The root image.
# opt: namespace: The namespace.
# txt: Returns 0/TRUE if the file is processed correctly; 1/FALSE otherwise.
# use: if process_file "my.template" "my" "my-image-folder" ".templates" "my" "base" "company"; then echo "File processed successfully"; fi
function process_file() {
local _file="${1}";
checkNotEmpty file "${_file}" 1;
local _output="${2}";
local _repoFolder="${3}";
local _templateFolder="${4}";
local _repo="${5}";
local _rootImage="${6}";
local _namespace="${7}";
local _backupHostSshPort="${10:-22}";
local -i _rescode=${FALSE};
local _temp1;
local _temp2;
if arrayContains "${_file}" "${__PROCESSED_FILES[@]}"; then
_rescode=${TRUE};
else
__PROCESSED_FILES+="${_file}";
checkNotEmpty output "${_output}" 2;
checkNotEmpty repoFolder "${_repoFolder}" 3;
checkNotEmpty templateFolder "${_templateFolder}" 4;
checkNotEmpty repo "${_repo}" 5;
checkNotEmpty rootImage "${_rootImage}" 6;
checkNotEmpty namespace "${_namespace}" 7;
local _settingsFile="$(dirname ${_file})/$(basename ${_file} .template).settings";
logTrace -n "Settings file for ${_file}";
if fileExists "${_settingsFile}"; then
logTraceResult SUCCESS "${_settingsFile}";
process_settings_file "${_settingsFile}";
else
logTraceResult FAILURE "${_settingsFile}";
fi
if createTempFile; then
_temp1="${RESULT}";
fi
if createTempFile; then
_temp2="${RESULT}";
fi
if isNotEmpty "${_temp1}" && isNotEmpty "${_temp2}" && \
debugDuration resolve_includes "${_file}" "${_temp1}" "${_repoFolder}" "${_templateFolder}" "${_repo}" "${_rootImage}" "${_namespace}" "${_backupHostSshPort}"; then
logTrace -n "Resolving @include_env in ${_file}";
if debugDuration resolve_include_env "${_temp1}" "${_temp2}" "${_repo}" "${_rootImage}" "${_namespace}" "${_backupHostSshPort}"; then
if debugDuration process_placeholders "${_temp2}" "${_output}" "${_repo}" "${_rootImage}" "${_namespace}" "${_backupHostSshPort}"; then
_rescode=${TRUE};
logTraceResult SUCCESS "done"
else
_rescode=${FALSE};
logTraceResult FAILURE "failed";
fi
else
_rescode=${FALSE};
logTraceResult FAILURE "failed";
fi
else
_rescode=${FALSE};
fi
fi
return ${_rescode}; file repoFolder templateFolder
}
# fun: resolve_included_file
# api: public
# txt: Resolves given included file.
# opt: file: The file name.
# opt: repoFolder: The repository folder.
# opt: templateFolder: The template folder.
# txt: Returns 0/TRUE if the file is found; 1/FALSE otherwise.
# use: if ! resolve_included_file "footer" "my-image-folder" ".templates"; then echo "'footer' not found"; fi
function resolve_included_file() {
local _file="${1}";
checkNotEmpty file "${_file}" 1;
local _repoFolder="${2}";
checkNotEmpty repoFolder "${_repoFolder}" 2;
local _templatesFolder="${3}";
checkNotEmpty templatesFolder "${_templatesFolder}" 3;
local _result;
local _rescode=${FALSE};
local d;
local _oldIFS="${IFS}";
IFS=$' \t\n';
for d in "${_templatesFolder}"; do
IFS="${_oldIFS}";
if [[ -f "${d}/${_file}" ]] \
|| [[ -f "${d}/$(basename ${_file} .template).template" ]]; then
_result="${d}/${_file}";
export RESULT="${_result}";
_rescode=${TRUE};
break;
fi
done
IFS="${_oldIFS}";
if isFalse ${_rescode}; then
if [[ $(eval "echo ${_file}") != "${_file}" ]]; then
resolve_included_file "$(eval "echo ${_file}")" "${_repoFolder}" "${_templatesFolder}";
_rescode=$?;
fi
fi
return ${_rescode};
}
# fun: resolve_includes input output repoFolder templateFolder repo rootImage namespace
# api: public
# txt: Resolves any @include in given file.
# opt: input: The input file.
# opt: output: The output file.
# opt: repoFolder: The repository folder.
# opt: templateFolder: The template folder.
# opt: repo: The image.
# opt: rootImage: The root image.
# opt: namespace: The namespace.
# opt: backupHostSshPort: The backup host's SSH port for this image (optional).
# txt: Returns 0/TRUE if the @include()s are resolved successfully; 1/FALSE otherwise.
# use: resolve_includes "my.template" "my" "my-image-folder" ".templates" "myImage" "myRoot" "example" "latest" "22"
function resolve_includes() {
local _input="${1}";
checkNotEmpty input "${_input}" 1;
local _output="${2}";
checkNotEmpty output "${_output}" 2;
local _repoFolder="${3}";
checkNotEmpty repoFolder "${_repoFolder}" 3;
local _templateFolder="${4}";
checkNotEmpty templateFolder "${_templateFolder}" 4;
local _repo="${5}";
checkNotEmpty repo "${_repo}" 5;
local _rootImage="${6}";
checkNotEmpty rootImage "${_rootImage}" 6;
local _namespace="${7}";
checkNotEmpty namespace "${_namespace}" 7;
local _backupHostSshPort="${8:-22}";
local _rescode;
local _match;
local _includedFile;
local line;
local _folder;
local _files;
logTrace -n "Resolving @include()s in ${_input}";
echo -n '' > "${_output}";
while IFS='' read -r line; do
_match=${FALSE};
_includedFile="";
if [[ "${line#@include(\"}" != "$line" ]] \
&& [[ "${line%\")}" != "$line" ]]; then
_ref="$(echo "$line" | sed 's/@include(\"\(.*\)\")/\1/g')";
if resolve_included_file "${_ref}" "${_repoFolder}" "${_templateFolder}"; then
_includedFile="${RESULT}";
if fileExists "${_includedFile}.template"; then
if process_file "${_includedFile}.template" "${_includedFile}" "${_repoFolder}" "${_templateFolder}" "${_repo}" "${_rootImage}" "${_namespace}" "${_backupHostSshPort}"; then
_match=${TRUE};
else
_match=${FALSE};
logTraceResult FAILURE "failed";
exitWithErrorCode CANNOT_PROCESS_TEMPLATE "${_includedFile}";
fi
elif fileExists "${_includedFile}.settings"; then
if process_settings_file "${_includedFile}.settings"; then
_match=${TRUE};
else
_match=${FALSE};
logTraceResult FAILURE "failed";
exitWithErrorCode CANNOT_PROCESS_TEMPLATE "${_includedFile}.settings";
fi
else
_match=${TRUE};
fi
if [ -d "${_templateFolder}/$(basename ${_includedFile})-files" ]; then
mkdir "${_repoFolder}/$(basename ${_includedFile})-files" 2> /dev/null;
rsync -azI "${PWD}/${_templateFolder#\./}/$(basename ${_includedFile})-files/" "${_repoFolder}/$(basename ${_includedFile})-files/"
_folder="${_repoFolder}/$(basename ${_includedFile})-files";
if folderExists "${_folder}"; then
# shopt -s nullglob dotglob;
_files=($(find "${_folder}" -type f -name '*.template' 2> /dev/null));
# shopt -u nullglob dotglob;
if isGreaterThan ${#_files[@]} 0; then
for p in ${_files[@]}; do
IFS="${_oldIFS}";
process_file "${p}" "$(dirname ${p})/$(basename ${p} .template)" "${_repoFolder}" "${_templateFolder}" "${_repo}" "${_rootImage}" "${_namespace}" "${_backupHostSshPort}";
done
IFS="${_oldIFS}";
fi
fi
fi
elif ! fileExists "${_ref}.template"; then
logTraceResult FAILURE "failed";
exitWithErrorCode TEMPLATE_DOES_NOT_EXIST "${_ref}";
else
_match=${FALSE};
_errorRef="${_ref}";
eval "echo ${_ref}" > /dev/null 2>&1;
if isTrue $?; then
_errorRef="${_input} contains ${_ref} with evaluates to $(eval "echo ${_ref}" 2> /dev/null), and it's not found in any of the expected paths: ${_repoFolder}, ${_templateFolder}";
fi
fi
fi
if isTrue ${_match}; then
cat "${_includedFile}" >> "${_output}";
else
echo "$line" >> "${_output}";
fi
done < "${_input}";
_rescode=$?;
if isEmpty "${_errorRef}" && isTrue ${_rescode}; then
logTraceResult SUCCESS "done";
else
logTraceResult FAILURE "failed";
fi
return ${_rescode};
}
# fun: process_settings_file file
# api: public
# txt: Processes a settings file for a template.
# opt: file: The settings file.
# txt: Returns 0/TRUE if the settings file was processed successfully; 1/FALSE otherwise.
# use: if process_settings_file "my.settings"; then echo "my.settings processed successfully"; fi
function process_settings_file() {
local _file="${1}";
checkNotEmpty file "${_file}" 1;
local -i _rescode=${FALSE};
logInfo -n "Reading ${_file}";
source "${_file}";
_rescode=$?;
if isTrue ${_rescode}; then
logInfoResult SUCCESS "done";
else
logInfoResult FAILURE "failed";
fi
return ${_rescode};
}
# fun: retrieve_standard_environment_variables
# api: public
# txt: Retrieves the standard environment variables.
# txt: Returns 0/TRUE always.
# use: retrieve_standard_environment_variables; echo "vars: ${RESULT}"
function retrieve_standard_environment_variables() {
export RESULT="TAG DATE TIME STACK BASE_IMAGE";
}
# fun: process_placeholders file output repo rootImage namespace backupHostSshPort
# api: public
# txt: Processes placeholders in given file.
# opt: file: The input file.
# opt: output: The output file.
# opt: repo: The image.
# opt: rootImage: The root image.
# opt: namespace: The namespace.
# opt: backupHostSshPort: The backup host's SSH port (optional).
# txt: Returns 0/TRUE if the file was processed successfully; 1/FALSE otherwise.
# use: if process_placeholders "my.template" "my" "myImage" "root" "example" "2222"; then echo "my.template -> my"; fi
function process_placeholders() {
local _file="${1}";
checkNotEmpty file "${_file}" 1;
local _output="${2}";
checkNotEmpty output "${_output}" 2;
local _repo="${3}";
checkNotEmpty repo "${_repo}" 3;
local _rootImage="${4}";
checkNotEmpty rootImage "${_rootImage}" 4;
local _namespace="${5}";
checkNotEmpty namespace "${_namespace}" 5;
local _backupHostSshPort="${6:-22}";
local -i _rescode;
local -i i;
local _oldIFS="${IFS}";
local _variables='';
local _customEnvVars;
local _standardEnvVars;
local _var;
retrieve_standard_environment_variables;
_standardEnvVars="${RESULT}";
retrieveCustomEnvironmentVariables;
_customEnvVars="${RESULT}";
logTrace -n "Resolving placeholders in ${_file}";
IFS="${DWIFS}";
for _var in ${_standardEnvVars} ${_customEnvVars}; do
IFS="${_oldIFS}";
if isNotEmpty "${_var}"; then
if isNotEmpty "${_variables}"; then
_variables="${_variables} ";
fi
_variables="${_variables}$(echo "${_var}" | awk -v dollar="$" -v quote="\"" '{printf("echo %s=\\\"%s%s{%s}%s\\\"", $0, quote, dollar, $0, quote);}' | sh)";
fi;
done;
IFS="${_oldIFS}";
_variables="${_variables} MAINTAINER=\"${AUTHOR} <${AUTHOR_EMAIL}>\" REPO=\"${_repo}\" IMAGE=\"${_repo}\" ROOT_IMAGE=\"${_rootImage}\" NAMESPACE=\"${_namespace}\" BACKUP_HOST_SSH_PORT=\"${_backupHostSshPort}\" DOLLAR=$ ";
replaceVariablesInFile "${_file}" "${_output}" ${_variables};
logTraceResult SUCCESS "done";
}
# fun: resolve_include_env input output image rootImage namespace backupHostSshPort
# api: public
# txt: Resolves any @include_env in given file.
# opt: input: The input file.
# opt: output: The output file.
# opt: image: The image.
# opt: rootImage: The root image.
# opt: namespace: The namespace.
# opt: backupHostSshPort: The backup host SSH port (optional).
# txt: Returns 0/TRUE if the @include_env is resolved successfully; 1/FALSE otherwise.
# use: if resolve_include_env "my.template" "my" "image" "base" "example" 2222; then echo "@include_env resolved"; fi
function resolve_include_env() {
local _input="${1}";
checkNotEmpty input "${_input}" 1;
local _output="${2}";
checkNotEmpty output "${_output}" 2;
local _image="${3}";
checkNotEmpty image "${_image}" 3;
local _rootImage="${4}";
checkNotEmpty rootImate "${_rootImage}" 4;
local _namespace="${5}";
checkNotEmpty namespace "${_namespace}" 5;
local _backupHostSshPort="${6:-22}";
local _includedFile;
local -i _rescode;
local _envVar;
local line;
local -a _envVars=();
local -i i;
local _oldIFS="${IFS}";
retrieveCustomEnvironmentVariables;
local _aux="${RESULT}";
for _envVar in ${_aux}; do
_envVars[${#_envVars[@]}]="${_envVar}";
done
_envVars[${#_envVars[@]}]="IMAGE";
_envVars[${#_envVars[@]}]="DATE";
_envVars[${#_envVars[@]}]="TIME";
_envVars[${#_envVars[@]}]="MAINTAINER";
_envVars[${#_envVars[@]}]="AUTHOR";
_envVars[${#_envVars[@]}]="AUTHOR_EMAIL";
_envVars[${#_envVars[@]}]="STACK";
_envVars[${#_envVars[@]}]="ROOT_IMAGE";
_envVars[${#_envVars[@]}]="BASE_IMAGE";
_envVars[${#_envVars[@]}]="STACK_SUFFIX";
_envVars[${#_envVars[@]}]="NAMESPACE";
_envVars[${#_envVars[@]}]="BACKUP_HOST_SSH_PORT";
logTrace -n "Resolving @include_env in ${_input}";
echo -n '' > "${_output}";
while IFS='' read -r line; do
IFS="${_oldIFS}";
_includedFile="";
if [[ "${line#@include_env}" != "$line" ]]; then
echo "ENV DW_DISABLE_ANSI_COLORS=TRUE \\" >> "${_output}";
echo -n " " >> "${_output}";
for ((i = 0; i < ${#_envVars[@]}; i++)); do \
_envVar="${_envVars[$i]}";
if [ "${_envVar#ENABLE_}" == "${_envVar}" ]; then
if [ $i -ne 0 ]; then
echo >> "${_output}";
echo -n " " >> "${_output}";
fi
echo "${_envVar}" | awk -v dollar="$" -v quote="\"" '{printf("echo -n \"SQ_%s=\\\"%s%s{%s}%s\\\"\"", $0, quote, dollar, $0, quote);}' | sh >> "${_output}"
if isLessThan $i $((${#_envVars[@]} - 1)); then
echo -n " \\" >> "${_output}";
fi
fi
done
echo >> "${_output}";
elif [[ "${line# +}" == "${line}" ]]; then
echo "$line" >> "${_output}";
fi
done < "${_input}";
_rescode=$?;
if isTrue ${_rescode}; then
logTraceResult SUCCESS "done";
else
logTraceResult FAILURE "failed";
fi
return ${_rescode};
}
# fun: update_log_category image
# api: public
# txt: Updates the log category to include the current image.
# opt: image: The image.
# txt: Returns 0/TRUE always.
# use: update_log_category "mysql"
function update_log_category() {
local _image="${1}";
checkNotEmpty image "${_image}" 1;
local _logCategory;
getLogCategory;
_logCategory="${RESULT%/*}/${_image}";
setLogCategory "${_logCategory}";
}
# fun: copy_license_file repo folder
# api: public
# txt: Copies the license file from specified folder to the repository folder.
# opt: repo: The repository.
# opt: folder: The folder where the license file is included.
# txt: Returns 0/TRUE always.
# use: copy_license_file "myImage" ${PWD}
function copy_license_file() {
local _repo="${1}";
checkNotEmpty repo "${_repo}" 1;
local _folder="${2}";
checkNotEmpty folder "${_folder}" 2;
if isEmpty "${LICENSE_FILE}"; then
exitWithErrorCode LICENSE_FILE_IS_MANDATORY;
fi
if fileExists "${_folder}/${LICENSE_FILE}"; then
logDebug -n "Using ${LICENSE_FILE} for ${_repo} image";
cp "${_folder}/${LICENSE_FILE}" "${_repo}/${LICENSE_FILE}";
if isTrue $?; then
logDebugResult SUCCESS "done";
else
logDebugResult FAILURE "failed";
exitWithErrorCode CANNOT_COPY_LICENSE_FILE;
fi
else
exitWithErrorCode LICENSE_FILE_DOES_NOT_EXIST "${_folder}/${LICENSE_FILE}";
fi
}
# fun: copy_copyright_preamble_file repo folder
# api: public
# txt: Copies the copyright-preamble file from specified folder to the repository folder.
# opt: repo: The repository.
# opt: folder: The folder where the copyright preamble file is included.
# txt: Returns 0/TRUE always.
# use: copy_copyright_preamble_file "myImage" ${PWD}
function copy_copyright_preamble_file() {
local _repo="${1}";
checkNotEmpty repo "${_repo}" 1;
local _folder="${2}";
checkNotEmpty folder "${_folder}" 2;
if isEmpty "${COPYRIGHT_PREAMBLE_FILE}"; then
exitWithErrorCode COPYRIGHT_PREAMBLE_FILE_IS_MANDATORY;
fi
if fileExists "${_folder}/${COPYRIGHT_PREAMBLE_FILE}"; then
logDebug -n "Using ${COPYRIGHT_PREAMBLE_FILE} for ${_repo} image";
cp "${_folder}/${COPYRIGHT_PREAMBLE_FILE}" "${_repo}/${COPYRIGHT_PREAMBLE_FILE}";
if isTrue $?; then
logDebugResult SUCCESS "done";
else
logDebugResult FAILURE "failed";
exitWithErrorCode CANNOT_COPY_COPYRIGHT_PREAMBLE_FILE;
fi
else
exitWithErrorCode COPYRIGHT_PREAMBLE_FILE_DOES_NOT_EXIST "${_folder}/${COPYRIGHT_PREAMBLE_FILE}";
fi
}
# fun: retrieve_backup_host_ssh_port repo
# api: public
# txt: Resolves the BACKUP_HOST_SSH_PORT variable.
# opt: repo: The image.
# txt: Returns 0/TRUE always.
# txt: RESULT contains the value of BACKUP_HOST_SSH_PORT variable.
# use: retrieve_backup_host_ssh_port mariadb; export BACKUP_HOST_SSH_PORT="${RESULT}"; fi
function retrieve_backup_host_ssh_port() {
local _repo="${1}";
checkNotEmpty repo "${_repo}" 1;
local _result;
if isNotEmpty "${SSHPORTS_FILE}" \
&& fileExists "${SSHPORTS_FILE}"; then
logDebug -n "Retrieving the ssh port of the backup host for ${_repo}";
_result="$(echo -n ''; (grep -e ${_repo} ${SSHPORTS_FILE} || echo ${_repo} 22) | awk '{print $2;}' | head -n 1)";
if isTrue $?; then
logDebugResult SUCCESS "${_result}";
export RESULT="${_result}";
else
logDebugResult FAILURE "not-found";
fi
else
_result="";
fi
}
# fun: retrieveDockerBuildOpts
# api: public
# txt: Retrieves the options for docker build.
# txt: Returns 0/TRUE always.
# txt: The variable RESULT contains the build options.
# use: retrieveDockerBuildOpts; docker build ${RESULT} .
function retrieveDockerBuildOpts() {
local _result=""; #--net=host ";
if isTrue ${NO_CACHE}; then
_result="--no-cache";
fi
export RESULT="${_result}";
}
# fun: build_repo repo
# api: public
# txt: Builds "${NAMESPACE}/${REPO}:${TAG}" image.
# opt: repo: The repository.
# txt: Returns 0/TRUE always.
# use: build_repo "myImage" "latest" "";
function build_repo() {
local _repo="${1}";
checkNotEmpty repo "${_repo}" 1;
local _canonicalTag="${2}";
local _tag;
local _cmdResult;
local _rootImage;
local _f;
retrieveNamespace;
local _namespace="${RESULT}";
local _buildOpts;
local _oldIFS="${IFS}";
retrieve_backup_host_ssh_port "${_repo}";
local _backupHostSshPort="${RESULT:-22}";
if is_32bit; then
_rootImage="${ROOT_IMAGE_32BIT}:${ROOT_IMAGE_VERSION}";
else
_rootImage="${ROOT_IMAGE_64BIT}:${ROOT_IMAGE_VERSION}";
fi
update_log_category "${_repo}";
defineEnvVar IMAGE MANDATORY "The image to build" "${_repo}";
local _logFile="${PWD}"/"${_repo}"/build.log;
rm -f "${_logFile}";
touch "${_logFile}";
logToFile "${_logFile}";
copy_license_file "${_repo}" "${PWD}";
copy_copyright_preamble_file "${_repo}" "${PWD}";
if isGreaterThan $(ls ${_repo}/*.template | grep -e '\.template$' | grep -v -e 'Dockerfile\.template$' | wc -l) 0; then
IFS="${DWIFS}";
for _f in $(ls ${_repo} | grep -e '\.template$' | grep -v -e 'Dockerfile\.template$'); do
IFS="${_oldIFS}";
logDebug -n "Processing ${_repo}/${_f}";
if process_file "${_repo}/${_f}" "${_repo}/$(basename ${_f} .template)" "${_repo}" "${INCLUDES_FOLDER}" "${_repo}" "${_rootImage}" "${_namespace}" "${_backupHostSshPort}"; then
logDebugResult SUCCESS "done";
else
logDebugResult FAILURE "failed";
exitWithErrorCode CANNOT_PROCESS_TEMPLATE "${_repo}/${_f}";
fi
done
IFS="${_oldIFS}";
fi
_tag="${TAG}";
if reduce_image_enabled; then
_rawTag="${TAG}-raw";
fi
_f="${_repo}/Dockerfile.template";
logDebug -n "Processing ${_f}";
if process_file "${_f}" "${_repo}/$(basename ${_f} .template)" "${_repo}" "${INCLUDES_FOLDER}" "${_repo}" "${_rootImage}" "${_namespace}" "${_backupHostSshPort}"; then
logDebugResult SUCCESS "done";
else
logDebugResult FAILURE "failed";
exitWithErrorCode CANNOT_PROCESS_TEMPLATE "${_f}";
fi
retrieveDockerBuildOpts;
_buildOpts="${RESULT}";
logInfo "docker build ${_buildOpts} -t ${_namespace}/${_repo}:${_tag} --rm=true ${_repo}";
DW.import command;
runCommandLongOutput "${DOCKER} build ${_buildOpts} -t ${_namespace}/${_repo}:${_tag}-b --rm=true ${_repo}";
_cmdResult=$?
logInfo -n "${_namespace}/${_repo}:${_tag}-b";
if isTrue ${_cmdResult}; then
logInfoResult SUCCESS "built"
else
logInfoResult FAILURE "not built"
exitWithErrorCode ERROR_BUILDING_REPOSITORY "${_repo}";
fi
logInfo -n "Adding build log to the resulting image";
if add_file_to_image "${_logFile}" "/${IMAGE}.log" "${_namespace}/${_repo}:${_tag}-b" "${_namespace}/${_repo}:${_tag}"; then
logInfoResult SUCCESS "done";
logInfo -n "Removing intermediate image";
if docker rmi "${_namespace}/${_repo}:${_tag}-b"; then
logInfoResult SUCCESS "done";
else
logInfoResult FAILURE "failed";
fi
else
logInfoResult FAILURE "failed";
fi
if reduce_image_enabled; then
reduce_image_size "${_namespace}" "${_repo}" "${_tag}" "${_canonicalTag}";
fi
}
# fun: add_file_to_image file destPath oldImage newImage
# api: public
# txt: Adds a file to given image, creating a new one.
# opt: file: The file to add.
# opt: destPath: The destination path.
# opt: oldImage: The old image.
# opt: newImage: The new image.
# txt: Returns 0/TRUE if the image could be built; 1/FALSE otherwise.
# use: if add_file_to_image build.log /build.log myImage:old myImage:new; then echo "Image built"; fi
function add_file_to_image() {
local _file="${1}";
checkNotEmpty file "${_file}" 1;
local _destPath="${2}";
checkNotEmpty destPath "${_destPath}" 2;
local _oldImage="${3}";
checkNotEmpty oldImage "${_oldImage}" 3;
local _newImage="${4}";
checkNotEmpty newImage "${_newImage}" 4;
createTempFolder;
local _tempFolder="${RESULT}";
local _dockerfile="${_tempFolder}/Dockerfile";
cp "${_file}" "${_tempFolder}";
local _fileName="$(basename "${_file}")";
logDebug -n "Creating Dockerfile";
cat <<EOF > "${_dockerfile}"
FROM ${_oldImage}
COPY ${_fileName} ${_destPath}
EOF
logDebugResult SUCCESS "done";
logDebug "Creating image ${_newImage}";
pushd "${_tempFolder}";
docker build -t ${_newImage} .;
_rescode=$?;
popd;
logDebug -n "Creating image ${_newImage}";
if isTrue ${_rescode}; then
logDebugResult SUCCESS "done";
else
logDebugResult FAILURE "failed";
fi
return ${_rescode};
}
# fun: add_file_to_image_using_a_temporary_container file destPath oldImage newImage
# api: public
# txt: Adds a file to given image, creating a new one.
# opt: file: The file to add.
# opt: destPath: The destination path.
# opt: oldImage: The old image.
# opt: newImage: The new image.
# txt: Returns 0/TRUE always, but can exit with an error.
# use: add_file_to_image_using_a_temporary_container build.log /build.log myImage:old myImage:new
function add_file_to_image_using_a_temporary_container() {
local _file="${1}";
checkNotEmpty file "${_file}" 1;
local _destPath="${2}";
checkNotEmpty destPath "${_destPath}" 2;
local _oldImage="${3}";
checkNotEmpty oldImage "${_oldImage}" 3;
local _newImage="${4}";
checkNotEmpty newImage "${_newImage}" 4;
logInfo -n "Creating a temporary container for ${_oldImage}";
local _containerId="$(docker create ${_oldImage})";
if isTrue $?; then
logInfoResult SUCCESS "${_containerId}";
else
logInfoResult FAILURE "failed";
exitWithErrorCode CANNOT_CREATE_A_DEAD_CONTAINER "${_oldImage}";
fi
logInfo -n "Copying ${_file} to ${_containerId} (${_oldImage})";
docker cp "${_file}" ${_containerId}:"${_destPath}";
if isTrue $?; then
logInfoResult SUCCESS "${_containerId}";
else
logInfoResult FAILURE "failed";
exitWithErrorCode CANNOT_COPY_FILE_TO_CONTAINER "${_oldImage}";
fi
logInfo -n "Committing ${_containerId}";
docker commit ${_containerId} "${_newImage}";
if isTrue $?; then
logInfoResult SUCCESS "${_newImage}";
else
logInfoResult FAILURE "failed";
exitWithErrorCode CANNOT_COMMIT_CONTAINER;
fi
logInfo -n "Deleting temporary container";
docker rm ${_containerId};
if isTrue $?; then
logInfoResult SUCCESS "${_containerId}";
else
logInfoResult FAILURE "failed";
exitWithErrorCode CANNOT_DELETE_CONTAINER "${_containerId}";
fi
}
# fun: overwrite_latest_tag namespace repo tag
# api: public
# txt: Overwrites the "latest" tag if requested.
# opt: namespace: The namespace.
# opt: repo: The repository.
# opt: tag: The tag.
# txt: Conditionally overwrites the "latest" tag.
# txt: Returns 0/TRUE always.
# use: overwrite_latest_tag "mycompany" "myimage" "0.11";
function overwrite_latest_tag() {
local _namespace="${1}";
checkNotEmpty namespace "${_namespace}" 1;
local _repo="${2}";
checkNotEmpty repo "${_repo}" 2;
local _tag="${3}";
checkNotEmpty tag "${_tag}" 3;
if overwrite_latest_enabled; then
logInfo -n "Tagging ${_namespace}/${_repo}:${_tag} as ${_namespace}/${_repo}:latest"
docker tag "${_namespace}/${_repo}:${_tag}" "${_namespace}/${_repo}:latest"
if isTrue $?; then
logInfoResult SUCCESS "${_namespace}/${_repo}:latest";
else
logInfoResult FAILURE "failed"
exitWithErrorCode ERROR_TAGGING_IMAGE "${_repo}";
fi
fi
}
# fun: annotate_successful_build repo tag
# api: public
# txt: Annotates a successful build.
# opt: repo: The repository.
# opt: tag: The tag.
# txt: Returns 0/TRUE if the build could be annotated successfully; 1/FALSE otherwise.
# use: if annotate_successful_build "myimage" "0.11"; then echo "Build annotated"; fi
function annotate_successful_build() {
local _repo="${1}";
checkNotEmpty repo "${_repo}" 1;
local _tag="${2}";
checkNotEmpty tag "${_tag}" 2;
touch "${_repo}"/.builds;
local -i _rescode=$?;
if isTrue ${_rescode}; then
echo "${_tag}: $(date)" >> "${_repo}"/.builds;
fi
return ${_rescode};
}
# fun: retrieveRemoteTag namespace repo tag
# api: public
# txt: Retrieves the remote tag.
# opt: namespace: The namespace.
# opt: repo: The repository name.
# opt: tag: The tag.
# txt: Returns 0/TRUE always.
# txt: RESULT contains the remote tag.
# use: retrieveRemoteTag "mycompany" "myImage" "latest"; echo "Remote tag: ${RESULT}";
function retrieveRemoteTag() {
local _namespace="${1}";
checkNotEmpty namespace "${_namespace}" 1;
local _repo="${2}";
checkNotEmpty repo "${_repo}" 2;
local _tag="${3}";
checkNotEmpty tag "${_tag}" 3;
local _result;
if areEqual "${REGISTRY}" "cloud.docker.com"; then
_result="${REGISTRY_NAMESPACE}/${_namespace}/${_repo}:${_tag}";
else
_result="${REGISTRY}/${_namespace}/${_repo}:${_tag}";
fi
export RESULT="${_result}";
}
# fun: registry_tag repo tag
# api: public
# txt: Tags the image anticipating it will be pushed to a Docker registry later.
# opt: repo: The repository.