Skip to content

Commit 21bbd82

Browse files
authored
Merge pull request #2811 from norio-nomura/update-template-alpine.sh
Add `hack/update-template-alpine.sh`
2 parents 1a07d5f + b56686c commit 21bbd82

File tree

2 files changed

+310
-0
lines changed

2 files changed

+310
-0
lines changed

hack/update-template-alpine.sh

+308
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
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 alpine_print_help() {
13+
cat <<HELP
14+
$(basename "${BASH_SOURCE[0]}"): Update the Alpine Linux image location in the specified templates
15+
16+
Usage:
17+
$(basename "${BASH_SOURCE[0]}") [--version-major-minor (<major>.<minor>|latest-stable)|--version-major <major> --version-minor <minor>] <template.yaml>...
18+
19+
Description:
20+
This script updates the Alpine Linux image location in the specified templates.
21+
Image location basename format:
22+
23+
<target vendor>_alpine-<version>-<arch>-<firmware>-<bootstrap>[-<machine>]-<image revision>.qcow2
24+
25+
Published Alpine Linux image information is fetched from the following URLs:
26+
27+
latest-stable: https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/cloud
28+
<major>.<minor>: https://dl-cdn.alpinelinux.org/alpine/v<major>.<minor>/releases/cloud
29+
30+
To parsing html, this script requires 'htmlq' or 'pup' command.
31+
The downloaded files will be cached in the Lima cache directory.
32+
33+
Examples:
34+
Update the Alpine Linux image location in templates/**.yaml:
35+
$ $(basename "${BASH_SOURCE[0]}") templates/**.yaml
36+
37+
Update the Alpine Linux image location to version 3.18 in ~/.lima/alpine/lima.yaml:
38+
$ $(basename "${BASH_SOURCE[0]}") --version-major-minor 3.18 ~/.lima/alpine/lima.yaml
39+
$ limactl factory-reset alpine
40+
41+
Flags:
42+
--version-major-minor (<major>.<minor>|latest-stable) Use the specified <major>.<minor> version or alias "latest-stable".
43+
The <major>.<minor> version must be 3.18 or later.
44+
--version-major <major> --version-minor <minor> Use the specified <major> and <minor> version.
45+
-h, --help Print this help message
46+
HELP
47+
}
48+
49+
# print the URL spec for the given location
50+
function alpine_url_spec_from_location() {
51+
local location=$1 jq_filter url_spec
52+
jq_filter='capture("
53+
^https://dl-cdn\\.alpinelinux\\.org/alpine/(?<path_version>v\\d+\\.\\d+|latest-stable)/releases/cloud/
54+
(?<target_vendor>[^_]+)_alpine-(?<version>\\d+\\.\\d+\\.\\d+)-(?<arch>[^-]+)-
55+
(?<firmware>[^-]+)-(?<bootstrap>[^-]+)(-(?<machine>metal|vm))?-(?<image_revision>r\\d+)\\.(?<file_extension>.*)$
56+
";"x")
57+
'
58+
url_spec=$(jq -e -r "${jq_filter}" <<<"\"${location}\"")
59+
echo "${url_spec}"
60+
}
61+
62+
readonly alpine_jq_filter_directory='"https://dl-cdn.alpinelinux.org/alpine/\(.path_version)/releases/cloud/"'
63+
readonly alpine_jq_filter_filename='
64+
"\(.target_vendor)_alpine-\(.version)-\(.arch)-\(.firmware)-\(.bootstrap)" +
65+
"\(if .machine then "-" + .machine else "" end)-\(.image_revision).\(.file_extension)"
66+
'
67+
68+
# print the location for the given URL spec
69+
function alpine_location_from_url_spec() {
70+
local -r url_spec=$1
71+
jq -e -r "${alpine_jq_filter_directory} + ${alpine_jq_filter_filename}" <<<"${url_spec}" ||
72+
error_exit "Failed to get the location for ${url_spec}"
73+
}
74+
75+
function alpine_image_directory_from_url_spec() {
76+
local -r url_spec=$1
77+
jq -e -r "${alpine_jq_filter_directory}" <<<"${url_spec}" ||
78+
error_exit "Failed to get the image directory for ${url_spec}"
79+
}
80+
81+
function alpine_image_filename_from_url_spec() {
82+
local -r url_spec=$1
83+
jq -e -r "${alpine_jq_filter_filename}" <<<"${url_spec}" ||
84+
error_exit "Failed to get the image filename for ${url_spec}"
85+
}
86+
87+
#
88+
function alpine_latest_image_entry_for_url_spec() {
89+
local url_spec=$1 arch image_directory downloaded_page links_in_page latest_version_info
90+
# shellcheck disable=SC2034
91+
arch=$(jq -r '.arch' <<<"${url_spec}")
92+
image_directory=$(alpine_image_directory_from_url_spec "${url_spec}")
93+
downloaded_page=$(download_to_cache "${image_directory}")
94+
if command -v htmlq >/dev/null; then
95+
links_in_page=$(htmlq 'pre a' --attribute href <"${downloaded_page}")
96+
elif command -v pup >/dev/null; then
97+
links_in_page=$(pup 'pre a attr{href}' <"${downloaded_page}")
98+
else
99+
error_exit "Please install 'htmlq' or 'pup' to list images from ${image_directory}"
100+
fi
101+
latest_version_info=$(jq -e -Rrs --argjson spec "${url_spec}" '
102+
[
103+
split("\n").[] |
104+
capture(
105+
"^\($spec.target_vendor)_alpine-(?<version>\\d+\\.\\d+\\.\\d+)-\($spec.arch)-" +
106+
"\($spec.firmware)-\($spec.bootstrap)\(if $spec.machine then "-" + $spec.machine else "" end)-" +
107+
"(?<image_revision>r\\d+)\\.\($spec.file_extension)"
108+
;"x"
109+
) |
110+
.version_number_array = ([.version | scan("\\d+") | tonumber])
111+
] | sort_by(.version_number_array, .image_revision) | last
112+
' <<<"${links_in_page}")
113+
[[ -n ${latest_version_info} ]] || return
114+
local newer_url_spec location sha512sum_location downloaded_sha256sum filename digest
115+
# prefer the v<major>.<minor> in the path
116+
newer_url_spec=$(jq -e -r ". + ${latest_version_info} | .path_version = \"v\" + (.version_number_array[:2]|map(tostring)|join(\".\"))" <<<"${url_spec}")
117+
location=$(alpine_location_from_url_spec "${newer_url_spec}")
118+
location=$(validate_url_without_redirect "${location}")
119+
sha512sum_location="${location}.sha512"
120+
downloaded_sha256sum=$(download_to_cache "${sha512sum_location}")
121+
filename=$(alpine_image_filename_from_url_spec "${newer_url_spec}")
122+
digest="sha512:$(<"${downloaded_sha256sum}")"
123+
[[ -n ${digest} ]] || error_exit "Failed to get the digest for ${filename}"
124+
json_vars location arch digest
125+
}
126+
127+
function alpine_cache_key_for_image_kernel() {
128+
local location=$1 url_spec
129+
url_spec=$(alpine_url_spec_from_location "${location}")
130+
jq -r '["alpine", .path_version, .target_vendor, .arch, .file_extension] | join(":")' <<<"${url_spec}"
131+
}
132+
133+
function alpine_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 Alpine Linux" >&2
136+
url_spec=$(alpine_url_spec_from_location "${location}" | jq -r ". + ${overriding}")
137+
image_entry=$(alpine_latest_image_entry_for_url_spec "${url_spec}")
138+
# shellcheck disable=SC2031
139+
if [[ -z ${image_entry} ]]; then
140+
error_exit "Failed to get the ${url_spec} image location for ${location}"
141+
elif jq -e ".location == \"${location}\"" <<<"${image_entry}" >/dev/null; then
142+
echo "Image location is up-to-date: ${location}" >&2
143+
else
144+
echo "${image_entry}"
145+
fi
146+
}
147+
148+
# check if the script is executed or sourced
149+
# shellcheck disable=SC1091
150+
if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then
151+
scriptdir=$(dirname "${BASH_SOURCE[0]}")
152+
# shellcheck source=./cache-common-inc.sh
153+
. "${scriptdir}/cache-common-inc.sh"
154+
155+
if ! command -v htmlq >/dev/null && ! command -v pup >/dev/null; then
156+
error_exit "Please install 'htmlq' or 'pup' to list images from https://dl-cdn.alpinelinux.org/alpine/<version>/releases/cloud/"
157+
fi
158+
# shellcheck source=/dev/null # avoid shellcheck hangs on source looping
159+
. "${scriptdir}/update-template.sh"
160+
else
161+
# this script is sourced
162+
if ! command -v htmlq >/dev/null && ! command -v pup >/dev/null; then
163+
echo "Please install 'htmlq' or 'pup' to list images from https://dl-cdn.alpinelinux.org/alpine/<version>/releases/cloud/" >&2
164+
elif [[ -v SUPPORTED_DISTRIBUTIONS ]]; then
165+
SUPPORTED_DISTRIBUTIONS+=("alpine")
166+
else
167+
declare -a SUPPORTED_DISTRIBUTIONS=("alpine")
168+
fi
169+
return 0
170+
fi
171+
172+
declare -a templates=()
173+
declare overriding='{}'
174+
declare version_major='' version_minor=''
175+
while [[ $# -gt 0 ]]; do
176+
case "$1" in
177+
-h | --help)
178+
alpine_print_help
179+
exit 0
180+
;;
181+
-d | --debug) set -x ;;
182+
--version-major-minor)
183+
if [[ -n ${2:-} && $2 != -* ]]; then
184+
version="$2"
185+
shift
186+
else
187+
error_exit "--version-major-minor requires a value"
188+
fi
189+
;&
190+
--version-major-minor=*)
191+
version=${version:-${1#*=}}
192+
overriding=$(
193+
version="${version#v}"
194+
if [[ ${version} =~ ^v?[0-9]+.[0-9]+ ]]; then
195+
version="$(echo "${version}" | cut -d. -f1-2)"
196+
[[ ${version%%.*} -gt 3 || (${version%%.*} -eq 3 && ${version#*.} -ge 18) ]] || error_exit "Alpine Linux version must be 3.18 or later"
197+
path_version="v${version}"
198+
elif [[ ${version} == "latest-stable" ]]; then
199+
path_version="latest-stable"
200+
else
201+
error_exit "--version-major-minor requires a value in the format <major>.<minor> or latest-stable"
202+
fi
203+
json_vars path_version <<<"${overriding}"
204+
)
205+
;;
206+
--version-major)
207+
if [[ -n ${2:-} && $2 != -* ]]; then
208+
version_major="$2"
209+
shift
210+
else
211+
error_exit "--version-major requires a value"
212+
fi
213+
;&
214+
--version-major=*)
215+
version_major=${version_major:-${1#*=}}
216+
[[ ${version_major} =~ ^[0-9]+$ ]] || error_exit "Please specify --version-major in numbers"
217+
;;
218+
--version-minor)
219+
if [[ -n ${2:-} && $2 != -* ]]; then
220+
version_minor="$2"
221+
shift
222+
else
223+
error_exit "--version-minor requires a value"
224+
fi
225+
;&
226+
--version-minor=*)
227+
version_minor=${version_minor:-${1#*=}}
228+
[[ ${version_minor} =~ ^[0-9]+$ ]] || error_exit "Please specify --version-minor in numbers"
229+
;;
230+
*.yaml) templates+=("$1") ;;
231+
*)
232+
error_exit "Unknown argument: $1"
233+
;;
234+
esac
235+
shift
236+
[[ -z ${overriding} ]] && overriding="{}"
237+
done
238+
239+
if ! jq -e '.path_version' <<<"${overriding}" >/dev/null; then # --version-major-minor is not specified
240+
if [[ -n ${version_major} && -n ${version_minor} ]]; then
241+
[[ ${version_major} -gt 3 || (${version_major} -eq 3 && ${version_minor} -ge 18) ]] || error_exit "Alpine Linux version must be 3.18 or later"
242+
# shellcheck disable=2034
243+
path_version="v${version_major}.${version_minor}"
244+
overriding=$(json_vars path_version <<<"${overriding}")
245+
elif [[ -n ${version_major} ]]; then
246+
error_exit "--version-minor is required when --version-major is specified"
247+
elif [[ -n ${version_minor} ]]; then
248+
error_exit "--version-major is required when --version-minor is specified"
249+
fi
250+
elif [[ -n ${version_major} || -n ${version_minor} ]]; then # --version-major-minor is specified
251+
echo "Ignoring --version-major and --version-minor because --version-major-minor is specified" >&2
252+
fi
253+
[[ ${overriding} == "{}" ]] && overriding='{"path_version":"latest-stable"}'
254+
255+
if [[ ${#templates[@]} -eq 0 ]]; then
256+
alpine_print_help
257+
exit 0
258+
fi
259+
260+
declare -A image_entry_cache=()
261+
262+
for template in "${templates[@]}"; do
263+
echo "Processing ${template}"
264+
# 1. extract location by parsing template using arch
265+
yq_filter="
266+
.images[] | [.location, .kernel.location, .kernel.cmdline] | @tsv
267+
"
268+
parsed=$(yq eval "${yq_filter}" "${template}")
269+
270+
# 3. get the image location
271+
arr=()
272+
while IFS= read -r line; do arr+=("${line}"); done <<<"${parsed}"
273+
locations=("${arr[@]}")
274+
for ((index = 0; index < ${#locations[@]}; index++)); do
275+
[[ ${locations[index]} != "null" ]] || continue
276+
set -e
277+
IFS=$'\t' read -r location kernel_location kernel_cmdline <<<"${locations[index]}"
278+
set +e # Disable 'set -e' to avoid exiting on error for the next assignment.
279+
cache_key=$(
280+
set -e # Enable 'set -e' for the next command.
281+
alpine_cache_key_for_image_kernel "${location}" "${kernel_location}"
282+
) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
283+
# shellcheck disable=2181
284+
[[ $? -eq 0 ]] || continue
285+
image_entry=$(
286+
set -e # Enable 'set -e' for the next command.
287+
if [[ -v image_entry_cache[${cache_key}] ]]; then
288+
echo "${image_entry_cache[${cache_key}]}"
289+
else
290+
alpine_image_entry_for_image_kernel "${location}" "${kernel_location}" "${overriding}"
291+
fi
292+
) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
293+
# shellcheck disable=2181
294+
[[ $? -eq 0 ]] || continue
295+
set -e
296+
image_entry_cache[${cache_key}]="${image_entry}"
297+
if [[ -n ${image_entry} ]]; then
298+
[[ ${kernel_cmdline} != "null" ]] &&
299+
jq -e 'has("kernel")' <<<"${image_entry}" >/dev/null &&
300+
image_entry=$(jq ".kernel.cmdline = \"${kernel_cmdline}\"" <<<"${image_entry}")
301+
echo "${image_entry}" | jq
302+
limactl edit --log-level error --set "
303+
.images[${index}] = ${image_entry}|
304+
(.images[${index}] | ..) style = \"double\"
305+
" "${template}"
306+
fi
307+
done
308+
done

hack/update-template.sh

+2
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then
156156
. "${scriptdir}/update-template-almalinux.sh"
157157
# shellcheck source=./update-template-rocky.sh
158158
. "${scriptdir}/update-template-rocky.sh"
159+
# shellcheck source=./update-template-alpine.sh
160+
. "${scriptdir}/update-template-alpine.sh"
159161
# shellcheck source=./update-template-oraclelinux.sh
160162
. "${scriptdir}/update-template-oraclelinux.sh"
161163
else

0 commit comments

Comments
 (0)