Skip to content

Commit a38e91a

Browse files
committed
Add update-template-archlinux.sh
```console $ hack/update-template-archlinux.sh update-template-archlinux.sh: Update the Arch-Linux image location in the specified templates Usage: update-template-archlinux.sh <template.yaml>... Description: This script updates the Arch-Linux 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: Arch-Linux-<arch>-cloudimg[-<date>.<CI_JOB_ID>].qcow2 Published Arch-Linux image information is fetched from the following URLs: x86_64: listing: https://gitlab.archlinux.org/api/v4/projects/archlinux%2Farch-boxes/packages details: https://gitlab.archlinux.org/api/v4/projects/archlinux%2Farch-boxes/packages/:package_id/package_files aarch64: https://github.com/mcginty/arch-boxes-arm/releases/ Using 'gh' CLI tool for fetching the latest release from GitHub. Examples: Update the Arch-Linux image location in templates/**.yaml: $ update-template-archlinux.sh templates/**.yaml Update the Arch-Linux image location in ~/.lima/archlinux/lima.yaml: $ update-template-archlinux.sh ~/.lima/archlinux/lima.yaml $ limactl factory-reset archlinux Flags: -h, --help Print this help message ``` Signed-off-by: Norio Nomura <[email protected]>
1 parent 63cea0e commit a38e91a

File tree

3 files changed

+302
-2
lines changed

3 files changed

+302
-2
lines changed

