Skip to content

Commit 2afaa9c

Browse files
committed
Add hack/update-template-almalinux.sh
```console $ hack/update-template-almalinux.sh update-template-almalinux.sh: Update the AlmaLinux image location in the specified templates Usage: update-template-almalinux.sh [--major-version <major version>] <template.yaml>... Description: This script updates the AlmaLinux image location in the specified templates. If the image location in the template contains a minor version and release date in the URL, the script replaces it with the latest available minor version and date. Image location basename format: AlmaLinux-<major version>-GenericCloud-[latest|<major version>.<minor version>-<date>].<arch>.qcow2 Published AlmaLinux image information is fetched from the following URLs: https://repo.almalinux.org/almalinux/<major version>/cloud/<arch>/images/ To parsing html, this script requires 'htmlq' or 'pup' command. The downloaded files will be cached in the Lima cache directory. Examples: Update the AlmaLinux image location in templates/**.yaml: $ update-template-almalinux.sh templates/**.yaml Update the AlmaLinux image location in ~/.lima/almalinux/lima.yaml: $ update-template-almalinux.sh ~/.lima/almalinux/lima.yaml $ limactl factory-reset almalinux Update the AlmaLinux image location to major version 9 in ~/.lima/almalinux/lima.yaml: $ update-template-almalinux.sh --major-version 9 ~/.lima/almalinux/lima.yaml $ limactl factory-reset almalinux Flags: --major-version <version> Use the specified version. The version must be 8 or later. -h, --help Print this help message ``` Signed-off-by: Norio Nomura <[email protected]>
1 parent 78ffc3a commit 2afaa9c

File tree

2 files changed

+286
-0
lines changed

2 files changed

+286
-0
lines changed

