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