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
60
mirror.sh
60
mirror.sh
@ -45,75 +45,47 @@ function collect_images() {
|
|||||||
echo "Images to mirror: ${WHAT_TO_MIRROR[*]}"
|
echo "Images to mirror: ${WHAT_TO_MIRROR[*]}"
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_amd64_image() {
|
function get_image() {
|
||||||
local image=$1
|
local image=$1
|
||||||
|
local arch=$2
|
||||||
|
|
||||||
image_name=$(echo "${image}" | cut -d":" -f 1)
|
image_name=$(echo "${image}" | cut -d":" -f 1)
|
||||||
image_version=$(echo "${image}" | cut -d":" -f 2)
|
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
|
fi
|
||||||
|
|
||||||
echo -ne "\t* Getting amd64 layers... "
|
echo -ne "\t* Getting ${arch} 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... "
|
|
||||||
|
|
||||||
# arm64 layers might be missing. So we just put "FAIL!" here and do nothing else.
|
# 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!"
|
echo "FAIL!"
|
||||||
|
|
||||||
|
# We presume that amd64 layers are always present. Returning an error here if they're absent.
|
||||||
|
if [ "${arch}" == "amd64" ]; then
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
echo -n "Downloaded, "
|
echo -n "Downloaded, "
|
||||||
|
|
||||||
# shellcheck disable=SC1083
|
# shellcheck disable=SC1083
|
||||||
image_hash=$(docker images -a | grep "^${image_name}" | grep "${image_version}" | awk {' print $3 '})
|
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!"
|
echo "but tagging failed!"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "tagged."
|
echo "tagged."
|
||||||
|
|
||||||
|
# Set multiarch flag for any other arch than amd64.
|
||||||
|
if [ "${arch}" != "amd64" ]; then
|
||||||
MULTIARCH=1
|
MULTIARCH=1
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
function login_to_registry() {
|
function login_to_registry() {
|
||||||
@ -133,13 +105,13 @@ function mirror() {
|
|||||||
MULTIARCH=0
|
MULTIARCH=0
|
||||||
|
|
||||||
# We presumes that amd64 image should always be available.
|
# 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!"
|
echo "! Image mirroring failed! Cannot obtain amd64 image!"
|
||||||
|
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
get_arm64_image "${image}"
|
get_image "arm64" "${image}"
|
||||||
push_multiarch_image "${image}"
|
push_multiarch_image "${image}"
|
||||||
cleanup "${image}"
|
cleanup "${image}"
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user