hack/update-template-almalinux.sh

Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
#!/usr/bin/env bash
2+
3+
set -eu -o pipefail
4+
5+
# Functions in this script assume error handling with 'set -e'.
6+
# To ensure 'set -e' works correctly:
7+
# - Use 'set +e' before assignments and '$(set -e; <function>)' to capture output without exiting on errors.
8+
# - Avoid calling functions directly in conditions to prevent disabling 'set -e'.
9+
# - Use 'shopt -s inherit_errexit' (Bash 4.4+) to avoid repeated 'set -e' in all '$(...)'.
10+
shopt -s inherit_errexit || error_exit "inherit_errexit not supported. Please use bash 4.4 or later."
11+
12+
function almalinux_print_help() {
13+
cat <<HELP
14+
$(basename "${BASH_SOURCE[0]}"): Update the AlmaLinux image location in the specified templates
15+
16+
Usage:
17+
$(basename "${BASH_SOURCE[0]}") [--major-version <major version>] <template.yaml>...
18+
19+
Description:
20+
This script updates the AlmaLinux image location in the specified templates.
21+
If the image location in the template contains a minor version and release date in the URL,
22+
the script replaces it with the latest available minor version and date.
23+
24+
Image location basename format:
25+
26+
AlmaLinux-<major version>-GenericCloud-[latest|<major version>.<minor version>-<date>].<arch>.qcow2
27+
28+
Published AlmaLinux image information is fetched from the following URLs:
29+
30+
https://repo.almalinux.org/almalinux/<major version>/cloud/<arch>/images/
31+
32+
To parsing html, this script requires 'htmlq' or 'pup' command.
33+
The downloaded files will be cached in the Lima cache directory.
34+
35+
Examples:
36+
Update the AlmaLinux image location in templates/**.yaml:
37+
$ $(basename "${BASH_SOURCE[0]}") templates/**.yaml
38+
39+
Update the AlmaLinux image location in ~/.lima/almalinux/lima.yaml:
40+
$ $(basename "${BASH_SOURCE[0]}") ~/.lima/almalinux/lima.yaml
41+
$ limactl factory-reset almalinux
42+
43+
Update the AlmaLinux image location to major version 9 in ~/.lima/almalinux/lima.yaml:
44+
$ $(basename "${BASH_SOURCE[0]}") --major-version 9 ~/.lima/almalinux/lima.yaml
45+
$ limactl factory-reset almalinux
46+
47+
Flags:
48+
--major-version <version> Use the specified version. The version must be 8 or later.
49+
-h, --help Print this help message
50+
HELP
51+
}
52+
53+
# print the URL spec for the given location
54+
function almalinux_url_spec_from_location() {
55+
local location=$1 jq_filter url_spec
56+
jq_filter='capture(
57+
"^https://repo\\.almalinux\\.org/almalinux/(?<path_version>\\d+(\\.\\d+)?)/cloud/(?<path_arch>[^/]+)/images/" +
58+
"AlmaLinux-(?<major_version>\\d+)-(?<target_vendor>.*)-" +
59+
"(latest|(?<major_minor_version>\\d+\\.\\d+)-(?<date>\\d{8}))\\.(?<arch>[^.]+).(?<file_extension>.*)$"
60+
;"x")
61+
'
62+
url_spec=$(jq -e -r "${jq_filter}" <<<"\"${location}\"")
63+
64+
jq -e '.path_arch == .arch' <<<"${url_spec}" >/dev/null ||
65+
error_exit "Validation failed: .path_arch != .arch: ${location}"
66+
echo "${url_spec}"
67+
}
68+
69+
readonly almalinux_jq_filter_directory='"https://repo.almalinux.org/almalinux/\(.path_version)/cloud/\(.path_arch)/images/"'
70+
readonly almalinux_jq_filter_filename='"AlmaLinux-\(.major_version)-\(.target_vendor)-\(if .date then .major_minor_version + "-" + .date else "latest" end).\(.arch).\(.file_extension)"'
71+
72+
# print the location for the given URL spec
73+
function almalinux_location_from_url_spec() {
74+
local -r url_spec=$1
75+
jq -e -r "${almalinux_jq_filter_directory} + ${almalinux_jq_filter_filename}" <<<"${url_spec}" ||
76+
error_exit "Failed to get the location for ${url_spec}"
77+
}
78+
79+
function almalinux_image_directory_from_url_spec() {
80+
local -r url_spec=$1
81+
jq -e -r "${almalinux_jq_filter_directory}" <<<"${url_spec}" ||
82+
error_exit "Failed to get the image directory for ${url_spec}"
83+
}
84+
85+
function almalinux_image_filename_from_url_spec() {
86+
local -r url_spec=$1
87+
jq -e -r "${almalinux_jq_filter_filename}" <<<"${url_spec}" ||
88+
error_exit "Failed to get the image filename for ${url_spec}"
89+
}
90+
91+
#
92+
function almalinux_latest_image_entry_for_url_spec() {
93+
local url_spec=$1 arch major_version_url_spec major_version_image_directory downloaded_page links_in_page latest_minor_version_info
94+
arch=$(jq -r '.arch' <<<"${url_spec}")
95+
# to detect minor version updates, we need to get the major version URL
96+
major_version_url_spec=$(jq -e -r '.path_version = .major_version' <<<"${url_spec}")
97+
major_version_image_directory=$(almalinux_image_directory_from_url_spec "${major_version_url_spec}")
98+
downloaded_page=$(download_to_cache "${major_version_image_directory}")
99+
if command -v htmlq >/dev/null; then
100+
links_in_page=$(htmlq 'pre a' --attribute href <"${downloaded_page}")
101+
elif command -v pup >/dev/null; then
102+
links_in_page=$(pup 'pre a attr{href}' <"${downloaded_page}")
103+
else
104+
error_exit "Please install 'htmlq' or 'pup' to list images from ${major_version_image_directory}"
105+
fi
106+
latest_minor_version_info=$(jq -e -Rrs --argjson spec "${url_spec}" '
107+
[
108+
split("\n").[] |
109+
capture(
110+
"^AlmaLinux-\($spec.major_version)-\($spec.target_vendor)-" +
111+
"(?<major_minor_version>\($spec.major_version)\\.\\d+)-" +
112+
"(?<date>\\d{8})\\.\($spec.arch)\\.\($spec.file_extension)$"
113+
;"x"
114+
) |
115+
.version_number_array = ([.major_minor_version | scan("\\d+") | tonumber])
116+
] | sort_by(.version_number_array, .date_and_ci_job_id) | last
117+
' <<<"${links_in_page}")
118+
[[ -n ${latest_minor_version_info} ]] || return
119+
local newer_url_spec location directory checksum_location downloaded_sha256sum filename digest
120+
# prefer the major_minor_version in the path
121+
newer_url_spec=$(jq -e -r ". + ${latest_minor_version_info} | .path_version = .major_minor_version" <<<"${url_spec}")
122+
location=$(almalinux_location_from_url_spec "${newer_url_spec}")
123+
directory=$(almalinux_image_directory_from_url_spec "${newer_url_spec}")
124+
checksum_location="${directory}CHECKSUM"
125+
downloaded_sha256sum=$(download_to_cache "${checksum_location}")
126+
filename=$(almalinux_image_filename_from_url_spec "${newer_url_spec}")
127+
digest=$(awk "/${filename}/{print \"sha256:\"\$1}" "${downloaded_sha256sum}")
128+
[[ -n ${digest} ]] || error_exit "Failed to get the SHA256 digest for ${filename}"
129+
json_vars location arch digest
130+
}
131+
132+
function almalinux_cache_key_for_image_kernel() {
133+
local location=$1 overriding=${3:-"{}"} url_spec
134+
url_spec=$(almalinux_url_spec_from_location "${location}" | jq -r ". + ${overriding}")
135+
jq -r '["almalinux", .major_minor_version // .major_version, .target_vendor,
136+
if .date then "timestamped" else "latest" end,
137+
.arch, .file_extension] | join(":")' <<<"${url_spec}"
138+
}
139+
140+
function almalinux_image_entry_for_image_kernel() {
141+
local location=$1 kernel_is_not_supported=$2 overriding=${3:-"{}"} url_spec image_entry=''
142+
[[ ${kernel_is_not_supported} == "null" ]] || echo "Updating kernel information is not supported on AlmaLinux" >&2
143+
url_spec=$(almalinux_url_spec_from_location "${location}" | jq -r ". + ${overriding}")
144+
if jq -e '.date' <<<"${url_spec}" >/dev/null; then
145+
image_entry=$(almalinux_latest_image_entry_for_url_spec "${url_spec}")
146+
else
147+
image_entry=$(
148+
# shellcheck disable=SC2030
149+
location=$(almalinux_location_from_url_spec "${url_spec}")
150+
location=$(validate_url_without_redirect "${location}")
151+
arch=$(jq -r '.path_arch' <<<"${url_spec}")
152+
json_vars location arch
153+
)
154+
fi
155+
# shellcheck disable=SC2031
156+
if [[ -z ${image_entry} ]]; then
157+
error_exit "Failed to get the ${url_spec} image location for ${location}"
158+
elif jq -e ".location == \"${location}\"" <<<"${image_entry}" >/dev/null; then
159+
echo "Image location is up-to-date: ${location}" >&2
160+
else
161+
echo "${image_entry}"
162+
fi
163+
}
164+
165+
# check if the script is executed or sourced
166+
# shellcheck disable=SC1091
167+
if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then
168+
scriptdir=$(dirname "${BASH_SOURCE[0]}")
169+
# shellcheck source=./cache-common-inc.sh
170+
. "${scriptdir}/cache-common-inc.sh"
171+
172+
if ! command -v htmlq >/dev/null && ! command -v pup >/dev/null; then
173+
error_exit "Please install 'htmlq' or 'pup' to list images from https://repo.almalinux.org/almalinux/<version>/cloud/<arch>/images/"
174+
fi
175+
# shellcheck source=/dev/null # avoid shellcheck hangs on source looping
176+
. "${scriptdir}/update-template.sh"
177+
else
178+
# this script is sourced
179+
if ! command -v htmlq >/dev/null && ! command -v pup >/dev/null; then
180+
echo "Please install 'htmlq' or 'pup' to list images from https://repo.almalinux.org/almalinux/<version>/cloud/<arch>/images/" >&2
181+
elif [[ -v SUPPORTED_DISTRIBUTIONS ]]; then
182+
SUPPORTED_DISTRIBUTIONS+=("almalinux")
183+
else
184+
declare -a SUPPORTED_DISTRIBUTIONS=("almalinux")
185+
fi
186+
return 0
187+
fi
188+
189+
declare -a templates=()
190+
declare overriding="{}"
191+
while [[ $# -gt 0 ]]; do
192+
case "$1" in
193+
-h | --help)
194+
almalinux_print_help
195+
exit 0
196+
;;
197+
-d | --debug) set -x ;;
198+
--major-version)
199+
if [[ -n $2 && $2 != -* ]]; then
200+
overriding=$(
201+
major_version="${2%%.*}"
202+
[[ ${major_version} -ge 8 ]] || error_exit "AlmaLinux major version must be 8 or later"
203+
# shellcheck disable=2034
204+
path_version="${major_version}"
205+
json_vars path_version major_version <<<"${overriding}"
206+
)
207+
shift
208+
else
209+
error_exit "--major-version requires a value"
210+
fi
211+
;;
212+
--major-version=*)
213+
overriding=$(
214+
major_version="${1#*=}"
215+
major_version="${major_version%%.*}"
216+
[[ ${major_version} -ge 8 ]] || error_exit "AlmaLinux major version must be 8 or later"
217+
# shellcheck disable=2034
218+
path_version="${major_version}"
219+
json_vars path_version major_version <<<"${overriding}"
220+
)
221+
;;
222+
*.yaml) templates+=("$1") ;;
223+
*)
224+
error_exit "Unknown argument: $1"
225+
;;
226+
esac
227+
shift
228+
[[ -z ${overriding} ]] && overriding="{}"
229+
done
230+
231+
if [[ ${#templates[@]} -eq 0 ]]; then
232+
almalinux_print_help
233+
exit 0
234+
fi
235+
236+
declare -A image_entry_cache=()
237+
238+
for template in "${templates[@]}"; do
239+
echo "Processing ${template}"
240+
# 1. extract location by parsing template using arch
241+
yq_filter="
242+
.images[] | [.location, .kernel.location, .kernel.cmdline] | @tsv
243+
"
244+
parsed=$(yq eval "${yq_filter}" "${template}")
245+
246+
# 3. get the image location
247+
arr=()
248+
while IFS= read -r line; do arr+=("${line}"); done <<<"${parsed}"
249+
locations=("${arr[@]}")
250+
for ((index = 0; index < ${#locations[@]}; index++)); do
251+
[[ ${locations[index]} != "null" ]] || continue
252+
set -e
253+
IFS=$'\t' read -r location kernel_location kernel_cmdline <<<"${locations[index]}"
254+
set +e # Disable 'set -e' to avoid exiting on error for the next assignment.
255+
cache_key=$(
256+
set -e # Enable 'set -e' for the next command.
257+
almalinux_cache_key_for_image_kernel "${location}" "${kernel_location}" "${overriding}"
258+
) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
259+
# shellcheck disable=2181
260+
[[ $? -eq 0 ]] || continue
261+
image_entry=$(
262+
set -e # Enable 'set -e' for the next command.
263+
if [[ -v image_entry_cache[${cache_key}] ]]; then
264+
echo "${image_entry_cache[${cache_key}]}"
265+
else
266+
almalinux_image_entry_for_image_kernel "${location}" "${kernel_location}" "${overriding}"
267+
fi
268+
) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
269+
# shellcheck disable=2181
270+
[[ $? -eq 0 ]] || continue
271+
set -e
272+
image_entry_cache[${cache_key}]="${image_entry}"
273+
if [[ -n ${image_entry} ]]; then
274+
[[ ${kernel_cmdline} != "null" ]] &&
275+
jq -e 'has("kernel")' <<<"${image_entry}" >/dev/null &&
276+
image_entry=$(jq ".kernel.cmdline = \"${kernel_cmdline}\"" <<<"${image_entry}")
277+
echo "${image_entry}" | jq
278+
limactl edit --log-level error --set "
279+
.images[${index}] = ${image_entry}|
280+
(.images[${index}] | ..) style = \"double\"
281+
" "${template}"
282+
fi
283+
done
284+
done

hack/update-template.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then
152152
. "${scriptdir}/update-template-archlinux.sh"
153153
# shellcheck source=./update-template-centos-stream.sh
154154
. "${scriptdir}/update-template-centos-stream.sh"
155+
# shellcheck source=./update-template-almalinux.sh
156+
. "${scriptdir}/update-template-almalinux.sh"
155157
else
156158
# this script is sourced
157159
return 0

0 commit comments

Comments
 (0)