| 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 rocky_print_help() { |
| 16 | cat <<HELP |
| 17 | $(basename "${BASH_SOURCE[0]}"): Update the Rocky Linux image location in the specified templates |
| 18 | |
| 19 | Usage: |
| 20 | $(basename "${BASH_SOURCE[0]}") [--version-major <major version>] <template.yaml>... |
| 21 | |
| 22 | Description: |
| 23 | This script updates the Rocky Linux image location in the specified templates. |
| 24 | If the image location in the template contains a minor version, release date, and job id in the URL, |
| 25 | the script replaces it with the latest available minor version, release date, and job id. |
| 26 | |
| 27 | Image location basename format: |
| 28 | |
| 29 | Rocky-<major version>-GenericCloud[.latest|-<type>.latest|-<major version>.<minor version>-<date>.<job id?>].<arch>.qcow2 |
| 30 | |
| 31 | Published Rocky Linux image information is fetched from the following URLs: |
| 32 | |
| 33 | https://dl.rockylinux.org/pub/rocky/<major version>/images/<arch>/ |
| 34 | |
| 35 | To parsing html, this script requires 'htmlq' or 'pup' command. |
| 36 | The downloaded files will be cached in the Lima cache directory. |
| 37 | |
| 38 | Examples: |
| 39 | Update the Rocky Linux image location in templates/**.yaml: |
| 40 | $ $(basename "${BASH_SOURCE[0]}") templates/**.yaml |
| 41 | |
| 42 | Update the Rocky Linux image location in ~/.lima/rocky/lima.yaml: |
| 43 | $ $(basename "${BASH_SOURCE[0]}") ~/.lima/rocky/lima.yaml |
| 44 | $ limactl factory-reset rocky |
| 45 | |
| 46 | Update the Rocky Linux image location to major version 9 in ~/.lima/rocky/lima.yaml: |
| 47 | $ $(basename "${BASH_SOURCE[0]}") --version-major 9 ~/.lima/rocky/lima.yaml |
| 48 | $ limactl factory-reset rocky |
| 49 | |
| 50 | Flags: |
| 51 | --version-major <version> Use the specified version. The version must be 8 or later. |
| 52 | -h, --help Print this help message |
| 53 | HELP |
| 54 | } |
| 55 | |
| 56 | |
| 57 | function rocky_url_spec_from_location() { |
| 58 | local location=$1 jq_filter url_spec |
| 59 | jq_filter='capture(" |
| 60 | ^https://dl\\.rockylinux\\.org/pub/rocky/(?<path_version>\\d+(\\.\\d+)?)/images/(?<path_arch>[^/]+)/ |
| 61 | Rocky-(?<major_version>\\d+)-(?<target_vendor>.*) |
| 62 | ( |
| 63 | -(?<type>.*)-(?<major_minor_version>\\d+\\.\\d+)-(?<date_and_ci_job_id>\\d{8}\\.\\d+)| |
| 64 | -(?<type_latest>.*)\\.latest| |
| 65 | \\.latest |
| 66 | )\\.(?<arch>[^.]+).(?<file_extension>.*)$ |
| 67 | ";"x") |
| 68 | ' |
| 69 | url_spec=$(jq -e -r "${jq_filter}" <<<"\"${location}\"") |
| 70 | |
| 71 | jq -e '.path_arch == .arch' <<<"${url_spec}" >/dev/null || |
| 72 | error_exit "Validation failed: .path_arch != .arch: ${location}" |
| 73 | echo "${url_spec}" |
| 74 | } |
| 75 | |
| 76 | readonly rocky_jq_filter_directory='"https://dl.rockylinux.org/pub/rocky/\(.path_version)/images/\(.path_arch)/"' |
| 77 | readonly rocky_jq_filter_filename=' |
| 78 | "Rocky-\(.major_version)-\(.target_vendor)\( |
| 79 | if .date_and_ci_job_id then |
| 80 | "-\(.type)-\(.major_minor_version)-\(.date_and_ci_job_id)" |
| 81 | else |
| 82 | if .type then |
| 83 | "-\(.type_latest).latest" |
| 84 | else |
| 85 | ".latest" |
| 86 | end |
| 87 | end |
| 88 | ).\(.arch).\(.file_extension)" |
| 89 | ' |
| 90 | |
| 91 | |
| 92 | function rocky_location_from_url_spec() { |
| 93 | local -r url_spec=$1 |
| 94 | jq -e -r "${rocky_jq_filter_directory} + ${rocky_jq_filter_filename}" <<<"${url_spec}" || |
| 95 | error_exit "Failed to get the location for ${url_spec}" |
| 96 | } |
| 97 | |
| 98 | function rocky_image_directory_from_url_spec() { |
| 99 | local -r url_spec=$1 |
| 100 | jq -e -r "${rocky_jq_filter_directory}" <<<"${url_spec}" || |
| 101 | error_exit "Failed to get the image directory for ${url_spec}" |
| 102 | } |
| 103 | |
| 104 | function rocky_image_filename_from_url_spec() { |
| 105 | local -r url_spec=$1 |
| 106 | jq -e -r "${rocky_jq_filter_filename}" <<<"${url_spec}" || |
| 107 | error_exit "Failed to get the image filename for ${url_spec}" |
| 108 | } |
| 109 | |
| 110 | |
| 111 | function rocky_latest_image_entry_for_url_spec() { |
| 112 | local url_spec=$1 arch major_version_url_spec major_version_image_directory downloaded_page links_in_page latest_minor_version_info |
| 113 | arch=$(jq -r '.arch' <<<"${url_spec}") |
| 114 | |
| 115 | |
| 116 | local major_version latest_minor |
| 117 | major_version=$(jq -r '.major_version' <<<"${url_spec}") |
| 118 | latest_minor=$(curl -s "https://dl.rockylinux.org/pub/rocky/" | grep -oE "href=[\"']${major_version}\.[0-9]+/" | sed -E "s/href=[\"']//;s/\///" | sort -V | tail -1) |
| 119 | |
| 120 | major_version_url_spec=$(jq -e -r --arg v "${latest_minor:-$major_version}" '.path_version = $v' <<<"${url_spec}") |
| 121 | major_version_image_directory=$(rocky_image_directory_from_url_spec "${major_version_url_spec}") |
| 122 | downloaded_page=$(download_to_cache "${major_version_image_directory}") |
| 123 | if command -v htmlq >/dev/null; then |
| 124 | links_in_page=$(htmlq 'pre a' --attribute href <"${downloaded_page}") |
| 125 | elif command -v pup >/dev/null; then |
| 126 | links_in_page=$(pup 'pre a attr{href}' <"${downloaded_page}") |
| 127 | fi |
| 128 | |
| 129 | |
| 130 | |
| 131 | if [[ -z ${links_in_page} ]]; then |
| 132 | links_in_page=$(grep -oE 'href="[^"]+"' "${downloaded_page}" | sed -E 's/href="([^"]+)"/\1/') |
| 133 | fi |
| 134 | |
| 135 | if [[ -z ${links_in_page} ]]; then |
| 136 | error_exit "Failed to list images from ${major_version_image_directory}" |
| 137 | fi |
| 138 | latest_minor_version_info=$(jq -e -Rrs --argjson spec "${url_spec}" ' |
| 139 | [ |
| 140 | split("\n")[] | |
| 141 | capture( |
| 142 | "^Rocky-\($spec.major_version)-\($spec.target_vendor)-\($spec.type)" + |
| 143 | "-(?<major_minor_version>\($spec.major_version)\\.\\d+)" + |
| 144 | "-(?<date_and_ci_job_id>\\d{8}\\.\\d+)\\.\($spec.arch)\\.\($spec.file_extension)$" |
| 145 | ;"x" |
| 146 | ) | |
| 147 | .version_number_array = ([.major_minor_version | scan("\\d+") | tonumber]) |
| 148 | ] | sort_by([.version_number_array, .date_and_ci_job_id]) | last |
| 149 | ' <<<"${links_in_page}") |
| 150 | [[ -n ${latest_minor_version_info} ]] || return |
| 151 | local newer_url_spec location sha256sum_location downloaded_sha256sum filename digest |
| 152 | |
| 153 | newer_url_spec=$(jq -e -r ". + ${latest_minor_version_info} | .path_version = .major_minor_version" <<<"${url_spec}") |
| 154 | location=$(rocky_location_from_url_spec "${newer_url_spec}") |
| 155 | sha256sum_location="${location}.CHECKSUM" |
| 156 | downloaded_sha256sum=$(download_to_cache "${sha256sum_location}") |
| 157 | filename=$(rocky_image_filename_from_url_spec "${newer_url_spec}") |
| 158 | |
| 159 | digest="sha256:$(awk "/SHA256 \(${filename}\) =/{print \$4}" "${downloaded_sha256sum}" | head -n 1)" |
| 160 | [[ -n ${digest} ]] || error_exit "Failed to get the SHA256 digest for ${filename}" |
| 161 | json_vars location arch digest |
| 162 | } |
| 163 | |
| 164 | function rocky_cache_key_for_image_kernel() { |
| 165 | local location=$1 url_spec |
| 166 | url_spec=$(rocky_url_spec_from_location "${location}") |
| 167 | jq -r '["rocky", .major_minor_version // .major_version, .target_vendor, |
| 168 | if .date_and_ci_job_id then "timestamped" else "latest" end, |
| 169 | .arch, .file_extension] | join(":")' <<<"${url_spec}" |
| 170 | } |
| 171 | |
| 172 | function rocky_image_entry_for_image_kernel() { |
| 173 | local location=$1 kernel_is_not_supported=$2 overriding=${3:-"{}"} url_spec image_entry='' |
| 174 | [[ ${kernel_is_not_supported} == "null" ]] || echo "Updating kernel information is not supported on Rocky Linux" >&2 |
| 175 | url_spec=$(rocky_url_spec_from_location "${location}" | jq -r ". + ${overriding}") |
| 176 | if jq -e '.date_and_ci_job_id' <<<"${url_spec}" >/dev/null; then |
| 177 | image_entry=$(rocky_latest_image_entry_for_url_spec "${url_spec}") |
| 178 | else |
| 179 | image_entry=$( |
| 180 | # shellcheck disable=SC2030 |
| 181 | location=$(rocky_location_from_url_spec "${url_spec}") |
| 182 | location=$(validate_url_without_redirect "${location}") |
| 183 | arch=$(jq -r '.path_arch' <<<"${url_spec}") |
| 184 | json_vars location arch |
| 185 | ) |
| 186 | fi |
| 187 | # shellcheck disable=SC2031 |
| 188 | if [[ -z ${image_entry} ]]; then |
| 189 | error_exit "Failed to get the ${url_spec} image location for ${location}" |
| 190 | elif jq -e ".location == \"${location}\"" <<<"${image_entry}" >/dev/null; then |
| 191 | echo "Image location is up-to-date: ${location}" >&2 |
| 192 | else |
| 193 | echo "${image_entry}" |
| 194 | fi |
| 195 | } |
| 196 | |
| 197 | # check if the script is executed or sourced |
| 198 | # shellcheck disable=SC1091 |
| 199 | if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then |
| 200 | scriptdir=$(dirname "${BASH_SOURCE[0]}") |
| 201 | # shellcheck source=./cache-common-inc.sh |
| 202 | . "${scriptdir}/cache-common-inc.sh" |
| 203 | |
| 204 | if ! command -v htmlq >/dev/null && ! command -v pup >/dev/null; then |
| 205 | error_exit "Please install 'htmlq' or 'pup' to list images from https://dl.rockylinux.org/pub/rocky/<version>/images/<arch>/" |
| 206 | fi |
| 207 | # shellcheck source=/dev/null # avoid shellcheck hangs on source looping |
| 208 | . "${scriptdir}/update-template.sh" |
| 209 | else |
| 210 | # this script is sourced |
| 211 | if ! command -v htmlq >/dev/null && ! command -v pup >/dev/null; then |
| 212 | echo "Please install 'htmlq' or 'pup' to list images from https://dl.rockylinux.org/pub/rocky/<version>/images/<arch>/" >&2 |
| 213 | elif [[ -v SUPPORTED_DISTRIBUTIONS ]]; then |
| 214 | SUPPORTED_DISTRIBUTIONS+=("rocky") |
| 215 | else |
| 216 | declare -a SUPPORTED_DISTRIBUTIONS=("rocky") |
| 217 | fi |
| 218 | return 0 |
| 219 | fi |
| 220 | |
| 221 | declare -a templates=() |
| 222 | declare overriding="{}" |
| 223 | while [[ $# -gt 0 ]]; do |
| 224 | case "$1" in |
| 225 | -h | --help) |
| 226 | rocky_print_help |
| 227 | exit 0 |
| 228 | ;; |
| 229 | -d | --debug) set -x ;; |
| 230 | --version-major) |
| 231 | if [[ -n $2 && $2 != -* ]]; then |
| 232 | overriding=$( |
| 233 | major_version="${2%%.*}" |
| 234 | [[ ${major_version} -ge 8 ]] || error_exit "Rocky Linux major version must be 8 or later" |
| 235 | # shellcheck disable=2034 |
| 236 | path_version="${major_version}" |
| 237 | json_vars path_version major_version <<<"${overriding}" |
| 238 | ) |
| 239 | shift |
| 240 | else |
| 241 | error_exit "--version-major requires a value" |
| 242 | fi |
| 243 | ;; |
| 244 | --version-major=*) |
| 245 | overriding=$( |
| 246 | major_version="${1#*=}" |
| 247 | major_version="${major_version%%.*}" |
| 248 | [[ ${major_version} -ge 8 ]] || error_exit "Rocky Linux major version must be 8 or later" |
| 249 | # shellcheck disable=2034 |
| 250 | path_version="${major_version}" |
| 251 | json_vars path_version major_version <<<"${overriding}" |
| 252 | ) |
| 253 | ;; |
| 254 | *.yaml) templates+=("$1") ;; |
| 255 | *) |
| 256 | error_exit "Unknown argument: $1" |
| 257 | ;; |
| 258 | esac |
| 259 | shift |
| 260 | [[ -z ${overriding} ]] && overriding="{}" |
| 261 | done |
| 262 | |
| 263 | if [[ ${#templates[@]} -eq 0 ]]; then |
| 264 | rocky_print_help |
| 265 | exit 0 |
| 266 | fi |
| 267 | |
| 268 | declare -A image_entry_cache=() |
| 269 | |
| 270 | for template in "${templates[@]}"; do |
| 271 | echo "Processing ${template}" |
| 272 | # 1. extract location by parsing template using arch |
| 273 | yq_filter=" |
| 274 | .images[] | [.location, .kernel.location, .kernel.cmdline] | @tsv |
| 275 | " |
| 276 | parsed=$(yq eval "${yq_filter}" "${template}") |
| 277 | |
| 278 | # 3. get the image location |
| 279 | arr=() |
| 280 | while IFS= read -r line; do arr+=("${line}"); done <<<"${parsed}" |
| 281 | locations=("${arr[@]}") |
| 282 | for ((index = 0; index < ${#locations[@]}; index++)); do |
| 283 | [[ ${locations[index]} != "null" ]] || continue |
| 284 | set -e |
| 285 | IFS=$'\t' read -r location kernel_location kernel_cmdline <<<"${locations[index]}" |
| 286 | set +e # Disable 'set -e' to avoid exiting on error for the next assignment. |
| 287 | cache_key=$( |
| 288 | set -e # Enable 'set -e' for the next command. |
| 289 | rocky_cache_key_for_image_kernel "${location}" "${kernel_location}" |
| 290 | ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition. |
| 291 | # shellcheck disable=2181 |
| 292 | [[ $? -eq 0 ]] || continue |
| 293 | image_entry=$( |
| 294 | set -e # Enable 'set -e' for the next command. |
| 295 | if [[ -v image_entry_cache[${cache_key}] ]]; then |
| 296 | echo "${image_entry_cache[${cache_key}]}" |
| 297 | else |
| 298 | rocky_image_entry_for_image_kernel "${location}" "${kernel_location}" "${overriding}" |
| 299 | fi |
| 300 | ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition. |
| 301 | # shellcheck disable=2181 |
| 302 | [[ $? -eq 0 ]] || continue |
| 303 | set -e |
| 304 | image_entry_cache[${cache_key}]="${image_entry}" |
| 305 | if [[ -n ${image_entry} ]]; then |
| 306 | [[ ${kernel_cmdline} != "null" ]] && |
| 307 | jq -e 'has("kernel")' <<<"${image_entry}" >/dev/null && |
| 308 | image_entry=$(jq ".kernel.cmdline = \"${kernel_cmdline}\"" <<<"${image_entry}") |
| 309 | echo "${image_entry}" | jq |
| 310 | limactl edit --log-level error --set " |
| 311 | .images[${index}] *= ${image_entry}| |
| 312 | (.images[${index}] | ..) style = \"double\" |
| 313 | " "${template}" |
| 314 | fi |
| 315 | done |
| 316 | done |