From 4930c5dc704df36a223b64915466f7b585f2aead Mon Sep 17 00:00:00 2001 From: thetechprepper Date: Wed, 20 Aug 2025 20:02:39 -0700 Subject: [PATCH] Post R5 Build 4 fixes - Added support for retry asset downloader --- overlay/opt/emcomm-tools/bin/et-common | 69 +++++++++++++++++++++++++- scripts/install-et-api.sh | 15 ++++-- 2 files changed, 79 insertions(+), 5 deletions(-) diff --git a/overlay/opt/emcomm-tools/bin/et-common b/overlay/opt/emcomm-tools/bin/et-common index bf45cf8..bc781dd 100755 --- a/overlay/opt/emcomm-tools/bin/et-common +++ b/overlay/opt/emcomm-tools/bin/et-common @@ -1,7 +1,7 @@ #!/bin/bash # Author : Gaston Gonzalez # Date : 1 February 2025 -# Updated : 7 June 2025 +# Updated : 20 August 2025 # Purpose : Common environment variables and functions # Standardized device paths for supported hardware @@ -74,3 +74,70 @@ start_and_wait_for_service() { exit_on_service_failure ${systemd_unit_name} } + +# download_with_retries downloads a file from a URL with retry and checksum support. +# +# This function attempts to download a file up to a maximum number of retries. +# If a checksum is provided, the downloaded file will be verified against it. +# On checksum mismatch, the file is deleted and the download retried. +# +# Args: +# url: The URL of the file to download. (required) +# filename: The local filename to save the file as. (required) +# checksum: Optional sha256 checksum string to validate the downloaded file. +# max_retries: Optional max number of retries (default: 3). +# wait_seconds: Optional wait time in seconds between retries (default: 2). +# +# Returns: +# 0 if the file was successfully downloaded (and checksum verified if given). +# 1 if arguments are missing, download fails after all retries, +# or checksum verification fails. +# +# Example: +# download_with_retries "https://example.com/file.tar.gz" "file.tar.gz" +# download_with_retries "https://example.com/file.tar.gz" "file.tar.gz" "abc123..." 5 3 +download_with_retries() { + local url="$1" + local filename="$2" + local checksum="$3" # expected checksum (sha256) + local max_retries="${4:-3}" # default = 3 retries + local wait_seconds="${5:-2}" # default = 2s wait between retries + + # Require at least URL and filename + if [[ -z "$url" || -z "$filename" ]]; then + echo "Usage: download_with_retries [checksum] [max_retries] [wait_seconds]" + return 1 + fi + + for ((i=1; i<=max_retries; i++)); do + echo "Attempt $i of $max_retries to download $url..." + + if curl -L -f -o "$filename" "$url"; then + echo "Download successful: $filename" + + if [[ -n "$checksum" ]]; then + echo "Verifying checksum..." + local file_checksum + file_checksum=$(sha256sum "$filename" | awk '{print $1}') + + if [[ "$file_checksum" == "$checksum" ]]; then + echo "Checksum OK" + return 0 + else + echo "Checksum mismatch (expected $checksum, got $file_checksum)" + rm -f "$filename" + fi + else + # no checksum provided, just succeed + return 0 + fi + else + echo "Download failed (attempt $i)." + fi + + sleep "$wait_seconds" + done + + echo "Download failed after $max_retries attempts." + return 1 +} diff --git a/scripts/install-et-api.sh b/scripts/install-et-api.sh index 29d517d..5ea12fb 100755 --- a/scripts/install-et-api.sh +++ b/scripts/install-et-api.sh @@ -7,6 +7,7 @@ trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG trap 'et-log "\"${last_command}\" command failed with exit code $?."' ERR . ./env.sh +. ../overlay/opt/emcomm-tools/bin/et-common APP=et-api VERSION=1.0.0 @@ -24,17 +25,23 @@ mkdir -v -p "${BIN_DIR}" "${DATA_DIR}" "${INDEX_DIR}" # Download and install et-api uber JAR URL="${BASE_URL}/${ET_API_JAR}" -et-log "Downloading ${APP}: ${URL}" -curl -s -L -o "${BIN_DIR}/${APP}" --fail ${URL} +download_with_retries ${URL} ${APP} +mv ${APP} ${BIN_DIR} chmod 755 "${BIN_DIR}/${APP}" # Download pre-built data sets -FILES=("faa.csv" "license.csv" "zip2geo.csv" "zip2geo-elevation.csv") +FILES=( + "faa.csv" + "license.csv" + "zip2geo.csv" + "zip2geo-elevation.csv" +) for file in "${FILES[@]}"; do URL="${BASE_URL}/${file}" et-log "Downloading data set: ${URL}" - curl -s -L -o "${DATA_DIR}/${file}" --fail ${URL} + download_with_retries ${URL} ${file} + mv ${file} ${DATA_DIR} done et-log "Applying permissions..."