hack/cache-common-inc.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ function hash_file() {
356356
# /Users/user/Library/Caches/lima/download/by-url-sha256/346ee1ff9e381b78ba08e2a29445960b5cd31c51f896fc346b82e26e345a5b9a/data # on macOS
357357
# /home/user/.cache/lima/download/by-url-sha256/346ee1ff9e381b78ba08e2a29445960b5cd31c51f896fc346b82e26e345a5b9a/data # on others
358358
function download_to_cache() {
359-
local cache_path
359+
local cache_path use_redirected_location=${2:-YES}
360360
cache_path=$(location_to_cache_path "$1")
361361
# before checking remote location, check if the data file is already downloaded and the time file is updated within 10 minutes
362362
if [[ -f ${cache_path}/data && -n "$(find "${cache_path}/time" -mmin -10 || true)" ]]; then
@@ -379,7 +379,11 @@ function download_to_cache() {
379379
code=$(jq -r '.http_code' <<<"${curl_info_json}")
380380
time=$(jq -r '.last_modified' <<<"${curl_info_json}")
381381
type=$(jq -r '.content_type' <<<"${curl_info_json}")
382-
url=$(jq -r '.url' <<<"${curl_info_json}")
382+
if [[ ${use_redirected_location} == "YES" ]]; then
383+
url=$(jq -r '.url' <<<"${curl_info_json}")
384+
else
385+
url=$1
386+
fi
383387
[[ ${code} == 200 ]] || error_exit "Failed to download $1"
384388

385389
cache_path=$(location_to_cache_path "${url}")
@@ -412,3 +416,8 @@ function download_to_cache() {
412416
[[ -f ${cache_path}/url ]] || echo -n "${url}" >"${cache_path}/url"
413417
echo "${cache_path}/data"
414418
}
419+
420+
# Download the file to the cache directory without redirect and print the path.
421+
function download_to_cache_without_redirect() {
422+
download_to_cache "$1" "NO"
423+
}

hack/update-template-archlinux.sh

Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
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 archlinux_print_help() {
13+
cat <<HELP
14+
$(basename "${BASH_SOURCE[0]}"): Update the Arch-Linux image location in the specified templates
15+
16+
Usage:
17+
$(basename "${BASH_SOURCE[0]}") <template.yaml>...
18+
19+
Description:
20+
This script updates the Arch-Linux 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: Arch-Linux-<arch>-cloudimg[-<date>.<CI_JOB_ID>].qcow2
24+
25+
Published Arch-Linux image information is fetched from the following URLs:
26+
27+
x86_64:
28+
listing: https://gitlab.archlinux.org/api/v4/projects/archlinux%2Farch-boxes/packages
29+
details: https://gitlab.archlinux.org/api/v4/projects/archlinux%2Farch-boxes/packages/:package_id/package_files
30+
31+
arm64:
32+
https://github.com/mcginty/arch-boxes-arm/releases/
33+
34+
Examples:
35+
Update the Arch-Linux image location in templates/**.yaml:
36+
$ $(basename "${BASH_SOURCE[0]}") templates/**.yaml
37+
38+
Update the Arch-Linux image location in ~/.lima/archlinux/lima.yaml:
39+
$ $(basename "${BASH_SOURCE[0]}") ~/.lima/archlinux/lima.yaml
40+
$ limactl factory-reset archlinux
41+
42+
Flags:
43+
-h, --help Print this help message
44+
HELP
45+
}
46+
47+
# print the URL spec for the given location
48+
# shellcheck disable=SC2034
49+
function archlinux_url_spec_from_location() {
50+
local location=$1 location_basename arch flavor source date_and_ci_job_id='' file_extension
51+
location_basename=$(basename "${location}")
52+
arch=$(echo "${location_basename}" | cut -d- -f3)
53+
flavor=$(echo "${location_basename}" | cut -d- -f4 | cut -d. -f1)
54+
case "${location}" in
55+
https://geo.mirror.pkgbuild.com/images/*)
56+
source="geo.mirror.pkgbuild.com"
57+
local -r date_and_ci_job_id_pattern='[0-9]{8}\.[0-9]+'
58+
if [[ ${location} =~ ${date_and_ci_job_id_pattern} ]]; then
59+
date_and_ci_job_id="${BASH_REMATCH[0]}"
60+
fi
61+
if [[ ${location_basename} =~ ${date_and_ci_job_id_pattern} ]]; then
62+
file_extension=${location_basename##*"${BASH_REMATCH[0]}".}
63+
else
64+
file_extension=${location_basename#*.}
65+
fi
66+
;;
67+
https://github.com/mcginty/arch-boxes-arm/releases/download/*)
68+
source="github.com/mcginty/arch-boxes-arm"
69+
local -r date_pattern='[0-9]{8}'
70+
if [[ ${location} =~ ${date_pattern} ]]; then
71+
date_and_ci_job_id="${BASH_REMATCH[0]}"
72+
file_extension=${location_basename#*"${date_and_ci_job_id}".*.}
73+
else
74+
error_exit "Failed to extract date from ${location}"
75+
fi
76+
;;
77+
*)
78+
# Unsupported location
79+
return 1
80+
;;
81+
esac
82+
json_vars source arch flavor date_and_ci_job_id file_extension
83+
}
84+
85+
# print the location for the given URL spec
86+
function archlinux_location_from_url_spec() {
87+
local url_spec=$1 source arch flavor date_and_ci_job_id file_extension location=''
88+
source=$(jq -r '.source' <<<"${url_spec}")
89+
arch=$(jq -r '.arch' <<<"${url_spec}")
90+
flavor=$(jq -r '.flavor' <<<"${url_spec}")
91+
date_and_ci_job_id=$(jq -r '.date_and_ci_job_id' <<<"${url_spec}")
92+
93+
file_extension=$(jq -r '.file_extension' <<<"${url_spec}")
94+
case "${source}" in
95+
geo.mirror.pkgbuild.com)
96+
location="https://geo.mirror.pkgbuild.com/images/"
97+
if [[ -n ${date_and_ci_job_id} ]]; then
98+
location+="v${date_and_ci_job_id}/Arch-Linux-${arch}-${flavor}-${date_and_ci_job_id}.${file_extension}"
99+
else
100+
location+="latest/Arch-Linux-${arch}-${flavor}.${file_extension}"
101+
fi
102+
;;
103+
github.com/mcginty/arch-boxes-arm) ;;
104+
*)
105+
error_exit "Unsupported source: ${source}"
106+
;;
107+
esac
108+
echo "${location}"
109+
}
110+
111+
# returns the image entry for the latest image in the gitlab mirror
112+
function archlinux_image_entry_for_image_kernel_gitlab_mirror() {
113+
local location=$1 url_spec=$2 arch flavor date_and_ci_job_id gitlab_package_api_base latest_package_id jq_filter latest_package_files
114+
arch=$(jq -r '.arch' <<<"${url_spec}")
115+
if ! jq -e '.date_and_ci_job_id' <<<"${url_spec}" >/dev/null; then
116+
json_vars location arch
117+
return 1
118+
fi
119+
flavor=$(jq -r '.flavor' <<<"${url_spec}")
120+
date_and_ci_job_id=$(jq -r '.date_and_ci_job_id' <<<"${url_spec}")
121+
file_extension=$(jq -r '.file_extension' <<<"${url_spec}")
122+
gitlab_package_api_base="https://gitlab.archlinux.org/api/v4/projects/archlinux%2Farch-boxes/packages"
123+
latest_package_id=$(curl --silent --show-error "${gitlab_package_api_base}" | jq -r 'last|.id') || error_exit "Failed to fetch latest package_id"
124+
jq_filter="
125+
.[]|select(.file_name|test(\"^Arch-Linux-${arch}-${flavor}-.*\\\.${file_extension}\$\"))
126+
"
127+
latest_package_file=$(curl -s "${gitlab_package_api_base}/${latest_package_id}/package_files" | jq -r "${jq_filter}") ||
128+
error_exit "Failed to fetch latest package_file"
129+
file_name=$(jq -r '.file_name' <<<"${latest_package_file}")
130+
digest="sha256:$(jq -r '.file_sha256' <<<"${latest_package_file}")"
131+
local -r date_and_ci_job_id_pattern='[0-9]{8}\.[0-9]+'
132+
[[ ${file_name} =~ ${date_and_ci_job_id_pattern} ]] || error_exit "Failed to extract date_and_ci_job_id from ${file_name}"
133+
date_and_ci_job_id="${BASH_REMATCH[0]}"
134+
updated_url_spec=$(json_vars date_and_ci_job_id <<<"${url_spec}")
135+
location=$(archlinux_location_from_url_spec "${updated_url_spec}")
136+
location=$(validate_url_without_redirect "${location}")
137+
json_vars location arch digest
138+
}
139+
140+
# returns the image entry for the latest image in the GitHub repo
141+
function archlinux_image_entry_for_image_kernel_github_com() {
142+
local location=$1 url_spec=$2 arch flavor file_extension repo jq_filter latest_location downloaded_img digest
143+
arch=$(jq -r '.arch' <<<"${url_spec}")
144+
if ! jq -e '.date_and_ci_job_id' <<<"${url_spec}" >/dev/null; then
145+
json_vars location arch
146+
return 1
147+
fi
148+
flavor=$(jq -r '.flavor' <<<"${url_spec}")
149+
file_extension=$(jq -r '.file_extension' <<<"${url_spec}")
150+
command -v gh >/dev/null || error_exit "gh is required for fetching the latest release from GitHub, but it's not installed"
151+
local -r repo_pattern='github.com/(.*)/releases/download/(.*)'
152+
if [[ ${location} =~ ${repo_pattern} ]]; then
153+
repo="${BASH_REMATCH[1]}"
154+
else
155+
error_exit "Failed to extract repo and release from ${location}"
156+
fi
157+
jq_filter=".assets[]|select(.name|test(\"^Arch-Linux-${arch}-${flavor}-.*\\\.${file_extension}\$\"))|.url"
158+
latest_location=$(gh release view --repo "${repo}" --json assets --jq "${jq_filter}" )
159+
[[ -n ${latest_location} ]] || error_exit "Failed to fetch the latest release URL from ${repo}"
160+
if [[ ${location} == "${latest_location}" ]]; then
161+
json_vars location arch
162+
return
163+
fi
164+
location=${latest_location}
165+
downloaded_img=$(download_to_cache_without_redirect "${latest_location}")
166+
# shellcheck disable=SC2034
167+
digest="sha256:$(sha256sum "${downloaded_img}" | cut -d' ' -f1)"
168+
json_vars location arch digest
169+
}
170+
171+
function archlinux_cache_key_for_image_kernel() {
172+
local location=$1 url_spec
173+
url_spec=$(archlinux_url_spec_from_location "${location}")
174+
jq -r '["archlinux", .source, .arch, .date_and_ci_job_id // empty]|join(":")' <<<"${url_spec}"
175+
}
176+
177+
function archlinux_image_entry_for_image_kernel() {
178+
local location=$1 url_spec source image_entry=''
179+
url_spec=$(archlinux_url_spec_from_location "${location}")
180+
source=$(jq -r '.source' <<<"${url_spec}")
181+
case "${source}" in
182+
geo.mirror.pkgbuild.com)
183+
image_entry=$(archlinux_image_entry_for_image_kernel_gitlab_mirror "${location}" "${url_spec}")
184+
;;
185+
github.com/mcginty/arch-boxes-arm)
186+
image_entry=$(archlinux_image_entry_for_image_kernel_github_com "${location}" "${url_spec}")
187+
;;
188+
*) error_exit "Unsupported source: ${source}" ;;
189+
esac
190+
if [[ -z ${image_entry} ]]; then
191+
error_exit "Failed to get the ${url_spec} image location for ${location}"
192+
elif jq -e ".location == \"${location}\"" <<<"${image_entry}" >/dev/null; then
193+
echo "Image location is up-to-date: ${location}" >&2
194+
else
195+
echo "${image_entry}"
196+
fi
197+
}
198+
199+
# check if the script is executed or sourced
200+
# shellcheck disable=SC1091
201+
if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then
202+
scriptdir=$(dirname "${BASH_SOURCE[0]}")
203+
# shellcheck source=./cache-common-inc.sh
204+
. "${scriptdir}/cache-common-inc.sh"
205+
206+
# shellcheck source=/dev/null # avoid shellcheck hangs on source looping
207+
. "${scriptdir}/update-template.sh"
208+
else
209+
# this script is sourced
210+
if [[ -v SUPPORTED_DISTRIBUTIONS ]]; then
211+
SUPPORTED_DISTRIBUTIONS+=("archlinux")
212+
else
213+
declare -a SUPPORTED_DISTRIBUTIONS=("archlinux")
214+
fi
215+
return 0
216+
fi
217+
218+
declare -a templates=()
219+
declare overriding="{}"
220+
while [[ $# -gt 0 ]]; do
221+
case "$1" in
222+
-h | --help)
223+
archlinux_print_help
224+
exit 0
225+
;;
226+
-d | --debug) set -x ;;
227+
*.yaml) templates+=("$1") ;;
228+
*)
229+
error_exit "Unknown argument: $1"
230+
;;
231+
esac
232+
shift
233+
[[ -z ${overriding} ]] && overriding="{}"
234+
done
235+
236+
if [[ ${#templates[@]} -eq 0 ]]; then
237+
archlinux_print_help
238+
exit 0
239+
fi
240+
241+
declare -A image_entry_cache=()
242+
243+
for template in "${templates[@]}"; do
244+
echo "Processing ${template}"
245+
# 1. extract location by parsing template using arch
246+
yq_filter="
247+
.images[] | [.location, .kernel.location, .kernel.cmdline] | @tsv
248+
"
249+
parsed=$(yq eval "${yq_filter}" "${template}")
250+
251+
# 3. get the image location
252+
arr=()
253+
while IFS= read -r line; do arr+=("${line}"); done <<<"${parsed}"
254+
locations=("${arr[@]}")
255+
for ((index = 0; index < ${#locations[@]}; index++)); do
256+
[[ ${locations[index]} != "null" ]] || continue
257+
set -e
258+
IFS=$'\t' read -r location kernel_location kernel_cmdline <<<"${locations[index]}"
259+
set +e # Disable 'set -e' to avoid exiting on error for the next assignment.
260+
cache_key=$(
261+
set -e # Enable 'set -e' for the next command.
262+
archlinux_cache_key_for_image_kernel "${location}" "${kernel_location}" "${overriding}"
263+
) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
264+
# shellcheck disable=2181
265+
[[ $? -eq 0 ]] || continue
266+
image_entry=$(
267+
set -e # Enable 'set -e' for the next command.
268+
if [[ -v image_entry_cache[${cache_key}] ]]; then
269+
echo "${image_entry_cache[${cache_key}]}"
270+
else
271+
archlinux_image_entry_for_image_kernel "${location}" "${kernel_location}" "${overriding}"
272+
fi
273+
) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
274+
# shellcheck disable=2181
275+
[[ $? -eq 0 ]] || continue
276+
set -e
277+
image_entry_cache[${cache_key}]="${image_entry}"
278+
if [[ -n ${image_entry} ]]; then
279+
[[ ${kernel_cmdline} != "null" ]] &&
280+
jq -e 'has("kernel")' <<<"${image_entry}" >/dev/null &&
281+
image_entry=$(jq ".kernel.cmdline = \"${kernel_cmdline}\"" <<<"${image_entry}")
282+
echo "${image_entry}" | jq
283+
limactl edit --log-level error --set "
284+
.images[${index}] = ${image_entry}|
285+
(.images[${index}] | ..) style = \"double\"
286+
" "${template}"
287+
fi
288+
done
289+
done

hack/update-template.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then
148148
. "${scriptdir}/update-template-ubuntu.sh"
149149
# shellcheck source=./update-template-debian.sh
150150
. "${scriptdir}/update-template-debian.sh"
151+
# shellcheck source=./update-template-archlinux.sh
152+
. "${scriptdir}/update-template-archlinux.sh"
151153
else
152154
# this script is sourced
153155
return 0

0 commit comments

Comments
 (0)