From be49433b4100c7efd4082dc1d7807b42d0afd537 Mon Sep 17 00:00:00 2001 From: Ricardo Pardini Date: Wed, 18 Jan 2023 00:31:31 +0100 Subject: [PATCH] armbian-next: use retries for downloading ORAS tooling - `do_with_retries()`: add `IS_A_RETRY` and `RETRY_FMT_MORE_THAN_ONCE` for convenient logging in retried functions --- lib/functions/general/oci-oras.sh | 28 +++++++++++++++++----------- lib/functions/general/retry.sh | 9 +++++++++ 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/lib/functions/general/oci-oras.sh b/lib/functions/general/oci-oras.sh index e47a2a124f..54cace5be9 100644 --- a/lib/functions/general/oci-oras.sh +++ b/lib/functions/general/oci-oras.sh @@ -42,17 +42,7 @@ function run_tool_oras() { declare ACTUAL_VERSION if [[ ! -f "${ORAS_BIN}" ]]; then - display_alert "Cache miss, downloading..." - display_alert "MACHINE: ${MACHINE}" "ORAS" "debug" - display_alert "Down URL: ${DOWN_URL}" "ORAS" "debug" - display_alert "ORAS_BIN: ${ORAS_BIN}" "ORAS" "debug" - - display_alert "Downloading required" "ORAS tooling" "info" - run_host_command_logged wget --no-verbose --progress=dot:giga -O "${ORAS_BIN}.tar.gz" "${DOWN_URL}" - run_host_command_logged tar -xf "${ORAS_BIN}.tar.gz" -C "${DIR_ORAS}" "oras" - run_host_command_logged rm -rf "${ORAS_BIN}.tar.gz" - run_host_command_logged mv -v "${DIR_ORAS}/oras" "${ORAS_BIN}" - run_host_command_logged chmod -v +x "${ORAS_BIN}" + do_with_retries 5 try_download_oras_tooling fi ACTUAL_VERSION="$("${ORAS_BIN}" version | grep "^Version" | xargs echo -n)" display_alert "Running ORAS ${ACTUAL_VERSION}" "ORAS" "debug" @@ -62,6 +52,22 @@ function run_tool_oras() { "${ORAS_BIN}" "$@" } +function try_download_oras_tooling() { + display_alert "MACHINE: ${MACHINE}" "ORAS" "debug" + display_alert "Down URL: ${DOWN_URL}" "ORAS" "debug" + display_alert "ORAS_BIN: ${ORAS_BIN}" "ORAS" "debug" + + display_alert "Downloading required" "ORAS tooling${RETRY_FMT_MORE_THAN_ONCE}" "info" + run_host_command_logged wget --no-verbose --progress=dot:giga -O "${ORAS_BIN}.tar.gz.tmp" "${DOWN_URL}" || { + return 1 + } + run_host_command_logged mv "${ORAS_BIN}.tar.gz.tmp" "${ORAS_BIN}.tar.gz" + run_host_command_logged tar -xf "${ORAS_BIN}.tar.gz" -C "${DIR_ORAS}" "oras" + run_host_command_logged rm -rf "${ORAS_BIN}.tar.gz" + run_host_command_logged mv "${DIR_ORAS}/oras" "${ORAS_BIN}" + run_host_command_logged chmod +x "${ORAS_BIN}" +} + function oras_push_artifact_file() { declare image_full_oci="${1}" # Something like "ghcr.io/rpardini/armbian-git-shallow/kernel-git:latest" declare upload_file="${2}" # Absolute path to the file to upload including the path and name diff --git a/lib/functions/general/retry.sh b/lib/functions/general/retry.sh index 544182e05b..0c3154d520 100644 --- a/lib/functions/general/retry.sh +++ b/lib/functions/general/retry.sh @@ -10,13 +10,22 @@ function do_with_retries() { while [[ $counter -lt $retries ]]; do counter=$((counter + 1)) declare -i RETRY_RUNS=${counter} + declare -i IS_A_RETRY=0 + declare RETRY_FMT_MORE_THAN_ONCE="" + if [[ ${RETRY_RUNS} -gt 1 ]]; then + IS_A_RETRY=1 + RETRY_FMT_MORE_THAN_ONCE=" (attempt ${RETRY_RUNS})" + fi + "$@" && return 0 # execute and return 0 if success; if not, let it loop; if [[ "${silent_retry}" == "yes" ]]; then : # do nothing else display_alert "Command failed, retrying in ${sleep_seconds}s" "$*" "warn" fi + unset IS_A_RETRY unset RETRY_RUNS + unset RETRY_FMT_MORE_THAN_ONCE sleep "${sleep_seconds}" done display_alert "Command failed ${counter} times, giving up" "$*" "warn"