Skip to content

Commit 77b6116

Browse files
committed
Add hack/update-template-alpine.sh
_This change does not include support for `https://github.com/lima-vm/alpine-lima`._ ```console $ hack/update-template-alpine.sh update-template-alpine.sh: Update the Alpine Linux image location in the specified templates Usage: update-template-alpine.sh [--version-major-minor <major>.<minor>] <template.yaml>... Description: This script updates the Alpine Linux image location in the specified templates. Image location basename format: <target vendor>_alpine-<version>-<arch>-<firmware>-<bootstrap>[-<machine>]-<image revision>.qcow2 Published Alpine Linux image information is fetched from the following URLs: latest-stable: https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/cloud <major>.<minor>: https://dl-cdn.alpinelinux.org/alpine/v<major>.<minor>/releases/cloud To parsing html, this script requires 'htmlq' or 'pup' command. The downloaded files will be cached in the Lima cache directory. Examples: Update the Alpine Linux image location in templates/**.yaml: $ update-template-alpine.sh templates/**.yaml Update the Alpine Linux image location to version 3.18 in ~/.lima/alpine/lima.yaml: $ update-template-alpine.sh --version-major-minor 3.18 ~/.lima/alpine/lima.yaml $ limactl factory-reset alpine Flags: --version-major-minor <major>.<minor> Use the specified <major>.<minor> version. The version must be 3.18 or later. -h, --help Print this help message ``` Signed-off-by: Norio Nomura <[email protected]> # Conflicts: # hack/update-template.sh
1 parent 1a07d5f commit 77b6116

File tree

2 files changed

+272
-0
lines changed

2 files changed

+272
-0
lines changed

hack/update-template-alpine.sh

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

hack/update-template.sh

Lines changed: 2 additions & 0 deletions
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)