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