Refactor mirror script, no more (almost) duplicate functions.
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
1537e05df7
commit
66ca161018
64
mirror.sh
64
mirror.sh
@ -45,60 +45,29 @@ function collect_images() {
|
||||
echo "Images to mirror: ${WHAT_TO_MIRROR[*]}"
|
||||
}
|
||||
|
||||
function get_amd64_image() {
|
||||
function get_image() {
|
||||
local image=$1
|
||||
local arch=$2
|
||||
|
||||
image_name=$(echo "${image}" | cut -d":" -f 1)
|
||||
image_version=$(echo "${image}" | cut -d":" -f 2)
|
||||
|
||||
# Check if amd64 layers should be fetched.
|
||||
if docker manifest inspect "${REMOTE_IMAGE}-amd64" &> /dev/null; then
|
||||
echo -e "\t* Layers for amd64 architecture for this image exist."
|
||||
|
||||
return
|
||||
# Check if layers for requested architecture should be fetched.
|
||||
if docker manifest inspect "${REMOTE_IMAGE}-${arch}" &> /dev/null; then
|
||||
echo -e "\t* Layers for ${arch} architecture for this image exist at registry we mirror to!"
|
||||
fi
|
||||
|
||||
echo -ne "\t* Getting amd64 layers... "
|
||||
|
||||
if ! docker pull --platform=linux/amd64 "${image}" &> /dev/null; then
|
||||
echo "FAIL!"
|
||||
|
||||
return 1
|
||||
fi
|
||||
|
||||
echo -n "Downloaded, "
|
||||
|
||||
# shellcheck disable=SC1083
|
||||
image_hash=$(docker images -a | grep "^${image_name}" | grep "${image_version}" | awk {' print $3 '})
|
||||
|
||||
if ! docker tag "${image_hash}" "${REMOTE_IMAGE}-amd64" &> /dev/null; then
|
||||
echo "but tagging failed!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "tagged."
|
||||
}
|
||||
|
||||
function get_arm64_image() {
|
||||
local image=$1
|
||||
|
||||
image_name=$(echo "${image}" | cut -d":" -f 1)
|
||||
image_version=$(echo "${image}" | cut -d":" -f 2)
|
||||
|
||||
# Check if arm64 layers should be fetched.
|
||||
if docker manifest inspect "${REMOTE_IMAGE}-arm64" &> /dev/null; then
|
||||
echo -e "\t* Layers for arm64 architecture for this image exist."
|
||||
|
||||
return
|
||||
fi
|
||||
|
||||
echo -ne "\t* Getting arm64 layers... "
|
||||
echo -ne "\t* Getting ${arch} layers... "
|
||||
|
||||
# arm64 layers might be missing. So we just put "FAIL!" here and do nothing else.
|
||||
if ! docker pull --platform=linux/arm64 "${image}" &> /dev/null; then
|
||||
if ! docker pull --platform=linux/"${arch}" "${image}" &> /dev/null; then
|
||||
echo "FAIL!"
|
||||
|
||||
return 1
|
||||
# We presume that amd64 layers are always present. Returning an error here if they're absent.
|
||||
if [ "${arch}" == "amd64" ]; then
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
echo -n "Downloaded, "
|
||||
@ -106,14 +75,17 @@ function get_arm64_image() {
|
||||
# shellcheck disable=SC1083
|
||||
image_hash=$(docker images -a | grep "^${image_name}" | grep "${image_version}" | awk {' print $3 '})
|
||||
|
||||
if ! docker tag "${image_hash}" "${REMOTE_IMAGE}-arm64" &> /dev/null; then
|
||||
if ! docker tag "${image_hash}" "${REMOTE_IMAGE}-${arch}" &> /dev/null; then
|
||||
echo "but tagging failed!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "tagged."
|
||||
|
||||
MULTIARCH=1
|
||||
# Set multiarch flag for any other arch than amd64.
|
||||
if [ "${arch}" != "amd64" ]; then
|
||||
MULTIARCH=1
|
||||
fi
|
||||
}
|
||||
|
||||
function login_to_registry() {
|
||||
@ -133,13 +105,13 @@ function mirror() {
|
||||
MULTIARCH=0
|
||||
|
||||
# We presumes that amd64 image should always be available.
|
||||
if ! get_amd64_image "${image}"; then
|
||||
if ! get_image "amd64" "${image}"; then
|
||||
echo "! Image mirroring failed! Cannot obtain amd64 image!"
|
||||
|
||||
return 1
|
||||
fi
|
||||
|
||||
get_arm64_image "${image}"
|
||||
get_image "arm64" "${image}"
|
||||
push_multiarch_image "${image}"
|
||||
cleanup "${image}"
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user