|
| 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 archlinux_print_help() { |
| 13 | + cat <<HELP |
| 14 | +$(basename "${BASH_SOURCE[0]}"): Update the Arch-Linux image location in the specified templates |
| 15 | +
|
| 16 | +Usage: |
| 17 | + $(basename "${BASH_SOURCE[0]}") <template.yaml>... |
| 18 | +
|
| 19 | +Description: |
| 20 | + This script updates the Arch-Linux image location in the specified templates. |
| 21 | + If the image location in the template contains a release date in the URL, the script replaces it with the latest available date. |
| 22 | +
|
| 23 | + Image location basename format: Arch-Linux-<arch>-cloudimg[-<date>.<CI_JOB_ID>].qcow2 |
| 24 | +
|
| 25 | + Published Arch-Linux image information is fetched from the following URLs: |
| 26 | +
|
| 27 | + x86_64: |
| 28 | + listing: https://gitlab.archlinux.org/api/v4/projects/archlinux%2Farch-boxes/packages |
| 29 | + details: https://gitlab.archlinux.org/api/v4/projects/archlinux%2Farch-boxes/packages/:package_id/package_files |
| 30 | + |
| 31 | + aarch64: |
| 32 | + https://github.com/mcginty/arch-boxes-arm/releases/ |
| 33 | + |
| 34 | + Using 'gh' CLI tool for fetching the latest release from GitHub. |
| 35 | +
|
| 36 | +Examples: |
| 37 | + Update the Arch-Linux image location in templates/**.yaml: |
| 38 | + $ $(basename "${BASH_SOURCE[0]}") templates/**.yaml |
| 39 | +
|
| 40 | + Update the Arch-Linux image location in ~/.lima/archlinux/lima.yaml: |
| 41 | + $ $(basename "${BASH_SOURCE[0]}") ~/.lima/archlinux/lima.yaml |
| 42 | + $ limactl factory-reset archlinux |
| 43 | +
|
| 44 | +Flags: |
| 45 | + -h, --help Print this help message |
| 46 | +HELP |
| 47 | +} |
| 48 | + |
| 49 | +# print the URL spec for the given location |
| 50 | +# shellcheck disable=SC2034 |
| 51 | +function archlinux_url_spec_from_location() { |
| 52 | + local location=$1 location_basename arch flavor source date_and_ci_job_id='' file_extension |
| 53 | + location_basename=$(basename "${location}") |
| 54 | + arch=$(echo "${location_basename}" | cut -d- -f3) |
| 55 | + flavor=$(echo "${location_basename}" | cut -d- -f4 | cut -d. -f1) |
| 56 | + case "${location}" in |
| 57 | + https://geo.mirror.pkgbuild.com/images/*) |
| 58 | + source="geo.mirror.pkgbuild.com" |
| 59 | + local -r date_and_ci_job_id_pattern='[0-9]{8}\.[0-9]+' |
| 60 | + if [[ ${location} =~ ${date_and_ci_job_id_pattern} ]]; then |
| 61 | + date_and_ci_job_id="${BASH_REMATCH[0]}" |
| 62 | + fi |
| 63 | + if [[ ${location_basename} =~ ${date_and_ci_job_id_pattern} ]]; then |
| 64 | + file_extension=${location_basename##*"${BASH_REMATCH[0]}".} |
| 65 | + else |
| 66 | + file_extension=${location_basename#*.} |
| 67 | + fi |
| 68 | + ;; |
| 69 | + https://github.com/mcginty/arch-boxes-arm/releases/download/*) |
| 70 | + source="github.com/mcginty/arch-boxes-arm" |
| 71 | + local -r date_pattern='[0-9]{8}' |
| 72 | + if [[ ${location} =~ ${date_pattern} ]]; then |
| 73 | + date_and_ci_job_id="${BASH_REMATCH[0]}" |
| 74 | + file_extension=${location_basename#*"${date_and_ci_job_id}".*.} |
| 75 | + else |
| 76 | + error_exit "Failed to extract date from ${location}" |
| 77 | + fi |
| 78 | + ;; |
| 79 | + *) |
| 80 | + # Unsupported location |
| 81 | + return 1 |
| 82 | + ;; |
| 83 | + esac |
| 84 | + json_vars source arch flavor date_and_ci_job_id file_extension |
| 85 | +} |
| 86 | + |
| 87 | +# print the location for the given URL spec |
| 88 | +function archlinux_location_from_url_spec() { |
| 89 | + local url_spec=$1 source arch flavor date_and_ci_job_id file_extension location='' |
| 90 | + source=$(jq -r '.source' <<<"${url_spec}") |
| 91 | + arch=$(jq -r '.arch' <<<"${url_spec}") |
| 92 | + flavor=$(jq -r '.flavor' <<<"${url_spec}") |
| 93 | + date_and_ci_job_id=$(jq -r '.date_and_ci_job_id' <<<"${url_spec}") |
| 94 | + |
| 95 | + file_extension=$(jq -r '.file_extension' <<<"${url_spec}") |
| 96 | + case "${source}" in |
| 97 | + geo.mirror.pkgbuild.com) |
| 98 | + location="https://geo.mirror.pkgbuild.com/images/" |
| 99 | + if [[ -n ${date_and_ci_job_id} ]]; then |
| 100 | + location+="v${date_and_ci_job_id}/Arch-Linux-${arch}-${flavor}-${date_and_ci_job_id}.${file_extension}" |
| 101 | + else |
| 102 | + location+="latest/Arch-Linux-${arch}-${flavor}.${file_extension}" |
| 103 | + fi |
| 104 | + ;; |
| 105 | + github.com/mcginty/arch-boxes-arm) ;; |
| 106 | + *) |
| 107 | + error_exit "Unsupported source: ${source}" |
| 108 | + ;; |
| 109 | + esac |
| 110 | + echo "${location}" |
| 111 | +} |
| 112 | + |
| 113 | +# returns the image entry for the latest image in the gitlab mirror |
| 114 | +function archlinux_image_entry_for_image_kernel_gitlab_mirror() { |
| 115 | + local location=$1 url_spec=$2 arch flavor date_and_ci_job_id gitlab_package_api_base latest_package_id jq_filter latest_package_file file_name digest updated_url_spec |
| 116 | + arch=$(jq -r '.arch' <<<"${url_spec}") |
| 117 | + if ! jq -e '.date_and_ci_job_id' <<<"${url_spec}" >/dev/null; then |
| 118 | + json_vars location arch |
| 119 | + return 1 |
| 120 | + fi |
| 121 | + flavor=$(jq -r '.flavor' <<<"${url_spec}") |
| 122 | + date_and_ci_job_id=$(jq -r '.date_and_ci_job_id' <<<"${url_spec}") |
| 123 | + file_extension=$(jq -r '.file_extension' <<<"${url_spec}") |
| 124 | + gitlab_package_api_base="https://gitlab.archlinux.org/api/v4/projects/archlinux%2Farch-boxes/packages" |
| 125 | + latest_package_id=$(curl --silent --show-error "${gitlab_package_api_base}" | jq -r 'last|.id') || error_exit "Failed to fetch latest package_id" |
| 126 | + jq_filter=" |
| 127 | + .[]|select(.file_name|test(\"^Arch-Linux-${arch}-${flavor}-.*\\\.${file_extension}\$\")) |
| 128 | + " |
| 129 | + latest_package_file=$(curl -s "${gitlab_package_api_base}/${latest_package_id}/package_files" | jq -r "${jq_filter}") || |
| 130 | + error_exit "Failed to fetch latest package_file" |
| 131 | + file_name=$(jq -r '.file_name' <<<"${latest_package_file}") |
| 132 | + digest="sha256:$(jq -r '.file_sha256' <<<"${latest_package_file}")" |
| 133 | + local -r date_and_ci_job_id_pattern='[0-9]{8}\.[0-9]+' |
| 134 | + [[ ${file_name} =~ ${date_and_ci_job_id_pattern} ]] || error_exit "Failed to extract date_and_ci_job_id from ${file_name}" |
| 135 | + date_and_ci_job_id="${BASH_REMATCH[0]}" |
| 136 | + updated_url_spec=$(json_vars date_and_ci_job_id <<<"${url_spec}") |
| 137 | + location=$(archlinux_location_from_url_spec "${updated_url_spec}") |
| 138 | + location=$(validate_url_without_redirect "${location}") |
| 139 | + json_vars location arch digest |
| 140 | +} |
| 141 | + |
| 142 | +# returns the image entry for the latest image in the GitHub repo |
| 143 | +function archlinux_image_entry_for_image_kernel_github_com() { |
| 144 | + local location=$1 url_spec=$2 arch flavor file_extension repo jq_filter latest_location downloaded_img digest |
| 145 | + arch=$(jq -r '.arch' <<<"${url_spec}") |
| 146 | + if ! jq -e '.date_and_ci_job_id' <<<"${url_spec}" >/dev/null; then |
| 147 | + json_vars location arch |
| 148 | + return 1 |
| 149 | + fi |
| 150 | + flavor=$(jq -r '.flavor' <<<"${url_spec}") |
| 151 | + file_extension=$(jq -r '.file_extension' <<<"${url_spec}") |
| 152 | + command -v gh >/dev/null || error_exit "gh is required for fetching the latest release from GitHub, but it's not installed" |
| 153 | + local -r repo_pattern='github.com/(.*)/releases/download/(.*)' |
| 154 | + if [[ ${location} =~ ${repo_pattern} ]]; then |
| 155 | + repo="${BASH_REMATCH[1]}" |
| 156 | + else |
| 157 | + error_exit "Failed to extract repo and release from ${location}" |
| 158 | + fi |
| 159 | + jq_filter=".assets[]|select(.name|test(\"^Arch-Linux-${arch}-${flavor}-.*\\\.${file_extension}\$\"))|.url" |
| 160 | + latest_location=$(gh release view --repo "${repo}" --json assets --jq "${jq_filter}") |
| 161 | + [[ -n ${latest_location} ]] || error_exit "Failed to fetch the latest release URL from ${repo}" |
| 162 | + if [[ ${location} == "${latest_location}" ]]; then |
| 163 | + json_vars location arch |
| 164 | + return |
| 165 | + fi |
| 166 | + location=${latest_location} |
| 167 | + downloaded_img=$(download_to_cache_without_redirect "${latest_location}") |
| 168 | + # shellcheck disable=SC2034 |
| 169 | + digest="sha512:$(sha512sum "${downloaded_img}" | cut -d' ' -f1)" |
| 170 | + json_vars location arch digest |
| 171 | +} |
| 172 | + |
| 173 | +function archlinux_cache_key_for_image_kernel() { |
| 174 | + local location=$1 url_spec |
| 175 | + url_spec=$(archlinux_url_spec_from_location "${location}") |
| 176 | + jq -r '["archlinux", .source, .arch, .date_and_ci_job_id // empty]|join(":")' <<<"${url_spec}" |
| 177 | +} |
| 178 | + |
| 179 | +function archlinux_image_entry_for_image_kernel() { |
| 180 | + local location=$1 url_spec source image_entry='' |
| 181 | + url_spec=$(archlinux_url_spec_from_location "${location}") |
| 182 | + source=$(jq -r '.source' <<<"${url_spec}") |
| 183 | + case "${source}" in |
| 184 | + geo.mirror.pkgbuild.com) |
| 185 | + image_entry=$(archlinux_image_entry_for_image_kernel_gitlab_mirror "${location}" "${url_spec}") |
| 186 | + ;; |
| 187 | + github.com/mcginty/arch-boxes-arm) |
| 188 | + image_entry=$(archlinux_image_entry_for_image_kernel_github_com "${location}" "${url_spec}") |
| 189 | + ;; |
| 190 | + *) error_exit "Unsupported source: ${source}" ;; |
| 191 | + esac |
| 192 | + if [[ -z ${image_entry} ]]; then |
| 193 | + error_exit "Failed to get the ${url_spec} image location for ${location}" |
| 194 | + elif jq -e ".location == \"${location}\"" <<<"${image_entry}" >/dev/null; then |
| 195 | + echo "Image location is up-to-date: ${location}" >&2 |
| 196 | + else |
| 197 | + echo "${image_entry}" |
| 198 | + fi |
| 199 | +} |
| 200 | + |
| 201 | +# check if the script is executed or sourced |
| 202 | +# shellcheck disable=SC1091 |
| 203 | +if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then |
| 204 | + scriptdir=$(dirname "${BASH_SOURCE[0]}") |
| 205 | + # shellcheck source=./cache-common-inc.sh |
| 206 | + . "${scriptdir}/cache-common-inc.sh" |
| 207 | + |
| 208 | + # shellcheck source=/dev/null # avoid shellcheck hangs on source looping |
| 209 | + . "${scriptdir}/update-template.sh" |
| 210 | +else |
| 211 | + # this script is sourced |
| 212 | + if [[ -v SUPPORTED_DISTRIBUTIONS ]]; then |
| 213 | + SUPPORTED_DISTRIBUTIONS+=("archlinux") |
| 214 | + else |
| 215 | + declare -a SUPPORTED_DISTRIBUTIONS=("archlinux") |
| 216 | + fi |
| 217 | + return 0 |
| 218 | +fi |
| 219 | + |
| 220 | +declare -a templates=() |
| 221 | +declare overriding="{}" |
| 222 | +while [[ $# -gt 0 ]]; do |
| 223 | + case "$1" in |
| 224 | + -h | --help) |
| 225 | + archlinux_print_help |
| 226 | + exit 0 |
| 227 | + ;; |
| 228 | + -d | --debug) set -x ;; |
| 229 | + *.yaml) templates+=("$1") ;; |
| 230 | + *) |
| 231 | + error_exit "Unknown argument: $1" |
| 232 | + ;; |
| 233 | + esac |
| 234 | + shift |
| 235 | + [[ -z ${overriding} ]] && overriding="{}" |
| 236 | +done |
| 237 | + |
| 238 | +if [[ ${#templates[@]} -eq 0 ]]; then |
| 239 | + archlinux_print_help |
| 240 | + exit 0 |
| 241 | +fi |
| 242 | + |
| 243 | +declare -A image_entry_cache=() |
| 244 | + |
| 245 | +for template in "${templates[@]}"; do |
| 246 | + echo "Processing ${template}" |
| 247 | + # 1. extract location by parsing template using arch |
| 248 | + yq_filter=" |
| 249 | + .images[] | [.location, .kernel.location, .kernel.cmdline] | @tsv |
| 250 | + " |
| 251 | + parsed=$(yq eval "${yq_filter}" "${template}") |
| 252 | + |
| 253 | + # 3. get the image location |
| 254 | + arr=() |
| 255 | + while IFS= read -r line; do arr+=("${line}"); done <<<"${parsed}" |
| 256 | + locations=("${arr[@]}") |
| 257 | + for ((index = 0; index < ${#locations[@]}; index++)); do |
| 258 | + [[ ${locations[index]} != "null" ]] || continue |
| 259 | + set -e |
| 260 | + IFS=$'\t' read -r location kernel_location kernel_cmdline <<<"${locations[index]}" |
| 261 | + set +e # Disable 'set -e' to avoid exiting on error for the next assignment. |
| 262 | + cache_key=$( |
| 263 | + set -e # Enable 'set -e' for the next command. |
| 264 | + archlinux_cache_key_for_image_kernel "${location}" "${kernel_location}" "${overriding}" |
| 265 | + ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition. |
| 266 | + # shellcheck disable=2181 |
| 267 | + [[ $? -eq 0 ]] || continue |
| 268 | + image_entry=$( |
| 269 | + set -e # Enable 'set -e' for the next command. |
| 270 | + if [[ -v image_entry_cache[${cache_key}] ]]; then |
| 271 | + echo "${image_entry_cache[${cache_key}]}" |
| 272 | + else |
| 273 | + archlinux_image_entry_for_image_kernel "${location}" "${kernel_location}" "${overriding}" |
| 274 | + fi |
| 275 | + ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition. |
| 276 | + # shellcheck disable=2181 |
| 277 | + [[ $? -eq 0 ]] || continue |
| 278 | + set -e |
| 279 | + image_entry_cache[${cache_key}]="${image_entry}" |
| 280 | + if [[ -n ${image_entry} ]]; then |
| 281 | + [[ ${kernel_cmdline} != "null" ]] && |
| 282 | + jq -e 'has("kernel")' <<<"${image_entry}" >/dev/null && |
| 283 | + image_entry=$(jq ".kernel.cmdline = \"${kernel_cmdline}\"" <<<"${image_entry}") |
| 284 | + echo "${image_entry}" | jq |
| 285 | + limactl edit --log-level error --set " |
| 286 | + .images[${index}] = ${image_entry}| |
| 287 | + (.images[${index}] | ..) style = \"double\" |
| 288 | + " "${template}" |
| 289 | + fi |
| 290 | + done |
| 291 | +done |
0 commit comments