From 7242d4004b55aeb3a0e4e47df8456c7b149c8fb3 Mon Sep 17 00:00:00 2001 From: "Stanislav N. aka pztrn" Date: Sun, 1 Jan 2023 22:52:55 +0500 Subject: [PATCH] Improved exit code logic for functions. --- mirror.sh | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/mirror.sh b/mirror.sh index c598643..3a201c9 100755 --- a/mirror.sh +++ b/mirror.sh @@ -56,6 +56,9 @@ function get_image() { # 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!" + + # This means that no mirrorring for arch-specific image is required. + return 2 fi echo -ne "\t* Getting ${arch} layers... " @@ -108,9 +111,11 @@ function mirror() { # We presumes that amd64 image should always be available. if ! get_image "${image}" "amd64"; then + exitcode=$? + echo "! Image mirroring failed! Cannot obtain amd64 image!" - return 1 + return ${exitcode} fi get_image "${image}" "arm64" @@ -121,7 +126,7 @@ function mirror() { function push_multiarch_image() { local image=$1 - if ! docker push -f "${REMOTE_IMAGE}"-amd64 &> /tmp/push-amd64; then + if ! docker push "${REMOTE_IMAGE}"-amd64 &> /tmp/push-amd64; then echo -e "\t! amd64 image push failed!" echo -e "\nLogs:\n\n" && cat /tmp/push-amd64 && echo -e "\n" @@ -129,7 +134,7 @@ function push_multiarch_image() { echo -e "\t* amd64 image pushed" fi - if ! docker push -f ="${REMOTE_IMAGE}"-arm64 &> /tmp/push-arm64; then + if ! docker push "${REMOTE_IMAGE}"-arm64 &> /tmp/push-arm64; then echo -e "\t! arm64 image push failed!" echo -e "\nLogs:\n\n" && cat /tmp/push-arm64 && echo -e "\n" @@ -167,8 +172,13 @@ collect_images for package in "${WHAT_TO_MIRROR[@]}"; do if ! mirror "${package}"; then - echo "! Failed to mirror package ${package}!" + exitcode=$? - exit 1 + echo "! Failed to mirror package ${package}! Exit code: ${exitcode} (1 is fatal)." + + # Shit happens only for exitcode 1. + if [ ${exitcode} -eq 1 ]; then + exit 1 + fi fi done