| 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 debian_print_help() { |
| 16 | cat <<HELP |
| 17 | $(basename "${BASH_SOURCE[0]}"): Update the Debian image location in the specified templates |
| 18 | |
| 19 | Usage: |
| 20 | $(basename "${BASH_SOURCE[0]}") [--backports[=<bool>]] [--daily[=<bool>]] [--timestamped[=<bool>]] [--version <version>] <template.yaml>... |
| 21 | |
| 22 | Description: |
| 23 | This script updates the Debian 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 version from the image location basename in the template. |
| 26 | |
| 27 | Image location basename format: debian-<version>[-backports]-genericcloud-<arch>[-daily][-<timestamp>].qcow2 |
| 28 | |
| 29 | Published Debian image information is fetched from the following URLs: |
| 30 | |
| 31 | https://cloud.debian.org/images/cloud/<codename>[-backports]/[daily/](latest|<timestamp>)/debian-<version>[-backports]-genericcloud-<arch>[-daily][-<timestamp>].json |
| 32 | |
| 33 | The downloaded JSON file will be cached in the Lima cache directory. |
| 34 | |
| 35 | Examples: |
| 36 | Update the Debian image location in templates/**.yaml: |
| 37 | $ $(basename "${BASH_SOURCE[0]}") templates/**.yaml |
| 38 | |
| 39 | Update the Debian image location in ~/.lima/debian/lima.yaml: |
| 40 | $ $(basename "${BASH_SOURCE[0]}") ~/.lima/debian/lima.yaml |
| 41 | $ limactl factory-reset debian |
| 42 | |
| 43 | Update the Debian image location to debian-13-genericcloud-<arch>.qcow2 in ~/.lima/debian/lima.yaml: |
| 44 | $ $(basename "${BASH_SOURCE[0]}") --version trixie ~/.lima/debian/lima.yaml |
| 45 | $ limactl factory-reset debian |
| 46 | |
| 47 | Flags: |
| 48 | --backports[=<bool>] Use the backports image |
| 49 | The boolean value can be true, false, 1, or 0 |
| 50 | --daily[=<bool>] Use the daily image |
| 51 | --timestamped[=<bool>] Use the timestamped image |
| 52 | --version <version> Use the specified version |
| 53 | The version can be a codename, version number, or alias (testing, stable, oldstable) |
| 54 | -h, --help Print this help message |
| 55 | HELP |
| 56 | } |
| 57 | |
| 58 | readonly debian_base_url=https://cloud.debian.org/images/cloud/ |
| 59 | |
| 60 | function debian_target_vendor_from_arch() { |
| 61 | local arch=$1 |
| 62 | case "$arch" in |
| 63 | amd64 | arm64) |
| 64 | echo "genericcloud" |
| 65 | ;; |
| 66 | *) |
| 67 | echo "generic" |
| 68 | ;; |
| 69 | esac |
| 70 | } |
| 71 | |
| 72 | readonly -A debian_version_to_codename=( |
| 73 | [10]=buster |
| 74 | [11]=bullseye |
| 75 | [12]=bookworm |
| 76 | [13]=trixie |
| 77 | [14]=forky |
| 78 | ) |
| 79 | |
| 80 | declare -A debian_codename_to_version |
| 81 | function debian_setup_codename_to_version() { |
| 82 | local version codename |
| 83 | for version in "${!debian_version_to_codename[@]}"; do |
| 84 | codename=${debian_version_to_codename[${version}]} |
| 85 | debian_codename_to_version[${codename}]="${version}" |
| 86 | done |
| 87 | readonly -A debian_codename_to_version |
| 88 | } |
| 89 | debian_setup_codename_to_version |
| 90 | |
| 91 | readonly -A debian_alias_to_codename=( |
| 92 | [testing]=trixie |
| 93 | [stable]=bookworm |
| 94 | [oldstable]=bullseye |
| 95 | ) |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | function debian_downloaded_json() { |
| 104 | local url_spec=$1 json_url_spec json_url |
| 105 | json_url_spec=$(jq -r '. | del(.timestamp) | .file_extension = "json"' <<<"${url_spec}") || error_exit "Failed to create JSON URL spec" |
| 106 | json_url=$(debian_location_from_url_spec "${json_url_spec}") |
| 107 | download_to_cache "${json_url}" |
| 108 | } |
| 109 | |
| 110 | function debian_digest_from_upload_entry() { |
| 111 | local upload_entry=$1 debian_digest digest |
| 112 | debian_digest=$(jq -e -r '.metadata.annotations."cloud.debian.org/digest"' <<<"${upload_entry}") || |
| 113 | error_exit "Failed to get the digest from ${upload_entry}" |
| 114 | case "${debian_digest%:*}" in |
| 115 | sha512) digest=$(echo "${debian_digest#*:}==" | base64 -d | xxd -p -c 128) || |
| 116 | error_exit "Failed to decode the digest from ${debian_digest}" ;; |
| 117 | *) error_exit "Unsupported digest type: ${debian_digest%:*}" ;; |
| 118 | esac |
| 119 | echo "${debian_digest/:*/:}${digest}" |
| 120 | } |
| 121 | |
| 122 | |
| 123 | function debian_image_url_timestamped() { |
| 124 | local url_spec=$1 debian_downloaded_json jq_filter upload_entry timestamp timestamped_url_spec location arch digest |
| 125 | debian_downloaded_json=$(debian_downloaded_json "${url_spec}") |
| 126 | |
| 127 | jq_filter=' |
| 128 | [.items[]|select(.kind == "Upload")| |
| 129 | select(.metadata.labels."upload.cloud.debian.org/image-format" == $ARGS.named.url_spec.image_format)]|first |
| 130 | ' |
| 131 | upload_entry=$(jq -e -r --argjson url_spec "${url_spec}" "${jq_filter}" "${debian_downloaded_json}") || |
| 132 | error_exit "Failed to find the upload entry from ${debian_downloaded_json}" |
| 133 | timestamp=$(jq -e -r '.metadata.labels."cloud.debian.org/version"' <<<"${upload_entry}") || |
| 134 | error_exit "Failed to get the timestamp from ${upload_entry}" |
| 135 | timestamped_url_spec=$(json_vars timestamp <<<"${url_spec}") |
| 136 | location=$(debian_location_from_url_spec "${timestamped_url_spec}") |
| 137 | location=$(validate_url_without_redirect "${location}") |
| 138 | arch=$(jq -e -r '.arch' <<<"${url_spec}") || error_exit "missing arch in ${url_spec}" |
| 139 | arch=$(limayaml_arch "${arch}") |
| 140 | digest=$(debian_digest_from_upload_entry "${upload_entry}") |
| 141 | json_vars location arch digest |
| 142 | } |
| 143 | |
| 144 | |
| 145 | function debian_image_url_not_timestamped() { |
| 146 | local url_spec=$1 location arch |
| 147 | location=$(debian_location_from_url_spec "${url_spec}") |
| 148 | location=$(validate_url_without_redirect "${location}") |
| 149 | arch=$(jq -e -r '.arch' <<<"${url_spec}") || error_exit "missing arch in ${url_spec}" |
| 150 | arch=$(limayaml_arch "${arch}") |
| 151 | json_vars location arch |
| 152 | } |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | function debian_version_resolve_aliases() { |
| 171 | local version=$1 |
| 172 | [[ -v debian_alias_to_codename[${version}] ]] && version=${debian_alias_to_codename[${version}]} |
| 173 | [[ -v debian_codename_to_version[${version}] ]] && version=${debian_codename_to_version[${version}]} |
| 174 | [[ -v debian_version_to_codename[${version}] ]] || error_exit "Unsupported version: ${version}" |
| 175 | [[ -z ${version} ]] || echo "${version}" |
| 176 | } |
| 177 | |
| 178 | function debian_arch_from_location_basename() { |
| 179 | local location=$1 location_basename arch |
| 180 | location_basename=$(basename "${location}") |
| 181 | location_basename=${location_basename/-backports/} |
| 182 | arch=$(echo "${location_basename}" | cut -d- -f4 | cut -d. -f1) |
| 183 | [[ -n ${arch} ]] || error_exit "Failed to get arch from ${location}" |
| 184 | echo "${arch}" |
| 185 | } |
| 186 | |
| 187 | |
| 188 | |
| 189 | function debian_target_vendor_from_location_basename() { |
| 190 | local location=$1 location_basename target_vendor |
| 191 | location_basename=$(basename "${location}") |
| 192 | location_basename=${location_basename/-backports/} |
| 193 | target_vendor=$(echo "${location_basename}" | cut -d- -f3) |
| 194 | [[ -n ${target_vendor} ]] || error_exit "Failed to get target_vendor from ${location}" |
| 195 | echo "${target_vendor}" |
| 196 | } |
| 197 | |
| 198 | function debian_file_extension_from_location_basename() { |
| 199 | local location=$1 location_basename file_extension |
| 200 | location_basename=$(basename "${location}") |
| 201 | file_extension=$(echo "${location_basename}" | cut -d. -f2-) |
| 202 | [[ -n ${file_extension} ]] || error_exit "Failed to get file extension from ${location}" |
| 203 | echo "${file_extension}" |
| 204 | } |
| 205 | |
| 206 | function debian_image_format_from_file_extension() { |
| 207 | local file_extension=$1 |
| 208 | case "${file_extension}" in |
| 209 | json) echo "json" ;; |
| 210 | qcow2) echo "qcow2" ;; |
| 211 | raw) echo "raw" ;; |
| 212 | tar.xz) echo "internal" ;; |
| 213 | *) error_exit "Unsupported file extension: ${file_extension}" ;; |
| 214 | esac |
| 215 | } |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | function debian_url_spec_from_location() { |
| 240 | local location=$1 backports=false daily=false timestamp='' codename version='' arch target_vendor file_extension image_format |
| 241 | local -r timestamp_pattern='[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]' |
| 242 | case "${location}" in |
| 243 | ${debian_base_url}*-backports/*) backports=true ;;& |
| 244 | ${debian_base_url}*/daily/*) daily=true ;;& |
| 245 | ${debian_base_url}*/${timestamp_pattern}/*) [[ ${location} =~ ${timestamp_pattern} ]] && timestamp=${BASH_REMATCH[0]} ;; |
| 246 | ${debian_base_url}*/latest/*) timestamp='' ;; |
| 247 | *) |
| 248 | |
| 249 | return 1 |
| 250 | ;; |
| 251 | esac |
| 252 | codename=$(echo "${location#"${debian_base_url}"}" | cut -d/ -f1 | cut -d- -f1) |
| 253 | [[ -v debian_codename_to_version[${codename}] ]] || error_exit "Unknown codename: ${codename}" |
| 254 | version=${debian_codename_to_version[${codename}]} |
| 255 | arch=$(debian_arch_from_location_basename "${location}") |
| 256 | target_vendor=$(debian_target_vendor_from_location_basename "${location}") |
| 257 | file_extension=$(debian_file_extension_from_location_basename "${location}") |
| 258 | image_format=$(debian_image_format_from_file_extension "${file_extension}") |
| 259 | json_vars backports daily timestamp version arch target_vendor file_extension image_format |
| 260 | } |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | function debian_location_from_url_spec() { |
| 283 | local url_spec=$1 base_url version backports daily timestamp arch target_vendor file_extension |
| 284 | base_url=${debian_base_url} |
| 285 | version=$(jq -e -r '.version' <<<"${url_spec}") |
| 286 | [[ -v debian_version_to_codename[${version}] ]] || error_exit "Unsupported version: ${version}" |
| 287 | base_url+=${debian_version_to_codename[${version}]} |
| 288 | backports=$(jq -r 'if .backports then "-backports" else empty end' <<<"${url_spec}") |
| 289 | base_url+=${backports}/ |
| 290 | daily=$(jq -r 'if .daily then "daily" else empty end' <<<"${url_spec}") |
| 291 | base_url+=${daily:+${daily}/} |
| 292 | timestamp=$(jq -r 'if .timestamp then .timestamp else empty end' <<<"${url_spec}") |
| 293 | base_url+=${timestamp:-latest}/ |
| 294 | arch=$(jq -e -r '.arch' <<<"${url_spec}") |
| 295 | target_vendor=$(jq -r '.target_vendor // empty' <<<"${url_spec}") |
| 296 | [[ -z ${target_vendor} ]] && target_vendor=$(debian_target_vendor_from_arch "${arch}") |
| 297 | file_extension=$(jq -e -r '.file_extension' <<<"${url_spec}") |
| 298 | base_url+=debian-${version}${backports}-${target_vendor}-${arch}${daily:+-${daily}}${timestamp:+-${timestamp}}.${file_extension} |
| 299 | echo "${base_url}" |
| 300 | } |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | function debian_cache_key_for_image_kernel_overriding() { |
| 315 | local location=$1 kernel_location=${2:-null} url_spec with_kernel='' version backports arch daily timestamped file_extension |
| 316 | url_spec=$(debian_url_spec_from_location "${location}") |
| 317 | [[ ${kernel_location} != "null" ]] && with_kernel=_with_kernel |
| 318 | version=$(jq -r '.version|if . then "-\(.)" else empty end' <<<"${url_spec}") |
| 319 | backports=$(jq -r 'if .backports then "-backports" else empty end' <<<"${url_spec}") |
| 320 | arch=$(jq -e -r '.arch' <<<"${url_spec}") |
| 321 | target_vendor=$(jq -r '.target_vendor // empty' <<<"${url_spec}") |
| 322 | [[ -z ${target_vendor} ]] && target_vendor=$(debian_target_vendor_from_arch "${arch}") |
| 323 | daily=$(jq -r 'if .daily then "-daily" else empty end' <<<"${url_spec}") |
| 324 | timestamped=$(jq -r 'if .timestamp then "-timestamped" else empty end' <<<"${url_spec}") |
| 325 | file_extension=$(jq -e -r '.file_extension' <<<"${url_spec}") |
| 326 | echo "debian${with_kernel}${version}${backports}-${target_vendor}-${arch}${daily}${timestamped}.${file_extension}" |
| 327 | } |
| 328 | |
| 329 | function debian_image_entry_for_image_kernel_overriding() { |
| 330 | local location=$1 kernel_location=$2 overriding=${3:-"{}"} url_spec timestamped |
| 331 | [[ ${kernel_location} == "null" ]] || error_exit "Updating image with kernel is not supported" |
| 332 | url_spec=$(debian_url_spec_from_location "${location}" | jq -r ". + ${overriding}") |
| 333 | timestamped=$(jq -r 'if .timestamp then "timestamped" else "not_timestamped" end' <<<"${url_spec}") |
| 334 | |
| 335 | local image_entry |
| 336 | image_entry=$(debian_image_url_"${timestamped}" "${url_spec}") |
| 337 | if [[ -z ${image_entry} ]]; then |
| 338 | error_exit "Failed to get the ${url_spec} image location for ${location}" |
| 339 | elif jq -e ".location == \"${location}\"" <<<"${image_entry}" >/dev/null; then |
| 340 | echo "Image location is up-to-date: ${location}" >&2 |
| 341 | else |
| 342 | echo "${image_entry}" |
| 343 | fi |
| 344 | } |
| 345 | |
| 346 | # check if the script is executed or sourced |
| 347 | # shellcheck disable=SC1091 |
| 348 | if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then |
| 349 | scriptdir=$(dirname "${BASH_SOURCE[0]}") |
| 350 | # shellcheck source=./cache-common-inc.sh |
| 351 | . "${scriptdir}/cache-common-inc.sh" |
| 352 | |
| 353 | # shellcheck source=/dev/null # avoid shellcheck hangs on source looping |
| 354 | . "${scriptdir}/update-template.sh" |
| 355 | else |
| 356 | # this script is sourced |
| 357 | if [[ -v SUPPORTED_DISTRIBUTIONS ]]; then |
| 358 | SUPPORTED_DISTRIBUTIONS+=("debian") |
| 359 | else |
| 360 | declare -a SUPPORTED_DISTRIBUTIONS=("debian") |
| 361 | fi |
| 362 | # required functions for Debian |
| 363 | function debian_cache_key_for_image_kernel() { debian_cache_key_for_image_kernel_overriding "$@"; } |
| 364 | function debian_image_entry_for_image_kernel() { debian_image_entry_for_image_kernel_overriding "$@"; } |
| 365 | |
| 366 | return 0 |
| 367 | fi |
| 368 | |
| 369 | declare -a templates=() |
| 370 | declare overriding="{}" |
| 371 | while [[ $# -gt 0 ]]; do |
| 372 | case "$1" in |
| 373 | -h | --help) |
| 374 | debian_print_help |
| 375 | exit 0 |
| 376 | ;; |
| 377 | -d | --debug) set -x ;; |
| 378 | --backports | --daily | --timestamped) |
| 379 | overriding=$(json "${1#--}" true <<<"${overriding}") |
| 380 | ;; |
| 381 | --backports=* | --daily=* | --timestamped=*) |
| 382 | overriding=$( |
| 383 | key=${1#--} value=$(validate_boolean "${1#*=}") |
| 384 | json "${key%%=*}" "${value}" <<<"${overriding}" |
| 385 | ) |
| 386 | ;; |
| 387 | --version) |
| 388 | if [[ -n $2 && $2 != -* ]]; then |
| 389 | overriding=$( |
| 390 | version=$(debian_version_resolve_aliases "$2") |
| 391 | json_vars version <<<"${overriding}" |
| 392 | ) |
| 393 | shift |
| 394 | else |
| 395 | error_exit "--version requires a value" |
| 396 | fi |
| 397 | ;; |
| 398 | --version=*) |
| 399 | overriding=$( |
| 400 | version=$(debian_version_resolve_aliases "${1#*=}") |
| 401 | json_vars version <<<"${overriding}" |
| 402 | ) |
| 403 | ;; |
| 404 | *.yaml) templates+=("$1") ;; |
| 405 | *) |
| 406 | error_exit "Unknown argument: $1" |
| 407 | ;; |
| 408 | esac |
| 409 | shift |
| 410 | [[ -z ${overriding} ]] && overriding="{}" |
| 411 | done |
| 412 | |
| 413 | if [[ ${#templates[@]} -eq 0 ]]; then |
| 414 | debian_print_help |
| 415 | exit 0 |
| 416 | fi |
| 417 | |
| 418 | declare -A image_entry_cache=() |
| 419 | |
| 420 | for template in "${templates[@]}"; do |
| 421 | echo "Processing ${template}" |
| 422 | # 1. extract location by parsing template using arch |
| 423 | yq_filter=" |
| 424 | .images[] | [.location, .kernel.location, .kernel.cmdline] | @tsv |
| 425 | " |
| 426 | parsed=$(yq eval "${yq_filter}" "${template}") |
| 427 | |
| 428 | # 3. get the image location |
| 429 | arr=() |
| 430 | while IFS= read -r line; do arr+=("${line}"); done <<<"${parsed}" |
| 431 | locations=("${arr[@]}") |
| 432 | for ((index = 0; index < ${#locations[@]}; index++)); do |
| 433 | [[ ${locations[index]} != "null" ]] || continue |
| 434 | set -e |
| 435 | IFS=$'\t' read -r location kernel_location kernel_cmdline <<<"${locations[index]}" |
| 436 | set +e # Disable 'set -e' to avoid exiting on error for the next assignment. |
| 437 | cache_key=$( |
| 438 | set -e # Enable 'set -e' for the next command. |
| 439 | debian_cache_key_for_image_kernel_overriding "${location}" "${kernel_location}" |
| 440 | ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition. |
| 441 | # shellcheck disable=2181 |
| 442 | [[ $? -eq 0 ]] || continue |
| 443 | image_entry=$( |
| 444 | set -e # Enable 'set -e' for the next command. |
| 445 | if [[ -v image_entry_cache[${cache_key}] ]]; then |
| 446 | echo "${image_entry_cache[${cache_key}]}" |
| 447 | else |
| 448 | debian_image_entry_for_image_kernel_overriding "${location}" "${kernel_location}" "${overriding}" |
| 449 | fi |
| 450 | ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition. |
| 451 | # shellcheck disable=2181 |
| 452 | [[ $? -eq 0 ]] || continue |
| 453 | set -e |
| 454 | image_entry_cache[${cache_key}]="${image_entry}" |
| 455 | if [[ -n ${image_entry} ]]; then |
| 456 | [[ ${kernel_cmdline} != "null" ]] && |
| 457 | jq -e 'has("kernel")' <<<"${image_entry}" >/dev/null && |
| 458 | image_entry=$(jq ".kernel.cmdline = \"${kernel_cmdline}\"" <<<"${image_entry}") |
| 459 | echo "${image_entry}" | jq |
| 460 | limactl edit --log-level error --set " |
| 461 | .images[${index}] *= ${image_entry}| |
| 462 | (.images[${index}] | ..) style = \"double\" |
| 463 | " "${template}" |
| 464 | fi |
| 465 | done |
| 466 | done |