| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | set -eu -o pipefail |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | shopt -s inherit_errexit || error_exit "inherit_errexit not supported. Please use bash 4.4 or later." |
| 14 | |
| 15 | function ubuntu_print_help() { |
| 16 | cat <<HELP |
| 17 | $(basename "${BASH_SOURCE[0]}"): Update the Ubuntu image location in the specified templates |
| 18 | |
| 19 | Usage: |
| 20 | $(basename "${BASH_SOURCE[0]}") [--flavor <flavor>|--minimal|--server] [--version <version>] <template.yaml>... |
| 21 | |
| 22 | Description: |
| 23 | This script updates the Ubuntu image location in the specified templates. |
| 24 | If the image location in the template contains a release date in the URL, the script replaces it with the latest available date. |
| 25 | If no flags are specified, the script uses the flavor and version from the image location basename in the template. |
| 26 | |
| 27 | Image location basename format: ubuntu-<version>-<flavor>-cloudimg-<arch>.img |
| 28 | |
| 29 | Released Ubuntu image information is fetched from the following URLs: |
| 30 | |
| 31 | Server: https://cloud-images.ubuntu.com/releases/stream/v1/com.ubuntu.cloud:released:download.json |
| 32 | Minimal: https://cloud-images.ubuntu.com/minimal/releases/stream/v1/com.ubuntu.cloud:released:download.json |
| 33 | |
| 34 | The downloaded JSON file will be cached in the Lima cache directory. |
| 35 | |
| 36 | Examples: |
| 37 | Update the Ubuntu image location in templates/**.yaml: |
| 38 | $ $(basename "${BASH_SOURCE[0]}") templates/**.yaml |
| 39 | |
| 40 | Update the Ubuntu image location in ~/.lima/ubuntu/lima.yaml: |
| 41 | $ $(basename "${BASH_SOURCE[0]}") ~/.lima/ubuntu/lima.yaml |
| 42 | $ limactl factory-reset ubuntu |
| 43 | |
| 44 | Update the Ubuntu image location to ubuntu-24.04-minimal-cloudimg-<arch>.img in ~/.lima/docker/lima.yaml: |
| 45 | $ $(basename "${BASH_SOURCE[0]}") --minimal --version 24.04 ~/.lima/docker/lima.yaml |
| 46 | $ limactl factory-reset docker |
| 47 | |
| 48 | Flags: |
| 49 | --flavor <flavor> Use the specified flavor image |
| 50 | --server Shortcut for --flavor server |
| 51 | --minimal Shortcut for --flavor minimal |
| 52 | --version <version> Use the specified version |
| 53 | The version can be an alias: latest, latest_lts, or lts. |
| 54 | -h, --help Print this help message |
| 55 | HELP |
| 56 | } |
| 57 | |
| 58 | readonly -A ubuntu_base_urls=( |
| 59 | ["minimal"]=https://cloud-images.ubuntu.com/minimal/releases/ |
| 60 | ["server"]=https://cloud-images.ubuntu.com/releases/ |
| 61 | ["daily"]=https://cloud-images.ubuntu.com/daily/ |
| 62 | ["daily:minimal"]=https://cloud-images.ubuntu.com/daily/server/minimal/daily/ |
| 63 | ) |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | function ubuntu_base_url() { |
| 72 | [[ -v ubuntu_base_urls[$1] ]] || error_exit "Unsupported flavor: $1" |
| 73 | echo "${ubuntu_base_urls[$1]}" |
| 74 | } |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | function ubuntu_downloaded_json() { |
| 83 | local flavor=$1 base_url json_url |
| 84 | case "${flavor}" in |
| 85 | daily*) |
| 86 | json_url=$(ubuntu_base_url "${flavor}")streams/v1/com.ubuntu.cloud:daily:download.json |
| 87 | ;; |
| 88 | *) |
| 89 | json_url=$(ubuntu_base_url "${flavor}")streams/v1/com.ubuntu.cloud:released:download.json |
| 90 | ;; |
| 91 | esac |
| 92 | download_to_cache "${json_url}" |
| 93 | } |
| 94 | |
| 95 | |
| 96 | function ubuntu_image_url_try_replace_release_with_version() { |
| 97 | local location=$1 release=$2 version=$3 location_using_version |
| 98 | set +e |
| 99 | location_using_version=$( |
| 100 | set -e |
| 101 | validate_url "${location/\/${release}\//\/${version}\/}" 2>/dev/null |
| 102 | ) |
| 103 | |
| 104 | if [[ $? -eq 0 ]]; then |
| 105 | echo "${location_using_version}" |
| 106 | else |
| 107 | echo "${location}" |
| 108 | fi |
| 109 | set -e |
| 110 | } |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | function ubuntu_limayaml_arch() { |
| 116 | local arch=$1 |
| 117 | arch=${arch/#amd64v[0-9]/amd64} |
| 118 | limayaml_arch "${arch}" |
| 119 | } |
| 120 | |
| 121 | |
| 122 | function ubuntu_image_url_latest() { |
| 123 | local flavor=$1 version=$2 arch=$3 path_suffix=$4 base_url ubuntu_downloaded_json jq_filter location_digest_release |
| 124 | base_url=$(ubuntu_base_url "${flavor}") |
| 125 | ubuntu_downloaded_json=$(ubuntu_downloaded_json "${flavor}") |
| 126 | jq_filter=" |
| 127 | [ |
| 128 | .products[\"com.ubuntu.cloud:${flavor}:${version}:${arch}\"] | |
| 129 | .release as \$release | |
| 130 | .versions[]?.items[] | select(.path | endswith(\"${path_suffix}\")) | |
| 131 | [\"${base_url}\"+.path, \"sha256:\"+.sha256, \$release] | @tsv |
| 132 | ] | last |
| 133 | " |
| 134 | location_digest_release=$(jq -r "${jq_filter}" "${ubuntu_downloaded_json}") |
| 135 | [[ ${location_digest_release} != "null" ]] || |
| 136 | error_exit "The URL for ubuntu-${version}-${flavor}-cloudimg-${arch}${path_suffix} is not provided at ${ubuntu_base_urls[${flavor}]}." |
| 137 | local location digest release location_using_version |
| 138 | read -r location digest release <<<"${location_digest_release}" |
| 139 | location=$(validate_url "${location}") |
| 140 | location=$(ubuntu_image_url_try_replace_release_with_version "${location}" "${release}" "${version}") |
| 141 | arch=$(ubuntu_limayaml_arch "${arch}") |
| 142 | json_vars location arch digest |
| 143 | } |
| 144 | |
| 145 | |
| 146 | function ubuntu_image_url_release() { |
| 147 | local flavor=$1 version=$2 arch=$3 path_suffix=$4 base_url ubuntu_downloaded_json |
| 148 | base_url=$(ubuntu_base_url "${flavor}") |
| 149 | ubuntu_downloaded_json=$(ubuntu_downloaded_json "${flavor}") |
| 150 | local jq_filter release location |
| 151 | jq_filter=" |
| 152 | [ |
| 153 | .products | to_entries[] as \$product_entry | |
| 154 | \$product_entry.value| select(.version == \"${version}\") | |
| 155 | .release |
| 156 | ] | first |
| 157 | " |
| 158 | release=$(jq -r "${jq_filter}" "${ubuntu_downloaded_json}") |
| 159 | [[ ${release} != "null" ]] || |
| 160 | error_exit "The URL for ubuntu-${version}-${flavor}-cloudimg-${arch}${path_suffix} is not provided at ${ubuntu_base_urls[${flavor}]}." |
| 161 | location=$(validate_url "${base_url}${release}/release/ubuntu-${version}-${flavor}-cloudimg-${arch}${path_suffix}") |
| 162 | location=$(ubuntu_image_url_try_replace_release_with_version "${location}" "${release}" "${version}") |
| 163 | arch=$(ubuntu_limayaml_arch "${arch}") |
| 164 | json_vars location arch |
| 165 | } |
| 166 | |
| 167 | |
| 168 | function ubuntu_image_url_daily() { |
| 169 | local flavor=$1 codename=$2 arch=$3 path_suffix=$4 base_url ubuntu_downloaded_json products_flavor jq_filter location_digest_version |
| 170 | base_url=$(ubuntu_base_url "${flavor}") |
| 171 | ubuntu_downloaded_json=$(ubuntu_downloaded_json "${flavor}") |
| 172 | case "${flavor}" in |
| 173 | daily:minimal) products_flavor="minimal" ;; |
| 174 | *) products_flavor="server" ;; |
| 175 | esac |
| 176 | jq_filter=" |
| 177 | [ |
| 178 | .products | to_entries[] as \$product_entry | |
| 179 | \$product_entry.value| select(.release == \"${codename}\" and .arch == \"${arch}\") | |
| 180 | .version as \$version | |
| 181 | .versions[]?.items[] | select(.path | endswith(\"${path_suffix}\")) | |
| 182 | [\"${base_url}\"+.path, \"sha256:\"+.sha256, \$version] | @tsv |
| 183 | ] | last |
| 184 | " |
| 185 | location_digest_version=$(jq -r "${jq_filter}" "${ubuntu_downloaded_json}") |
| 186 | [[ ${location_digest_version} != "null" ]] || |
| 187 | error_exit "The URL for ${codename}-${products_flavor}-cloudimg-${arch}${path_suffix} is not provided at ${ubuntu_base_urls[${flavor}]}." |
| 188 | local location digest version location_using_version |
| 189 | read -r location digest version <<<"${location_digest_version}" |
| 190 | location=$(validate_url "${location}") |
| 191 | location=$(ubuntu_image_url_try_replace_release_with_version "${location}" "${codename}" "${version}") |
| 192 | arch=$(ubuntu_limayaml_arch "${arch}") |
| 193 | json_vars location arch digest |
| 194 | } |
| 195 | |
| 196 | |
| 197 | function ubuntu_image_url_current() { |
| 198 | local flavor=$1 codename=$2 arch=$3 path_suffix=$4 base_url ubuntu_downloaded_json path_prefix products_flavor |
| 199 | base_url=$(ubuntu_base_url "${flavor}") |
| 200 | ubuntu_downloaded_json=$(ubuntu_downloaded_json "${flavor}") |
| 201 | case "${flavor}" in |
| 202 | daily:minimal) |
| 203 | path_prefix="" |
| 204 | products_flavor="minimal" |
| 205 | ;; |
| 206 | *) |
| 207 | path_prefix="server/" |
| 208 | products_flavor="server" |
| 209 | ;; |
| 210 | esac |
| 211 | local jq_filter version location |
| 212 | jq_filter=" |
| 213 | [ |
| 214 | .products | to_entries[] as \$product_entry | |
| 215 | \$product_entry.value| select(.release == \"${codename}\" and .arch == \"${arch}\") | |
| 216 | .version |
| 217 | ] | first |
| 218 | " |
| 219 | version=$(jq -r "${jq_filter}" "${ubuntu_downloaded_json}") |
| 220 | [[ ${version} != "null" ]] || |
| 221 | error_exit "The URL for ubuntu-${version}-${products_flavor}-cloudimg-${arch}${path_suffix} is not provided at ${ubuntu_base_urls[${flavor}]}." |
| 222 | location=$(validate_url "${base_url}${path_prefix}${codename}/current/${codename}-${products_flavor}-cloudimg-${arch}${path_suffix}") |
| 223 | location=$(ubuntu_image_url_try_replace_release_with_version "${location}" "${codename}" "${version}") |
| 224 | arch=$(ubuntu_limayaml_arch "${arch}") |
| 225 | json_vars location arch |
| 226 | } |
| 227 | |
| 228 | function ubuntu_file_info() { |
| 229 | local location=$1 location_dirname sha256sums location_basename digest |
| 230 | location=$(validate_url "${location}") |
| 231 | location_dirname=$(dirname "${location}") |
| 232 | sha256sums=$(download_to_cache "${location_dirname}/SHA256SUMS") |
| 233 | location_basename=$(basename "${location}") |
| 234 | |
| 235 | digest=${location+$(awk "/${location_basename}/{print \"sha256:\"\$1}" "${sha256sums}")} |
| 236 | json_vars location digest |
| 237 | } |
| 238 | |
| 239 | # ubuntu_image_entry_with_kernel_info prints image entry with kernel and initrd info. |
| 240 | # $1: image_entry |
| 241 | # e.g. |
| 242 | # ```console |
| 243 | # ubuntu_image_entry_with_kernel_info '{"location":"https://cloud-images.ubuntu.com/minimal/releases/24.04/release-20210914/ubuntu-24.04-minimal-cloudimg-amd64.img"}' |
| 244 | # {"location":"https://cloud-images.ubuntu.com/minimal/releases/24.04/release-20210914/ubuntu-24.04-minimal-cloudimg-amd64.img","kernel":{"location":"https://cloud-images.ubuntu.com/minimal/releases/24.04/release-20210914/ubuntu-24.04-minimal-cloudimg-vmlinuz-generic","digest":"sha256:..."}} |
| 245 | # ``` |
| 246 | # shellcheck disable=SC2034 |
| 247 | function ubuntu_image_entry_with_kernel_info() { |
| 248 | local image_entry=$1 location |
| 249 | location=$(jq -e -r '.location' <<<"${image_entry}") |
| 250 | local location_dirname location_basename location_prefix |
| 251 | location_dirname=$(dirname "${location}")/unpacked |
| 252 | location_basename=$(basename "${location}") |
| 253 | case "${location_basename}" in |
| 254 | ubuntu*) location_prefix="${location_dirname}/$(echo "${location_basename}" | cut -d- -f1-5 | cut -d. -f1-2)" ;; |
| 255 | *) location_prefix="${location_dirname}/$(echo "${location_basename}" | cut -d- -f1-4 | cut -d. -f1)" ;; |
| 256 | esac |
| 257 | |
| 258 | local kernel initrd |
| 259 | set +e # Disable 'set -e' to avoid exiting on error for the next assignment. |
| 260 | kernel=$( |
| 261 | set -e |
| 262 | ubuntu_file_info "${location_prefix}-vmlinuz-generic" |
| 263 | ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition. |
| 264 | # shellcheck disable=2181 |
| 265 | [[ $? -eq 0 ]] || error_exit "kernel image not found at ${location_prefix}-vmlinuz-generic" |
| 266 | initrd=$( |
| 267 | set -e |
| 268 | ubuntu_file_info "${location_prefix}-initrd-generic" 2>/dev/null |
| 269 | ) # may not exist |
| 270 | set -e |
| 271 | json_vars kernel initrd <<<"${image_entry}" |
| 272 | } |
| 273 | |
| 274 | function ubuntu_flavor_from_location_basename() { |
| 275 | local location=$1 location_basename flavor |
| 276 | location_basename=$(basename "${location}") |
| 277 | case "${location_basename}" in |
| 278 | ubuntu*) flavor=$(echo "${location_basename}" | cut -d- -f3) ;; |
| 279 | *) |
| 280 | flavor=$(echo "${location_basename}" | cut -d- -f2) |
| 281 | case "${flavor}" in |
| 282 | minimal) flavor="daily:minimal" ;; |
| 283 | *) flavor="daily" ;; |
| 284 | esac |
| 285 | ;; |
| 286 | esac |
| 287 | [[ -n ${flavor} ]] || error_exit "Failed to get flavor from ${location}" |
| 288 | echo "${flavor}" |
| 289 | } |
| 290 | |
| 291 | # ubuntu_version_from_location_basename prints the version from the location basename. |
| 292 | # On daily images, it prints the codename. |
| 293 | function ubuntu_version_from_location_basename() { |
| 294 | local location=$1 location_basename version |
| 295 | location_basename=$(basename "${location}") |
| 296 | case "${location_basename}" in |
| 297 | ubuntu*) version=$(echo "${location_basename}" | cut -d- -f2) ;; |
| 298 | *) version=$(echo "${location_basename}" | cut -d- -f1) ;; |
| 299 | esac |
| 300 | [[ -n ${version} ]] || error_exit "Failed to get version from ${location}" |
| 301 | echo "${version}" |
| 302 | } |
| 303 | |
| 304 | # ubuntu_version_latest_lts prints the latest LTS version for the given flavor. |
| 305 | # e.g. |
| 306 | # ```console |
| 307 | # ubuntu_version_latest_lts minimal |
| 308 | # 24.04 |
| 309 | # ``` |
| 310 | function ubuntu_version_latest_lts() { |
| 311 | local flavor=${1:-server} |
| 312 | ubuntu_downloaded_json=$(ubuntu_downloaded_json "${flavor}") |
| 313 | jq -e -r '[.products[]|.version|select(endswith(".04"))]|last // empty' "${ubuntu_downloaded_json}" |
| 314 | } |
| 315 | |
| 316 | # ubuntu_version_latest prints the latest version for the given flavor. |
| 317 | # e.g. |
| 318 | # ```console |
| 319 | # ubuntu_version_latest minimal |
| 320 | # 24.10 |
| 321 | # ``` |
| 322 | function ubuntu_version_latest() { |
| 323 | local flavor=${1:-server} |
| 324 | ubuntu_downloaded_json=$(ubuntu_downloaded_json "${flavor}") |
| 325 | jq -e -r '[.products[]|.version]|last // empty' "${ubuntu_downloaded_json}" |
| 326 | } |
| 327 | |
| 328 | # ubuntu_version_resolve_aliases resolves the version aliases. |
| 329 | # e.g. |
| 330 | # ```console |
| 331 | # ubuntu_version_resolve_aliases https://cloud-images.ubuntu.com/minimal/releases/24.04/release-20210914/ubuntu-24.04-minimal-cloudimg-amd64.img minimal latest |
| 332 | # 24.10 |
| 333 | # ubuntu_version_resolve_aliases https://cloud-images.ubuntu.com/minimal/releases/24.04/release-20210914/ubuntu-24.04-minimal-cloudimg-amd64.img minimal latest_lts |
| 334 | # 24.04 |
| 335 | # ubuntu_version_resolve_aliases https://cloud-images.ubuntu.com/minimal/releases/24.04/release-20210914/ubuntu-24.04-minimal-cloudimg-amd64.img |
| 336 | # |
| 337 | # ``` |
| 338 | function ubuntu_version_resolve_aliases() { |
| 339 | local location=$1 flavor version |
| 340 | flavor=${2:-$(ubuntu_flavor_from_location_basename "${location}")} |
| 341 | version=${3:-} |
| 342 | case "${version}" in |
| 343 | latest_lts | lts) ubuntu_version_latest_lts "${flavor}" ;; |
| 344 | latest) ubuntu_version_latest "${flavor}" ;; |
| 345 | *) echo "${version}" ;; |
| 346 | esac |
| 347 | } |
| 348 | |
| 349 | function ubuntu_arch_from_location_basename() { |
| 350 | local location=$1 location_basename arch |
| 351 | location_basename=$(basename "${location}") |
| 352 | case "${location_basename}" in |
| 353 | ubuntu*) arch=$(echo "${location_basename}" | cut -d- -f5 | cut -d. -f1) ;; |
| 354 | *) arch=$(echo "${location_basename}" | cut -d- -f4 | cut -d. -f1) ;; |
| 355 | esac |
| 356 | [[ -n ${arch} ]] || error_exit "Failed to get arch from ${location}" |
| 357 | echo "${arch}" |
| 358 | } |
| 359 | |
| 360 | function ubuntu_path_suffix_from_location_basename() { |
| 361 | local location=$1 arch path_suffix |
| 362 | arch=$(ubuntu_arch_from_location_basename "${location}") |
| 363 | path_suffix="${location##*"${arch}"}" |
| 364 | [[ -n ${path_suffix} ]] || error_exit "Failed to get path suffix from ${location}" |
| 365 | echo "${path_suffix}" |
| 366 | } |
| 367 | |
| 368 | # ubuntu_location_url_spec prints the URL spec for the given location. |
| 369 | # If the location is not supported, it returns 1. |
| 370 | # e.g. |
| 371 | # ```console |
| 372 | # ubuntu_location_url_spec https://cloud-images.ubuntu.com/minimal/releases/24.04/release-20210914/ubuntu-24.04-minimal-cloudimg-amd64.img |
| 373 | # latest |
| 374 | # ubuntu_location_url_spec https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-amd64.img |
| 375 | # release |
| 376 | # ``` |
| 377 | function ubuntu_location_url_spec() { |
| 378 | local location=$1 url_spec |
| 379 | case "${location}" in |
| 380 | https://cloud-images.ubuntu.com/minimal/releases/*/release/*) url_spec=release ;; |
| 381 | https://cloud-images.ubuntu.com/minimal/releases/*/release-*/*) url_spec=latest ;; |
| 382 | https://cloud-images.ubuntu.com/releases/*/release/*) url_spec=release ;; |
| 383 | https://cloud-images.ubuntu.com/releases/*/release-*/*) url_spec=latest ;; |
| 384 | https://cloud-images.ubuntu.com/daily/server/minimal/daily/*/current/*) url_spec=current ;; |
| 385 | https://cloud-images.ubuntu.com/daily/server/minimal/daily/*/*/*) url_spec=daily ;; |
| 386 | https://cloud-images.ubuntu.com/daily/server/*/current/*) url_spec=current ;; |
| 387 | https://cloud-images.ubuntu.com/daily/server/*/*/*) url_spec=daily ;; |
| 388 | *) |
| 389 | # echo "Unsupported image location: ${location}" >&2 |
| 390 | return 1 |
| 391 | ;; |
| 392 | esac |
| 393 | echo "${url_spec}" |
| 394 | } |
| 395 | |
| 396 | # ubuntu_cache_key_for_image_kernel_flavor_version prints the cache key for the given location, kernel_location, flavor, and version. |
| 397 | # If the image location is not supported, it returns 1. |
| 398 | # kernel_location is not validated. |
| 399 | # e.g. |
| 400 | # ```console |
| 401 | # ubuntu_cache_key_for_image_kernel_flavor_version https://cloud-images.ubuntu.com/minimal/releases/24.04/release-20210914/ubuntu-24.04-minimal-cloudimg-amd64.img |
| 402 | # ubuntu_latest_24.04-minimal-amd64-release-.img |
| 403 | # ubuntu_cache_key_for_image_kernel_flavor_version https://cloud-images.ubuntu.com/minimal/releases/24.04/release-20210914/ubuntu-24.04-minimal-cloudimg-amd64.img https://... |
| 404 | # ubuntu_latest_with_kernel_24.04-minimal-amd64-release-.img |
| 405 | # ubuntu_cache_key_for_image_kernel_flavor_version https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-amd64.img null |
| 406 | # ubuntu_release_24.04-server-amd64-.img |
| 407 | # ``` |
| 408 | function ubuntu_cache_key_for_image_kernel_flavor_version() { |
| 409 | local location=$1 kernel_location=${2:-null} url_spec with_kernel='' flavor version arch path_suffix |
| 410 | url_spec=$(ubuntu_location_url_spec "${location}") |
| 411 | [[ ${kernel_location} != "null" ]] && with_kernel=_with_kernel |
| 412 | flavor=$(ubuntu_flavor_from_location_basename "${location}") |
| 413 | version=$(ubuntu_version_from_location_basename "${location}") |
| 414 | arch=$(ubuntu_arch_from_location_basename "${location}") |
| 415 | path_suffix=$(ubuntu_path_suffix_from_location_basename "${location}") |
| 416 | echo "ubuntu_${url_spec}${with_kernel}_${version}-${flavor}-${arch}-${path_suffix}" |
| 417 | } |
| 418 | |
| 419 | function ubuntu_image_entry_for_image_kernel_flavor_version() { |
| 420 | local location=$1 kernel_location=$2 url_spec |
| 421 | url_spec=$(ubuntu_location_url_spec "${location}") |
| 422 | |
| 423 | local flavor version arch path_suffix |
| 424 | flavor=${3:-$(ubuntu_flavor_from_location_basename "${location}")} |
| 425 | version=${4:-$(ubuntu_version_from_location_basename "${location}")} |
| 426 | arch=$(ubuntu_arch_from_location_basename "${location}") |
| 427 | path_suffix=$(ubuntu_path_suffix_from_location_basename "${location}") |
| 428 | |
| 429 | local image_entry |
| 430 | image_entry=$(ubuntu_image_url_"${url_spec}" "${flavor}" "${version}" "${arch}" "${path_suffix}") |
| 431 | if [[ -z ${image_entry} ]]; then |
| 432 | error_exit "Failed to get the ${url_spec} image location for ${location}" |
| 433 | elif jq -e ".location == \"${location}\"" <<<"${image_entry}" >/dev/null; then |
| 434 | echo "Image location is up-to-date: ${location}" >&2 |
| 435 | elif [[ ${kernel_location} != "null" ]]; then |
| 436 | ubuntu_image_entry_with_kernel_info "${image_entry}" |
| 437 | else |
| 438 | echo "${image_entry}" |
| 439 | fi |
| 440 | } |
| 441 | |
| 442 | # check if the script is executed or sourced |
| 443 | # shellcheck disable=SC1091 |
| 444 | if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then |
| 445 | scriptdir=$(dirname "${BASH_SOURCE[0]}") |
| 446 | # shellcheck source=./cache-common-inc.sh |
| 447 | . "${scriptdir}/cache-common-inc.sh" |
| 448 | |
| 449 | # shellcheck source=/dev/null # avoid shellcheck hangs on source looping |
| 450 | . "${scriptdir}/update-template.sh" |
| 451 | else |
| 452 | # this script is sourced |
| 453 | if [[ -v SUPPORTED_DISTRIBUTIONS ]]; then |
| 454 | SUPPORTED_DISTRIBUTIONS+=("ubuntu") |
| 455 | else |
| 456 | declare -a SUPPORTED_DISTRIBUTIONS=("ubuntu") |
| 457 | fi |
| 458 | # required functions for Ubuntu |
| 459 | function ubuntu_cache_key_for_image_kernel() { ubuntu_cache_key_for_image_kernel_flavor_version "$@"; } |
| 460 | function ubuntu_image_entry_for_image_kernel() { ubuntu_image_entry_for_image_kernel_flavor_version "$@"; } |
| 461 | |
| 462 | return 0 |
| 463 | fi |
| 464 | |
| 465 | declare -a templates=() |
| 466 | declare overriding_flavor= |
| 467 | declare overriding_version= |
| 468 | while [[ $# -gt 0 ]]; do |
| 469 | case "$1" in |
| 470 | -h | --help) |
| 471 | ubuntu_print_help |
| 472 | exit 0 |
| 473 | ;; |
| 474 | -d | --debug) set -x ;; |
| 475 | --flavor) |
| 476 | if [[ -n $2 && $2 != -* ]]; then |
| 477 | overriding_flavor="$2" |
| 478 | shift |
| 479 | else |
| 480 | error_exit "--flavor requires a value" |
| 481 | fi |
| 482 | ;; |
| 483 | --flavor=*) overriding_flavor="${1#*=}" ;; |
| 484 | --minimal) overriding_flavor="minimal" ;; |
| 485 | --server) overriding_flavor="server" ;; |
| 486 | --version) |
| 487 | if [[ -n $2 && $2 != -* ]]; then |
| 488 | overriding_version="$2" |
| 489 | shift |
| 490 | else |
| 491 | error_exit "--version requires a value" |
| 492 | fi |
| 493 | ;; |
| 494 | --version=*) overriding_version="${1#*=}" ;; |
| 495 | *.yaml) templates+=("$1") ;; |
| 496 | *) |
| 497 | error_exit "Unknown argument: $1" |
| 498 | ;; |
| 499 | esac |
| 500 | shift |
| 501 | done |
| 502 | |
| 503 | if [[ ${#templates[@]} -eq 0 ]]; then |
| 504 | ubuntu_print_help |
| 505 | exit 0 |
| 506 | fi |
| 507 | |
| 508 | declare -A image_entry_cache=() |
| 509 | |
| 510 | for template in "${templates[@]}"; do |
| 511 | echo "Processing ${template}" |
| 512 | # 1. extract location by parsing template using arch |
| 513 | yq_filter=" |
| 514 | .images[] | [.location, .kernel.location, .kernel.cmdline] | @tsv |
| 515 | " |
| 516 | parsed=$(yq eval "${yq_filter}" "${template}") |
| 517 | |
| 518 | # 3. get the image location |
| 519 | arr=() |
| 520 | while IFS= read -r line; do arr+=("${line}"); done <<<"${parsed}" |
| 521 | locations=("${arr[@]}") |
| 522 | for ((index = 0; index < ${#locations[@]}; index++)); do |
| 523 | [[ ${locations[index]} != "null" ]] || continue |
| 524 | set -e |
| 525 | IFS=$'\t' read -r location kernel_location kernel_cmdline <<<"${locations[index]}" |
| 526 | set +e # Disable 'set -e' to avoid exiting on error for the next assignment. |
| 527 | overriding_version=$( |
| 528 | set -e # Enable 'set -e' for the next command. |
| 529 | ubuntu_version_resolve_aliases "${location}" "${overriding_flavor}" "${overriding_version}" |
| 530 | ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition. |
| 531 | # shellcheck disable=2181 |
| 532 | [[ $? -eq 0 ]] || continue |
| 533 | cache_key=$( |
| 534 | set -e # Enable 'set -e' for the next command. |
| 535 | ubuntu_cache_key_for_image_kernel_flavor_version "${location}" "${kernel_location}" |
| 536 | ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition. |
| 537 | # shellcheck disable=2181 |
| 538 | [[ $? -eq 0 ]] || continue |
| 539 | image_entry=$( |
| 540 | set -e # Enable 'set -e' for the next command. |
| 541 | if [[ -v image_entry_cache[${cache_key}] ]]; then |
| 542 | echo "${image_entry_cache[${cache_key}]}" |
| 543 | else |
| 544 | ubuntu_image_entry_for_image_kernel_flavor_version "${location}" "${kernel_location}" "${overriding_flavor}" "${overriding_version}" |
| 545 | fi |
| 546 | ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition. |
| 547 | # shellcheck disable=2181 |
| 548 | [[ $? -eq 0 ]] || continue |
| 549 | set -e |
| 550 | image_entry_cache[${cache_key}]="${image_entry}" |
| 551 | if [[ -n ${image_entry} ]]; then |
| 552 | [[ ${kernel_cmdline} != "null" ]] && |
| 553 | jq -e 'has("kernel")' <<<"${image_entry}" >/dev/null && |
| 554 | image_entry=$(jq ".kernel.cmdline = \"${kernel_cmdline}\"" <<<"${image_entry}") |
| 555 | echo "${image_entry}" | jq |
| 556 | limactl edit --log-level error --set " |
| 557 | .images[${index}] *= ${image_entry}| |
| 558 | (.images[${index}] | ..) style = \"double\" |
| 559 | " "${template}" |
| 560 | fi |
| 561 | done |
| 562 | done |