1+ #! /bin/bash
2+
3+ # -------------------------------------------------------------------------------
4+ # Configuration
5+ # -------------------------------------------------------------------------------
6+ # This section contains the variables you might want to change.
7+
8+ # The temporary directory where files will be downloaded.
9+ # It's a good practice to create a unique temporary directory for each script run.
10+ TEMP_DIR=$( mktemp -d)
11+
12+ # The BASE destination directory for the downloaded image files.
13+ # Subdirectories for each distro will be created inside this one.
14+ # Make sure this directory exists before running the script.
15+ # Must be executed by the cloudstack user on machine hosting the public download site.
16+ # It will be publicly available at https://download.cloudstack.org/templates/cloud-images/
17+ DEST_DIR=" ~/repository/templates/cloud-images/"
18+
19+ # The directory where log files will be stored.
20+ # Make sure this directory exists.
21+ LOG_DIR=" ~/log/cloud-image-downloader"
22+ LOG_FILE=" ${LOG_DIR} /run_$( date +%Y%m%d_%H%M%S) .log"
23+ ERROR_LOG_FILE=" ${LOG_DIR} /error_$( date +%Y%m%d_%H%M%S) .log"
24+
25+ # -------------------------------------------------------------------------------
26+ # Image Definitions
27+ # -------------------------------------------------------------------------------
28+ # To add a new image, you must add an entry to BOTH arrays below.
29+
30+ # 1. Add the destination filename and the download URL.
31+ declare -A IMAGE_URLS=(
32+ [" Rocky-8-GenericCloud.latest.aarch64.qcow2" ]=" https://dl.rockylinux.org/pub/rocky/8/images/aarch64/Rocky-8-GenericCloud.latest.aarch64.qcow2"
33+ [" Rocky-9-GenericCloud.latest.aarch64.qcow2" ]=" https://dl.rockylinux.org/pub/rocky/9/images/aarch64/Rocky-9-GenericCloud.latest.aarch64.qcow2"
34+ [" Rocky-9-GenericCloud.latest.x86_64.qcow2" ]=" https://dl.rockylinux.org/pub/rocky/9/images/x86_64/Rocky-9-GenericCloud.latest.x86_64.qcow2"
35+ [" 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"
36+ [" 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"
37+ [" debian-12-genericcloud-amd64.qcow2" ]=" https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-amd64.qcow2"
38+ [" debian-12-genericcloud-arm64.qcow2" ]=" https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-arm64.qcow2"
39+ [" ubuntu-24.04-server-cloudimg-amd64.img" ]=" https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-amd64.img"
40+ [" ubuntu-22.04-server-cloudimg-amd64.img" ]=" https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64.img"
41+ [" ubuntu-20.04-server-cloudimg-amd64.img" ]=" https://cloud-images.ubuntu.com/releases/20.04/release/ubuntu-20.04-server-cloudimg-amd64.img"
42+ [" ubuntu-24.04-server-cloudimg-arm64.img" ]=" https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-arm64.img"
43+ [" ubuntu-22.04-server-cloudimg-arm64.img" ]=" https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-arm64.img"
44+ [" ubuntu-20.04-server-cloudimg-arm64.img" ]=" https://cloud-images.ubuntu.com/releases/20.04/release/ubuntu-20.04-server-cloudimg-arm64.img"
45+ [" OL9U5_x86_64-kvm-b259.qcow2" ]=" https://yum.oracle.com/templates/OracleLinux/OL9/u5/x86_64/OL9U5_x86_64-kvm-b259.qcow2"
46+ [" OL8U10_x86_64-kvm-b258.qcow2" ]=" https://yum.oracle.com/templates/OracleLinux/OL8/u10/x86_64/OL8U10_x86_64-kvm-b258.qcow2"
47+ [" OL9U5_aarch64-kvm-b126.qcow2" ]=" https://yum.oracle.com/templates/OracleLinux/OL9/u5/aarch64/OL9U5_aarch64-kvm-b126.qcow2"
48+ [" OL8U10_aarch64-kvm-b122.qcow2" ]=" https://yum.oracle.com/templates/OracleLinux/OL8/u10/aarch64/OL8U10_aarch64-kvm-b122.qcow2"
49+ [" Rocky-8-GenericCloud.latest.x86_64.qcow2" ]=" https://dl.rockylinux.org/pub/rocky/8/images/x86_64/Rocky-8-GenericCloud.latest.x86_64.qcow2"
50+ )
51+
52+ # 2. Add the destination filename and its corresponding distribution subdirectory name.
53+ declare -A IMAGE_DISTROS=(
54+ [" Rocky-8-GenericCloud.latest.aarch64.qcow2" ]=" rockylinux"
55+ [" Rocky-8-GenericCloud.latest.x86_64.qcow2" ]=" rockylinux"
56+ [" Rocky-9-GenericCloud.latest.aarch64.qcow2" ]=" rockylinux"
57+ [" Rocky-9-GenericCloud.latest.x86_64.qcow2" ]=" rockylinux"
58+ [" openSUSE-Leap-15.5-Minimal-VM.x86_64-Cloud.qcow2" ]=" opensuse"
59+ [" openSUSE-Leap-15.5-Minimal-VM.aarch64-Cloud.qcow2" ]=" opensuse"
60+ [" debian-12-genericcloud-amd64.qcow2" ]=" debian"
61+ [" debian-12-genericcloud-arm64.qcow2" ]=" debian"
62+ [" ubuntu-24.04-server-cloudimg-amd64.img" ]=" ubuntu"
63+ [" ubuntu-22.04-server-cloudimg-amd64.img" ]=" ubuntu"
64+ [" ubuntu-20.04-server-cloudimg-amd64.img" ]=" ubuntu"
65+ [" ubuntu-24.04-server-cloudimg-arm64.img" ]=" ubuntu"
66+ [" ubuntu-22.04-server-cloudimg-arm64.img" ]=" ubuntu"
67+ [" ubuntu-20.04-server-cloudimg-arm64.img" ]=" ubuntu"
68+ [" OL9U5_x86_64-kvm-b259.qcow2" ]=" oraclelinux"
69+ [" OL8U10_x86_64-kvm-b258.qcow2" ]=" oraclelinux"
70+ [" OL9U5_aarch64-kvm-b126.qcow2" ]=" oraclelinux"
71+ [" OL8U10_aarch64-kvm-b122.qcow2" ]=" oraclelinux"
72+ )
73+
74+
75+ # -------------------------------------------------------------------------------
76+ # Main Script Logic
77+ # -------------------------------------------------------------------------------
78+
79+ # Function to log messages
80+ log () {
81+ echo " $( date +' %Y-%m-%d %H:%M:%S' ) - $1 " | tee -a " $LOG_FILE "
82+ }
83+
84+ # Ensure base destination and log directories exist
85+ mkdir -p " $DEST_DIR "
86+ mkdir -p " $LOG_DIR "
87+
88+ log " Starting image download process."
89+ log " Temporary directory: $TEMP_DIR "
90+ log " Base destination directory: $DEST_DIR "
91+ log " Log file: $LOG_FILE "
92+ log " Error log file: $ERROR_LOG_FILE "
93+
94+ # Loop through the image URLs
95+ for filename in " ${! IMAGE_URLS[@]} " ; do
96+ url=" ${IMAGE_URLS[$filename]} "
97+ distro=" ${IMAGE_DISTROS[$filename]} "
98+
99+ # Check if a distro is defined for the file
100+ if [ -z " $distro " ]; then
101+ error_message=" No distribution directory defined for $filename . Skipping."
102+ log " ERROR: $error_message "
103+ echo " $( date +' %Y-%m-%d %H:%M:%S' ) - $error_message " >> " $ERROR_LOG_FILE "
104+ continue
105+ fi
106+
107+ distro_dest_dir=" ${DEST_DIR} /${distro} "
108+ temp_filepath=" ${TEMP_DIR} /${filename} "
109+ dest_filepath=" ${distro_dest_dir} /${filename} "
110+
111+ log " --------------------------------------------------"
112+ log " Starting download for: $filename "
113+ log " URL: $url "
114+
115+ # Download the file to the temporary directory
116+ wget --progress=bar:force:noscroll -O " $temp_filepath " " $url "
117+ download_status=$?
118+
119+ if [ $download_status -ne 0 ]; then
120+ # Handle download failure
121+ error_message=" Failed to download $filename from $url . wget exit code: $download_status "
122+ log " ERROR: $error_message "
123+ echo " $( date +' %Y-%m-%d %H:%M:%S' ) - $error_message " >> " $ERROR_LOG_FILE "
124+ else
125+ # Handle download success
126+ log " Successfully downloaded $filename to temporary location."
127+
128+ # Ensure the specific distro directory exists
129+ log " Ensuring destination directory exists: $distro_dest_dir "
130+ mkdir -p " $distro_dest_dir "
131+
132+ # Move the file to the destination directory, replacing any existing file
133+ log " Moving $filename to $dest_filepath "
134+ mv -f " $temp_filepath " " $dest_filepath "
135+ move_status=$?
136+
137+ if [ $move_status -ne 0 ]; then
138+ error_message=" Failed to move $filename to $dest_filepath . mv exit code: $move_status "
139+ log " ERROR: $error_message "
140+ echo " $( date +' %Y-%m-%d %H:%M:%S' ) - $error_message " >> " $ERROR_LOG_FILE "
141+ else
142+ log " Successfully moved $filename ."
143+ fi
144+ fi
145+ done
146+
147+ log " Generate checksum"
148+ # Create md5 checksum
149+ checksum_file=" md5sum.txt"
150+ sha512_checksum_file=" sha512sum.txt"
151+
152+ cd " $DEST_DIR "
153+ find . -type f ! -iname ' *.txt' -exec md5sum {} \; > " $checksum_file "
154+ find . -type f ! -iname ' *.txt' -exec sha512sum {} \; > " $sha512_checksum_file "
155+ checksum_status=$?
156+ if [ $checksum_status -ne 0 ]; then
157+ error_message=" Failed to create md5 checksum. md5sum exit code: $checksum_status "
158+ log " ERROR: $error_message "
159+ echo " $( date +' %Y-%m-%d %H:%M:%S' ) - $error_message " >> " $ERROR_LOG_FILE "
160+ else
161+ log " Successfully created checksum file: $checksum_file "
162+ fi
163+
164+ log " --------------------------------------------------"
165+ log " Image download process finished."
166+
167+ # Clean up the temporary directory
168+ rm -rf " $TEMP_DIR "
169+ log " Temporary directory $TEMP_DIR removed."
0 commit comments