[ALL] Enhance Docker Image Fetching Script for Clarity and Robustness
- Streamlined variable declarations for improved readability. - Added error checking to ensure successful retrieval of the image digest. - Encapsulated core functionality into 'fetch_and_copy_image' function for better script structure. - Introduced a 'lock_file' variable to clarify the file locking mechanism. - Combined 'set' options into a single line and added 'pipefail' for robust error handling. - Refined comments and structured the script into clearly defined sections.
This commit is contained in:
parent
3c29193f3f
commit
3851ed9fab
|
@ -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}"
|
||||
|
|
Loading…
Reference in New Issue