-
Notifications
You must be signed in to change notification settings - Fork 14
/
BuildSourceImage.sh
executable file
·1258 lines (1131 loc) · 38 KB
/
BuildSourceImage.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
# This script builds a Source Image via "drivers" to collect source
export ABV_NAME="SrcImg"
# TODO maybe a flag for this?
export source_image_suffix="-source"
# output version string
_version() {
echo "$(basename "${0}") version 0.2.0-dev"
}
# output the cli usage and exit
_usage() {
_version
echo "Usage: $(basename "$0") [-D] [-b <path>] [-c <path>] [-e <path>] [-r <path>] [-o <path>] [-p <image>] [-l] [-d <drivers>]"
echo ""
echo " Container Source Image tool"
echo ""
echo -e " -b <path>\tbase path for source image builds"
echo -e " -c <path>\tbuild context for the container image. Can be provided via CONTEXT_DIR env variable"
echo -e " -e <path>\textra src for the container image. Can be provided multiple times"
echo -e " -s <path>\tdirectory of SRPMS to add. Can be provided via SRPM_DIR env variable"
echo -e " -o <path>\toutput the OCI image to path. Can be provided via OUTPUT_DIR env variable"
echo -e " -d <drivers>\tenumerate specific source drivers to run"
echo -e " -l\t\tlist the source drivers available"
echo -e " -p <image>\tpush source image to specified reference after build"
echo -e " -D\t\tdebuging output. Can be set via DEBUG env variable"
echo -e " -h\t\tthis usage information"
echo -e " -v\t\tversion"
echo -e ""
echo -e " Subcommands:"
echo -e " unpack\tUnpack an OCI layout to a rootfs directory"
echo -e ""
}
# sanity checks on startup
_init() {
set -o pipefail
# check for tools we depend on
for cmd in jq skopeo file find tar stat date ; do
if [ -z "$(command -v ${cmd})" ] ; then
# TODO: maybe this could be individual checks so it can report
# where to find the tools
_error "please install package to provide '${cmd}'"
fi
done
if [ -z "$(command -v dnf)" ] && [ -z "$(command -v yumdownloader)" ] ; then
_error "please provide either 'dnf download' or 'yumdownloader'"
fi
}
# enable access to some of functions as subcommands!
_subcommand() {
local command="${1}"
local ret
shift
case "${command}" in
unpack)
# (vb) i'd prefer this subcommand directly match the function name, but it isn't as pretty.
unpack_img "${@}"
ret=$?
exit "${ret}"
;;
esac
}
# _is_sourced tests whether this script is being source, or executed directly
_is_sourced() {
# https://unix.stackexchange.com/a/215279
# thanks @tianon
[ "${FUNCNAME[${#FUNCNAME[@]} - 1]}" == 'source' ]
}
# count $character $string
_count_char_in_string() {
c="${2//[^${1}]}"
echo -n ${#c}
}
# size of file/directory in bytes
_size() {
du -b "${1}" | awk '{ ORS=""; print $1 }'
}
# date timestamp in RFC 3339, to the nanosecond, but slightly golang style ...
_date_ns() {
date --rfc-3339=ns | tr ' ' 'T' | tr -d '\n'
}
# local `mktemp -d`
_mktemp_d() {
local v
v=$(mktemp -d "${TMPDIR:-/tmp}/${ABV_NAME}.XXXXXX")
_debug "mktemp -d --> ${v}"
echo "${v}"
}
# local `mktemp`
_mktemp() {
local v
v=$(mktemp "${TMPDIR:-/tmp}/${ABV_NAME}.XXXXXX")
_debug "mktemp --> ${v}"
echo "${v}"
}
# local rm -rf
_rm_rf() {
_debug "rm -rf ${*}"
rm -rf "${@}"
}
# local mkdir -p
_mkdir_p() {
if [ -n "${DEBUG}" ] ; then
mkdir -vp "${@}"
else
mkdir -p "${@}"
fi
}
# local tar
_tar() {
if [ -n "${DEBUG}" ] ; then
tar -v "${@}"
else
tar "${@}"
fi
}
_rpm_download() {
if [ "$(command -v yumdownloader)" != "" ] ; then
yumdownloader "${@}"
else
dnf download "${@}"
fi
}
# output things, only when $DEBUG is set
_debug() {
if [ -n "${DEBUG}" ] ; then
echo "[${ABV_NAME}][DEBUG] ${*}" >&2
fi
}
# general echo but with prefix
_info() {
echo "[${ABV_NAME}][INFO] ${*}"
}
_warn() {
echo "[${ABV_NAME}][WARN] ${*}" >&2
}
# general echo but with prefix
_error() {
echo "[${ABV_NAME}][ERROR] ${*}" >&2
exit 1
}
#
# parse the OCI image reference, accounting for:
# * transport name
# * presence or lack of transport port number
# * presence or lack of digest
# * presence or lack of image tag
#
#
# return the image reference's digest, if any
#
parse_img_digest() {
local ref="${1}"
local digest=""
if [ "$(_count_char_in_string '@' "${ref}")" -gt 0 ] ; then
digest="${ref##*@}" # the digest after the "@"
fi
echo -n "${digest}"
}
#
# determine image base name (without tag or digest)
#
parse_img_base() {
local ref="${1%@*}" # just the portion before the digest "@"
local base="${ref}" # default base is their reference
local last_word="" # splitting up their reference to get the last word/chunk
last_word="$(echo "${ref}" | tr '/' '\n' | tail -1 )"
if [ "$(_count_char_in_string ':' "${last_word}")" -gt 0 ] ; then
# which means everything before it is the base image name, **including
# transport (which could have a port delineation), and even a URI like network ports.
base="$(echo "${ref}" | rev | cut -d : -f 2 | rev )"
fi
echo -n "${base}"
}
#
# determine, or guess, the image tag from the provided image reference
#
parse_img_tag() {
local ref="${1%@*}" # just the portion before the digest "@"
local tag="latest" # default tag
if [ -z "${ref}" ] ; then
echo -n "${tag}"
return 0
fi
local last_word="" # splitting up their reference to get the last word/chunk
last_word="$(echo "${ref}" | tr '/' '\n' | tail -1 )"
if [ "$(_count_char_in_string ':' "${last_word}")" -gt 0 ] ; then
# if there are colons in the last segment after '/', then get that tag name
tag="${last_word#*:}" # this parameter expansion removes the prefix pattern before the ':'
fi
echo -n "${tag}"
}
#
# an inline prefixer for containers/image tools
#
ref_prefix() {
local ref="${1}"
local pfxs
local ret
# get the supported prefixes of the current version of skopeo
mapfile -t pfxs < <(skopeo copy --help | grep -A1 "Supported transports:" | grep -v "Supported transports" | sed 's/, /\n/g')
ret=$?
if [ ${ret} -ne 0 ] ; then
return ${ret}
fi
for pfx in "${pfxs[@]}" ; do
if echo "${ref}" | grep -q "^${pfx}:" ; then
# break if we match a known prefix
echo "${ref}"
return 0
fi
done
# else default
echo "docker://${ref}"
}
#
# an inline namer for the source image
# Initially this is a tagging convention (which if we try estesp/manifest-tool
# can be directly mapped into a manifest-list/image-index).
#
ref_src_img_tag() {
local ref="${1}"
echo -n "$(parse_img_tag "${ref}")""${source_image_suffix}"
}
#
# call out to registry for the image reference's digest checksum
#
fetch_img_digest() {
local ref="${1}"
local dgst
local ret
## TODO: check for authfile, creds, and whether it's an insecure registry
dgst=$(skopeo inspect "$(ref_prefix "${ref}")" | jq .Digest | tr -d \")
ret=$?
if [ $ret -ne 0 ] ; then
echo "ERROR: check the image reference: ${ref}" >&2
return $ret
fi
echo -n "${dgst}"
}
#
# pull down the image to an OCI layout
# arguments: image ref
# returns: path:tag to the OCI layout
#
# any commands should only output to stderr, so that the caller can receive the
# path reference to the OCI layout.
#
fetch_img() {
local ref="${1}"
local dst="${2}"
local base
local tag
local dgst
local from
local ret
_mkdir_p "${dst}"
base="$(parse_img_base "${ref}")"
tag="$(parse_img_tag "${ref}")"
dgst="$(parse_img_digest "${ref}")"
from=""
# skopeo currently only support _either_ tag _or_ digest, so we'll be specific.
if [ -n "${dgst}" ] ; then
from="$(ref_prefix "${base}")@${dgst}"
else
from="$(ref_prefix "${base}"):${tag}"
fi
## TODO: check for authfile, creds, and whether it's an insecure registry
## destination name must have the image tag included (umoci expects it)
skopeo \
copy \
"${from}" \
"oci:${dst}:${tag}" >&2
ret=$?
if [ ${ret} -ne 0 ] ; then
return ${ret}
fi
echo -n "${dst}:${tag}"
}
#
# upack_img <oci layout path> <unpack path>
#
unpack_img() {
local image_dir="${1}"
local unpack_dir="${2}"
local ret
while getopts ":h" opts; do
case "${opts}" in
*)
echo "$0 unpack <oci layout path> <unpack path>"
return 1
;;
esac
done
shift $((OPTIND-1))
if [ -z "${image_dir}" ] || [ -z "${unpack_dir}" ] ; then
_error "[unpack_img] blank arguments provided"
fi
if [ -d "${unpack_dir}" ] ; then
_rm_rf "${unpack_dir}"
fi
if [ -n "$(command -v umoci)" ] ; then
# can be done as non-root (even in a non-root container)
unpack_img_umoci "${image_dir}" "${unpack_dir}"
ret=$?
if [ ${ret} -ne 0 ] ; then
return ${ret}
fi
else
# can be done as non-root (even in a non-root container)
unpack_img_bash "${image_dir}" "${unpack_dir}"
ret=$?
if [ ${ret} -ne 0 ] ; then
return ${ret}
fi
fi
}
#
# unpack an image layout using only jq and bash
#
unpack_img_bash() {
local image_dir="${1}"
local unpack_dir="${2}"
local mnfst_dgst
local layer_dgsts
local ret
_debug "unpacking with bash+jq"
# for compat with umoci (which wants the image tag as well)
if echo "${image_dir}" | grep -q ":" ; then
image_dir="${image_dir%:*}"
fi
mnfst_dgst="$(jq '.manifests[0].digest' "${image_dir}"/index.json | tr -d \")"
ret=$?
if [ ${ret} -ne 0 ] ; then
return ${ret}
fi
# TODO this will need to be refactored when we start seeing +zstd layers.
# Then it will be better to no just get a list of digests, but maybe to
# iterate on each descriptor independently?
layer_dgsts="$(jq '.layers | map(select(.mediaType == "application/vnd.oci.image.layer.v1.tar+gzip"),select(.mediaType == "application/vnd.oci.image.layer.v1.tar"),select(.mediaType == "application/vnd.docker.image.rootfs.diff.tar.gzip")) | .[] | .digest' "${image_dir}"/blobs/"${mnfst_dgst/://}" | tr -d \")"
ret=$?
if [ ${ret} -ne 0 ] ; then
return ${ret}
fi
_mkdir_p "${unpack_dir}/rootfs"
for dgst in ${layer_dgsts} ; do
path="${image_dir}/blobs/${dgst/://}"
tmp_file=$(_mktemp)
zcat "${path}" | _tar -t > "$tmp_file"
# look for '.wh.' entries. They must be removed from the rootfs
# _before_ extracting the archive, then the .wh. entries themselves
# need to not remain afterwards
grep '\.wh\.' "${tmp_file}" | while read -r wh_path ; do
# if `some/path/.wh.foo` then `rm -rf `${unpack_dir}/some/path/foo`
# if `some/path/.wh..wh..opq` then `rm -rf `${unpack_dir}/some/path/*`
if [ "$(basename "${wh_path}")" == ".wh..wh..opq" ] ; then
_rm_rf "${unpack_dir}/rootfs/$(dirname "${wh_path}")/*"
elif basename "${wh_path}" | grep -qe '^\.wh\.' ; then
name=$(basename "${wh_path}" | sed -e 's/^\.wh\.//')
_rm_rf "${unpack_dir}/rootfs/$(dirname "${wh_path}")/${name}"
fi
done
_info "[unpacking] layer ${dgst}"
# unpack layer to rootfs (without whiteouts)
zcat "${path}" | _tar --restrict --no-xattr --no-acls --no-selinux --exclude='*.wh.*' -x -C "${unpack_dir}/rootfs"
ret=$?
if [ ${ret} -ne 0 ] ; then
return ${ret}
fi
# some of the directories get unpacked as 0555, so removing them gives an EPERM
find "${unpack_dir}" -type d -exec chmod 0755 "{}" \;
done
}
#
# unpack using umoci
#
unpack_img_umoci() {
local image_dir="${1}"
local unpack_dir="${2}"
_debug "unpacking with umoci"
# always assume we're not root I reckon
umoci unpack --rootless --image "${image_dir}" "${unpack_dir}" >&2
ret=$?
return $ret
}
#
# copy an image from one location to another
#
push_img() {
local src="${1}"
local dst="${2}"
_debug "pushing image ${src} to ${dst}"
## TODO: check for authfile, creds, and whether it's an insecure registry
skopeo copy --quiet --dest-tls-verify=false "$(ref_prefix "${src}")" "$(ref_prefix "${dst}")" # XXX for demo only
#skopeo copy "$(ref_prefix "${src}")" "$(ref_prefix "${dst}")"
ret=$?
return $ret
}
#
# sets up a basic new OCI layout, for an image with the provided (or default 'latest') tag
#
layout_new() {
local out_dir="${1}"
local image_tag="${2:-latest}"
local ret
if [ -n "$(command -v umoci)" ] ; then
layout_new_umoci "${out_dir}" "${image_tag}"
ret=$?
if [ ${ret} -ne 0 ] ; then
return ${ret}
fi
else
layout_new_bash "${out_dir}" "${image_tag}"
ret=$?
if [ ${ret} -ne 0 ] ; then
return ${ret}
fi
fi
}
#
# sets up new OCI layout, using `umoci`
#
layout_new_umoci() {
local out_dir="${1}"
local image_tag="${2:-latest}"
local ret
# umoci expects the layout path to _not_ exist and will fail if it does exist
_rm_rf "${out_dir}"
umoci init --layout "${out_dir}"
ret=$?
if [ "${ret}" -ne 0 ] ; then
return "${ret}"
fi
# XXX currently does not support adding the rich annotations like I've done with the _bash
# https://github.com/openSUSE/umoci/issues/298
umoci new --image "${out_dir}:${image_tag}"
ret=$?
if [ "${ret}" -ne 0 ] ; then
return "${ret}"
fi
}
#
# sets up new OCI layout, all with bash and jq
#
layout_new_bash() {
local out_dir="${1}"
local image_tag="${2:-latest}"
local config
local mnfst
local config_sum
local mnfst_sum
local ret
_mkdir_p "${out_dir}/blobs/sha256"
echo '{"imageLayoutVersion":"1.0.0"}' > "${out_dir}/oci-layout"
config='
{
"created": "'$(_date_ns)'",
"architecture": "amd64",
"os": "linux",
"config": {},
"rootfs": {
"type": "layers",
"diff_ids": []
}
}
'
config_sum=$(echo "${config}" | jq -c | tr -d '\n' | sha256sum | awk '{ ORS=""; print $1 }')
ret=$?
if [ "${ret}" -ne 0 ] ; then
return "${ret}"
fi
echo "${config}" | jq -c | tr -d '\n' > "${out_dir}/blobs/sha256/${config_sum}"
ret=$?
if [ "${ret}" -ne 0 ] ; then
return "${ret}"
fi
mnfst='
{
"schemaVersion": 2,
"config": {
"mediaType": "application/vnd.oci.image.config.v1+json",
"digest": "sha256:'"${config_sum}"'",
"size": '"$(_size "${out_dir}"/blobs/sha256/"${config_sum}")"'
},
"layers": []
}
'
mnfst_sum=$(echo "${mnfst}" | jq -c | tr -d '\n' | sha256sum | awk '{ ORS=""; print $1 }')
echo "${mnfst}" | jq -c | tr -d '\n' > "${out_dir}/blobs/sha256/${mnfst_sum}"
echo '
{
"schemaVersion": 2,
"manifests": [
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"digest": "sha256:'"${mnfst_sum}"'",
"size": '"$(_size "${out_dir}"/blobs/sha256/"${mnfst_sum}")"',
"annotations": {
"org.opencontainers.image.ref.name": "'"${image_tag}"'"
}
}
]
}
' | jq -c | tr -d '\n' > "${out_dir}/index.json"
}
# call this for every artifact, to insert it into an OCI layout
# args:
# * a path to the layout
# * a path to the artifact
# * the path inside the tar
# * json file to slurp in as annotations for this layer's OCI descriptor
# * tag used in the layout (default is 'latest')
#
layout_insert() {
local out_dir="${1}"
local artifact_path="${2}"
local tar_path="${3}"
local annotations_file="${4}"
local image_tag="${5:-latest}"
local ret
if [ -n "$(command -v umoci)" ] ; then
layout_insert_umoci "${out_dir}" "${artifact_path}" "${tar_path}" "${annotations_file}" "${image_tag}"
ret=$?
if [ ${ret} -ne 0 ] ; then
return ${ret}
fi
else
layout_insert_bash "${out_dir}" "${artifact_path}" "${tar_path}" "${annotations_file}" "${image_tag}"
ret=$?
if [ ${ret} -ne 0 ] ; then
return ${ret}
fi
fi
}
layout_insert_umoci() {
local out_dir="${1}"
local artifact_path="${2}"
local tar_path="${3}"
local annotations_file="${4}"
local image_tag="${5:-latest}"
local sum
local ret
# prep the blob path for inside the layer, so we can just copy that whole path in
tmpdir="$(_mktemp_d)"
# TODO account for "artifact_path" being a directory?
sum="$(sha256sum "${artifact_path}" | awk '{ print $1 }')"
_mkdir_p "${tmpdir}/blobs/sha256"
cp "${artifact_path}" "${tmpdir}/blobs/sha256/${sum}"
if [ "$(basename "${tar_path}")" == "$(basename "${artifact_path}")" ] ; then
_mkdir_p "${tmpdir}/$(dirname "${tar_path}")"
# TODO this symlink need to be relative path, not to `/blobs/...`
ln -s "/blobs/sha256/${sum}" "${tmpdir}/${tar_path}"
else
_mkdir_p "${tmpdir}/${tar_path}"
# TODO this symlink need to be relative path, not to `/blobs/...`
ln -s "/blobs/sha256/${sum}" "${tmpdir}/${tar_path}/$(basename "${artifact_path}")"
fi
# XXX currently does not support adding the rich annotations like I've done with the _bash
# https://github.com/openSUSE/umoci/issues/298
# XXX this insert operation can not disable compression
# https://github.com/openSUSE/umoci/issues/300
umoci insert \
--rootless \
--image "${out_dir}:${image_tag}" \
--history.created "$(_date_ns)" \
--history.comment "#(nop) $(_version) adding artifact: ${sum}" \
"${tmpdir}" "/"
ret=$?
if [ ${ret} -ne 0 ] ; then
return ${ret}
fi
}
layout_insert_bash() {
local out_dir="${1}"
local artifact_path="${2}"
local tar_path="${3}"
local annotations_file="${4}"
local image_tag="${5:-latest}"
local mnfst_list
local mnfst_dgst
local mnfst
local tmpdir
local sum
local tmptar
local tmptar_sum
local tmptar_size
local config_sum
local tmpconfig
local tmpconfig_sum
local tmpconfig_size
local tmpmnfst
local tmpmnfst_sum
local tmpmnfst_size
local tmpmnfst_list
mnfst_list="${out_dir}/index.json"
# get the digest to the manifest
test -f "${mnfst_list}" || return 1
mnfst_dgst="$(jq --arg tag "${image_tag}" '
.manifests[]
| select(.annotations."org.opencontainers.image.ref.name" == $tag )
| .digest
' "${mnfst_list}" | tr -d \" | tr -d '\n' )"
mnfst="${out_dir}/blobs/${mnfst_dgst/://}"
test -f "${mnfst}" || return 1
# make tar of new object
tmpdir="$(_mktemp_d)"
# TODO account for "artifact_path" being a directory?
sum="$(sha256sum "${artifact_path}" | awk '{ print $1 }')"
# making a blob store in the layer
_mkdir_p "${tmpdir}/blobs/sha256"
cp "${artifact_path}" "${tmpdir}/blobs/sha256/${sum}"
if [ "$(basename "${tar_path}")" == "$(basename "${artifact_path}")" ] ; then
_mkdir_p "${tmpdir}/$(dirname "${tar_path}")"
# TODO this symlink need to be relative path, not to `/blobs/...`
ln -s "../blobs/sha256/${sum}" "${tmpdir}/${tar_path}"
else
_mkdir_p "${tmpdir}/${tar_path}"
# TODO this symlink need to be relative path, not to `/blobs/...`
ln -s "../blobs/sha256/${sum}" "${tmpdir}/${tar_path}/$(basename "${artifact_path}")"
fi
tmptar="$(_mktemp)"
# zero all the things for as consistent blobs as possible
_tar -C "${tmpdir}" --sort=name --mtime=@0 --owner=0 --group=0 --mode='a+rw' --no-xattrs --no-selinux --no-acls -cf "${tmptar}" .
_rm_rf "${tmpdir}"
# checksum tar and move to blobs/sha256/$checksum
tmptar_sum="$(sha256sum "${tmptar}" | awk '{ ORS=""; print $1 }')"
tmptar_size="$(_size "${tmptar}")"
mv "${tmptar}" "${out_dir}/blobs/sha256/${tmptar_sum}"
# find and read the prior config, mapped from the manifest
config_sum="$(jq '.config.digest' "${mnfst}" | tr -d \")"
# use `jq` to append to prior config
tmpconfig="$(_mktemp)"
jq -c \
--arg date "$(_date_ns)" \
--arg tmptar_sum "sha256:${tmptar_sum}" \
--arg comment "#(nop) $(_version) adding artifact: ${sum}" \
'
.created = $date
| .rootfs.diff_ids += [ $tmptar_sum ]
| .history += [
{
"created": $date,
"created_by": $comment
}
]
' "${out_dir}/blobs/${config_sum/://}" > "${tmpconfig}"
_rm_rf "${out_dir}/blobs/${config_sum/://}"
# rename the config blob to its new checksum
tmpconfig_sum="$(sha256sum "${tmpconfig}" | awk '{ ORS=""; print $1 }')"
tmpconfig_size="$(_size "${tmpconfig}")"
mv "${tmpconfig}" "${out_dir}/blobs/sha256/${tmpconfig_sum}"
# append layers list in the manifest, and its new config mapping
tmpmnfst="$(_mktemp)"
jq -c \
--arg tmpconfig_sum "sha256:${tmpconfig_sum}" \
--arg tmpconfig_size "${tmpconfig_size}" \
--arg tmptar_sum "sha256:${tmptar_sum}" \
--arg tmptar_size "${tmptar_size}" \
--arg sum "sha256:${sum}" \
--slurpfile annotations_slurp "${annotations_file}" \
'
.config.digest = $tmpconfig_sum
| .config.size = ($tmpconfig_size|tonumber)
| ( {
"source.artifact.filename.checksum": $sum
} + $annotations_slurp[0] ) as $annotations_merge
| .layers += [
{
"mediaType": "application/vnd.oci.image.layer.v1.tar",
"size": ($tmptar_size|tonumber),
"digest": $tmptar_sum,
"annotations": $annotations_merge
}
]
' "${mnfst}" > "${tmpmnfst}"
ret=$?
if [ $ret -ne 0 ] ; then
return 1
fi
_rm_rf "${mnfst}"
# rename the manifest blob to its new checksum
tmpmnfst_sum="$(sha256sum "${tmpmnfst}" | awk '{ ORS=""; print $1 }')"
tmpmnfst_size="$(_size "${tmpmnfst}")"
mv "${tmpmnfst}" "${out_dir}/blobs/sha256/${tmpmnfst_sum}"
_debug "updated ${out_dir}/blobs/sha256/${tmpmnfst_sum}"
# map the mnfst_list to the new mnfst checksum
tmpmnfst_list="$(_mktemp)"
jq -c \
--arg tag "${image_tag}" \
--arg tmpmnfst_sum "sha256:${tmpmnfst_sum}" \
--arg tmpmnfst_size "${tmpmnfst_size}" \
'
[(.manifests[] | select(.annotations."org.opencontainers.image.ref.name" != $tag) )] as $manifests_reduced
| [
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"digest": $tmpmnfst_sum,
"size": ($tmpmnfst_size|tonumber),
"annotations": {
"com.redhat.image.type": "source",
"org.opencontainers.image.ref.name": $tag
}
}
] as $manifests_new
| .manifests = $manifests_reduced + $manifests_new
' "${mnfst_list}" > "${tmpmnfst_list}"
ret=$?
if [ $ret -ne 0 ] ; then
return 1
fi
mv "${tmpmnfst_list}" "${mnfst_list}"
_debug "manifest-list updated ${mnfst_list}"
}
#
# Source Collection Drivers
#
# presently just bash functions. *notice* prefix the function name as `sourcedriver_`
# May become a ${ABV_NAME}/drivers.d/
#
# Arguments:
# * image ref
# * path to inspect
# * output path for source (specifc to this driver)
# * output path for JSON file of source's annotations
#
# The JSON of source annotations is the key to discovering the source artifact
# to be added and including rich metadata about that archive into the final
# image.
# The name of each JSON file is appending '.json' to the artifact's name. So if
# you have `foo-1.0.src.rpm` then there MUST be a corresponding
# `foo-1.0.src.rpm.json`.
# The data structure in this annotation is just a dict/hashmap, with key/val
# according to
# https://github.com/opencontainers/image-spec/blob/master/annotations.md
#
#
# driver to determine and fetch source rpms, based on the rootfs
#
sourcedriver_rpm_fetch() {
local self="${0#sourcedriver_*}"
local ref="${1}"
local rootfs="${2}"
local out_dir="${3}"
local manifest_dir="${4}"
local release
local rpm
local srcrpm_buildtime
local srcrpm_pkgid
local srcrpm_name
local srcrpm_version
local srcrpm_epoch
local srcrpm_release
local srcrpm_license
local mimetype
# Get the RELEASEVER from the image
release=$(rpm -q --queryformat "%{VERSION}\n" --root "${rootfs}" -f /etc/os-release)
# From the rootfs of the works image, build out the src rpms to operate over
for srcrpm in $(rpm -qa --root "${rootfs}" --queryformat '%{SOURCERPM}\n' | grep -v '^gpg-pubkey' | sort -u) ; do
if [ "${srcrpm}" == "(none)" ] ; then
continue
fi
rpm=${srcrpm%*.src.rpm}
if [ ! -f "${out_dir}/${srcrpm}" ] ; then
_debug "--> fetching ${srcrpm}"
_rpm_download \
--quiet \
--installroot "${rootfs}" \
--release "${release}" \
--destdir "${out_dir}" \
--source \
"${rpm}"
ret=$?
if [ $ret -ne 0 ] ; then
_warn "failed to fetch ${srcrpm}"
continue
fi
else
_debug "--> using cached ${srcrpm}"
fi
# TODO one day, check and confirm with %{sourcepkgid}
# https://bugzilla.redhat.com/show_bug.cgi?id=1741715
#rpm_sourcepkgid=$(rpm -q --root ${rootfs} --queryformat '%{sourcepkgid}' "${rpm}")
srcrpm_buildtime=$(rpm -qp --nosignature --qf '%{buildtime}' "${out_dir}"/"${srcrpm}" )
srcrpm_pkgid=$(rpm -qp --nosignature --qf '%{pkgid}' "${out_dir}"/"${srcrpm}" )
srcrpm_name=$(rpm -qp --nosignature --qf '%{name}' "${out_dir}"/"${srcrpm}" )
srcrpm_version=$(rpm -qp --nosignature --qf '%{version}' "${out_dir}"/"${srcrpm}" )
srcrpm_epoch=$(rpm -qp --nosignature --qf '%{epoch}' "${out_dir}"/"${srcrpm}" )
srcrpm_release=$(rpm -qp --nosignature --qf '%{release}' "${out_dir}"/"${srcrpm}" )
srcrpm_license=$(rpm -qp --nosignature --qf '%{license}' "${out_dir}"/"${srcrpm}" )
mimetype="$(file --brief --mime-type "${out_dir}"/"${srcrpm}")"
jq \
-n \
--arg filename "${srcrpm}" \
--arg name "${srcrpm_name}" \
--arg version "${srcrpm_version}" \
--arg epoch "${srcrpm_epoch}" \
--arg release "${srcrpm_release}" \
--arg license "${srcrpm_license}" \
--arg buildtime "${srcrpm_buildtime}" \
--arg mimetype "${mimetype}" \
'
{
"source.artifact.filename": $filename,
"source.artifact.name": $name,
"source.artifact.version": $version,
"source.artifact.epoch": $epoch,
"source.artifact.release": $release,
"source.artifact.license": $license,
"source.artifact.mimetype": $mimetype,
"source.artifact.buildtime": $buildtime
}
' \
> "${manifest_dir}/${srcrpm}.json"
ret=$?
if [ $ret -ne 0 ] ; then
return 1
fi
done
}
#
# driver to only package rpms from a provided rpm directory
# (koji use-case)
#
sourcedriver_rpm_dir() {
local self="${0#sourcedriver_*}"
local ref="${1}"
local rootfs="${2}"
local out_dir="${3}"
local manifest_dir="${4}"
local srcrpm_buildtime
local srcrpm_pkgid
local srcrpm_name
local srcrpm_version
local srcrpm_epoch
local srcrpm_release
local srcrpm_license
local mimetype
if [ -n "${SRPM_DIR}" ]; then
_debug "[$self] writing to $out_dir and $manifest_dir"
find "${SRPM_DIR}" -type f -name '*src.rpm' | while read -r srcrpm ; do
cp "${srcrpm}" "${out_dir}"
srcrpm="$(basename "${srcrpm}")"
_debug "[$self] --> ${srcrpm}"
srcrpm_buildtime=$(rpm -qp --nosignature --qf '%{buildtime}' "${out_dir}"/"${srcrpm}" )
srcrpm_pkgid=$(rpm -qp --nosignature --qf '%{pkgid}' "${out_dir}"/"${srcrpm}" )
srcrpm_name=$(rpm -qp --nosignature --qf '%{name}' "${out_dir}"/"${srcrpm}" )
srcrpm_version=$(rpm -qp --nosignature --qf '%{version}' "${out_dir}"/"${srcrpm}" )
srcrpm_epoch=$(rpm -qp --nosignature --qf '%{epoch}' "${out_dir}"/"${srcrpm}" )
srcrpm_release=$(rpm -qp --nosignature --qf '%{release}' "${out_dir}"/"${srcrpm}" )
srcrpm_license=$(rpm -qp --nosignature --qf '%{license}' "${out_dir}"/"${srcrpm}" )
mimetype="$(file --brief --mime-type "${out_dir}"/"${srcrpm}")"
jq \
-n \
--arg filename "${srcrpm}" \
--arg name "${srcrpm_name}" \
--arg version "${srcrpm_version}" \
--arg epoch "${srcrpm_epoch}" \
--arg release "${srcrpm_release}" \
--arg license "${srcrpm_license}" \
--arg buildtime "${srcrpm_buildtime}" \
--arg mimetype "${mimetype}" \
--arg pkgid "${srcrpm_pkgid}" \
'
{
"source.artifact.filename": $filename,
"source.artifact.name": $name,
"source.artifact.version": $version,
"source.artifact.epoch": $version,
"source.artifact.release": $release,
"source.artifact.license": $license,
"source.artifact.mimetype": $mimetype,
"source.artifact.pkgid": $pkgid,
"source.artifact.buildtime": $buildtime
}
' \
> "${manifest_dir}/${srcrpm}.json"
ret=$?
if [ $ret -ne 0 ] ; then
return 1
fi
done
fi
}
#
# If the caller specified a context directory,
#
# slightly special driver, as it has a flag/env passed in, that it uses
#
sourcedriver_context_dir() {
local self="${0#sourcedriver_*}"
local ref="${1}"
local rootfs="${2}"
local out_dir="${3}"
local manifest_dir="${4}"
local tarname
local mimetype
local source_info
if [ -n "${CONTEXT_DIR}" ]; then
_debug "$self: writing to $out_dir and $manifest_dir"
tarname="context.tar"
_tar -C "${CONTEXT_DIR}" \
--sort=name --mtime=@0 --owner=0 --group=0 --mode='a+rw' --no-xattrs --no-selinux --no-acls \
-cf "${out_dir}/${tarname}" .