-
Notifications
You must be signed in to change notification settings - Fork 27
/
docker_utils_functions
executable file
·1022 lines (971 loc) · 31.6 KB
/
docker_utils_functions
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
# base script
# set -euo pipefail
# TODO: Check the need for the preset
ARCH="$(uname -m)"
MANIFEST=0
TYPE=''
OPTIND=1
#
# Utility function to returns OS Release Name
#
# Usage:
# get_os_release image-id
#
# Example:
# get_os_release almalinux:9
# Return `AlmaLinux release 9.0 (Emerald Puma)`
#
get_os_release() {
local -r image=${1}
# issue docker pull, redirect messages to null
docker pull ${image} > /dev/null
local rel=$(docker run --rm ${image} cat /etc/system-release)
echo $rel
}
#
# Utility function to returns OS Release Version
#
# Usage:
# get_os_release image-id
#
# Example:
# get_os_release almalinux:9
# Return `9.0`
#
get_os_release_version() {
local -r image=${1}
# issue docker pull, redirect messages to null
docker pull ${image} > /dev/null
local rel=$(docker run --rm ${image} awk '{ print $3 }' /etc/system-release )
# local rel=$(docker run --rm ${image} awk -F= '$1=="VERSION_ID" { print $2 ;}' /etc/os-release | tr -d '"')
echo $rel
}
#
# Utility function to returns docker image hash value
# When given image is not available, pulled from remote registory
#
# Usage:
# get_os_release image-id
#
# Example:
# get_image_hash almalinux:9
# Return `sha256:d5c6ee0317ab996cc7684a6dcbffb061bdaec76e46e9f301be0bd3a41bed3493`
#
get_image_hash() {
local -r image=${1}
# issue docker pull, redirect messages to null
docker pull ${image} > /dev/null
local hash=$(docker inspect ${image} | jq '.[] .Id' | tr -d '"')
echo $hash
}
#
# Utility function to provide arch names for registory path.
# It is usefull to build container registry tagName string dynamically
#
# Usage:
# get_registry_arch machinArch
#
# Example:
# get_registry_arch x86_64
# Returns `amd64`
#
get_registry_arch() {
local -r arch="${1}"
case "${arch}" in
i386|amd64|arm64v8|ppc64le|s390x)
echo "${arch}";;
x86_64)
echo 'amd64';;
aarch64|arm64|arm64/v8)
echo 'arm64v8';;
i386|i486|i586|i686|x86)
echo 'i386';;
*)
echo "Error: unsupported architecture ${arch}" 1>&2
exit 2
;;
esac
}
#
# Utility function to provide platform arch name(s) for docker build step.
# It is usefull to build command dynamically in docker build, input parameter
#
# Usage:
# get_platform_arch machinArch
#
# Example:
# get_platform_arch aarch64
# Returns `arm64/v8`
#
get_platform_arch() {
local -r arch="${1}"
case "${arch}" in
386|amd64|arm64|arm64/v8|ppc64le|s390x)
echo "${arch}";;
x86_64)
echo 'amd64';;
aarch64)
echo 'arm64/v8';;
i386|i486|i586|i686|x86)
echo '386';;
*)
echo "Error: unsupported architecture ${arch}" 1>&2
exit 2
;;
esac
}
#
# Utility function to sanitize build arch parameter
# Used to populate arch list for processing single or all
#
# TODO: this function my need work to use/support for other
# variants of RHEL
#
# Usage:
# get_build_arch arch
#
# Example:
# get_build_arch all
# Returns `x86_64 aarch64 ppc64le s390x`
#
get_build_arch() {
local -r arch="${2}"
local -r version=${1}
case "${arch}" in
x86_64|aarch64|ppc64le|s390x)
echo "${arch}";;
all)
case "${version}" in
8)
echo 'x86_64 aarch64 ppc64le s390x';;
9)
echo 'x86_64 aarch64 ppc64le s390x';;
esac
;;
*)
echo "Error: unsupported architecture ${arch}" 1>&2
exit 2
;;
esac
}
#
# Utility function to sanitize build image type parameter
# Used to populate image type list for processing single or all
#
# Usage:
# get_build_types imageType
#
# Example:
# get_build_types "micro"
# returns "micro"
# get_build_types "all"
# returns "default minimal micro base init"
#
get_build_types() {
local -r type="${1}"
case "${type}" in
default|minimal|micro|base|init)
echo "${type}";;
all)
echo 'default minimal micro base init';;
official)
echo 'default minimal';;
ubi)
echo 'micro base init';;
*)
echo "Error: unsupported image type ${type}" 1>&2
exit 2
;;
esac
}
#
# Utility function to provide generalized machine arch
# used for output filenames based on $(uname -m)
#
# Usage:
# get_machine_arch imageType
#
# Example:
# get_machine_arch "amd64"
# returns "x86_64"
# get_machine_arch "arm64/v8"
# returns "aarch64"
#
get_machine_arch() {
local -r arch="${1}"
case "${arch}" in
x86_64|aarch64|ppc64le|s390x)
echo "${arch}";;
amd64)
echo 'x86_64';;
arm64/v8|arm64|arm64v8)
echo 'aarch64';;
386|i386|i486|i586|i686|x86)
echo 'i686';;
*)
echo "Error: unsupported architecture ${arch}" 1>&2
exit 2
;;
esac
}
#
# Checks Almalinux versiona build arch is supported
#
# Usage:
# check_build_support_arch "osVersion" "arch"
#
# Example
# check_build_support_arch 8 aarch64
#
check_build_support_arch() {
local -r version="${1}"
local -r arch="${2}"
case "${version}" in
8)
case "${arch}" in
x86_64|aarch64|ppc64le|s390x|all)
echo "supported";;
*)
echo "Error: unsupported architecture ${arch} for version ${version}" 1>&2
exit 2
;;
esac
;;
9)
case "${arch}" in
x86_64|aarch64|ppc64le|s390x|all)
echo "supported";;
*)
echo "Error: unsupported architecture ${arch} for version ${version}" 1>&2
exit 2
;;
esac
;;
*)
echo "Error: unsupported version ${arch}" 1>&2
exit 2
;;
esac
}
#
# Function for internal reference use only.
# Build for singleArch, singleType of container image from public repo
#
# TODO: if build and push needed, buildx can be used, need more work
#
# build_single_image_using_repos "OsVersion" "arch" "imageType" "dockerTag" "dockerBase"
#
build_single_image_using_repos(){
local -r al_version="${1}"
local -r arch_platform=$(get_platform_arch "${2}")
local -r type="${3}"
local -r tag="${4}"
local -r base="${5}"
# local -r build_arg="--squash"
echo "Building for arch: $arch_platform"
docker build "--platform=linux/${arch_platform}" --build-arg SYSBASE="${base}" -t "${tag}" -f "dockerfiles/al${al_version}/Dockerfile.${type}" .
}
#
# Function to build one container image from rootfs files,
# usually right before publishing the images. This function defined
# internal use only
#
# Usage:
# build_single_image_using_rootfs "osVersion" "arch" "imageType" "dockerTags" "skipDockerPush"
#
# Example:
# build_single_image_using_rootfs "8" "all" "micro" "srbala/8-micro" "false"
#
build_single_image_using_rootfs(){
local -r al_version="${1}"
local -r arch_platform=$(get_platform_arch "${2}")
local -r type="${3}"
local -r tags="${4}"
local -r skip="${5}"
local -r arch_machine=$(get_machine_arch "${2}")
local tag_list=""
## assemble tags one string to pass
echo "Tags input: $tags"
for tag in $tags
do
tag_list="${tag_list} -t $tag"
done
echo "Formated tags: $tag_list"
## TODO: replace buildx ? build and push together
docker build "--platform=linux/${arch_platform}" ${tag_list} -f "Dockerfile-${arch_machine}-${type}" .
# Circle thru to publish
if [[ "$skip" == "false" ]]; then
for tag in $tags
do
docker push "${tag}"
done
fi
}
#
# Extract rootfs file from a container image (image_tag), creates rootfs `rootfs_outfile_prefix.tar.xz`
#
# gen_rootfs image_tag rootfs_outfile_prefix
#
gen_rootfs() {
local -r image="${1}"
local -r tname="${2}"
local rootfs_tmp="rootfs_tmp_${2}"
tcnt=$(docker inspect $image | jq '.[] | .RootFS.Layers | length')
echo "Found $tcnt layer(s) in image '$image'."
if [ $tcnt -ne 1 ]; then
echo "Only single layer image is supported at this time. Use '--squash' option to create single layer image."
exit
fi
echo "Setting up temp work dir ..."
mkdir -p $rootfs_tmp && cd $rootfs_tmp
echo "Saving docker/container image ..."
docker save $image -o rootfstmp.tar
trepo=$(tar -tf rootfstmp.tar | grep layer)
tcnt=$(echo $trepo | tr ' ' '\n' | grep layer | wc -l | xargs)
# echo "Found $tcnt layer(s) in image '$image'."
if [ $tcnt -eq 1 ]; then
echo "Extracting rootfs $trepo ..."
tar -xf rootfstmp.tar $trepo
mv $trepo $tname.tar
echo "Compressing rootfs ..."
xz $tname.tar
mv $tname* ../
cd ..
F1=$(ls *.tar* | grep $tname)
TMSG="Task complete. Output rootfs located at $PWD/$F1"
else
echo "Only single layer image is supported at this time. Use '--squash' option to create single layer image."
cd ..
TMSG="Error Exit, task complete."
fi
echo "Perform cleanup ..."
rm -rf $rootfs_tmp
echo $TMSG
}
#
# pull_docker_official "9" "0" "minimal" "20220623"
#
# pull_docker_official for "default" or "minimal", do-not rebuild for publish
#
pull_docker_official() {
local -r al_version="${1}"
local -r rel_version="${2}"
local -r type="${3}"
local -r repo_prefix="${4}"
local -r date_suffix="${5}"
local tag="$al_version.$rel_version"
if [[ $type == "minimal" ]]; then
tag="$tag-$type"
fi
# pull with full release tag from docker
local tag1=$tag
tag="$tag-$date_suffix"
echo "docker pull docker.io/amd64/almalinux:$tag1"
echo "docker tag docker.io/amd64/almalinux:$tag1 $repo_prefix/amd64:$tag"
docker pull docker.io/amd64/almalinux:$tag1
docker tag docker.io/amd64/almalinux:$tag1 "$repo_prefix/amd64:$tag"
docker push "$repo_prefix/amd64:$tag"
docker pull docker.io/arm64v8/almalinux:$tag1
docker tag docker.io/arm64v8/almalinux:$tag1 "$repo_prefix/arm64v8:$tag"
docker push "$repo_prefix/arm64v8:$tag"
docker pull docker.io/ppc64le/almalinux:$tag1
docker tag docker.io/ppc64le/almalinux:$tag1 "$repo_prefix/ppc64le:$tag"
docker push "$repo_prefix/ppc64le:$tag"
# if [[ $al_version == "9" ]]; then
docker pull docker.io/s390x/almalinux:$tag1
docker tag docker.io/s390x/almalinux:$tag1 "$repo_prefix/s390x:$tag"
docker push "$repo_prefix/s390x:$tag"
# fi
}
# build_push_manifest 9 0 micro 20220623
build_push_manifest() {
local -r al_version="${1}"
local -r rel_version="${2}"
local -r input_type="${3}"
local -r repos="${4}"
local -r date_suffix="${5}"
local -r tags="latest ${1} ${1}.${2} ${1}.${2}-${5}"
local types=$(get_build_types ${input_type})
for repo_prefix in $repos;
do
for type in $types;
do
if [[ $repo_prefix == *"docker"* && $type == "default" ]]; then
echo "No place holder for default in user space"
else
if [[ $type == "default" || $type == "minimal" ]]; then
pull_docker_official "${1}" "${2}" "${type}" "${repo_prefix}" "${5}"
else
build_images "${1}" "${2}" "all" "${type}" "${repo_prefix}" "${5}" "rootfs" "false"
fi
for tag in $tags
do
local part1="$repo_prefix"
local part2=""
local stype="-"
if [[ $type == "default" ]]; then
part1="$part1/almalinux"
stype=""
else
part1="$part1/$al_version-$type"
stype="$stype$type"
fi
part1="$part1:$tag"
local tag_suffix="${al_version}.${rel_version}${stype}-${date_suffix}"
part2="${part2} --amend ${repo_prefix}/amd64:${tag_suffix}"
part2="${part2} --amend ${repo_prefix}/arm64v8:${tag_suffix}"
part2="${part2} --amend ${repo_prefix}/ppc64le:${tag_suffix}"
#if [ "${al_version}" == '9' ]; then
part2="${part2} --amend ${repo_prefix}/s390x:${tag_suffix}"
#fi
# echo "Part1: ${part1}"
# echo "Part2: ${part2}"
# rm returns error when repo/tag not found
# docker manifest rm $part1
docker manifest create ${part1} ${part2}
docker manifest push "${part1}"
done
fi
done
done
}
function formatTags() {
local -r al_verison="${1}"
local -r rel_version="${2}"
local -r repos="${3}"
local -r arch="${4}"
local -r separator="${5}"
local build_tag=""
for repo_prefix in $repos
do
build_tag=${build_tag}${repo_prefix}'/'$(get_registry_arch ${arch})$separator${al_version}.${rel_version}
if [ "$type" == "default" ]; then
build_tag="${build_tag}-${tag_date} "
else
build_tag="${build_tag}-${type}-${tag_date} "
fi
done
echo $build_tag
}
#
# Main function which uses `docker build` option to build images
# and extract rootfs files from it. Based on input params, this can
# handle multiple arch and multiple image types in loop
#
#
#
build_images () {
# 8 or 9 and release version
local -r al_version="${1}"
local -r rel_version="${2}"
# input arch env,
local -r input_arch="${3}"
# input type env
local -r input_type="${4}"
# repo_prefix="srbala"
local -r repos="${5}"
local repo_sep=":"
# tag_date=$(date +%Y%m%d)
local -r tag_date="${6}"
local -r from="${7}"
local -r skip="${8:=false}"
local arch_list=$(get_build_arch ${al_version} ${input_arch})
local type_list=$(get_build_types ${input_type})
echo "************************************************"
echo "* *"
echo "* JOB INPUT VALUES *"
echo "* *"
echo "************************************************"
echo "Input version : ${al_version}.${rel_version}"
echo "Input Arch : ${input_arch}"
echo "Input type : ${input_type}"
echo "repos : ${repos}"
echo "date_tag : ${tag_date}"
echo "Input source : ${from}"
echo "Computed arch list: ${arch_list}"
echo "Computed type list: ${type_list}"
echo "************************************************"
if [[ -z "$type_list" ]]; then
echo "Validating input type failed ... ${input_type}"
exit 2
fi
# Temp hack to cleanup manifest
if [[ "$from" == "rootfs" ]]; then
rm -rf ~/.docker/manifests
fi
status=$(check_build_support_arch "${1}" "${3}")
echo "Validating input values ... ${status}"
local repo_prefix="docker.io/srbala"
if [[ $status == 'supported' && ! -z "$type_list" ]]; then
echo "Ready for processing ..."
for type in ${type_list};
do
echo ""
echo "************************************************"
echo " Building container image type: $type "
echo "************************************************"
echo ""
for arch in ${arch_list};
do
echo ""
echo "************************************************"
echo " Building container image type $type, arch $arch "
echo "************************************************"
echo ""
tags1=$(formatTags "${al_version}" "${rel_version}" "${repos}" "${arch}" "${repo_sep}")
build_tag=""
for atag in $tags1
do
build_tag="${atag}"
done
###build_tag='almalinux/'$arch$repo_sep'8-'$type'-20220608';
#build_tag=${repo_prefix}'/'$(get_registry_arch ${arch})$repo_sep${al_version}.${rel_version}
#if [ "$type" == "default" ]; then
# build_tag=$build_tag'-'$tag_date
#else
# build_tag=$build_tag'-'$type'-'$tag_date
#fi
echo "Computed build tag: ${build_tag} from '${tags1}'"
if [[ "$from" == "repos" ]]; then
tag=""
echo "build_single_image_using_repos ${al_version} ${arch} $type $build_tag ${al_sysbase}"
build_single_image_using_repos ${al_version} ${arch} $type $build_tag ${al_sysbase}
file_prefix='almalinux-'${al_version}'-docker-'$(get_machine_arch ${arch})'-'$type
gen_rootfs $build_tag $file_prefix
#docker push $build_tag
fi
if [[ "$from" == "rootfs" ]]; then
build_single_image_using_rootfs "${al_version}" "${arch}" "$type" "$tags1" "$skip"
fi
done
done
else
echo "Input value error"
fi
}
#
# Utility returns most recent commit of a given file
# used in `library/almalinux`, back reference to source repo
#
# get the most recent commit which modified any of "$@"
#
# Usage:
# fileCommit "lookBackFilePathAndName"
#
# Example:
# fileCommit "library/almalinux"
#
fileCommit() {
git log -1 --format='format:%H' HEAD -- "$@"
}
#
# Utility function to get git hash using local repo
#
# git rev-parse development
# git rev-parse origin/master
# git rev-parse master~3
# git rev-parse HEAD@{2.days.ago}
#
# Get head commit of given git-branch
#
gitBranchLastCommit() {
local -r branch="$1"
git rev-parse $branch
}
#
# GitHub API call to specific branch commit to get json details
# Returns JSON data for further processing
#
# Usage:
# jsonLastCommit "branchName"
#
# Example:
# jsonLastCommit "al8-20220901-amd64"
#
jsonLastCommit() {
local -r branch="$1"
git_url="https://api.github.com/repos/k2s-ossc/docker-images/commits/$branch"
# TODO fix run_env
if [ "${run_env}" == "prd" ]; then
git_url="https://api.github.com/repos/almalinux/docker-images/commits/$branch"
fi
curl -s $git_url
}
#
# List all branch eligible to publish from DEVL, bash version
# Groovy version this function is used for pipeline choice options
#
# curl -s https://api.github.com/repos/almalinuxautobot/docker-images/branches | jq '.[] .name' | grep al8 | tr -d '"' | awk -v inp="al8-20220901" ' $1 > inp { printf $1"\n" }'
#
# Usage:
# getDevlLibBranchlist "OsVersion"
#
# Example:
# getDevlLibBranchlist "8"
# Returns:
# al8-20220901
# al8-20220901-015007
# al8-20220903-015007
# al8-20220912-002721
# al8-20220913-041422
#
getDevlLibBranchlist() {
rel="${1}"
prd=$(getProdLibBranchPrefix $rel)
curl -s https://api.github.com/repos/almalinuxautobot/docker-images/branches | jq '.[] .name' | grep al${rel} | tr -d '"' | grep -v template | awk -v inp="${prd}" ' $1 > inp { printf $1"\n" }'
# echo $list
}
#
# Utility function to get production library prefix from github.com
#
# Usage:
# getProdLibBranchPrefix "OsVersion"
#
# Example:
# getProdLibBranchPrefix "9"
# Returns "al9-20220901"
#
getProdLibBranchPrefix() {
local -r ver="${1}"
ownerPath="${2:-docker-library/official-images/master}"
url="https://raw.githubusercontent.com/${ownerPath}/library/almalinux"
ref_branch=$(curl -s $url | grep GitFetch | grep al$1 | sort -ru | awk 'FNR < 2 { print $2 } ' | sed 's/-s390x//g;s=refs/heads/==g')
echo $ref_branch
}
#
# Utility function to get git branch last commit full-hash
# This function called to generate library file dynamically
#
# Usage:
# gitBranchLastCommitHash "branchName"
#
# Example:
# gitBranchLastCommitHash "al8-20220901-amd64"
# Returns "bedad373efe5d04b08772808df80f3b68bc3267c"
#
gitBranchLastCommitHash() {
local -r branch="$1"
hash=$(git ls-remote -h https://github.com/AlmaLinux/docker-images | grep "${branch}" | awk '{ print $1 }')
# hash=$(echo $json | jq '.sha' | xargs)
echo $hash
}
#
# Utility function to check existance of production branch and
# prepare new set of branch(s), on new sources. Function fails
# when try to create new branch with existing name, no replace
#
# This function may be called twice, once for validate another
# to perform action
#
# Usage:
# validateOrPrepareBranches "OsVersion" "ReleaseDate" "DevlBranch" "paramOption" "trueOrFalse"
#
# Example:
# validateOrPrepareBranches "8" "20220903" "al8-20220903-015007" "Get Branches" "false"
# - New branch, success
# validateOrPrepareBranches "8" "20220901" "al8-20220901" "No Changes" "true"
# - Use prod branch, success
# validateOrPrepareBranches "8" "20220901" "al8-20220901-015007" "Get Branches" "false"
# - since prod already exits, fails
#
validateOrPrepareBranches() {
echo "In validateOrPrepareBranches ..."
local -r ver="${1}"
local -r releaseDate="${2}"
local -r sourceBranch="${3}"
paramOption="${4:-No Changes}"
validateOnly="${5:-true}"
createRepoSkipNoFail="${6:-false}"
gitusr="${7:-skip-push}"
gitpwd="${8:-skip-push}"
failOnRepoExist="true"
createRepos="false"
repoUrlProd="https://github.com/AlmaLinux/docker-images"
repoUrlDevl="https://github.com/almalinuxautobot/docker-images"
repoPrefix="al${ver}-${releaseDate}"
# needed echo to capture value is zero
repoCount=$(echo $(git ls-remote -h ${repoUrlProd} | grep ${repoPrefix} | wc -l | xargs))
echo $repoCount
echo "Repo Prefix: ${repoPrefix}, found ${repoCount} branches in ${repoUrlProd}"
echo "Source branch: ${sourceBranch}"
if [[ ${repoCount} > 0 ]]; then
echo "Production branches already exits, possible production branch. Perform additional check ..."
if [[ "${sourceBranch}" == "${repoPrefix}" ]]; then
echo "Input source branch matches repo prefix (No Changes), Using existing branches to continue ..."
failOnRepoExist="false"
createRepos="false"
else
echo "Input source branch does not match ${repoPrefix}, but production branches already exits ..."
failOnRepoExist="true"
createRepos="false"
fi
else
echo "New production branches using prefix ${repoPrefix} will be created based on ${sourceBranch} (Get Branches) ..."
failOnRepoExist="false"
createRepos="true"
fi
if [[ "${failOnRepoExist}" == 'true' ]]; then
if [[ "${createRepoSkipNoFail}" == 'true' ]]; then
echo "failOnRepoExist: ${failOnRepoExist} and, createRepoSkipNoFail: ${createRepoSkipNoFail} returning ..."
return 0
fi
echo "Exit cond: failOnRepoExist, check messages above"
exit 12
fi
if [[ "${paramOption}" == 'No Changes' && "${createRepos}" == 'true' ]]; then
echo "Exit cond: paramOption, check messages above"
exit 12
fi
if [[ "${validateOnly}" == 'true' ]]; then
echo "Valiation complete, returning ..."
return 0
fi
if [[ "${createRepos}" == 'true' ]]; then
echo "Setting up new branches ..."
else
echo "Exit cond: createRepos is false, check messages above"
exit 12
fi
# mkdir -p work
rm -rf "work/source${ver}"
git clone --single-branch --branch=$sourceBranch $repoUrlDevl "work/source${ver}"
cd "work/source${ver}"
git remote add alma "${repoUrlProd}"
git checkout -b "${repoPrefix}"
# git push alma "${repoPrefix}"
if [[ "${gitusr}" != "skip-push" ]]; then
git push https://${gitusr}:${gitpwd}@github.com/AlmaLinux/docker-images.git "${repoPrefix}"
fi
ls -al
git branch
docker build --no-cache -t "al${ver}-publish" -f Dockerfile-x86_64-default .
#
arch_list="x86_64 aarch64 ppc64le s390x"
for march in $arch_list;
do
reg_arch=$(get_registry_arch ${march})
echo "Creating orphan branch ${repoPrefix}-$reg_arch"
git checkout --orphan "${repoPrefix}-$reg_arch"
for arch in $arch_list;
do
if [ $march != $arch ]; then
# echo "Deleteing ... git rm -f *${arch}* files"
echo "Removing other arch files"
git rm -f "*${arch}*"
fi
done
git status
git commit -m "Adding RootFS files"
sleep 3
echo "git push to prod url"
#git push alma "${repoPrefix}-$reg_arch"
if [[ "${gitusr}" != "skip-push" ]]; then
git push https://${gitusr}:${gitpwd}@github.com/AlmaLinux/docker-images.git "${repoPrefix}-${reg_arch}"
fi
sleep 3
git checkout $sourceBranch
sleep 2
done
git branch
cd ../..
# rm -rf "work/source${ver}"
echo "done"
}
#
# Generate AlmaLinux 8 related tags
#
# Usage:
# almaLibrary8Tags tagDate (YYYYMMDD)
#
# Example:
# almaLibrary8Tags "20220901"
#
almaLibrary8Tags() {
local -r tagdate="$1"
echo
cat <<-EOE
Tags: 8, 8.9, 8.9-$tagdate
GitFetch: refs/heads/al8-$tagdate-amd64
GitCommit: $(gitBranchLastCommitHash "al8-$tagdate-amd64")
amd64-File: Dockerfile-x86_64-default
arm64v8-GitFetch: refs/heads/al8-$tagdate-arm64v8
arm64v8-GitCommit: $(gitBranchLastCommitHash "al8-$tagdate-arm64v8")
arm64v8-File: Dockerfile-aarch64-default
ppc64le-GitFetch: refs/heads/al8-$tagdate-ppc64le
ppc64le-GitCommit: $(gitBranchLastCommitHash "al8-$tagdate-ppc64le")
ppc64le-File: Dockerfile-ppc64le-default
s390x-GitFetch: refs/heads/al8-$tagdate-s390x
s390x-GitCommit: $(gitBranchLastCommitHash "al8-$tagdate-s390x")
s390x-File: Dockerfile-s390x-default
Architectures: amd64, arm64v8, ppc64le, s390x
Tags: 8-minimal, 8.9-minimal, 8.9-minimal-$tagdate
GitFetch: refs/heads/al8-$tagdate-amd64
GitCommit: $(gitBranchLastCommitHash "al8-$tagdate-amd64")
amd64-File: Dockerfile-x86_64-minimal
arm64v8-GitFetch: refs/heads/al8-$tagdate-arm64v8
arm64v8-GitCommit: $(gitBranchLastCommitHash "al8-$tagdate-arm64v8")
arm64v8-File: Dockerfile-aarch64-minimal
ppc64le-GitFetch: refs/heads/al8-$tagdate-ppc64le
ppc64le-GitCommit: $(gitBranchLastCommitHash "al8-$tagdate-ppc64le")
ppc64le-File: Dockerfile-ppc64le-minimal
s390x-GitFetch: refs/heads/al8-$tagdate-s390x
s390x-GitCommit: $(gitBranchLastCommitHash "al8-$tagdate-s390x")
s390x-File: Dockerfile-s390x-minimal
Architectures: amd64, arm64v8, ppc64le, s390x
EOE
}
#
# Generate AlmaLinux 9 related tags
#
# Usage:
# almaLibrary9Tags tagDate (YYYYMMDD)
#
# Example:
# almaLibrary9Tags "20220901"
#
almaLibrary9Tags() {
local -r tagdate="$1"
echo
cat <<-EOE
Tags: latest, 9, 9.3, 9.3-$tagdate
GitFetch: refs/heads/al9-$tagdate-amd64
GitCommit: $(gitBranchLastCommitHash "al9-$tagdate-amd64")
amd64-File: Dockerfile-x86_64-default
arm64v8-GitFetch: refs/heads/al9-$tagdate-arm64v8
arm64v8-GitCommit: $(gitBranchLastCommitHash "al9-$tagdate-arm64v8")
arm64v8-File: Dockerfile-aarch64-default
ppc64le-GitFetch: refs/heads/al9-$tagdate-ppc64le
ppc64le-GitCommit: $(gitBranchLastCommitHash "al9-$tagdate-ppc64le")
ppc64le-File: Dockerfile-ppc64le-default
s390x-GitFetch: refs/heads/al9-$tagdate-s390x
s390x-GitCommit: $(gitBranchLastCommitHash "al9-$tagdate-s390x")
s390x-File: Dockerfile-s390x-default
Architectures: amd64, arm64v8, ppc64le, s390x
Tags: minimal, 9-minimal, 9.3-minimal, 9.3-minimal-$tagdate
GitFetch: refs/heads/al9-$tagdate-amd64
GitCommit: $(gitBranchLastCommitHash "al9-$tagdate-amd64")
amd64-File: Dockerfile-x86_64-minimal
arm64v8-GitFetch: refs/heads/al9-$tagdate-arm64v8
arm64v8-GitCommit: $(gitBranchLastCommitHash "al9-$tagdate-arm64v8")
arm64v8-File: Dockerfile-aarch64-minimal
ppc64le-GitFetch: refs/heads/al9-$tagdate-ppc64le
ppc64le-GitCommit: $(gitBranchLastCommitHash "al9-$tagdate-ppc64le")
ppc64le-File: Dockerfile-ppc64le-minimal
s390x-GitFetch: refs/heads/al9-$tagdate-s390x
s390x-GitCommit: $(gitBranchLastCommitHash "al9-$tagdate-s390x")
s390x-File: Dockerfile-s390x-minimal
Architectures: amd64, arm64v8, ppc64le, s390x
EOE
}
#
# Almalinux library Header block with static block of history versions
#
# Usage:
# almaLibraryHeader
#
function almaLibraryHeader() {
cat <<-EOH
# This file is generated using https://github.com/almalinux/docker-images/blob/$(fileCommit "$self")/$self
Maintainers: The AlmaLinux OS Foundation <[email protected]> (@AlmaLinux)
GitRepo: https://github.com/AlmaLinux/docker-images.git
EOH
}
#
# Function to create environment file to process based input params
#
# Usage:
# almaEnvBuilder "fileName" "MajorVersion" "MinorVersion" "archType" "imageType" "runEnv" "runDate"
#
# Example:
# almaEnvBuilder "env.al9" "9" "0" "all" "all" "prd" "20220901"
#
function almaEnvBuilder() {
local -r os="$2"
local -r rel="$3"
arch="${4:-all}"
type="${5:-all}"
run_env="${6:-devl}"
run_date="${7:-today}"
date_suffix="$(date +'%Y%m%d')"
if [[ "${run_date}" != "today" ]]; then
date_suffix="${run_date}"
fi
branch="al${os}-$(date +'%Y%m%d-%H%M%S')"
repo_prefix="docker.io/srbala quay.io/almalinuxautobot"
repo="quay.io/almalinuxautobot"
gitorg="almalinuxautobot"
if [ "${run_env}" == "prd" ]; then
repo_prefix="docker.io/almalinux quay.io/almalinuxorg"
repo="quay.io/almalinuxorg"
gitorg="AlmaLinux"
fi
# sysbase="srbala/bootstrap:8"
sysbase="almalinux:${os}"
cat > "$1" <<-EOH
# major version 8 or 9
al_os_version="$os"
# minor version, 0 thru 9
al_rel_version="$rel"
# build_arch: all OR x86_64 aarch64 ppc64le s390x
al_arch="$arch"
# build_types: all OR ubi OR default minimal micro base init
al_types="$type"
al_sysbase="$sysbase"
# single or multipe
# 'quay.io/almalinuxautobot' 'docker.io/srbala quay.io/almalinuxautobot'
al_repo_prefix="$repo_prefix"
# date, today or yyyymmdd
al_date_suffix="$date_suffix"
al_git_build_branch=main
al_git_publish_url=https://github.com/${gitorg}/docker-images.git
al_git_publish_branch="$branch"
## Quay.io amd64 URLs
SYS_BASE=${repo}/amd64:${os}.${rel}-base-$date_suffix
SYS_INIT=${repo}/amd64:${os}.${rel}-init-$date_suffix
SYS_MICRO=${repo}/amd64:${os}.${rel}-micro-$date_suffix
SYS_DEFAULT=${repo}/amd64:${os}.${rel}-$date_suffix
SYS_MINIMAL=${repo}/amd64:${os}.${rel}-minimal-$date_suffix
## Quay.io amd64 URLs
# SYS_BASE=${repo}/${os}-base
# SYS_INIT=${repo}/${os}-init
# SYS_MICRO=${repo}/${os}-micro
# SYS_DEFAULT=${repo}:${os}
# SYS_MINIMAL=${repo}:${os}-minimal
EOH
}
# Generate the license and copyright header for dynamic files use
print_env_header() {
cat > "$1" <<-EOI
# ------------------------------------------------------------------------------
# NOTE: THIS FILE IS GENERATED, PLEASE DO NOT EDIT IT DIRECTLY.
# ------------------------------------------------------------------------------
#
EOI
}
#
# curl -LO https://raw.githubusercontent.com/docker-library/official-images/master/library/almalinux
# filter last AL8 and AL9 branch info
# cat almalinux | grep GitFetch | sort -ru | awk 'FNR < 3 { print $2 } ' | sort
# OR filter al-8 or al-9 using grep
#