Skip to content

Commit

Permalink
update-template-ubuntu.sh: add `ubuntu_image_entry_for_image_kernel_f…
Browse files Browse the repository at this point in the history
…lavor_version`

Signed-off-by: Norio Nomura <norio.nomura@gmail.com>
  • Loading branch information
norio-nomura committed Oct 14, 2024
1 parent 0cf1c9f commit 153a6e2
Showing 1 changed file with 43 additions and 30 deletions.
73 changes: 43 additions & 30 deletions hack/update-template-ubuntu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,10 @@ function ubuntu_image_url_release() {
}

# ubuntu_kernel_info_for_image_url returns the kernel and initrd location and digest for the given location.
function ubuntu_kernel_info_for_image_url() {
local location=$1 location_dirname sha256sums location_basename
function ubuntu_kernel_info_for_image_entry() {
local image_entry=$1 location
location=$(jq -r '.location' <<<"${image_entry}")
local location_dirname sha256sums location_basename
location_dirname=$(dirname "${location}")/unpacked
sha256sums=$(download_to_cache "${location_dirname}/SHA256SUMS")
location_basename="$(basename "${location}" | cut -d- -f1-5 | cut -d. -f1-2)"
Expand Down Expand Up @@ -235,6 +237,35 @@ function ubuntu_location_url_spec() {
echo "${url_spec}"
}

function ubuntu_image_entry_for_image_kernel_flavor_version() {
local -r location=$1 kernel_location=$2

local url_spec flavor version arch path_suffix
# shellcheck disable=2310
url_spec=$(ubuntu_location_url_spec "${location}") || return 1
flavor=${3:-$(ubuntu_flavor_from_location "${location}")}
version=${4:-$(ubuntu_version_from_location "${location}")}
arch=$(ubuntu_arch_from_location "${location}")
path_suffix=$(ubuntu_path_suffix_from_location "${location}")

local image_entry
image_entry=$(ubuntu_image_url_"${url_spec}" "${flavor}" "${version}" "${arch}" "${path_suffix}")
if [[ -z ${image_entry} ]]; then
echo "Failed to get the ${url_spec} image location for ${location}" >&2
return 1
elif jq -e ".location == \"${location}\"" <<<"${image_entry}" >/dev/null; then
echo "Image location is up-to-date: ${location}" >&2
return 0
fi
if [[ ${kernel_location} != "null" ]]; then
kernel_info=$(ubuntu_kernel_info_for_image_entry "${image_entry}")
if [[ -n ${kernel_info} ]]; then
image_entry=$(jq -c ". + ${kernel_info}" <<<"${image_entry}")
fi
fi
echo "${image_entry}"
}

declare -a templates=()
declare overriding_flavor=
declare overriding_version=
Expand Down Expand Up @@ -295,34 +326,16 @@ for template in "${templates[@]}"; do
for ((index = 0; index < ${#locations[@]}; index++)); do
[[ ${locations[index]} != "null" ]] || continue
IFS=$'\t' read -r location kernel_location kernel_cmdline initrd_location <<<"${locations[index]}"
location_before="${location}"

# shellcheck disable=2310
url_spec=$(ubuntu_location_url_spec "${location}") || continue

flavor=${overriding_flavor:-$(ubuntu_flavor_from_location "${location}")}
version=${overriding_version:-$(ubuntu_version_from_location "${location}")}
arch=$(ubuntu_arch_from_location "${location}")
path_suffix=$(ubuntu_path_suffix_from_location "${location}")
image_entry=$(ubuntu_image_url_"${url_spec}" "${flavor}" "${version}" "${arch}" "${path_suffix}")
if [[ -z ${image_entry} ]]; then
echo "Failed to get the ${url_spec} image location for ${location}" >&2
continue
elif location=$(jq -r .location <<<"${image_entry}") && [[ "${location}" == "${location_before}" ]]; then
continue
fi
if [[ ${kernel_location} != "null" ]]; then
kernel_info=$(ubuntu_kernel_info_for_image_url "${location}")
if [[ -n ${kernel_info} ]]; then
image_entry=$(jq -c ". + ${kernel_info}" <<<"${image_entry}")
[[ ${kernel_cmdline} != "null" ]] && image_entry=$(jq ".kernel.cmdline = \"${kernel_cmdline}\"" <<<"${image_entry}")
fi
# shellcheck disable=SC2310
image_entry=$(ubuntu_image_entry_for_image_kernel_flavor_version "${location}" "${kernel_location}" "${overriding_flavor}" "${overriding_version}") || continue
echo "${image_entry}" | jq
if [[ -n "${image_entry}" ]]; then
[[ ${kernel_cmdline} != "null" ]] && image_entry=$(jq ".kernel.cmdline = \"${kernel_cmdline}\"" <<<"${image_entry}")
limactl edit --log-level error --set "
[(.images.[] | path)].[${index}] as \$path|
setpath(\$path; ${image_entry})
.images[${index}].[] style = \"double\"
" "${template}"
fi
echo "${image_entry}"|jq
limactl edit --log-level error --set "
[(.images.[] | path)].[${index}] as \$path|
setpath(\$path; ${image_entry})
.images[${index}].[] style = \"double\"
" "${template}"
done
done

0 comments on commit 153a6e2

Please sign in to comment.