|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -eu -o pipefail |
| 4 | + |
| 5 | +# Functions in this script assume error handling with 'set -e'. |
| 6 | +# To ensure 'set -e' works correctly: |
| 7 | +# - Use 'set +e' before assignments and '$(set -e; <function>)' to capture output without exiting on errors. |
| 8 | +# - Avoid calling functions directly in conditions to prevent disabling 'set -e'. |
| 9 | +# - Use 'shopt -s inherit_errexit' (Bash 4.4+) to avoid repeated 'set -e' in all '$(...)'. |
| 10 | +shopt -s inherit_errexit || error_exit "inherit_errexit not supported. Please use bash 4.4 or later." |
| 11 | + |
| 12 | +function opensuse_print_help() { |
| 13 | + cat <<HELP |
| 14 | +$(basename "${BASH_SOURCE[0]}"): Update the openSUSE Linux image location in the specified templates |
| 15 | +
|
| 16 | +Usage: |
| 17 | + $(basename "${BASH_SOURCE[0]}") [--version-major-minor (<major>.<minor>|current|stable|tumbleweed)|--version-major <major> --version-minor <minor>] <template.yaml>... |
| 18 | +
|
| 19 | +Description: |
| 20 | + This script updates the openSUSE Linux image location in the specified templates. |
| 21 | + Image location basename format: |
| 22 | +
|
| 23 | + openSUSE-(Leap-<major minor version>|Tumbleweed)-Minimal-VM.<arch>-Cloud.qcow2 |
| 24 | +
|
| 25 | + Published openSUSE Linux image information is fetched from the following URLs: |
| 26 | +
|
| 27 | + Leap: |
| 28 | + <major>.<minor>: https://download.opensuse.org/distribution/leap/<major>.<minor>/appliances/?jsontable |
| 29 | + current: https://download.opensuse.org/distribution/openSUSE-current/appliances/?jsontable |
| 30 | + stable: https://download.opensuse.org/distribution/openSUSE-stable/appliances/?jsontable |
| 31 | + |
| 32 | + Tumbleweed: |
| 33 | + x86_64: https://download.opensuse.org/tumbleweed/appliances/?jsontable |
| 34 | + not x86_64: https://download.opensuse.org/ports/<arch>/tumbleweed/appliances/?jsontable |
| 35 | +
|
| 36 | + The downloaded files will be cached in the Lima cache directory. |
| 37 | +
|
| 38 | +Examples: |
| 39 | + Update the openSUSE Linux image location in templates/**.yaml: |
| 40 | + $ $(basename "${BASH_SOURCE[0]}") templates/**.yaml |
| 41 | +
|
| 42 | + Update the openSUSE Linux image location to version 15.6 in ~/.lima/opensuse/lima.yaml: |
| 43 | + $ $(basename "${BASH_SOURCE[0]}") --version-major-minor 15.6 ~/.lima/opensuse/lima.yaml |
| 44 | + $ limactl factory-reset opensuse |
| 45 | +
|
| 46 | + Update the openSUSE Linux image location to tumbleweed in ~/.lima/opensuse/lima.yaml: |
| 47 | + $ $(basename "${BASH_SOURCE[0]}") --version-major-minor tumbleweed ~/.lima/opensuse/lima.yaml |
| 48 | + $ limactl factory-reset opensuse |
| 49 | +
|
| 50 | +Flags: |
| 51 | + --version-major-minor (<major>.<minor>|current|stable|tumbleweed) Use the specified <major>.<minor> version or |
| 52 | + aliases "current", "stable", or "tumbleweed". |
| 53 | + The <major>.<minor> version must be 15.0 or later. |
| 54 | + --version-major <major> --version-minor <minor> Use the specified <major> and <minor> version. |
| 55 | + -h, --help Print this help message |
| 56 | +HELP |
| 57 | +} |
| 58 | + |
| 59 | +# print the URL spec for the given location |
| 60 | +function opensuse_url_spec_from_location() { |
| 61 | + local location=$1 jq_filter url_spec |
| 62 | + jq_filter='capture(" |
| 63 | + ^https://download\\.opensuse\\.org/(?: |
| 64 | + distribution/(?: |
| 65 | + leap/(?<path_version_leap>\\d+\\.\\d+)| |
| 66 | + openSUSE-(?<path_version_leap_alias>current|stable) |
| 67 | + )| |
| 68 | + (?:ports/aarch64/)?(?<path_version_tumbleweed>tumbleweed) |
| 69 | + )/appliances/ |
| 70 | + openSUSE-(?<version>Leap-\\d+\\.\\d+|Tumbleweed)-Minimal-VM |
| 71 | + \\.(?<arch>[^-]+)(?<major_minor_patch>-\\d+\\.\\d+\\.\\d+)?-(?<target_vendor>.*)(?<build_info>-Build\\d+\\.\\d+)?\\.(?<file_extension>.*)$ |
| 72 | + ";"x") | |
| 73 | + .path_version = (.path_version_leap // .path_version_leap_alias // .path_version_tumbleweed) |
| 74 | + ' |
| 75 | + url_spec=$(jq -e -r "${jq_filter}" <<<"\"${location}\"") |
| 76 | + echo "${url_spec}" |
| 77 | +} |
| 78 | +
|
| 79 | +readonly opensuse_jq_filter_directory='"https://download.opensuse.org/\( |
| 80 | + if .path_version == "tumbleweed" then |
| 81 | + if .arch != "x86_64" then |
| 82 | + "ports/\(.arch)/" |
| 83 | + else |
| 84 | + "" |
| 85 | + end + "tumbleweed" |
| 86 | + else |
| 87 | + "distribution/" + |
| 88 | + if .path_version == "current" or .path_version == "stable" then |
| 89 | + "openSUSE-\(.path_version)" |
| 90 | + else |
| 91 | + "leap/\(.path_version)" |
| 92 | + end |
| 93 | + end |
| 94 | +)/appliances/"' |
| 95 | +readonly opensuse_jq_filter_filename=' |
| 96 | + "openSUSE-\( |
| 97 | + if .version == "tumbleweed" then "Tumbleweed" else "Leap-\(.version)" end |
| 98 | + )-Minimal-VM.\(.arch)\( |
| 99 | + if .major_minor_patch then .major_minor_patch else "" end |
| 100 | + )-\(.target_vendor)\( |
| 101 | + if .build_info then .build_info else "" end |
| 102 | + ).\(.file_extension)" |
| 103 | +' |
| 104 | +
|
| 105 | +# print the location for the given URL spec |
| 106 | +function opensuse_location_from_url_spec() { |
| 107 | + local -r url_spec=$1 |
| 108 | + jq -e -r "${opensuse_jq_filter_directory} + ${opensuse_jq_filter_filename}" <<<"${url_spec}" || |
| 109 | + error_exit "Failed to get the location for ${url_spec}" |
| 110 | +} |
| 111 | +
|
| 112 | +function opensuse_image_directory_from_url_spec() { |
| 113 | + local -r url_spec=$1 |
| 114 | + jq -e -r "${opensuse_jq_filter_directory}" <<<"${url_spec}" || |
| 115 | + error_exit "Failed to get the image directory for ${url_spec}" |
| 116 | +} |
| 117 | +
|
| 118 | +function opensuse_image_filename_from_url_spec() { |
| 119 | + local -r url_spec=$1 |
| 120 | + jq -e -r "${opensuse_jq_filter_filename}" <<<"${url_spec}" || |
| 121 | + error_exit "Failed to get the image filename for ${url_spec}" |
| 122 | +} |
| 123 | +
|
| 124 | +function opensuse_json_url_from_url_spec() { |
| 125 | + local -r url_spec=$1 |
| 126 | + local json_url |
| 127 | + json_url="$(opensuse_image_directory_from_url_spec "${url_spec}")?jsontable" |
| 128 | + echo "${json_url}" |
| 129 | +} |
| 130 | +
|
| 131 | +# |
| 132 | +function opensuse_latest_image_entry_for_url_spec() { |
| 133 | + local url_spec=$1 arch json_url downloaded_json latest_version_info |
| 134 | + # shellcheck disable=SC2034 |
| 135 | + arch=$(jq -r '.arch' <<<"${url_spec}") |
| 136 | + json_url="$(opensuse_image_directory_from_url_spec "${url_spec}")?jsontable" |
| 137 | + downloaded_json=$(download_to_cache "${json_url}") |
| 138 | + latest_version_info=$(jq -e -r --argjson spec "${url_spec}" ' |
| 139 | + [ |
| 140 | + .data |sort_by(.mtime)|.[].name| |
| 141 | + if $spec.major_minor_patch then |
| 142 | + capture( |
| 143 | + "^openSUSE-(?:Leap-(?<version_leap>\\d+\\.\\d+)|(?<version_tumbleweed>Tumbleweed))-Minimal-VM |
| 144 | + \\.\($spec.arch)(?<major_minor_patch>-\\d+\\.\\d+\\.\\d+)?-\($spec.target_vendor)(?<build_info>-Build\\d+\\.\\d+)?\\.\($spec.file_extension)$" |
| 145 | + ;"x" |
| 146 | + ) |
| 147 | + else |
| 148 | + capture( |
| 149 | + "^openSUSE-(?:Leap-(?<version_leap>\\d+\\.\\d+)|(?<version_tumbleweed>Tumbleweed))-Minimal-VM |
| 150 | + \\.\($spec.arch)-\($spec.target_vendor)\\.\($spec.file_extension)$" |
| 151 | + ;"x" |
| 152 | + ) |
| 153 | + end | |
| 154 | + .version = (.version_leap // (.version_tumbleweed|ascii_downcase)) | |
| 155 | + .version_number_array = ([.version | scan("\\d+") | tonumber]) |
| 156 | + ] | sort_by(.version_number_array, .image_revision) | last |
| 157 | + ' <"${downloaded_json}") |
| 158 | + [[ -n ${latest_version_info} ]] || return |
| 159 | + local newer_url_spec location |
| 160 | + # prefer the <major>.<minor> in the path |
| 161 | + newer_url_spec=$(jq -e -r ". + ${latest_version_info} | .path_version = .version" <<<"${url_spec}") |
| 162 | + location=$(opensuse_location_from_url_spec "${newer_url_spec}") |
| 163 | + location=$(validate_url_without_redirect "${location}") |
| 164 | +
|
| 165 | + # Digest is not used here because URLs containing dates are not retained long-term. |
| 166 | + # Instead, URLs without dates must be used, and their content is often updated without a URL change, |
| 167 | + # resulting in only the digest being updated. Therefore, recording the digest is not meaningful. |
| 168 | + # |
| 169 | + # local sha256sum_location downloaded_sha256sum filename digest |
| 170 | + # sha256sum_location="${location}.sha256" |
| 171 | + # downloaded_sha256sum=$(download_to_cache "${sha256sum_location}") |
| 172 | + # filename=$(opensuse_image_filename_from_url_spec "${newer_url_spec}") |
| 173 | + # digest="sha256:$(awk '{print $1}' <"${downloaded_sha256sum}")" |
| 174 | + # [[ -n ${digest} ]] || error_exit "Failed to get the digest for ${filename}" |
| 175 | + json_vars location arch # digest |
| 176 | +} |
| 177 | +
|
| 178 | +function opensuse_cache_key_for_image_kernel() { |
| 179 | + local location=$1 url_spec |
| 180 | + url_spec=$(opensuse_url_spec_from_location "${location}") |
| 181 | + jq -r '["opensuse", .path_version, .target_vendor, .arch, .file_extension] | join(":")' <<<"${url_spec}" |
| 182 | +} |
| 183 | +
|
| 184 | +function opensuse_image_entry_for_image_kernel() { |
| 185 | + local location=$1 kernel_is_not_supported=$2 url_spec path_version overriding image_entry='' |
| 186 | + [[ ${kernel_is_not_supported} == "null" ]] || echo "Updating kernel information is not supported on openSUSE Linux" >&2 |
| 187 | + url_spec=$(opensuse_url_spec_from_location "${location}") |
| 188 | + path_version=$(jq -r '.path_version' <<<"${url_spec}") |
| 189 | + if [[ ${path_version} == "tumbleweed" ]]; then |
| 190 | + overriding=${3:-'{"path_version":"tumbleweed"}'} |
| 191 | + else |
| 192 | + overriding=${3:-'{"path_version":"stable"}'} |
| 193 | + fi |
| 194 | + url_spec=$(jq -r '. + '"${overriding}" <<<"${url_spec}") |
| 195 | + image_entry=$(opensuse_latest_image_entry_for_url_spec "${url_spec}") |
| 196 | + # shellcheck disable=SC2031 |
| 197 | + if [[ -z ${image_entry} ]]; then |
| 198 | + error_exit "Failed to get the ${url_spec} image location for ${location}" |
| 199 | + elif jq -e ".location == \"${location}\"" <<<"${image_entry}" >/dev/null; then |
| 200 | + echo "Image location is up-to-date: ${location}" >&2 |
| 201 | + else |
| 202 | + echo "${image_entry}" |
| 203 | + fi |
| 204 | +} |
| 205 | +
|
| 206 | +# check if the script is executed or sourced |
| 207 | +# shellcheck disable=SC1091 |
| 208 | +if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then |
| 209 | + scriptdir=$(dirname "${BASH_SOURCE[0]}") |
| 210 | + # shellcheck source=./cache-common-inc.sh |
| 211 | + . "${scriptdir}/cache-common-inc.sh" |
| 212 | +
|
| 213 | + # shellcheck source=/dev/null # avoid shellcheck hangs on source looping |
| 214 | + . "${scriptdir}/update-template.sh" |
| 215 | +else |
| 216 | + # this script is sourced |
| 217 | + if [[ -v SUPPORTED_DISTRIBUTIONS ]]; then |
| 218 | + SUPPORTED_DISTRIBUTIONS+=("opensuse") |
| 219 | + else |
| 220 | + declare -a SUPPORTED_DISTRIBUTIONS=("opensuse") |
| 221 | + fi |
| 222 | + return 0 |
| 223 | +fi |
| 224 | +
|
| 225 | +declare -a templates=() |
| 226 | +declare overriding='{}' |
| 227 | +declare version_major='' version_minor='' |
| 228 | +while [[ $# -gt 0 ]]; do |
| 229 | + case "$1" in |
| 230 | + -h | --help) |
| 231 | + opensuse_print_help |
| 232 | + exit 0 |
| 233 | + ;; |
| 234 | + -d | --debug) set -x ;; |
| 235 | + --version-major-minor) |
| 236 | + if [[ -n ${2:-} && $2 != -* ]]; then |
| 237 | + version="$2" |
| 238 | + shift |
| 239 | + else |
| 240 | + error_exit "--version-major-minor requires a value" |
| 241 | + fi |
| 242 | + ;& |
| 243 | + --version-major-minor=*) |
| 244 | + version=${version:-${1#*=}} |
| 245 | + overriding=$( |
| 246 | + version="${version#v}" |
| 247 | + if [[ ${version} =~ ^v?[0-9]+.[0-9]+ ]]; then |
| 248 | + version="$(echo "${version}" | cut -d. -f1-2)" |
| 249 | + [[ ${version%%.*} -ge 15 ]] || error_exit "openSUSE Linux version must be 15.0 or later" |
| 250 | + path_version="${version}" |
| 251 | + elif [[ ${version} == "current" || ${version} == "stable" || ${version} == "tumbleweed" ]]; then |
| 252 | + path_version=${version} |
| 253 | + else |
| 254 | + error_exit "--version-major-minor requires a value in the format <major>.<minor>, current, stable, or tumbleweed" |
| 255 | + fi |
| 256 | + json_vars path_version <<<"${overriding}" |
| 257 | + ) |
| 258 | + ;; |
| 259 | + --version-major) |
| 260 | + if [[ -n ${2:-} && $2 != -* ]]; then |
| 261 | + version_major="$2" |
| 262 | + shift |
| 263 | + else |
| 264 | + error_exit "--version-major requires a value" |
| 265 | + fi |
| 266 | + ;& |
| 267 | + --version-major=*) |
| 268 | + version_major=${version_major:-${1#*=}} |
| 269 | + [[ ${version_major} =~ ^[0-9]+$ ]] || error_exit "Please specify --version-major in numbers" |
| 270 | + ;; |
| 271 | + --version-minor) |
| 272 | + if [[ -n ${2:-} && $2 != -* ]]; then |
| 273 | + version_minor="$2" |
| 274 | + shift |
| 275 | + else |
| 276 | + error_exit "--version-minor requires a value" |
| 277 | + fi |
| 278 | + ;& |
| 279 | + --version-minor=*) |
| 280 | + version_minor=${version_minor:-${1#*=}} |
| 281 | + [[ ${version_minor} =~ ^[0-9]+$ ]] || error_exit "Please specify --version-minor in numbers" |
| 282 | + ;; |
| 283 | + *.yaml) templates+=("$1") ;; |
| 284 | + *) |
| 285 | + error_exit "Unknown argument: $1" |
| 286 | + ;; |
| 287 | + esac |
| 288 | + shift |
| 289 | + [[ -z ${overriding} ]] && overriding="{}" |
| 290 | +done |
| 291 | +
|
| 292 | +if ! jq -e '.path_version' <<<"${overriding}" >/dev/null; then # --version-major-minor is not specified |
| 293 | + if [[ -n ${version_major} && -n ${version_minor} ]]; then |
| 294 | + [[ ${version_major} -ge 15 ]] || error_exit "openSUSE Linux version must be 15.0 or later" |
| 295 | + # shellcheck disable=2034 |
| 296 | + path_version="${version_major}.${version_minor}" |
| 297 | + overriding=$(json_vars path_version <<<"${overriding}") |
| 298 | + elif [[ -n ${version_major} ]]; then |
| 299 | + error_exit "--version-minor is required when --version-major is specified" |
| 300 | + elif [[ -n ${version_minor} ]]; then |
| 301 | + error_exit "--version-major is required when --version-minor is specified" |
| 302 | + fi |
| 303 | +elif [[ -n ${version_major} || -n ${version_minor} ]]; then # --version-major-minor is specified |
| 304 | + echo "Ignoring --version-major and --version-minor because --version-major-minor is specified" >&2 |
| 305 | +fi |
| 306 | +[[ ${overriding} == "{}" ]] && overriding='' |
| 307 | +
|
| 308 | +if [[ ${#templates[@]} -eq 0 ]]; then |
| 309 | + opensuse_print_help |
| 310 | + exit 0 |
| 311 | +fi |
| 312 | +
|
| 313 | +declare -A image_entry_cache=() |
| 314 | +
|
| 315 | +for template in "${templates[@]}"; do |
| 316 | + echo "Processing ${template}" |
| 317 | + # 1. extract location by parsing template using arch |
| 318 | + yq_filter=" |
| 319 | + .images[] | [.location, .kernel.location, .kernel.cmdline] | @tsv |
| 320 | + " |
| 321 | + parsed=$(yq eval "${yq_filter}" "${template}") |
| 322 | +
|
| 323 | + # 3. get the image location |
| 324 | + arr=() |
| 325 | + while IFS= read -r line; do arr+=("${line}"); done <<<"${parsed}" |
| 326 | + locations=("${arr[@]}") |
| 327 | + for ((index = 0; index < ${#locations[@]}; index++)); do |
| 328 | + [[ ${locations[index]} != "null" ]] || continue |
| 329 | + set -e |
| 330 | + IFS=$'\t' read -r location kernel_location kernel_cmdline <<<"${locations[index]}" |
| 331 | + set +e # Disable 'set -e' to avoid exiting on error for the next assignment. |
| 332 | + cache_key=$( |
| 333 | + set -e # Enable 'set -e' for the next command. |
| 334 | + opensuse_cache_key_for_image_kernel "${location}" "${kernel_location}" |
| 335 | + ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition. |
| 336 | + # shellcheck disable=2181 |
| 337 | + [[ $? -eq 0 ]] || continue |
| 338 | + image_entry=$( |
| 339 | + set -e # Enable 'set -e' for the next command. |
| 340 | + if [[ -v image_entry_cache[${cache_key}] ]]; then |
| 341 | + echo "${image_entry_cache[${cache_key}]}" |
| 342 | + else |
| 343 | + opensuse_image_entry_for_image_kernel "${location}" "${kernel_location}" "${overriding}" |
| 344 | + fi |
| 345 | + ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition. |
| 346 | + # shellcheck disable=2181 |
| 347 | + [[ $? -eq 0 ]] || continue |
| 348 | + set -e |
| 349 | + image_entry_cache[${cache_key}]="${image_entry}" |
| 350 | + if [[ -n ${image_entry} ]]; then |
| 351 | + [[ ${kernel_cmdline} != "null" ]] && |
| 352 | + jq -e 'has("kernel")' <<<"${image_entry}" >/dev/null && |
| 353 | + image_entry=$(jq ".kernel.cmdline = \"${kernel_cmdline}\"" <<<"${image_entry}") |
| 354 | + echo "${image_entry}" | jq |
| 355 | + limactl edit --log-level error --set " |
| 356 | + .images[${index}] = ${image_entry}| |
| 357 | + (.images[${index}] | ..) style = \"double\" |
| 358 | + " "${template}" |
| 359 | + fi |
| 360 | + done |
| 361 | +done |
0 commit comments