-
Notifications
You must be signed in to change notification settings - Fork 0
/
image-update
executable file
·390 lines (346 loc) · 11.7 KB
/
image-update
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/bin/bash
#
# Download the latest CentOS, Debian, Fedora or Ubuntu cloud image
# Source: https://github.com/trfore/proxmox-template-scripts
#
# Copyright 2022 Taylor Fore
#
# Licensed 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.
set -e
APPEND_DATE=false
BACKUP_IMAGES=false
REMOVE_OLD_IMAGE=false
STORAGE_PATH='/var/lib/vz/template/iso'
function err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')] ${0##*/} Error: $*" >&2
exit 2
}
function help() {
echo "
Usage: ${0##*/} -d <DISTRO_NAME> -r <RELEASE_NAME> [ARGS]
Examples:
${0##*/} -d ubuntu -r 20
${0##*/} -d ubuntu -r 20 --remove
${0##*/} -d ubuntu -r 20 --storage /tmp
${0##*/} -d debian -r 11 --remove
Arguments:
--help, -h Display this help message and exit
--backup, -b Backup the existing image before creating a new one (default: false)
This flag is not compatiable with --remove flag.
--date Append the date to the image name, e.g. 'ubuntu*-\$DATE.img' (default: false)
--distro, -d Specify the distribution name, e.g. 'ubuntu'.
--remove Remove old images before updating (default: false)
This flag is not compatiable with --backup flag.
--release, -r Specify the release version, e.g. 'focal' or '20'
--storage, -s Specify a storage path for the image (default: '/var/lib/vz/template/iso')
Alternative Usage: ${0##*/} <DISTRO_NAME>-<RELEASE_NAME> [ARGS]
Examples:
${0##*/} ubuntu-20
${0##*/} debian-11 --remove
"
exit 1
}
function usage() {
printf "Usage: %s -d <DISTRO_NAME> -r <RELEASE_NAME> [ARGS] \n" "${0##*/}" >&2
exit 1
}
#######################################
# Backup image files.
# Arguments:
# $1 - file, string.
# Outputs:
# Writes files to the current directory.
#######################################
function backup_images() {
shopt -s extglob
# avoid overwriting existing backups
files="${1%.*}!(*.backup.*)"
for file in $files; do
echo "Backing up ${file}"
cp -nv "$file" "${file%.*}.backup.${file##*.}"
done
shopt -u extglob
}
#######################################
# Compare the SHASUM of a local cloud image to remote image.
# Globals:
# file_basename
# file_name
# remote_shasum_url
# shasum_algorithm
# Returns:
# 0 if SHASUM matches.
# 1 if SHASUM does not match.
#######################################
function check_shasum() {
local current_shasum latest_shasum
tmpfile=$(mktemp /tmp/pve-image-shasum.XXXXXX)
# get latest shasums
curl -sL "${remote_shasum_url}" -o "${tmpfile}"
latest_shasum=$(grep "${file_name}" "${tmpfile}" | grep -oE '[a-z0-9]{64,128}' -)
# qcow2 images are expected to be converted into a raw format (*.img) on PVE
current_shasum=$(shasum -a "${shasum_algorithm}" "${file_basename}".img | awk '{print $1}')
echo "Current SHASUM: ${current_shasum}"
echo "Latest SHASUM: ${latest_shasum}"
rm "$tmpfile"
if [[ $latest_shasum == "${current_shasum}" ]]; then
echo "SHASUM match, image is up-to-date."
return 0
else
echo "No SHASUM match, image is outdated."
return 1
fi
}
#######################################
# Get Cloud Image Release Date
# Arguments:
# $1 - url of the cloud image, string.
# $2 - file name of the cloud image, string.
# Outputs:
# file_date, string.
#######################################
function get_image_date() {
local url file_date
url=$(dirname "${1}")
# get dates in YYYY-MM-DD or YYYY-MMM-DD format
file_date=$(curl -sL "${url}" | grep "${2}" | grep -oiEm 1 '[0-9]{4}-([0-9]{2}-[0-9]{2}|[a-z]{3}-[0-9]{2})')
echo "${file_date}"
}
#######################################
# Set Cloud Image Values
# Globals:
# DISTRO_NAME
# RELEASE_NAME
# Returns:
# file_name, string.
# remote_url, string.
# remote_shasum_url, string.
# shasum_algorithm, string.
#######################################
function set_image_values() {
if [[ "${DISTRO_NAME}" == "debian" ]]; then
case $RELEASE_NAME in
buster | 10)
file_name="debian-10-generic-amd64.qcow2"
remote_url="https://cloud.debian.org/images/cloud/buster/latest/debian-10-generic-amd64.qcow2"
remote_shasum_url="https://cloud.debian.org/images/cloud/buster/latest/SHA512SUMS"
shasum_algorithm="512"
;;
bullseye | 11)
file_name="debian-11-generic-amd64.qcow2"
remote_url="https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-generic-amd64.qcow2"
remote_shasum_url="https://cloud.debian.org/images/cloud/bullseye/latest/SHA512SUMS"
shasum_algorithm="512"
;;
bookworm | 12)
file_name="debian-12-generic-amd64.qcow2"
remote_url="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2"
remote_shasum_url="https://cloud.debian.org/images/cloud/bookworm/latest/SHA512SUMS"
shasum_algorithm="512"
;;
trixie | 13)
err "Debian 13 'Trixie' is not available"
# file_name="debian-13-generic-amd64.qcow2"
# remote_url= "https://cloud.debian.org/images/cloud/trixie/latest/debian-13-generic-amd64.qcow2"
# remote_shasum_url="https://cloud.debian.org/images/cloud/trixie/latest/SHA512SUMS"
# shasum_algorithm="512"
;;
*)
err "Unknown distro, only works for Debian 10-12"
;;
esac
elif [[ "${DISTRO_NAME}" == "centos" ]]; then
case $RELEASE_NAME in
9)
file_name="CentOS-Stream-GenericCloud-9-latest.x86_64.qcow2"
remote_url="https://cloud.centos.org/centos/9-stream/x86_64/images/CentOS-Stream-GenericCloud-9-latest.x86_64.qcow2"
remote_shasum_url="https://cloud.centos.org/centos/9-stream/x86_64/images/CentOS-Stream-GenericCloud-9-latest.x86_64.qcow2.SHA256SUM"
shasum_algorithm="256"
;;
*)
err "Unknown distro, only works for CentOS Stream 9"
;;
esac
elif [[ "${DISTRO_NAME}" == "fedora" ]]; then
case $RELEASE_NAME in
40)
file_name="Fedora-Cloud-Base-Generic.x86_64-40-1.14.qcow2"
remote_url="https://download.fedoraproject.org/pub/fedora/linux/releases/40/Cloud/x86_64/images/Fedora-Cloud-Base-Generic.x86_64-40-1.14.qcow2"
remote_shasum_url="https://download.fedoraproject.org/pub/fedora/linux/releases/40/Cloud/x86_64/images/Fedora-Cloud-40-1.14-x86_64-CHECKSUM"
shasum_algorithm="256"
;;
41)
file_name="Fedora-Cloud-Base-Generic-41-1.4.x86_64.qcow2"
remote_url="https://download.fedoraproject.org/pub/fedora/linux/releases/41/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-41-1.4.x86_64.qcow2"
remote_shasum_url="https://download.fedoraproject.org/pub/fedora/linux/releases/41/Cloud/x86_64/images/Fedora-Cloud-41-1.4-x86_64-CHECKSUM"
shasum_algorithm="256"
;;
*)
err "Unknown distro, only works for Fedora 40+"
;;
esac
elif [[ "${DISTRO_NAME}" == "ubuntu" ]]; then
case $RELEASE_NAME in
focal | 20)
file_name="ubuntu-20.04-server-cloudimg-amd64.img"
remote_url="https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-amd64.img"
remote_shasum_url="https://cloud-images.ubuntu.com/releases/focal/release/SHA256SUMS"
shasum_algorithm="256"
;;
jammy | 22)
file_name="ubuntu-22.04-server-cloudimg-amd64.img"
remote_url="https://cloud-images.ubuntu.com/releases/jammy/release/ubuntu-22.04-server-cloudimg-amd64.img"
remote_shasum_url="https://cloud-images.ubuntu.com/releases/jammy/release/SHA256SUMS"
shasum_algorithm="256"
;;
noble | 24)
file_name="ubuntu-24.04-server-cloudimg-amd64.img"
remote_url="https://cloud-images.ubuntu.com/releases/noble/release/ubuntu-24.04-server-cloudimg-amd64.img"
remote_shasum_url="https://cloud-images.ubuntu.com/releases/noble/release/SHA256SUMS"
shasum_algorithm="256"
;;
*)
err "Unknown distro, only works for Ubuntu LTS 20+"
;;
esac
else
err "Unsupported distro. Please specify 'centos', 'debian', 'fedora' or 'ubuntu'."
fi
readonly file_name remote_url remote_shasum_url shasum_algorithm
return 0
}
#######################################
# Allow users to pass a single arg for distro and release
# This is a workaround for naming systemd timers
# Arguments:
# $1 - string, ex: ubuntu-20
# Returns:
# DISTRO_NAME, string.
# RELEASE_NAME, string.
#######################################
function split_arg() {
DISTRO_NAME=${1%-*}
RELEASE_NAME=${1#*-}
}
function main() {
if [[ ($? -ne 0) || ($# -eq 0) ]]; then
usage
fi
OPTIONS=hd:r:s:b
LONGOPTS=help,distro:,release:,storage:,backup,date,remove
NOARG_OPTS=(-h --help -b --backup --date --remove)
TEMP=$(getopt -n "${0##*/}" -o $OPTIONS --long $LONGOPTS -- "${@}") || exit 2
eval set -- "$TEMP"
unset TEMP
while true; do
[[ ! ${NOARG_OPTS[*]} =~ ${1} ]] && [[ ${2} == -* ]] && {
err "Missing argument for ${1}"
}
case "${1}" in
-h | --help)
help
;;
--backup | -b)
BACKUP_IMAGES=true
shift
continue
;;
--date)
APPEND_DATE=true
shift
continue
;;
--distro | -d)
DISTRO_NAME=${2,,}
shift 2
continue
;;
--release | -r)
RELEASE_NAME=${2,,}
shift 2
continue
;;
--remove)
REMOVE_OLD_IMAGE=true
shift
continue
;;
--storage | -s)
STORAGE_PATH=${2}
shift 2
continue
;;
--)
if [ -n "${2}" ]; then
split_arg "${2,,}"
fi
shift
break
;;
*)
err "Parsing arguments in main()"
;;
esac
done
readonly DISTRO_NAME RELEASE_NAME REMOVE_OLD_IMAGE STORAGE_PATH
if [ -z "${DISTRO_NAME}" ] || [ -z "${RELEASE_NAME}" ]; then
if [ -z "${DISTRO_NAME}" ]; then err "Missing Distribution"; fi
if [ -z "${RELEASE_NAME}" ]; then err "Missing Release"; fi
usage
fi
if [ ! -d "${STORAGE_PATH}" ]; then
err "Storage path does not exist! Value: ${STORAGE_PATH}"
fi
if [ ! -w "${STORAGE_PATH}" ]; then
err "You do not have write permission to ${STORAGE_PATH}, are you root?"
fi
if [ ${REMOVE_OLD_IMAGE} = true ] && [ ${BACKUP_IMAGES} = true ]; then
err "Cannot remove old images and backup new ones at the same time!"
fi
echo "Distribution: ${DISTRO_NAME}"
echo "Release: ${RELEASE_NAME}"
echo "Storage: ${STORAGE_PATH}"
echo "Remove Old Images: ${REMOVE_OLD_IMAGE}"
set_image_values "$@"
if [ ${APPEND_DATE} = true ]; then
image_date=$(get_image_date "${remote_url}" "${file_name}")
file_basename="${file_name%.*}-${image_date}"
else
file_basename="${file_name%.*}"
fi
readonly file_basename
cd "${STORAGE_PATH}"
if [[ ! -e "${file_basename}.img" ]]; then
echo "Downloading image..."
curl -L "${remote_url}" -o "${file_basename}.img"
echo "Done, new image downloaded!"
else
if check_shasum "$@"; then
echo "Done, image is current."
else
if [ ${REMOVE_OLD_IMAGE} = true ]; then
echo "Removing old images..."
# remove original and backup images
rm "${file_name%.*}"*
elif [ ${BACKUP_IMAGES} = true ]; then
# backup all images with or without date
backup_images "${file_name}"
fi
echo "Downloading image..."
curl -L "${remote_url}" -o "${file_basename}.img"
echo "Done, new image downloaded!"
fi
fi
exit
}
main "$@"