Skip to content

Commit 153a6e2

Browse files
committed
update-template-ubuntu.sh: add ubuntu_image_entry_for_image_kernel_flavor_version
Signed-off-by: Norio Nomura <[email protected]>
1 parent 0cf1c9f commit 153a6e2

File tree

1 file changed

+43
-30
lines changed

1 file changed

+43
-30
lines changed

hack/update-template-ubuntu.sh

+43-30
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,10 @@ function ubuntu_image_url_release() {
151151
}
152152

153153
# ubuntu_kernel_info_for_image_url returns the kernel and initrd location and digest for the given location.
154-
function ubuntu_kernel_info_for_image_url() {
155-
local location=$1 location_dirname sha256sums location_basename
154+
function ubuntu_kernel_info_for_image_entry() {
155+
local image_entry=$1 location
156+
location=$(jq -r '.location' <<<"${image_entry}")
157+
local location_dirname sha256sums location_basename
156158
location_dirname=$(dirname "${location}")/unpacked
157159
sha256sums=$(download_to_cache "${location_dirname}/SHA256SUMS")
158160
location_basename="$(basename "${location}" | cut -d- -f1-5 | cut -d. -f1-2)"
@@ -235,6 +237,35 @@ function ubuntu_location_url_spec() {
235237
echo "${url_spec}"
236238
}
237239

240+
function ubuntu_image_entry_for_image_kernel_flavor_version() {
241+
local -r location=$1 kernel_location=$2
242+
243+
local url_spec flavor version arch path_suffix
244+
# shellcheck disable=2310
245+
url_spec=$(ubuntu_location_url_spec "${location}") || return 1
246+
flavor=${3:-$(ubuntu_flavor_from_location "${location}")}
247+
version=${4:-$(ubuntu_version_from_location "${location}")}
248+
arch=$(ubuntu_arch_from_location "${location}")
249+
path_suffix=$(ubuntu_path_suffix_from_location "${location}")
250+
251+
local image_entry
252+
image_entry=$(ubuntu_image_url_"${url_spec}" "${flavor}" "${version}" "${arch}" "${path_suffix}")
253+
if [[ -z ${image_entry} ]]; then
254+
echo "Failed to get the ${url_spec} image location for ${location}" >&2
255+
return 1
256+
elif jq -e ".location == \"${location}\"" <<<"${image_entry}" >/dev/null; then
257+
echo "Image location is up-to-date: ${location}" >&2
258+
return 0
259+
fi
260+
if [[ ${kernel_location} != "null" ]]; then
261+
kernel_info=$(ubuntu_kernel_info_for_image_entry "${image_entry}")
262+
if [[ -n ${kernel_info} ]]; then
263+
image_entry=$(jq -c ". + ${kernel_info}" <<<"${image_entry}")
264+
fi
265+
fi
266+
echo "${image_entry}"
267+
}
268+
238269
declare -a templates=()
239270
declare overriding_flavor=
240271
declare overriding_version=
@@ -295,34 +326,16 @@ for template in "${templates[@]}"; do
295326
for ((index = 0; index < ${#locations[@]}; index++)); do
296327
[[ ${locations[index]} != "null" ]] || continue
297328
IFS=$'\t' read -r location kernel_location kernel_cmdline initrd_location <<<"${locations[index]}"
298-
location_before="${location}"
299-
300-
# shellcheck disable=2310
301-
url_spec=$(ubuntu_location_url_spec "${location}") || continue
302-
303-
flavor=${overriding_flavor:-$(ubuntu_flavor_from_location "${location}")}
304-
version=${overriding_version:-$(ubuntu_version_from_location "${location}")}
305-
arch=$(ubuntu_arch_from_location "${location}")
306-
path_suffix=$(ubuntu_path_suffix_from_location "${location}")
307-
image_entry=$(ubuntu_image_url_"${url_spec}" "${flavor}" "${version}" "${arch}" "${path_suffix}")
308-
if [[ -z ${image_entry} ]]; then
309-
echo "Failed to get the ${url_spec} image location for ${location}" >&2
310-
continue
311-
elif location=$(jq -r .location <<<"${image_entry}") && [[ "${location}" == "${location_before}" ]]; then
312-
continue
313-
fi
314-
if [[ ${kernel_location} != "null" ]]; then
315-
kernel_info=$(ubuntu_kernel_info_for_image_url "${location}")
316-
if [[ -n ${kernel_info} ]]; then
317-
image_entry=$(jq -c ". + ${kernel_info}" <<<"${image_entry}")
318-
[[ ${kernel_cmdline} != "null" ]] && image_entry=$(jq ".kernel.cmdline = \"${kernel_cmdline}\"" <<<"${image_entry}")
319-
fi
329+
# shellcheck disable=SC2310
330+
image_entry=$(ubuntu_image_entry_for_image_kernel_flavor_version "${location}" "${kernel_location}" "${overriding_flavor}" "${overriding_version}") || continue
331+
echo "${image_entry}" | jq
332+
if [[ -n "${image_entry}" ]]; then
333+
[[ ${kernel_cmdline} != "null" ]] && image_entry=$(jq ".kernel.cmdline = \"${kernel_cmdline}\"" <<<"${image_entry}")
334+
limactl edit --log-level error --set "
335+
[(.images.[] | path)].[${index}] as \$path|
336+
setpath(\$path; ${image_entry})
337+
.images[${index}].[] style = \"double\"
338+
" "${template}"
320339
fi
321-
echo "${image_entry}"|jq
322-
limactl edit --log-level error --set "
323-
[(.images.[] | path)].[${index}] as \$path|
324-
setpath(\$path; ${image_entry})
325-
.images[${index}].[] style = \"double\"
326-
" "${template}"
327340
done
328341
done

0 commit comments

Comments
 (0)