diff --git a/buildroot-external/package/ovos-containers/fetch-container-image.sh b/buildroot-external/package/ovos-containers/fetch-container-image.sh index 1a65a9c0..1fffcab1 100755 --- a/buildroot-external/package/ovos-containers/fetch-container-image.sh +++ b/buildroot-external/package/ovos-containers/fetch-container-image.sh @@ -1,28 +1,33 @@ #!/usr/bin/env bash + # Original script from Home Assistant -set -e -set -u -set -o pipefail - -arch=$1 -image=$2 -dl_dir=$3 -dst_dir=$4 +set -euo pipefail +# Variables +arch="$1" +image="$2" +dl_dir="$3" +dst_dir="$4" image_name="docker.io/smartgic/${image}" full_image_name="${image_name}:alpha" -image_digest=$(skopeo --override-arch "${arch}" inspect --retry-times=5 "docker://${full_image_name}" | jq -r '.Digest') +# Fetch image digest +image_digest=$(skopeo --override-arch "${arch}" inspect --retry-times=5 "docker://${full_image_name}" | jq -r '.Digest') +if [ -z "${image_digest}" ]; then + echo "Failed to fetch digest for ${full_image_name}" + exit 1 +fi + +# Prepare file paths image_file_name="${full_image_name//[:\/]/_}@${image_digest//[:\/]/_}" image_file_path="${dl_dir}/${image_file_name}.tar" dst_image_file_path="${dst_dir}/${image_file_name}.tar" +lock_file="${image_file_path}.lock" -( - # Use file locking to avoid race condition - flock --verbose 3 - if [ ! -f "${image_file_path}" ] - then +# Function to fetch and copy image +fetch_and_copy_image() { + if [ ! -f "${image_file_path}" ]; then echo "Fetching image: ${full_image_name} (digest ${image_digest})" skopeo --override-arch "${arch}" copy "docker://${image_name}@${image_digest}" "docker-archive:${image_file_path}:${full_image_name}" else @@ -30,4 +35,10 @@ dst_image_file_path="${dst_dir}/${image_file_name}.tar" fi cp "${image_file_path}" "${dst_image_file_path}" -) 3>"${image_file_path}.lock" +} + +# Main execution +{ + flock --verbose 3 + fetch_and_copy_image +} 3>"${lock_file}"