From fb90b4e46e887b73084109b1058875c6faa2d833 Mon Sep 17 00:00:00 2001 From: Manoj Kumar Date: Tue, 28 Oct 2025 15:43:54 +0530 Subject: [PATCH 1/7] Add cloud image downloader script --- tools/utils/cloud-image-downloader.sh | 169 ++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 tools/utils/cloud-image-downloader.sh diff --git a/tools/utils/cloud-image-downloader.sh b/tools/utils/cloud-image-downloader.sh new file mode 100644 index 000000000000..bf61b9302d92 --- /dev/null +++ b/tools/utils/cloud-image-downloader.sh @@ -0,0 +1,169 @@ +#!/bin/bash + +#------------------------------------------------------------------------------- +# Configuration +#------------------------------------------------------------------------------- +# This section contains the variables you might want to change. + +# The temporary directory where files will be downloaded. +# It's a good practice to create a unique temporary directory for each script run. +TEMP_DIR=$(mktemp -d) + +# The BASE destination directory for the downloaded image files. +# Subdirectories for each distro will be created inside this one. +# Make sure this directory exists before running the script. +# Must be executed by the cloudstack user on machine hosting the public download site. +# It will be publicly available at https://download.cloudstack.org/templates/cloud-images/ +DEST_DIR="~/repository/templates/cloud-images/" + +# The directory where log files will be stored. +# Make sure this directory exists. +LOG_DIR="~/log/cloud-image-downloader" +LOG_FILE="${LOG_DIR}/run_$(date +%Y%m%d_%H%M%S).log" +ERROR_LOG_FILE="${LOG_DIR}/error_$(date +%Y%m%d_%H%M%S).log" + +#------------------------------------------------------------------------------- +# Image Definitions +#------------------------------------------------------------------------------- +# To add a new image, you must add an entry to BOTH arrays below. + +# 1. Add the destination filename and the download URL. +declare -A IMAGE_URLS=( + ["Rocky-8-GenericCloud.latest.aarch64.qcow2"]="https://dl.rockylinux.org/pub/rocky/8/images/aarch64/Rocky-8-GenericCloud.latest.aarch64.qcow2" + ["Rocky-9-GenericCloud.latest.aarch64.qcow2"]="https://dl.rockylinux.org/pub/rocky/9/images/aarch64/Rocky-9-GenericCloud.latest.aarch64.qcow2" + ["Rocky-9-GenericCloud.latest.x86_64.qcow2"]="https://dl.rockylinux.org/pub/rocky/9/images/x86_64/Rocky-9-GenericCloud.latest.x86_64.qcow2" + ["openSUSE-Leap-15.5-Minimal-VM.x86_64-Cloud.qcow2"]="https://download.opensuse.org/distribution/leap/15.5/appliances/openSUSE-Leap-15.5-Minimal-VM.x86_64-Cloud.qcow2" + ["openSUSE-Leap-15.5-Minimal-VM.aarch64-Cloud.qcow2"]="https://download.opensuse.org/distribution/leap/15.5/appliances/openSUSE-Leap-15.5-Minimal-VM.aarch64-Cloud.qcow2" + ["debian-12-genericcloud-amd64.qcow2"]="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-amd64.qcow2" + ["debian-12-genericcloud-arm64.qcow2"]="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-arm64.qcow2" + ["ubuntu-24.04-server-cloudimg-amd64.img"]="https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-amd64.img" + ["ubuntu-22.04-server-cloudimg-amd64.img"]="https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64.img" + ["ubuntu-20.04-server-cloudimg-amd64.img"]="https://cloud-images.ubuntu.com/releases/20.04/release/ubuntu-20.04-server-cloudimg-amd64.img" + ["ubuntu-24.04-server-cloudimg-arm64.img"]="https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-arm64.img" + ["ubuntu-22.04-server-cloudimg-arm64.img"]="https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-arm64.img" + ["ubuntu-20.04-server-cloudimg-arm64.img"]="https://cloud-images.ubuntu.com/releases/20.04/release/ubuntu-20.04-server-cloudimg-arm64.img" + ["OL9U5_x86_64-kvm-b259.qcow2"]="https://yum.oracle.com/templates/OracleLinux/OL9/u5/x86_64/OL9U5_x86_64-kvm-b259.qcow2" + ["OL8U10_x86_64-kvm-b258.qcow2"]="https://yum.oracle.com/templates/OracleLinux/OL8/u10/x86_64/OL8U10_x86_64-kvm-b258.qcow2" + ["OL9U5_aarch64-kvm-b126.qcow2"]="https://yum.oracle.com/templates/OracleLinux/OL9/u5/aarch64/OL9U5_aarch64-kvm-b126.qcow2" + ["OL8U10_aarch64-kvm-b122.qcow2"]="https://yum.oracle.com/templates/OracleLinux/OL8/u10/aarch64/OL8U10_aarch64-kvm-b122.qcow2" + ["Rocky-8-GenericCloud.latest.x86_64.qcow2"]="https://dl.rockylinux.org/pub/rocky/8/images/x86_64/Rocky-8-GenericCloud.latest.x86_64.qcow2" +) + +# 2. Add the destination filename and its corresponding distribution subdirectory name. +declare -A IMAGE_DISTROS=( + ["Rocky-8-GenericCloud.latest.aarch64.qcow2"]="rockylinux" + ["Rocky-8-GenericCloud.latest.x86_64.qcow2"]="rockylinux" + ["Rocky-9-GenericCloud.latest.aarch64.qcow2"]="rockylinux" + ["Rocky-9-GenericCloud.latest.x86_64.qcow2"]="rockylinux" + ["openSUSE-Leap-15.5-Minimal-VM.x86_64-Cloud.qcow2"]="opensuse" + ["openSUSE-Leap-15.5-Minimal-VM.aarch64-Cloud.qcow2"]="opensuse" + ["debian-12-genericcloud-amd64.qcow2"]="debian" + ["debian-12-genericcloud-arm64.qcow2"]="debian" + ["ubuntu-24.04-server-cloudimg-amd64.img"]="ubuntu" + ["ubuntu-22.04-server-cloudimg-amd64.img"]="ubuntu" + ["ubuntu-20.04-server-cloudimg-amd64.img"]="ubuntu" + ["ubuntu-24.04-server-cloudimg-arm64.img"]="ubuntu" + ["ubuntu-22.04-server-cloudimg-arm64.img"]="ubuntu" + ["ubuntu-20.04-server-cloudimg-arm64.img"]="ubuntu" + ["OL9U5_x86_64-kvm-b259.qcow2"]="oraclelinux" + ["OL8U10_x86_64-kvm-b258.qcow2"]="oraclelinux" + ["OL9U5_aarch64-kvm-b126.qcow2"]="oraclelinux" + ["OL8U10_aarch64-kvm-b122.qcow2"]="oraclelinux" +) + + +#------------------------------------------------------------------------------- +# Main Script Logic +#------------------------------------------------------------------------------- + +# Function to log messages +log() { + echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE" +} + +# Ensure base destination and log directories exist +mkdir -p "$DEST_DIR" +mkdir -p "$LOG_DIR" + +log "Starting image download process." +log "Temporary directory: $TEMP_DIR" +log "Base destination directory: $DEST_DIR" +log "Log file: $LOG_FILE" +log "Error log file: $ERROR_LOG_FILE" + +# Loop through the image URLs +for filename in "${!IMAGE_URLS[@]}"; do + url="${IMAGE_URLS[$filename]}" + distro="${IMAGE_DISTROS[$filename]}" + + # Check if a distro is defined for the file + if [ -z "$distro" ]; then + error_message="No distribution directory defined for $filename. Skipping." + log "ERROR: $error_message" + echo "$(date +'%Y-%m-%d %H:%M:%S') - $error_message" >> "$ERROR_LOG_FILE" + continue + fi + + distro_dest_dir="${DEST_DIR}/${distro}" + temp_filepath="${TEMP_DIR}/${filename}" + dest_filepath="${distro_dest_dir}/${filename}" + + log "--------------------------------------------------" + log "Starting download for: $filename" + log "URL: $url" + + # Download the file to the temporary directory + wget --progress=bar:force:noscroll -O "$temp_filepath" "$url" + download_status=$? + + if [ $download_status -ne 0 ]; then + # Handle download failure + error_message="Failed to download $filename from $url. wget exit code: $download_status" + log "ERROR: $error_message" + echo "$(date +'%Y-%m-%d %H:%M:%S') - $error_message" >> "$ERROR_LOG_FILE" + else + # Handle download success + log "Successfully downloaded $filename to temporary location." + + # Ensure the specific distro directory exists + log "Ensuring destination directory exists: $distro_dest_dir" + mkdir -p "$distro_dest_dir" + + # Move the file to the destination directory, replacing any existing file + log "Moving $filename to $dest_filepath" + mv -f "$temp_filepath" "$dest_filepath" + move_status=$? + + if [ $move_status -ne 0 ]; then + error_message="Failed to move $filename to $dest_filepath. mv exit code: $move_status" + log "ERROR: $error_message" + echo "$(date +'%Y-%m-%d %H:%M:%S') - $error_message" >> "$ERROR_LOG_FILE" + else + log "Successfully moved $filename." + fi + fi +done + +log "Generate checksum" +# Create md5 checksum +checksum_file="md5sum.txt" +sha512_checksum_file="sha512sum.txt" + +cd "$DEST_DIR" +find . -type f ! -iname '*.txt' -exec md5sum {} \; > "$checksum_file" +find . -type f ! -iname '*.txt' -exec sha512sum {} \; > "$sha512_checksum_file" +checksum_status=$? +if [ $checksum_status -ne 0 ]; then + error_message="Failed to create md5 checksum. md5sum exit code: $checksum_status" + log "ERROR: $error_message" + echo "$(date +'%Y-%m-%d %H:%M:%S') - $error_message" >> "$ERROR_LOG_FILE" +else + log "Successfully created checksum file: $checksum_file" +fi + +log "--------------------------------------------------" +log "Image download process finished." + +# Clean up the temporary directory +rm -rf "$TEMP_DIR" +log "Temporary directory $TEMP_DIR removed." \ No newline at end of file From 582b1432d1327c8ee065e7715852a8b51b727297 Mon Sep 17 00:00:00 2001 From: Manoj Kumar Date: Tue, 28 Oct 2025 16:32:35 +0530 Subject: [PATCH 2/7] add apache license --- tools/utils/cloud-image-downloader.sh | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) mode change 100644 => 100755 tools/utils/cloud-image-downloader.sh diff --git a/tools/utils/cloud-image-downloader.sh b/tools/utils/cloud-image-downloader.sh old mode 100644 new mode 100755 index bf61b9302d92..d321330afce2 --- a/tools/utils/cloud-image-downloader.sh +++ b/tools/utils/cloud-image-downloader.sh @@ -1,4 +1,20 @@ -#!/bin/bash +#!/usr/bin/env bash +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. #------------------------------------------------------------------------------- # Configuration From 112be9b768d77215fc5c04ac72c652edc99ce418 Mon Sep 17 00:00:00 2001 From: Manoj Kumar Date: Tue, 28 Oct 2025 16:45:15 +0530 Subject: [PATCH 3/7] trim whitespace --- tools/utils/cloud-image-downloader.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/utils/cloud-image-downloader.sh b/tools/utils/cloud-image-downloader.sh index d321330afce2..0548584fbd88 100755 --- a/tools/utils/cloud-image-downloader.sh +++ b/tools/utils/cloud-image-downloader.sh @@ -111,7 +111,7 @@ log "Error log file: $ERROR_LOG_FILE" for filename in "${!IMAGE_URLS[@]}"; do url="${IMAGE_URLS[$filename]}" distro="${IMAGE_DISTROS[$filename]}" - + # Check if a distro is defined for the file if [ -z "$distro" ]; then error_message="No distribution directory defined for $filename. Skipping." @@ -140,7 +140,7 @@ for filename in "${!IMAGE_URLS[@]}"; do else # Handle download success log "Successfully downloaded $filename to temporary location." - + # Ensure the specific distro directory exists log "Ensuring destination directory exists: $distro_dest_dir" mkdir -p "$distro_dest_dir" From dfe1fb477db466a4a8893818ddc4f5fc5d22b8e4 Mon Sep 17 00:00:00 2001 From: Manoj Kumar Date: Tue, 28 Oct 2025 18:09:05 +0530 Subject: [PATCH 4/7] Update tools/utils/cloud-image-downloader.sh Co-authored-by: dahn --- tools/utils/cloud-image-downloader.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/utils/cloud-image-downloader.sh b/tools/utils/cloud-image-downloader.sh index 0548584fbd88..d45848fc4c4b 100755 --- a/tools/utils/cloud-image-downloader.sh +++ b/tools/utils/cloud-image-downloader.sh @@ -182,4 +182,4 @@ log "Image download process finished." # Clean up the temporary directory rm -rf "$TEMP_DIR" -log "Temporary directory $TEMP_DIR removed." \ No newline at end of file +log "Temporary directory $TEMP_DIR removed.” From 6a98dd4503abddd2120c5ffee1af3f76cca6176c Mon Sep 17 00:00:00 2001 From: Manoj Kumar Date: Tue, 28 Oct 2025 22:38:06 +0530 Subject: [PATCH 5/7] use logger utility, cleanup logs and use single log file for info and error --- tools/utils/cloud-image-downloader.sh | 147 +++++++++++++++++++------- 1 file changed, 107 insertions(+), 40 deletions(-) diff --git a/tools/utils/cloud-image-downloader.sh b/tools/utils/cloud-image-downloader.sh index d45848fc4c4b..1733d21b2057 100755 --- a/tools/utils/cloud-image-downloader.sh +++ b/tools/utils/cloud-image-downloader.sh @@ -30,13 +30,67 @@ TEMP_DIR=$(mktemp -d) # Make sure this directory exists before running the script. # Must be executed by the cloudstack user on machine hosting the public download site. # It will be publicly available at https://download.cloudstack.org/templates/cloud-images/ -DEST_DIR="~/repository/templates/cloud-images/" +DEST_DIR="${HOME}/repository/templates/cloud-images" # The directory where log files will be stored. # Make sure this directory exists. -LOG_DIR="~/log/cloud-image-downloader" -LOG_FILE="${LOG_DIR}/run_$(date +%Y%m%d_%H%M%S).log" -ERROR_LOG_FILE="${LOG_DIR}/error_$(date +%Y%m%d_%H%M%S).log" +LOG_DIR="${HOME}/log/cloud-image-downloader" +LOG_FILE="${LOG_DIR}/cloud-image-downloader_$(date +%Y%m%d_%H%M%S).log" +LOG_RETENTION_DAYS=30 + +LOGGER_TAG="cloud-image-downloader" +LOGGER_FACILITY="user" + +log_message() { + local priority=$1 + shift + local message="$*" + local timestamp=$(date +'%Y-%m-%d %H:%M:%S') + + # Log to file + echo "${timestamp} [${priority}] ${message}" >> "$LOG_FILE" + + # Log to console + echo "${timestamp} [${priority}] ${message}" + + # Log to syslog using logger utility + logger -t "${LOGGER_TAG}" -p "${LOGGER_FACILITY}.${priority}" -- "${message}" +} + +log_info() { + log_message "info" "$@" +} + +log_warn() { + log_message "warning" "$@" +} + +log_error() { + log_message "err" "$@" +} + +cleanup_old_logs() { + log_info "Cleaning up log files older than ${LOG_RETENTION_DAYS} days..." + + if [ ! -d "$LOG_DIR" ]; then + log_warn "Log directory does not exist: $LOG_DIR" + return + fi + + local deleted_count=0 + + # Find and delete log files older than retention period + while IFS= read -r -d '' log_file; do + rm -f "$log_file" + deleted_count=$((deleted_count + 1)) + done < <(find "$LOG_DIR" -name "*.log" -type f -mtime +${LOG_RETENTION_DAYS} -print0 2>/dev/null) + + if [ $deleted_count -gt 0 ]; then + log_info "Deleted $deleted_count old log file(s)" + else + log_info "No old log files to delete" + fi +} #------------------------------------------------------------------------------- # Image Definitions @@ -87,25 +141,43 @@ declare -A IMAGE_DISTROS=( ["OL8U10_aarch64-kvm-b122.qcow2"]="oraclelinux" ) +#------------------------------------------------------------------------------- +# Cleanup Handler +#------------------------------------------------------------------------------- + +cleanup_on_exit() { + local exit_code=$? + if [ -d "$TEMP_DIR" ]; then + rm -rf "$TEMP_DIR" + log_info "Temporary directory $TEMP_DIR removed." + fi + + if [ $exit_code -ne 0 ]; then + log_error "Script exited with error code: $exit_code" + fi +} + +trap cleanup_on_exit EXIT INT TER #------------------------------------------------------------------------------- # Main Script Logic #------------------------------------------------------------------------------- -# Function to log messages -log() { - echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE" -} +if ! command -v logger &> /dev/null; then + log_warn "logger utility not found - syslog logging disabled" +fi # Ensure base destination and log directories exist mkdir -p "$DEST_DIR" mkdir -p "$LOG_DIR" -log "Starting image download process." -log "Temporary directory: $TEMP_DIR" -log "Base destination directory: $DEST_DIR" -log "Log file: $LOG_FILE" -log "Error log file: $ERROR_LOG_FILE" +# Clean up old logs first +cleanup_old_logs + +log_info "Starting image download process." +log_info "Temporary directory: $TEMP_DIR" +log_info "Base destination directory: $DEST_DIR" +log_info "Log file: $LOG_FILE" # Loop through the image URLs for filename in "${!IMAGE_URLS[@]}"; do @@ -114,9 +186,7 @@ for filename in "${!IMAGE_URLS[@]}"; do # Check if a distro is defined for the file if [ -z "$distro" ]; then - error_message="No distribution directory defined for $filename. Skipping." - log "ERROR: $error_message" - echo "$(date +'%Y-%m-%d %H:%M:%S') - $error_message" >> "$ERROR_LOG_FILE" + log_error "No distribution directory defined for $filename. Skipping." continue fi @@ -124,9 +194,9 @@ for filename in "${!IMAGE_URLS[@]}"; do temp_filepath="${TEMP_DIR}/${filename}" dest_filepath="${distro_dest_dir}/${filename}" - log "--------------------------------------------------" - log "Starting download for: $filename" - log "URL: $url" + log_info "--------------------------------------------------" + log_info "Starting download for: $filename" + log_info "URL: $url" # Download the file to the temporary directory wget --progress=bar:force:noscroll -O "$temp_filepath" "$url" @@ -134,52 +204,49 @@ for filename in "${!IMAGE_URLS[@]}"; do if [ $download_status -ne 0 ]; then # Handle download failure - error_message="Failed to download $filename from $url. wget exit code: $download_status" - log "ERROR: $error_message" - echo "$(date +'%Y-%m-%d %H:%M:%S') - $error_message" >> "$ERROR_LOG_FILE" + log_error "Failed to download $filename from $url. wget exit code: $download_status" else # Handle download success - log "Successfully downloaded $filename to temporary location." + log_info "Successfully downloaded $filename to temporary location." # Ensure the specific distro directory exists - log "Ensuring destination directory exists: $distro_dest_dir" + log_info "Ensuring destination directory exists: $distro_dest_dir" mkdir -p "$distro_dest_dir" # Move the file to the destination directory, replacing any existing file - log "Moving $filename to $dest_filepath" + log_info "Moving $filename to $dest_filepath" mv -f "$temp_filepath" "$dest_filepath" move_status=$? if [ $move_status -ne 0 ]; then - error_message="Failed to move $filename to $dest_filepath. mv exit code: $move_status" - log "ERROR: $error_message" - echo "$(date +'%Y-%m-%d %H:%M:%S') - $error_message" >> "$ERROR_LOG_FILE" + log_error "Failed to move $filename to $dest_filepath. mv exit code: $move_status" else - log "Successfully moved $filename." + log_info "Successfully moved $filename." fi fi done -log "Generate checksum" +log_info "Generate checksum" # Create md5 checksum checksum_file="md5sum.txt" sha512_checksum_file="sha512sum.txt" cd "$DEST_DIR" find . -type f ! -iname '*.txt' -exec md5sum {} \; > "$checksum_file" -find . -type f ! -iname '*.txt' -exec sha512sum {} \; > "$sha512_checksum_file" checksum_status=$? if [ $checksum_status -ne 0 ]; then - error_message="Failed to create md5 checksum. md5sum exit code: $checksum_status" - log "ERROR: $error_message" - echo "$(date +'%Y-%m-%d %H:%M:%S') - $error_message" >> "$ERROR_LOG_FILE" + log_error "Failed to create md5 checksum. md5sum exit code: $checksum_status" else - log "Successfully created checksum file: $checksum_file" + log_info "Successfully created checksum file: $checksum_file" fi -log "--------------------------------------------------" -log "Image download process finished." +find . -type f ! -iname '*.txt' -exec sha512sum {} \; > "$sha512_checksum_file" +sha512_checksum_status=$? +if [ $sha512_checksum_status -ne 0 ]; then + log_error "Failed to create sha512 checksum. sha512sum exit code: $sha512_checksum_status" +else + log_info "Successfully created checksum file: $sha512_checksum_file" +fi -# Clean up the temporary directory -rm -rf "$TEMP_DIR" -log "Temporary directory $TEMP_DIR removed.” +log_info "--------------------------------------------------" +log_info "Image download process finished." From 4c806143c05c4f0a8cf6b1a0b6c116491b51b00d Mon Sep 17 00:00:00 2001 From: Manoj Kumar Date: Wed, 29 Oct 2025 15:27:26 +0530 Subject: [PATCH 6/7] Update tools/utils/cloud-image-downloader.sh Co-authored-by: dahn --- tools/utils/cloud-image-downloader.sh | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tools/utils/cloud-image-downloader.sh b/tools/utils/cloud-image-downloader.sh index 1733d21b2057..c02578203139 100755 --- a/tools/utils/cloud-image-downloader.sh +++ b/tools/utils/cloud-image-downloader.sh @@ -48,10 +48,7 @@ log_message() { local timestamp=$(date +'%Y-%m-%d %H:%M:%S') # Log to file - echo "${timestamp} [${priority}] ${message}" >> "$LOG_FILE" - - # Log to console - echo "${timestamp} [${priority}] ${message}" + echo "${timestamp} [${priority}] ${message}” | tee ${LOG_FILE} # Log to syslog using logger utility logger -t "${LOGGER_TAG}" -p "${LOGGER_FACILITY}.${priority}" -- "${message}" From ccbeca527b980b421ecf8eb6216a6b3bb43f52b0 Mon Sep 17 00:00:00 2001 From: Manoj Kumar Date: Wed, 29 Oct 2025 15:49:47 +0530 Subject: [PATCH 7/7] fix missing conditional block --- tools/utils/cloud-image-downloader.sh | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/tools/utils/cloud-image-downloader.sh b/tools/utils/cloud-image-downloader.sh index c02578203139..c419bba789fd 100755 --- a/tools/utils/cloud-image-downloader.sh +++ b/tools/utils/cloud-image-downloader.sh @@ -40,6 +40,7 @@ LOG_RETENTION_DAYS=30 LOGGER_TAG="cloud-image-downloader" LOGGER_FACILITY="user" +LOGGER_AVAILABLE=false log_message() { local priority=$1 @@ -48,10 +49,12 @@ log_message() { local timestamp=$(date +'%Y-%m-%d %H:%M:%S') # Log to file - echo "${timestamp} [${priority}] ${message}” | tee ${LOG_FILE} + echo "${timestamp} [${priority}] ${message}" | tee -a "${LOG_FILE}" # Log to syslog using logger utility - logger -t "${LOGGER_TAG}" -p "${LOGGER_FACILITY}.${priority}" -- "${message}" + if [ "${LOGGER_AVAILABLE}" = true ]; then + logger -t "${LOGGER_TAG}" -p "${LOGGER_FACILITY}.${priority}" -- "${message}" + fi } log_info() { @@ -154,14 +157,14 @@ cleanup_on_exit() { fi } -trap cleanup_on_exit EXIT INT TER +trap cleanup_on_exit EXIT INT TERM #------------------------------------------------------------------------------- # Main Script Logic #------------------------------------------------------------------------------- -if ! command -v logger &> /dev/null; then - log_warn "logger utility not found - syslog logging disabled" +if command -v logger &> /dev/null; then + LOGGER_AVAILABLE=true fi # Ensure base destination and log directories exist @@ -176,6 +179,13 @@ log_info "Temporary directory: $TEMP_DIR" log_info "Base destination directory: $DEST_DIR" log_info "Log file: $LOG_FILE" +# Inform about logger status +if [ "${LOGGER_AVAILABLE}" = true ]; then + log_info "Syslog logging enabled (tag: ${LOGGER_TAG})" +else + log_warn "Syslog logging disabled - logger utility not found" +fi + # Loop through the image URLs for filename in "${!IMAGE_URLS[@]}"; do url="${IMAGE_URLS[$filename]}"