|
| 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 fedora_print_help() { |
| 13 | + cat <<HELP |
| 14 | +$(basename "${BASH_SOURCE[0]}"): Update the Fedora Linux image location in the specified templates |
| 15 | +
|
| 16 | +Usage: |
| 17 | + $(basename "${BASH_SOURCE[0]}") [--version (<version number>|release|development[/<version number>]|rawhide)] <template.yaml>... |
| 18 | +
|
| 19 | +Description: |
| 20 | + This script updates the Fedora Linux image location in the specified templates. |
| 21 | + Image location basename format: |
| 22 | +
|
| 23 | + Fedora-Cloud-Base[-<target vendor>]-<version>-<build info>.<arch>.qcow2 |
| 24 | + Fedora-Cloud-Base[-<target vendor>].<arch>-<version>-<build info>.qcow2 |
| 25 | +
|
| 26 | + Published Fedora Linux image information is fetched from the following URL: |
| 27 | +
|
| 28 | + ${fedora_image_list_url} |
| 29 | +
|
| 30 | + The downloaded files will be cached in the Lima cache directory. |
| 31 | +
|
| 32 | +Examples: |
| 33 | + Update the Fedora Linux image location in templates/**.yaml: |
| 34 | + $ $(basename "${BASH_SOURCE[0]}") templates/**.yaml |
| 35 | +
|
| 36 | + Update the Fedora Linux image location to version 41 in ~/.lima/fedora/lima.yaml: |
| 37 | + $ $(basename "${BASH_SOURCE[0]}") --version 41 ~/.lima/fedora/lima.yaml |
| 38 | + $ limactl factory-reset fedora |
| 39 | +
|
| 40 | +Flags: |
| 41 | + --version <version> Use the specified version. |
| 42 | + The version must be <version number>, 'release', 'development[/<version number>]', or 'rawhide'. |
| 43 | + -h, --help Print this help message |
| 44 | +HELP |
| 45 | +} |
| 46 | + |
| 47 | +# print the URL spec for the given location |
| 48 | +function fedora_url_spec_from_location() { |
| 49 | + local location=$1 jq_filter url_spec |
| 50 | + jq_filter='capture(" |
| 51 | + ^https://download\\.fedoraproject\\.org/pub/fedora/linux/(?<path_version>(releases|development)/(\\d+|rawhide))/Cloud/(?<path_arch>[^/]+)/images/ |
| 52 | + Fedora-Cloud-Base(?<target_vendor>-Generic)?( |
| 53 | + (-(?<version_before_arch>\\d+|Rawhide)-(?<build_info_before_arch>[^-]+)(?<arch_postfix>\\.[^.]+))| |
| 54 | + ((?<arch_prefix>\\.[^-]+)-(?<version_after_arch>\\d+|Rawhide)-(?<build_info_after_arch>[^-]+)) |
| 55 | + )\\.(?<file_extension>.*)$ |
| 56 | + ";"x") | |
| 57 | + .version = (.version_before_arch // .version_after_arch) | |
| 58 | + .build_info = (.build_info_before_arch // .build_info_after_arch ) | |
| 59 | + map_values(. // empty) # remove null values |
| 60 | + ' |
| 61 | + url_spec=$(jq -e -r "${jq_filter}" <<<"\"${location}\"") |
| 62 | + echo "${url_spec}" |
| 63 | +} |
| 64 | +
|
| 65 | +readonly fedora_jq_filter_directory='"https://download.fedoraproject.org/pub/fedora/linux/\(.path_version)/Cloud/\(.path_arch)/images/"' |
| 66 | +readonly fedora_jq_filter_filename=' |
| 67 | + "Fedora-Cloud-Base\(.target_vendor // "")\(.arch_prefix // "")-\(.version)-\(.build_info)\(.arch_postfix // "").\(.file_extension)" |
| 68 | +' |
| 69 | +
|
| 70 | +readonly fedora_jq_filter_checksum_filename=' |
| 71 | + "Fedora-Cloud-\( |
| 72 | + if .path_version|startswith("development/") then |
| 73 | + "images-\(.version)-\(.path_arch)-\(.build_info)" |
| 74 | + else |
| 75 | + "\(.version)-\(.build_info)-\(.path_arch)" |
| 76 | + end |
| 77 | + )-CHECKSUM" |
| 78 | +' |
| 79 | +
|
| 80 | +# print the location for the given URL spec |
| 81 | +function fedora_location_from_url_spec() { |
| 82 | + local -r url_spec=$1 |
| 83 | + jq -e -r "${fedora_jq_filter_directory} + ${fedora_jq_filter_filename}" <<<"${url_spec}" || |
| 84 | + error_exit "Failed to get the location for ${url_spec}" |
| 85 | +} |
| 86 | +
|
| 87 | +function fedora_image_directory_from_url_spec() { |
| 88 | + local -r url_spec=$1 |
| 89 | + jq -e -r "${fedora_jq_filter_directory}" <<<"${url_spec}" || |
| 90 | + error_exit "Failed to get the image directory for ${url_spec}" |
| 91 | +} |
| 92 | +
|
| 93 | +function fedora_image_filename_from_url_spec() { |
| 94 | + local -r url_spec=$1 |
| 95 | + jq -e -r "${fedora_jq_filter_filename}" <<<"${url_spec}" || |
| 96 | + error_exit "Failed to get the image filename for ${url_spec}" |
| 97 | +} |
| 98 | +
|
| 99 | +function fedora_image_checksum_filename_from_url_spec() { |
| 100 | + local -r url_spec=$1 |
| 101 | + jq -e -r "${fedora_jq_filter_checksum_filename}" <<<"${url_spec}" || |
| 102 | + error_exit "Failed to get the checksum filename for ${url_spec}" |
| 103 | +} |
| 104 | +
|
| 105 | +readonly fedora_image_list_url='https://dl.fedoraproject.org/pub/fedora/imagelist-fedora' |
| 106 | +# |
| 107 | +function fedora_latest_image_entry_for_url_spec() { |
| 108 | + local url_spec=$1 arch image_list spec_for_query latest_version_info |
| 109 | + # shellcheck disable=SC2034 |
| 110 | + arch=$(jq -r '.path_arch' <<<"${url_spec}") |
| 111 | + image_list=$(download_to_cache "${fedora_image_list_url}") |
| 112 | + spec_for_query=$(jq -r '. | {path_version, path_arch, file_extension}' <<<"${url_spec}") |
| 113 | + latest_version_info=$(jq -e -Rrs --argjson spec "${spec_for_query}" ' |
| 114 | + [ |
| 115 | + split("\n").[] | |
| 116 | + capture(" |
| 117 | + ^\\./linux/(?<path_version>\($spec.path_version))/Cloud/\($spec.path_arch)/images/ |
| 118 | + Fedora-Cloud-Base(?<target_vendor>-Generic)?( |
| 119 | + -(?<version_before_arch>\\d+|Rawhide)-(?<build_info_before_arch>[^-]+)(?<arch_postfix>\\.\($spec.path_arch))| |
| 120 | + (?<arch_prefix>\\.\($spec.path_arch))-(?<version_after_arch>\\d+|Rawhide)-(?<build_info_after_arch>[^-]+) |
| 121 | + )\\.\($spec.file_extension)$ |
| 122 | + ";"x") | |
| 123 | + .version = (.version_before_arch // .version_after_arch) | |
| 124 | + .build_info = (.build_info_before_arch // .build_info_after_arch) | |
| 125 | + # do not remove null values. we need them for creating newer_url_spec |
| 126 | + # map_values(. // empty) | |
| 127 | + .version_number_array = ([(if (.version|test("\\d+")) then (.version|tonumber) else .version end)] + [.build_info | scan("\\d+") | tonumber]) |
| 128 | + ] | sort_by(.version_number_array) | last |
| 129 | + ' <"${image_list}" || error_exit "Failed to get the latest version info for ${spec_for_query}") |
| 130 | + [[ -n ${latest_version_info} ]] || return |
| 131 | + local newer_url_spec directory filename location sha512sum_location downloaded_sha256sum digest |
| 132 | + # prefer the v<major>.<minor> in the path |
| 133 | + newer_url_spec=$(jq -e -r ". + ${latest_version_info}" <<<"${url_spec}") |
| 134 | + directory=$(fedora_image_directory_from_url_spec "${newer_url_spec}") |
| 135 | + filename=$(fedora_image_filename_from_url_spec "${newer_url_spec}") |
| 136 | + location="${directory}${filename}" |
| 137 | + # validate the location. use original url since the location may be redirected to some mirror |
| 138 | + location=$(validate_url_without_redirect "${location}") |
| 139 | + sha512sum_location="${directory}$(fedora_image_checksum_filename_from_url_spec "${newer_url_spec}")" |
| 140 | + # download the checksum file and get the sha256sum |
| 141 | + # cache original url since the checksum file may be redirected to some mirror |
| 142 | + downloaded_sha256sum=$(download_to_cache_without_redirect "${sha512sum_location}") |
| 143 | + digest="sha256:$(awk "/SHA256 \(${filename}\) =/{print \$4}" "${downloaded_sha256sum}")" |
| 144 | + [[ -n ${digest} ]] || error_exit "Failed to get the digest for ${filename}" |
| 145 | + json_vars location arch digest |
| 146 | +} |
| 147 | +
|
| 148 | +function fedora_cache_key_for_image_kernel() { |
| 149 | + local location=$1 url_spec |
| 150 | + url_spec=$(fedora_url_spec_from_location "${location}") |
| 151 | + jq -r '["fedora", .path_version, .target_vendor, .path_arch, .file_extension] | join(":")' <<<"${url_spec}" |
| 152 | +} |
| 153 | +
|
| 154 | +function fedora_image_entry_for_image_kernel() { |
| 155 | + local location=$1 kernel_is_not_supported=$2 overriding=${3:-'{"path_version":"releases/\\d+"}'} url_spec image_entry='' |
| 156 | + [[ ${kernel_is_not_supported} == "null" ]] || echo "Updating kernel information is not supported on Fedora Linux" >&2 |
| 157 | + url_spec=$(fedora_url_spec_from_location "${location}" | jq -r ". + ${overriding}") |
| 158 | + image_entry=$(fedora_latest_image_entry_for_url_spec "${url_spec}") |
| 159 | + # shellcheck disable=SC2031 |
| 160 | + if [[ -z ${image_entry} ]]; then |
| 161 | + error_exit "Failed to get the ${url_spec} image location for ${location}" |
| 162 | + elif jq -e ".location == \"${location}\"" <<<"${image_entry}" >/dev/null; then |
| 163 | + echo "Image location is up-to-date: ${location}" >&2 |
| 164 | + else |
| 165 | + echo "${image_entry}" |
| 166 | + fi |
| 167 | +} |
| 168 | +
|
| 169 | +# check if the script is executed or sourced |
| 170 | +# shellcheck disable=SC1091 |
| 171 | +if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then |
| 172 | + scriptdir=$(dirname "${BASH_SOURCE[0]}") |
| 173 | + # shellcheck source=./cache-common-inc.sh |
| 174 | + . "${scriptdir}/cache-common-inc.sh" |
| 175 | +
|
| 176 | + # shellcheck source=/dev/null # avoid shellcheck hangs on source looping |
| 177 | + . "${scriptdir}/update-template.sh" |
| 178 | +else |
| 179 | + # this script is sourced |
| 180 | + if [[ -v SUPPORTED_DISTRIBUTIONS ]]; then |
| 181 | + SUPPORTED_DISTRIBUTIONS+=("fedora") |
| 182 | + else |
| 183 | + declare -a SUPPORTED_DISTRIBUTIONS=("fedora") |
| 184 | + fi |
| 185 | + return 0 |
| 186 | +fi |
| 187 | +
|
| 188 | +declare -a templates=() |
| 189 | +declare overriding='{}' |
| 190 | +while [[ $# -gt 0 ]]; do |
| 191 | + case "$1" in |
| 192 | + -h | --help) |
| 193 | + fedora_print_help |
| 194 | + exit 0 |
| 195 | + ;; |
| 196 | + -d | --debug) set -x ;; |
| 197 | + --version) |
| 198 | + if [[ -n ${2:-} && $2 != -* ]]; then |
| 199 | + version="$2" |
| 200 | + shift |
| 201 | + else |
| 202 | + error_exit "--version requires a value" |
| 203 | + fi |
| 204 | + ;& |
| 205 | + --version=*) |
| 206 | + version=${version:-${1#*=}} |
| 207 | + overriding=$( |
| 208 | + [[ ${version} =~ ^[0-9]+$ ]] && path_version="releases/${version}" |
| 209 | + [[ ${version} =~ ^releases?$ ]] && path_version="releases/\d+" |
| 210 | + [[ ${version} == "development" ]] && path_version="development/\d+" |
| 211 | + [[ ${version} =~ ^(releases|development)/([0-9]+)$ ]] && path_version="${version}" |
| 212 | + [[ ${version} =~ ^(development/)?rawhide$ ]] && path_version="development/rawhide" |
| 213 | + [[ -n ${path_version:-} ]] || |
| 214 | + error_exit "The version must be <version number>, 'release', 'development[/<version number>'], or 'rawhide'." |
| 215 | + json_vars path_version <<<"${overriding}" |
| 216 | + ) |
| 217 | + ;; |
| 218 | + *.yaml) templates+=("$1") ;; |
| 219 | + *) |
| 220 | + error_exit "Unknown argument: $1" |
| 221 | + ;; |
| 222 | + esac |
| 223 | + shift |
| 224 | + [[ -z ${overriding} ]] && overriding="{}" |
| 225 | +done |
| 226 | +
|
| 227 | +if [[ ${#templates[@]} -eq 0 ]]; then |
| 228 | + fedora_print_help |
| 229 | + exit 0 |
| 230 | +fi |
| 231 | +
|
| 232 | +declare -A image_entry_cache=() |
| 233 | +
|
| 234 | +for template in "${templates[@]}"; do |
| 235 | + echo "Processing ${template}" |
| 236 | + # 1. extract location by parsing template using arch |
| 237 | + yq_filter=" |
| 238 | + .images[] | [.location, .kernel.location, .kernel.cmdline] | @tsv |
| 239 | + " |
| 240 | + parsed=$(yq eval "${yq_filter}" "${template}") |
| 241 | +
|
| 242 | + # 3. get the image location |
| 243 | + arr=() |
| 244 | + while IFS= read -r line; do arr+=("${line}"); done <<<"${parsed}" |
| 245 | + locations=("${arr[@]}") |
| 246 | + for ((index = 0; index < ${#locations[@]}; index++)); do |
| 247 | + [[ ${locations[index]} != "null" ]] || continue |
| 248 | + set -e |
| 249 | + IFS=$'\t' read -r location kernel_location kernel_cmdline <<<"${locations[index]}" |
| 250 | + set +e # Disable 'set -e' to avoid exiting on error for the next assignment. |
| 251 | + cache_key=$( |
| 252 | + set -e # Enable 'set -e' for the next command. |
| 253 | + fedora_cache_key_for_image_kernel "${location}" "${kernel_location}" |
| 254 | + ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition. |
| 255 | + # shellcheck disable=2181 |
| 256 | + [[ $? -eq 0 ]] || continue |
| 257 | + image_entry=$( |
| 258 | + set -e # Enable 'set -e' for the next command. |
| 259 | + if [[ -v image_entry_cache[${cache_key}] ]]; then |
| 260 | + echo "${image_entry_cache[${cache_key}]}" |
| 261 | + else |
| 262 | + fedora_image_entry_for_image_kernel "${location}" "${kernel_location}" "${overriding}" |
| 263 | + fi |
| 264 | + ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition. |
| 265 | + # shellcheck disable=2181 |
| 266 | + [[ $? -eq 0 ]] || continue |
| 267 | + set -e |
| 268 | + image_entry_cache[${cache_key}]="${image_entry}" |
| 269 | + if [[ -n ${image_entry} ]]; then |
| 270 | + [[ ${kernel_cmdline} != "null" ]] && |
| 271 | + jq -e 'has("kernel")' <<<"${image_entry}" >/dev/null && |
| 272 | + image_entry=$(jq ".kernel.cmdline = \"${kernel_cmdline}\"" <<<"${image_entry}") |
| 273 | + echo "${image_entry}" | jq |
| 274 | + limactl edit --log-level error --set " |
| 275 | + .images[${index}] = ${image_entry}| |
| 276 | + (.images[${index}] | ..) style = \"double\" |
| 277 | + " "${template}" |
| 278 | + fi |
| 279 | + done |
| 280 | +done |
0 commit comments