Skip to content

Commit f93c601

Browse files
authored
Merge pull request #2820 from norio-nomura/update-template-oraclelinux.sh
Add `hack/update-template-oraclelinux.sh`
2 parents 361fe23 + 2cba996 commit f93c601

10 files changed

+250
-19
lines changed

hack/update-template-almalinux.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ function almalinux_latest_image_entry_for_url_spec() {
130130
}
131131
132132
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}")
133+
local location=$1 url_spec
134+
url_spec=$(almalinux_url_spec_from_location "${location}")
135135
jq -r '["almalinux", .major_minor_version // .major_version, .target_vendor,
136136
if .date then "timestamped" else "latest" end,
137137
.arch, .file_extension] | join(":")' <<<"${url_spec}"
@@ -254,7 +254,7 @@ for template in "${templates[@]}"; do
254254
set +e # Disable 'set -e' to avoid exiting on error for the next assignment.
255255
cache_key=$(
256256
set -e # Enable 'set -e' for the next command.
257-
almalinux_cache_key_for_image_kernel "${location}" "${kernel_location}" "${overriding}"
257+
almalinux_cache_key_for_image_kernel "${location}" "${kernel_location}"
258258
) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
259259
# shellcheck disable=2181
260260
[[ $? -eq 0 ]] || continue

hack/update-template-archlinux.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ for template in "${templates[@]}"; do
261261
set +e # Disable 'set -e' to avoid exiting on error for the next assignment.
262262
cache_key=$(
263263
set -e # Enable 'set -e' for the next command.
264-
archlinux_cache_key_for_image_kernel "${location}" "${kernel_location}" "${overriding}"
264+
archlinux_cache_key_for_image_kernel "${location}" "${kernel_location}"
265265
) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
266266
# shellcheck disable=2181
267267
[[ $? -eq 0 ]] || continue

