Skip to content

Commit 84da279

Browse files
Merge pull request #4604 from ggiguash/optimize_skopeo_copy
USHIFT-5288: Mirroring of images in CI should be performed for the current platform only
2 parents bb71e59 + 82f7bb2 commit 84da279

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

scripts/mirror-images.sh

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,30 @@ function usage() {
2121
function skopeo_retry() {
2222
for attempt in $(seq 3) ; do
2323
if ! skopeo "$@" ; then
24-
echo "WARNING: Failed to run skopeo, retry #${attempt}"
24+
echo "WARNING: Failed to run skopeo, retry #${attempt}" >&2
2525
else
2626
return 0
2727
fi
2828
sleep $(( "${attempt}" * 10 ))
2929
done
3030

31-
echo "ERROR: Failed to run skopeo, quitting after 3 tries"
31+
echo "ERROR: Failed to run skopeo, quitting after 3 tries" >&2
3232
return 1
3333
}
3434

35+
function skopeo_opts_for_image() {
36+
local -r img_pull_file="$1"
37+
local -r img_src_ref="$2"
38+
39+
# Cannot copy a single platform for manifest image references
40+
local media_type
41+
media_type=$(skopeo_retry inspect --raw --authfile "${img_pull_file}" "${img_src_ref}" | jq -r .mediaType)
42+
if [ "${media_type}" == "application/vnd.docker.distribution.manifest.list.v2+json" ] ; then
43+
echo -n "--all"
44+
fi
45+
echo -n ""
46+
}
47+
3548
function mirror_registry() {
3649
local -r img_pull_file=$1
3750
local -r img_file_list=$2
@@ -50,25 +63,29 @@ function mirror_registry() {
5063
# Add the target registry prefix
5164
dst_img="${dest_registry}/${dst_img}"
5265
dst_img_no_tag="${dest_registry}/${dst_img_no_tag}"
66+
# Determine skopeo options
67+
local -r skopeo_opts=$(skopeo_opts_for_image "${img_pull_file}" docker://"${src_img}")
5368

5469
# Run the image mirror and tag command
5570
echo "Mirroring '${src_img}' to '${dst_img}'"
56-
skopeo_retry copy --all --quiet \
71+
# shellcheck disable=SC2086
72+
skopeo_retry copy ${skopeo_opts} --quiet \
5773
--preserve-digests \
5874
--authfile "${img_pull_file}" \
5975
docker://"${src_img}" docker://"${dst_img}"
6076

6177
echo "Tagging '${dst_img_no_tag}' as 'latest'"
62-
skopeo_retry copy --all --quiet \
78+
# shellcheck disable=SC2086
79+
skopeo_retry copy ${skopeo_opts} --quiet \
6380
--preserve-digests \
6481
--authfile "${img_pull_file}" \
6582
docker://"${dst_img}" docker://"${dst_img_no_tag}:latest"
66-
6783
}
6884

6985
# Export functions for xargs to use
7086
export -f process_image_copy
7187
export -f skopeo_retry
88+
export -f skopeo_opts_for_image
7289
# Note that images are passed as one argument by replacing "{}" in xarg input.
7390
xargs -P 8 -I {} -a "${img_file_list}" \
7491
bash -c 'process_image_copy "$@"' _ "${img_pull_file}" "${dest_registry}" "{}"
@@ -87,11 +104,14 @@ function registry_to_dir() {
87104
# Remove the source registry prefix
88105
local dst_img
89106
dst_img=$(echo "${src_img}" | cut -d '/' -f 2-)
107+
# Determine skopeo options
108+
local -r skopeo_opts=$(skopeo_opts_for_image "${img_pull_file}" docker://"${src_img}")
90109

91110
# Run the image download command
92111
echo "Downloading '${src_img}' to '${local_dir}'"
93112
mkdir -p "${local_dir}/${dst_img}"
94-
skopeo_retry copy --all --quiet \
113+
# shellcheck disable=SC2086
114+
skopeo_retry copy ${skopeo_opts} --quiet \
95115
--preserve-digests \
96116
--authfile "${img_pull_file}" \
97117
docker://"${src_img}" dir://"${local_dir}/${dst_img}"
@@ -100,6 +120,7 @@ function registry_to_dir() {
100120
# Export functions for xargs to use
101121
export -f process_image_copy
102122
export -f skopeo_retry
123+
export -f skopeo_opts_for_image
103124
# Generate a list for each image and run copy in parallel.
104125
# Note that the image is passed by replacing "{}" in xarg input.
105126
xargs -P 8 -I {} -a "${img_file_list}" \
@@ -123,16 +144,20 @@ function dir_to_registry() {
123144
# Add the target registry prefix and remove SHA
124145
local -r dst_img="${dest_registry}/${src_img}"
125146
local -r dst_img_no_tag="${dest_registry}/${src_img%%[@:]*}"
147+
# Determine skopeo options
148+
local -r skopeo_opts=$(skopeo_opts_for_image "${img_pull_file}" dir://"${src_img}")
126149

127150
# Run the image upload and tag commands
128151
echo "Uploading '${src_img}' to '${dst_img}'"
129-
skopeo_retry copy --all --quiet \
152+
# shellcheck disable=SC2086
153+
skopeo_retry copy ${skopeo_opts} --quiet \
130154
--preserve-digests \
131155
--authfile "${img_pull_file}" \
132156
dir://"${local_dir}/${src_img}" docker://"${dst_img}"
133157

134158
echo "Tagging '${dst_img}' as '${dst_img_no_tag}:latest'"
135-
skopeo_retry copy --all --quiet \
159+
# shellcheck disable=SC2086
160+
skopeo_retry copy ${skopeo_opts} --quiet \
136161
--preserve-digests \
137162
--authfile "${img_pull_file}" \
138163
docker://"${dst_img}" docker://"${dst_img_no_tag}:latest"
@@ -141,6 +166,7 @@ function dir_to_registry() {
141166
# Export functions for xargs to use
142167
export -f process_image_copy
143168
export -f skopeo_retry
169+
export -f skopeo_opts_for_image
144170

145171
# Generate a list with an incremental counter for each image and run copy in parallel.
146172
# Note that the counter and image pairs are passed as one argument by replacing "{}" in xarg input.

test/bin/pyutils/build_bootc_images.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ def main():
568568

569569
# Determine versions of RPM packages
570570
set_rpm_version_info_vars()
571-
# Prepare container image lists for mirroring registries
571+
# Prepare container images list for mirroring registries
572572
common.delete_file(CONTAINER_LIST)
573573
if args.no_extract_images:
574574
common.print_msg("Skipping container image extraction")
@@ -578,6 +578,9 @@ def main():
578578
extract_container_images(f"4.{FAKE_NEXT_MINOR_VERSION}.*", NEXT_REPO, CONTAINER_LIST, args.dry_run)
579579
extract_container_images(PREVIOUS_RELEASE_VERSION, PREVIOUS_RELEASE_REPO, CONTAINER_LIST, args.dry_run)
580580
extract_container_images(YMINUS2_RELEASE_VERSION, YMINUS2_RELEASE_REPO, CONTAINER_LIST, args.dry_run)
581+
# Sort the images list, only leaving unique entries
582+
common.sort_uniq_file(CONTAINER_LIST)
583+
581584
# Process package source templates
582585
ipkgdir = f"{SCRIPTDIR}/../package-sources-bootc"
583586
for ifile in os.listdir(ipkgdir):

test/bin/pyutils/common.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,17 @@ def delete_file(file_path: str):
197197
pass
198198

199199

200+
def sort_uniq_file(file_path: str):
201+
"""Sort the file by lines and overwrite it with the sorted and unique line output"""
202+
# Read, sort, and filter unique lines
203+
with open(file_path, 'r') as f:
204+
unique_lines = sorted(set(f.readlines()))
205+
206+
# Write the result back to the file
207+
with open(file_path, 'w') as f:
208+
f.writelines(unique_lines)
209+
210+
200211
def file_has_valid_lines(file_path: str) -> bool:
201212
"""Check if a text file contains at least one non-empty, non-commented line"""
202213
try:

0 commit comments

Comments
 (0)