From 6576bc7f78a51e174411e0e1286b9d937274f170 Mon Sep 17 00:00:00 2001 From: Darleev <50947177+Darleev@users.noreply.github.com> Date: Mon, 19 Jul 2021 09:29:23 +0100 Subject: [PATCH] [macOS] Added HTTP status code check to download_with_retries (#3716) * Disable exit on error temporary to implement retry logic based on exit code * Check HTTP response code and retry if it's not 200 * Make variables local to not interfere with other scripts Co-authored-by: Mikhail Timofeev <48208649+miketimofeev@users.noreply.github.com> --- images/macos/provision/utils/utils.sh | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/images/macos/provision/utils/utils.sh b/images/macos/provision/utils/utils.sh index e11f17e8..c4c80d0a 100755 --- a/images/macos/provision/utils/utils.sh +++ b/images/macos/provision/utils/utils.sh @@ -10,24 +10,29 @@ download_with_retries() { local COMPRESSED="$4" if [[ $COMPRESSED == "compressed" ]]; then - COMMAND="curl $URL -4 -sL --compressed -o '$DEST/$NAME'" + local COMMAND="curl $URL -4 -sL --compressed -o '$DEST/$NAME' -w '%{http_code}'" else - COMMAND="curl $URL -4 -sL -o '$DEST/$NAME'" + local COMMAND="curl $URL -4 -sL -o '$DEST/$NAME' -w '%{http_code}'" fi - echo "Downloading $URL..." + echo "Downloading '$URL' to '${DEST}/${NAME}'..." retries=20 interval=30 while [ $retries -gt 0 ]; do ((retries--)) - eval $COMMAND - if [ $? != 0 ]; then - echo "Unable to download $URL, next attempt in $interval sec, $retries attempts left" - sleep $interval - else - echo "$URL was downloaded successfully to $DEST/$NAME" + # Temporary disable exit on error to retry on non-zero exit code + set +e + http_code=$(eval $COMMAND) + exit_code=$? + if [ $http_code -eq 200 ] && [ $exit_code -eq 0 ]; then + echo "Download completed" return 0 + else + echo "Error — Either HTTP response code for '$URL' is wrong - '$http_code' or exit code is not 0 - '$exit_code'. Waiting $interval seconds before the next attempt, $retries attempts left" + sleep 30 fi + # Enable exit on error back + set -e done echo "Could not download $URL"