hack/update-template-centos-stream.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Description:
2020
This script updates the CentOS Stream image location in the specified templates.
2121
If the image location in the template contains a release date in the URL, the script replaces it with the latest available date.
2222
23-
Image location basename format: CentOS[Stream-GenericCloud-<version>-[latest|<date>.0].<arch>.qcow2
23+
Image location basename format: CentOS-Stream-GenericCloud-<version>-[latest|<date>.0].<arch>.qcow2
2424
2525
Published CentOS Stream image information is fetched from the following URLs:
2626
@@ -121,8 +121,8 @@ function centos_latest_image_entry_for_url_spec() {
121121
}
122122
123123
function centos_cache_key_for_image_kernel() {
124-
local location=$1 overriding=${3:-"{}"} url_spec
125-
url_spec=$(centos_url_spec_from_location "${location}" | jq -r ". + ${overriding}")
124+
local location=$1 url_spec
125+
url_spec=$(centos_url_spec_from_location "${location}")
126126
jq -r '["centos", .version, .target_vendor,
127127
if .date_and_ci_job_id then "timestamped" else "latest" end,
128128
.arch, .file_extension] | join(":")' <<<"${url_spec}"
@@ -241,7 +241,7 @@ for template in "${templates[@]}"; do
241241
set +e # Disable 'set -e' to avoid exiting on error for the next assignment.
242242
cache_key=$(
243243
set -e # Enable 'set -e' for the next command.
244-
centos_cache_key_for_image_kernel "${location}" "${kernel_location}" "${overriding}"
244+
centos_cache_key_for_image_kernel "${location}" "${kernel_location}"
245245
) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
246246
# shellcheck disable=2181
247247
[[ $? -eq 0 ]] || continue

hack/update-template-debian.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ function debian_location_from_url_spec() {
285285
# debian_release_24.04-server-amd64-.img
286286
# ```
287287
function debian_cache_key_for_image_kernel_overriding() {
288-
local location=$1 kernel_location=${2:-null} overriding=${3:-"{}"} url_spec with_kernel='' version backports arch daily timestamped file_extension
289-
url_spec=$(debian_url_spec_from_location "${location}" | jq -r ". + ${overriding}")
288+
local location=$1 kernel_location=${2:-null} url_spec with_kernel='' version backports arch daily timestamped file_extension
289+
url_spec=$(debian_url_spec_from_location "${location}")
290290
[[ ${kernel_location} != "null" ]] && with_kernel=_with_kernel
291291
version=$(jq -r '.version|if . then "-\(.)" else empty end' <<<"${url_spec}")
292292
backports=$(jq -r 'if .backports then "-backports" else empty end' <<<"${url_spec}")
@@ -407,7 +407,7 @@ for template in "${templates[@]}"; do
407407
set +e # Disable 'set -e' to avoid exiting on error for the next assignment.
408408
cache_key=$(
409409
set -e # Enable 'set -e' for the next command.
410-
debian_cache_key_for_image_kernel_overriding "${location}" "${kernel_location}" "${overriding}"
410+
debian_cache_key_for_image_kernel_overriding "${location}" "${kernel_location}"
411411
) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
412412
# shellcheck disable=2181
413413
[[ $? -eq 0 ]] || continue

hack/update-template-oraclelinux.sh

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
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 oraclelinux_print_help() {
13+
cat <<HELP
14+
$(basename "${BASH_SOURCE[0]}"): Update the Oracle Linux image location in the specified templates
15+
16+
Usage:
17+
$(basename "${BASH_SOURCE[0]}") [--version-major <major version>] <template.yaml>...
18+
19+
Description:
20+
This script updates the Oracle Linux image location in the specified templates.
21+
Image location basename format:
22+
23+
OL<major version>U<minor version>_<arch>-kvm[-cloud]-b<build number>.qcow2
24+
25+
Published Oracle Linux image information is fetched from the following URLs:
26+
27+
OL8:
28+
x86_64: https://yum.oracle.com/templates/OracleLinux/ol8-template.json
29+
aarch64: https://yum.oracle.com/templates/OracleLinux/ol8_aarch64-cloud-template.json
30+
31+
OL9:
32+
x86_64: https://yum.oracle.com/templates/OracleLinux/ol9-template.json
33+
aarch64: https://yum.oracle.com/templates/OracleLinux/ol9_aarch64-cloud-template.json
34+
35+
The downloaded files will be cached in the Lima cache directory.
36+
37+
Examples:
38+
Update the Oracle Linux image location in templates/**.yaml:
39+
$ $(basename "${BASH_SOURCE[0]}") templates/**.yaml
40+
41+
Update the Oracle Linux image location to major version 9 in ~/.lima/oraclelinux/lima.yaml:
42+
$ $(basename "${BASH_SOURCE[0]}") --version-major 9 ~/.lima/oraclelinux/lima.yaml
43+
$ limactl factory-reset oraclelinux
44+
45+
Flags:
46+
--version-major <major version> Use the specified Oracle Linux <major version>.
47+
The major version must be 7+ for x86_64 or 8+ for aarch64.
48+
-h, --help Print this help message
49+
HELP
50+
}
51+
52+
# print the URL spec for the given location
53+
function oraclelinux_url_spec_from_location() {
54+
local location=$1 jq_filter url_spec
55+
jq_filter='capture("
56+
^https://yum\\.oracle\\.com/templates/OracleLinux/OL(?<path_major_version>\\d+)/u(?<path_minor_version>\\d+)/(?<path_arch>[^/]+)/
57+
OL(?<major_version>\\d+)U(?<minor_version>\\d+)_(?<arch>[^-]+)-(?<type>[^-]+)(?<cloud>-cloud)?-b(?<build_number>\\d+)\\.(?<file_extension>.*)$
58+
";"x")
59+
'
60+
url_spec=$(jq -e -r "${jq_filter}" <<<"\"${location}\"")
61+
echo "${url_spec}"
62+
}
63+
64+
readonly oraclelinux_jq_filter_json_url='
65+
"https://yum.oracle.com/templates/OracleLinux/" +
66+
"ol\(.path_major_version)\(if .path_arch != "x86_64" then "_" + .path_arch else "" end)\(.cloud // "")-template.json"
67+
'
68+
69+
function oraclelinux_json_url_from_url_spec() {
70+
local -r url_spec=$1
71+
jq -e -r "${oraclelinux_jq_filter_json_url}" <<<"${url_spec}" ||
72+
error_exit "Failed to get the JSON url for ${url_spec}"
73+
}
74+
75+
function oraclelinux_latest_image_entry_for_url_spec() {
76+
local url_spec=$1 arch json_url downloaded_json latest_version_info
77+
# shellcheck disable=SC2034
78+
arch=$(jq -r '.arch' <<<"${url_spec}")
79+
json_url=$(oraclelinux_json_url_from_url_spec "${url_spec}")
80+
downloaded_json=$(download_to_cache "${json_url}")
81+
latest_version_info="$(jq -e -r --argjson spec "${url_spec}" '{
82+
location: ("https://yum.oracle.com" + .base_url + "/" + .[$spec.type].image),
83+
sha256: ("sha256:" + .[$spec.type].sha256)
84+
}' <"${downloaded_json}")"
85+
[[ -n ${latest_version_info} ]] || return
86+
local location digest
87+
# prefer the v<major>.<minor> in the path
88+
location=$(jq -e -r '.location' <<<"${latest_version_info}")
89+
location=$(validate_url_without_redirect "${location}")
90+
# shellcheck disable=SC2034
91+
digest=$(jq -e -r '.sha256' <<<"${latest_version_info}")
92+
json_vars location arch digest
93+
}
94+
95+
function oraclelinux_cache_key_for_image_kernel() {
96+
local location=$1 overriding=${3:-"{}"} url_spec
97+
url_spec=$(oraclelinux_url_spec_from_location "${location}" | jq -r ". + ${overriding}")
98+
jq -r '["oraclelinux", .path_major_version, .type, .cloud // empty, .arch, .file_extension] | join(":")' <<<"${url_spec}"
99+
}
100+
101+
function oraclelinux_image_entry_for_image_kernel() {
102+
local location=$1 kernel_is_not_supported=$2 overriding=${3:-"{}"} url_spec image_entry=''
103+
[[ ${kernel_is_not_supported} == "null" ]] || echo "Updating kernel information is not supported on Oracle Linux" >&2
104+
url_spec=$(oraclelinux_url_spec_from_location "${location}" | jq -r ". + ${overriding}")
105+
image_entry=$(oraclelinux_latest_image_entry_for_url_spec "${url_spec}")
106+
# shellcheck disable=SC2031
107+
if [[ -z ${image_entry} ]]; then
108+
error_exit "Failed to get the ${url_spec} image location for ${location}"
109+
elif jq -e ".location == \"${location}\"" <<<"${image_entry}" >/dev/null; then
110+
echo "Image location is up-to-date: ${location}" >&2
111+
else
112+
echo "${image_entry}"
113+
fi
114+
}
115+
116+
# check if the script is executed or sourced
117+
# shellcheck disable=SC1091
118+
if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then
119+
scriptdir=$(dirname "${BASH_SOURCE[0]}")
120+
# shellcheck source=./cache-common-inc.sh
121+
. "${scriptdir}/cache-common-inc.sh"
122+
123+
# shellcheck source=/dev/null # avoid shellcheck hangs on source looping
124+
. "${scriptdir}/update-template.sh"
125+
else
126+
# this script is sourced
127+
if [[ -v SUPPORTED_DISTRIBUTIONS ]]; then
128+
SUPPORTED_DISTRIBUTIONS+=("oraclelinux")
129+
else
130+
declare -a SUPPORTED_DISTRIBUTIONS=("oraclelinux")
131+
fi
132+
return 0
133+
fi
134+
135+
declare -a templates=()
136+
declare overriding='{}'
137+
while [[ $# -gt 0 ]]; do
138+
case "$1" in
139+
-h | --help)
140+
oraclelinux_print_help
141+
exit 0
142+
;;
143+
-d | --debug) set -x ;;
144+
--version-major)
145+
if [[ -n $2 && $2 != -* ]]; then
146+
overriding=$(
147+
path_major_version="${2}"
148+
[[ ${path_major_version} =~ ^[0-9]+$ ]] || error_exit "Oracle Linux major version must be a number"
149+
[[ ${path_major_version} -eq 7 ]] && echo 'Oracle Linux major version 7 exists only for x86_64. It may fail for aarch64.' >&2
150+
[[ ${path_major_version} -gt 7 ]] || error_exit "Oracle Linux major version must be 7+ for x86_64 or 8+ for aarch64"
151+
json_vars path_major_version <<<"${overriding}"
152+
)
153+
shift
154+
else
155+
error_exit "--version-major requires a value"
156+
fi
157+
;;
158+
--version-major=*)
159+
overriding=$(
160+
path_major_version="${1#*=}"
161+
[[ ${path_major_version} =~ ^[0-9]+$ ]] || error_exit "Oracle Linux major version must be a number"
162+
[[ ${path_major_version} -eq 7 ]] && echo 'Oracle Linux major version 7 exists only for x86_64. It may fail for aarch64.' >&2
163+
[[ ${path_major_version} -gt 7 ]] || error_exit "Oracle Linux major version must be 7+ for x86_64 or 8+ for aarch64"
164+
json_vars path_major_version <<<"${overriding}"
165+
)
166+
;;
167+
*.yaml) templates+=("$1") ;;
168+
*)
169+
error_exit "Unknown argument: $1"
170+
;;
171+
esac
172+
shift
173+
[[ -z ${overriding} ]] && overriding="{}"
174+
done
175+
176+
if [[ ${#templates[@]} -eq 0 ]]; then
177+
oraclelinux_print_help
178+
exit 0
179+
fi
180+
181+
declare -A image_entry_cache=()
182+
183+
for template in "${templates[@]}"; do
184+
echo "Processing ${template}"
185+
# 1. extract location by parsing template using arch
186+
yq_filter="
187+
.images[] | [.location, .kernel.location, .kernel.cmdline] | @tsv
188+
"
189+
parsed=$(yq eval "${yq_filter}" "${template}")
190+
191+
# 3. get the image location
192+
arr=()
193+
while IFS= read -r line; do arr+=("${line}"); done <<<"${parsed}"
194+
locations=("${arr[@]}")
195+
for ((index = 0; index < ${#locations[@]}; index++)); do
196+
[[ ${locations[index]} != "null" ]] || continue
197+
set -e
198+
IFS=$'\t' read -r location kernel_location kernel_cmdline <<<"${locations[index]}"
199+
set +e # Disable 'set -e' to avoid exiting on error for the next assignment.
200+
cache_key=$(
201+
set -e # Enable 'set -e' for the next command.
202+
oraclelinux_cache_key_for_image_kernel "${location}" "${kernel_location}" "${overriding}"
203+
) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
204+
# shellcheck disable=2181
205+
[[ $? -eq 0 ]] || continue
206+
image_entry=$(
207+
set -e # Enable 'set -e' for the next command.
208+
if [[ -v image_entry_cache[${cache_key}] ]]; then
209+
echo "${image_entry_cache[${cache_key}]}"
210+
else
211+
oraclelinux_image_entry_for_image_kernel "${location}" "${kernel_location}" "${overriding}"
212+
fi
213+
) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
214+
# shellcheck disable=2181
215+
[[ $? -eq 0 ]] || continue
216+
set -e
217+
image_entry_cache[${cache_key}]="${image_entry}"
218+
if [[ -n ${image_entry} ]]; then
219+
[[ ${kernel_cmdline} != "null" ]] &&
220+
jq -e 'has("kernel")' <<<"${image_entry}" >/dev/null &&
221+
image_entry=$(jq ".kernel.cmdline = \"${kernel_cmdline}\"" <<<"${image_entry}")
222+
echo "${image_entry}" | jq
223+
limactl edit --log-level error --set "
224+
.images[${index}] = ${image_entry}|
225+
(.images[${index}] | ..) style = \"double\"
226+
" "${template}"
227+
fi
228+
done
229+
done

hack/update-template-rocky.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ function rocky_latest_image_entry_for_url_spec() {
145145
}
146146
147147
function rocky_cache_key_for_image_kernel() {
148-
local location=$1 overriding=${3:-"{}"} url_spec
149-
url_spec=$(rocky_url_spec_from_location "${location}" | jq -r ". + ${overriding}")
148+
local location=$1 url_spec
149+
url_spec=$(rocky_url_spec_from_location "${location}")
150150
jq -r '["rocky", .major_minor_version // .major_version, .target_vendor,
151151
if .date_and_ci_job_id then "timestamped" else "latest" end,
152152
.arch, .file_extension] | join(":")' <<<"${url_spec}"
@@ -269,7 +269,7 @@ for template in "${templates[@]}"; do
269269
set +e # Disable 'set -e' to avoid exiting on error for the next assignment.
270270
cache_key=$(
271271
set -e # Enable 'set -e' for the next command.
272-
rocky_cache_key_for_image_kernel "${location}" "${kernel_location}" "${overriding}"
272+
rocky_cache_key_for_image_kernel "${location}" "${kernel_location}"
273273
) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
274274
# shellcheck disable=2181
275275
[[ $? -eq 0 ]] || continue

hack/update-template-ubuntu.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,8 @@ function ubuntu_cache_key_for_image_kernel_flavor_version() {
302302
local location=$1 kernel_location=${2:-null} url_spec with_kernel='' flavor version arch path_suffix
303303
url_spec=$(ubuntu_location_url_spec "${location}")
304304
[[ ${kernel_location} != "null" ]] && with_kernel=_with_kernel
305-
flavor=${3:-$(ubuntu_flavor_from_location_basename "${location}")}
306-
version=${4:-$(ubuntu_version_from_location_basename "${location}")}
305+
flavor=$(ubuntu_flavor_from_location_basename "${location}")
306+
version=$(ubuntu_version_from_location_basename "${location}")
307307
arch=$(ubuntu_arch_from_location_basename "${location}")
308308
path_suffix=$(ubuntu_path_suffix_from_location_basename "${location}")
309309
echo "ubuntu_${url_spec}${with_kernel}_${version}-${flavor}-${arch}-${path_suffix}"
@@ -425,7 +425,7 @@ for template in "${templates[@]}"; do
425425
[[ $? -eq 0 ]] || continue
426426
cache_key=$(
427427
set -e # Enable 'set -e' for the next command.
428-
ubuntu_cache_key_for_image_kernel_flavor_version "${location}" "${kernel_location}" "${overriding_flavor}" "${overriding_version}"
428+
ubuntu_cache_key_for_image_kernel_flavor_version "${location}" "${kernel_location}"
429429
) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
430430
# shellcheck disable=2181
431431
[[ $? -eq 0 ]] || continue

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-oraclelinux.sh
160+
. "${scriptdir}/update-template-oraclelinux.sh"
159161
else
160162
# this script is sourced
161163
return 0

templates/oraclelinux-8.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This template requires Lima v0.9.0 or later.
2-
# Oracle image licence: https://www.oracle.com/downloads/licenses/oracle-linux-license.html
2+
# Oracle image license: https://www.oracle.com/downloads/licenses/oracle-linux-license.html
33
# Image source: https://yum.oracle.com/oracle-linux-templates.html
44

55
# NOTE: EL8-based distros are known not to work on M1 chips: https://github.com/lima-vm/lima/issues/841

templates/oraclelinux-9.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This template requires Lima v0.11.3 or later.
2-
# Oracle image licence: https://www.oracle.com/downloads/licenses/oracle-linux-license.html
2+
# Oracle image license: https://www.oracle.com/downloads/licenses/oracle-linux-license.html
33
# Image source: https://yum.oracle.com/oracle-linux-templates.html
44

55
images:

0 commit comments

Comments
 (0)