@@ -30,13 +30,67 @@ TEMP_DIR=$(mktemp -d)
3030# Make sure this directory exists before running the script.
3131# Must be executed by the cloudstack user on machine hosting the public download site.
3232# It will be publicly available at https://download.cloudstack.org/templates/cloud-images/
33- DEST_DIR=" ~ /repository/templates/cloud-images/ "
33+ DEST_DIR=" ${HOME} /repository/templates/cloud-images"
3434
3535# The directory where log files will be stored.
3636# Make sure this directory exists.
37- LOG_DIR=" ~/log/cloud-image-downloader"
38- LOG_FILE=" ${LOG_DIR} /run_$( date +%Y%m%d_%H%M%S) .log"
39- ERROR_LOG_FILE=" ${LOG_DIR} /error_$( date +%Y%m%d_%H%M%S) .log"
37+ LOG_DIR=" ${HOME} /log/cloud-image-downloader"
38+ LOG_FILE=" ${LOG_DIR} /cloud-image-downloader_$( date +%Y%m%d_%H%M%S) .log"
39+ LOG_RETENTION_DAYS=30
40+
41+ LOGGER_TAG=" cloud-image-downloader"
42+ LOGGER_FACILITY=" user"
43+
44+ log_message () {
45+ local priority=$1
46+ shift
47+ local message=" $* "
48+ local timestamp=$( date +' %Y-%m-%d %H:%M:%S' )
49+
50+ # Log to file
51+ echo " ${timestamp} [${priority} ] ${message} " >> " $LOG_FILE "
52+
53+ # Log to console
54+ echo " ${timestamp} [${priority} ] ${message} "
55+
56+ # Log to syslog using logger utility
57+ logger -t " ${LOGGER_TAG} " -p " ${LOGGER_FACILITY} .${priority} " -- " ${message} "
58+ }
59+
60+ log_info () {
61+ log_message " info" " $@ "
62+ }
63+
64+ log_warn () {
65+ log_message " warning" " $@ "
66+ }
67+
68+ log_error () {
69+ log_message " err" " $@ "
70+ }
71+
72+ cleanup_old_logs () {
73+ log_info " Cleaning up log files older than ${LOG_RETENTION_DAYS} days..."
74+
75+ if [ ! -d " $LOG_DIR " ]; then
76+ log_warn " Log directory does not exist: $LOG_DIR "
77+ return
78+ fi
79+
80+ local deleted_count=0
81+
82+ # Find and delete log files older than retention period
83+ while IFS= read -r -d ' ' log_file; do
84+ rm -f " $log_file "
85+ deleted_count=$(( deleted_count + 1 ))
86+ done < <( find " $LOG_DIR " -name " *.log" -type f -mtime +${LOG_RETENTION_DAYS} -print0 2> /dev/null)
87+
88+ if [ $deleted_count -gt 0 ]; then
89+ log_info " Deleted $deleted_count old log file(s)"
90+ else
91+ log_info " No old log files to delete"
92+ fi
93+ }
4094
4195# -------------------------------------------------------------------------------
4296# Image Definitions
@@ -87,25 +141,43 @@ declare -A IMAGE_DISTROS=(
87141 [" OL8U10_aarch64-kvm-b122.qcow2" ]=" oraclelinux"
88142)
89143
144+ # -------------------------------------------------------------------------------
145+ # Cleanup Handler
146+ # -------------------------------------------------------------------------------
147+
148+ cleanup_on_exit () {
149+ local exit_code=$?
150+ if [ -d " $TEMP_DIR " ]; then
151+ rm -rf " $TEMP_DIR "
152+ log_info " Temporary directory $TEMP_DIR removed."
153+ fi
154+
155+ if [ $exit_code -ne 0 ]; then
156+ log_error " Script exited with error code: $exit_code "
157+ fi
158+ }
159+
160+ trap cleanup_on_exit EXIT INT TER
90161
91162# -------------------------------------------------------------------------------
92163# Main Script Logic
93164# -------------------------------------------------------------------------------
94165
95- # Function to log messages
96- log () {
97- echo " $( date +' %Y-%m-%d %H:%M:%S' ) - $1 " | tee -a " $LOG_FILE "
98- }
166+ if ! command -v logger & > /dev/null; then
167+ log_warn " logger utility not found - syslog logging disabled"
168+ fi
99169
100170# Ensure base destination and log directories exist
101171mkdir -p " $DEST_DIR "
102172mkdir -p " $LOG_DIR "
103173
104- log " Starting image download process."
105- log " Temporary directory: $TEMP_DIR "
106- log " Base destination directory: $DEST_DIR "
107- log " Log file: $LOG_FILE "
108- log " Error log file: $ERROR_LOG_FILE "
174+ # Clean up old logs first
175+ cleanup_old_logs
176+
177+ log_info " Starting image download process."
178+ log_info " Temporary directory: $TEMP_DIR "
179+ log_info " Base destination directory: $DEST_DIR "
180+ log_info " Log file: $LOG_FILE "
109181
110182# Loop through the image URLs
111183for filename in " ${! IMAGE_URLS[@]} " ; do
@@ -114,72 +186,67 @@ for filename in "${!IMAGE_URLS[@]}"; do
114186
115187 # Check if a distro is defined for the file
116188 if [ -z " $distro " ]; then
117- error_message=" No distribution directory defined for $filename . Skipping."
118- log " ERROR: $error_message "
119- echo " $( date +' %Y-%m-%d %H:%M:%S' ) - $error_message " >> " $ERROR_LOG_FILE "
189+ log_error " No distribution directory defined for $filename . Skipping."
120190 continue
121191 fi
122192
123193 distro_dest_dir=" ${DEST_DIR} /${distro} "
124194 temp_filepath=" ${TEMP_DIR} /${filename} "
125195 dest_filepath=" ${distro_dest_dir} /${filename} "
126196
127- log " --------------------------------------------------"
128- log " Starting download for: $filename "
129- log " URL: $url "
197+ log_info " --------------------------------------------------"
198+ log_info " Starting download for: $filename "
199+ log_info " URL: $url "
130200
131201 # Download the file to the temporary directory
132202 wget --progress=bar:force:noscroll -O " $temp_filepath " " $url "
133203 download_status=$?
134204
135205 if [ $download_status -ne 0 ]; then
136206 # Handle download failure
137- error_message=" Failed to download $filename from $url . wget exit code: $download_status "
138- log " ERROR: $error_message "
139- echo " $( date +' %Y-%m-%d %H:%M:%S' ) - $error_message " >> " $ERROR_LOG_FILE "
207+ log_error " Failed to download $filename from $url . wget exit code: $download_status "
140208 else
141209 # Handle download success
142- log " Successfully downloaded $filename to temporary location."
210+ log_info " Successfully downloaded $filename to temporary location."
143211
144212 # Ensure the specific distro directory exists
145- log " Ensuring destination directory exists: $distro_dest_dir "
213+ log_info " Ensuring destination directory exists: $distro_dest_dir "
146214 mkdir -p " $distro_dest_dir "
147215
148216 # Move the file to the destination directory, replacing any existing file
149- log " Moving $filename to $dest_filepath "
217+ log_info " Moving $filename to $dest_filepath "
150218 mv -f " $temp_filepath " " $dest_filepath "
151219 move_status=$?
152220
153221 if [ $move_status -ne 0 ]; then
154- error_message=" Failed to move $filename to $dest_filepath . mv exit code: $move_status "
155- log " ERROR: $error_message "
156- echo " $( date +' %Y-%m-%d %H:%M:%S' ) - $error_message " >> " $ERROR_LOG_FILE "
222+ log_error " Failed to move $filename to $dest_filepath . mv exit code: $move_status "
157223 else
158- log " Successfully moved $filename ."
224+ log_info " Successfully moved $filename ."
159225 fi
160226 fi
161227done
162228
163- log " Generate checksum"
229+ log_info " Generate checksum"
164230# Create md5 checksum
165231checksum_file=" md5sum.txt"
166232sha512_checksum_file=" sha512sum.txt"
167233
168234cd " $DEST_DIR "
169235find . -type f ! -iname ' *.txt' -exec md5sum {} \; > " $checksum_file "
170- find . -type f ! -iname ' *.txt' -exec sha512sum {} \; > " $sha512_checksum_file "
171236checksum_status=$?
172237if [ $checksum_status -ne 0 ]; then
173- error_message=" Failed to create md5 checksum. md5sum exit code: $checksum_status "
174- log " ERROR: $error_message "
175- echo " $( date +' %Y-%m-%d %H:%M:%S' ) - $error_message " >> " $ERROR_LOG_FILE "
238+ log_error " Failed to create md5 checksum. md5sum exit code: $checksum_status "
176239else
177- log " Successfully created checksum file: $checksum_file "
240+ log_info " Successfully created checksum file: $checksum_file "
178241fi
179242
180- log " --------------------------------------------------"
181- log " Image download process finished."
243+ find . -type f ! -iname ' *.txt' -exec sha512sum {} \; > " $sha512_checksum_file "
244+ sha512_checksum_status=$?
245+ if [ $sha512_checksum_status -ne 0 ]; then
246+ log_error " Failed to create sha512 checksum. sha512sum exit code: $sha512_checksum_status "
247+ else
248+ log_info " Successfully created checksum file: $sha512_checksum_file "
249+ fi
182250
183- # Clean up the temporary directory
184- rm -rf " $TEMP_DIR "
185- log " Temporary directory $TEMP_DIR removed.”
251+ log_info " --------------------------------------------------"
252+ log_info " Image download process finished."
0 commit comments