Skip to content

Commit 2d0cd7d

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 [--version <version>] <template.yaml>... Description: This script updates the AlmaLinux image location in the specified templates. If the image location in the template contains a release date in the URL, the script replaces it with the latest available date. Image location basename format: CentOS[Stream-GenericCloud-<version>-[latest|<date>.0].<arch>.qcow2 Published AlmaLinux image information is fetched from the following URLs: https://repo.almalinux.org/almalinux/<major version>-stream/<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 9-Stream in ~/.lima/almalinux/lima.yaml: $ update-template-almalinux.sh --version 9-stream ~/.lima/almalinux/lima.yaml $ limactl factory-reset almalinux Flags: --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 cb0f1d3 commit 2d0cd7d

File tree

2 files changed

+275
-0
lines changed

2 files changed

+275
-0
lines changed

hack/update-template-almalinux.sh

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