Skip to content

Commit f04c0f5

Browse files
committed
Add hack/update-template-oraclelinux.sh
```console $ hack/update-template-oraclelinux.sh update-template-oraclelinux.sh: Update the Oracle Linux image location in the specified templates Usage: update-template-oraclelinux.sh [--version-major <major version>] <template.yaml>... Description: This script updates the Oracle Linux image location in the specified templates. Image location basename format: OL<major version>U<minor version>_<arch>-kvm[-cloud]-b<build number>.qcow2 Published Oracle Linux image information is fetched from the following URLs: OL8: x86_64: https://yum.oracle.com/templates/OracleLinux/ol8-template.json aarch64: https://yum.oracle.com/templates/OracleLinux/ol8_aarch64-cloud-template.json OL9: x86_64: https://yum.oracle.com/templates/OracleLinux/ol9-template.json aarch64: https://yum.oracle.com/templates/OracleLinux/ol9_aarch64-cloud-template.json The downloaded files will be cached in the Lima cache directory. Examples: Update the Oracle Linux image location in templates/**.yaml: $ update-template-oraclelinux.sh templates/**.yaml Update the Oracle Linux image location to major version 9 in ~/.lima/oraclelinux/lima.yaml: $ update-template-oraclelinux.sh --version-major 9 ~/.lima/oraclelinux/lima.yaml $ limactl factory-reset oraclelinux Flags: --version-major <major version> Use the specified Oracle Linux <major version>. The major version must be 7+ for x86_64 or 8+ for aarch64. -h, --help Print this help message ``` Signed-off-by: Norio Nomura <[email protected]>
1 parent f4e50c2 commit f04c0f5

File tree

1 file changed

+229
-0
lines changed

1 file changed

+229
-0
lines changed

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 <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<version>U<release>_<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 version 9 in ~/.lima/oraclelinux/lima.yaml:
42+
$ $(basename "${BASH_SOURCE[0]}") --version 9 ~/.lima/oraclelinux/lima.yaml
43+
$ limactl factory-reset oraclelinux
44+
45+
Flags:
46+
--version <version> Use the specified Oracle Linux <version>.
47+
The 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_version>\\d+)/u(?<path_release>\\d+)/(?<path_arch>[^/]+)/
57+
OL(?<version>\\d+)U(?<release>\\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_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_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)
145+
if [[ -n $2 && $2 != -* ]]; then
146+
overriding=$(
147+
path_version="${2}"
148+
[[ ${path_version} =~ ^[0-9]+$ ]] || error_exit "Oracle Linux version must be a number"
149+
[[ ${path_version} -eq 7 ]] && echo 'Oracle Linux version 7 exists only for x86_64. It may fail for aarch64.' >&2
150+
[[ ${path_version} -gt 7 ]] || error_exit "Oracle Linux version must be 7+ for x86_64 or 8+ for aarch64"
151+
json_vars path_version <<<"${overriding}"
152+
)
153+
shift
154+
else
155+
error_exit "--version requires a value"
156+
fi
157+
;;
158+
--version=*)
159+
overriding=$(
160+
path_version="${1#*=}"
161+
[[ ${path_version} =~ ^[0-9]+$ ]] || error_exit "Oracle Linux version must be a number"
162+
[[ ${path_version} -eq 7 ]] && echo 'Oracle Linux version 7 exists only for x86_64. It may fail for aarch64.' >&2
163+
[[ ${path_version} -gt 7 ]] || error_exit "Oracle Linux version must be 7+ for x86_64 or 8+ for aarch64"
164+
json_vars path_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

0 commit comments

Comments
 (0)