|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# SPDX-FileCopyrightText: Copyright The Lima Authors |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +set -eu -o pipefail |
| 7 | + |
| 8 | +# Functions in this script assume error handling with 'set -e'. |
| 9 | +# To ensure 'set -e' works correctly: |
| 10 | +# - Use 'set +e' before assignments and '$(set -e; <function>)' to capture output without exiting on errors. |
| 11 | +# - Avoid calling functions directly in conditions to prevent disabling 'set -e'. |
| 12 | +# - Use 'shopt -s inherit_errexit' (Bash 4.4+) to avoid repeated 'set -e' in all '$(...)'. |
| 13 | +shopt -s inherit_errexit || error_exit "inherit_errexit not supported. Please use bash 4.4 or later." |
| 14 | + |
| 15 | +function amazonlinux_print_help() { |
| 16 | + cat <<HELP |
| 17 | +$(basename "${BASH_SOURCE[0]}"): Update the Amazon Linux 2023 image location in the specified templates |
| 18 | +
|
| 19 | +Usage: |
| 20 | + $(basename "${BASH_SOURCE[0]}") <template.yaml>... |
| 21 | +
|
| 22 | +Description: |
| 23 | + This script updates the Amazon Linux 2023 image location in the specified templates. |
| 24 | + Image location format: |
| 25 | + https://cdn.amazonlinux.com/al2023/os-images/<version>/<folder>/<filename> |
| 26 | +
|
| 27 | + Latest version information is fetched from: |
| 28 | + https://cdn.amazonlinux.com/al2023/os-images/latest/ |
| 29 | +
|
| 30 | +Examples: |
| 31 | + Update the Amazon Linux 2023 image location in templates/**.yaml: |
| 32 | + $ $(basename "${BASH_SOURCE[0]}") templates/**.yaml |
| 33 | +
|
| 34 | +Flags: |
| 35 | + -h, --help Print this help message |
| 36 | +HELP |
| 37 | +} |
| 38 | + |
| 39 | +function amazonlinux_cache_key_for_image_kernel() { |
| 40 | + local location=$1 |
| 41 | + case "${location}" in |
| 42 | + https://cdn.amazonlinux.com/al2023/os-images/*) ;; |
| 43 | + *) return 1 ;; |
| 44 | + esac |
| 45 | + # Cache key based on architecture and variant derived from location |
| 46 | + # We use a simple heuristic: file path components. |
| 47 | + # The URL structure is .../os-images/<version>/<folder>/... |
| 48 | + # We want a key that represents "amazonlinux:al2023:<folder>" |
| 49 | + local folder |
| 50 | + if [[ "${location}" == *"/kvm-arm64/"* ]]; then |
| 51 | + folder="kvm-arm64" |
| 52 | + elif [[ "${location}" == *"/kvm/"* ]]; then |
| 53 | + folder="kvm" |
| 54 | + else |
| 55 | + return 1 |
| 56 | + fi |
| 57 | + echo "amazonlinux:al2023:${folder}" |
| 58 | +} |
| 59 | + |
| 60 | +function amazonlinux_image_entry_for_image_kernel() { |
| 61 | + local location=$1 |
| 62 | + case "${location}" in |
| 63 | + https://cdn.amazonlinux.com/al2023/os-images/*) ;; |
| 64 | + *) return 1 ;; |
| 65 | + esac |
| 66 | + |
| 67 | + local latest_url |
| 68 | + # Get the redirect URL to find the latest version |
| 69 | + latest_url=$(curl -Ls -o /dev/null -w '%{url_effective}' https://cdn.amazonlinux.com/al2023/os-images/latest/) |
| 70 | + local version |
| 71 | + version=$(basename "${latest_url}") |
| 72 | + |
| 73 | + local arch folder |
| 74 | + if [[ "${location}" == *"x86_64"* ]]; then |
| 75 | + # shellcheck disable=SC2034 |
| 76 | + arch="x86_64" |
| 77 | + folder="kvm" |
| 78 | + elif [[ "${location}" == *"arm64"* ]] || [[ "${location}" == *"aarch64"* ]]; then |
| 79 | + # shellcheck disable=SC2034 |
| 80 | + arch="aarch64" |
| 81 | + folder="kvm-arm64" |
| 82 | + else |
| 83 | + error_exit "Unknown arch for amazonlinux location: ${location}" |
| 84 | + fi |
| 85 | + |
| 86 | + local base_url="https://cdn.amazonlinux.com/al2023/os-images/${version}/${folder}" |
| 87 | + local checksums |
| 88 | + checksums=$(download_to_cache "${base_url}/SHA256SUMS") |
| 89 | + |
| 90 | + local filename digest line |
| 91 | + # Find the qcow2 file in SHA256SUMS |
| 92 | + line=$(grep "\.qcow2$" "${checksums}" | head -n 1) |
| 93 | + [[ -n ${line} ]] || error_exit "No qcow2 image found in ${base_url}/SHA256SUMS" |
| 94 | + |
| 95 | + # shellcheck disable=SC2034 |
| 96 | + digest=$(echo "${line}" | awk '{print "sha256:"$1}') |
| 97 | + filename=$(echo "${line}" | awk '{print $2}') |
| 98 | + |
| 99 | + # shellcheck disable=SC2034 |
| 100 | + local location="${base_url}/${filename}" |
| 101 | + |
| 102 | + json_vars location arch digest |
| 103 | +} |
| 104 | + |
| 105 | +# check if the script is executed or sourced |
| 106 | +# shellcheck disable=SC1091 |
| 107 | +if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then |
| 108 | + scriptdir=$(dirname "${BASH_SOURCE[0]}") |
| 109 | + # shellcheck source=./cache-common-inc.sh |
| 110 | + . "${scriptdir}/cache-common-inc.sh" |
| 111 | + |
| 112 | + # shellcheck source=/dev/null # avoid shellcheck hangs on source looping |
| 113 | + . "${scriptdir}/update-template.sh" |
| 114 | +else |
| 115 | + # this script is sourced |
| 116 | + if [[ -v SUPPORTED_DISTRIBUTIONS ]]; then |
| 117 | + SUPPORTED_DISTRIBUTIONS+=("amazonlinux") |
| 118 | + else |
| 119 | + declare -a SUPPORTED_DISTRIBUTIONS=("amazonlinux") |
| 120 | + fi |
| 121 | + return 0 |
| 122 | +fi |
| 123 | + |
| 124 | +declare -a templates=() |
| 125 | +while [[ $# -gt 0 ]]; do |
| 126 | + case "$1" in |
| 127 | + -h | --help) |
| 128 | + amazonlinux_print_help |
| 129 | + exit 0 |
| 130 | + ;; |
| 131 | + -d | --debug) set -x ;; |
| 132 | + *.yaml) templates+=("$1") ;; |
| 133 | + *) |
| 134 | + error_exit "Unknown argument: $1" |
| 135 | + ;; |
| 136 | + esac |
| 137 | + shift |
| 138 | +done |
| 139 | + |
| 140 | +if [[ ${#templates[@]} -eq 0 ]]; then |
| 141 | + amazonlinux_print_help |
| 142 | + exit 0 |
| 143 | +fi |
| 144 | + |
| 145 | +declare -A image_entry_cache=() |
| 146 | + |
| 147 | +for template in "${templates[@]}"; do |
| 148 | + echo "Processing ${template}" |
| 149 | + # 1. extract location by parsing template |
| 150 | + yq_filter=" |
| 151 | + .images[] | [.location, .kernel.location, .kernel.cmdline] | @tsv |
| 152 | + " |
| 153 | + parsed=$(yq eval "${yq_filter}" "${template}") |
| 154 | + |
| 155 | + # 2. get the image location |
| 156 | + arr=() |
| 157 | + while IFS= read -r line; do arr+=("${line}"); done <<<"${parsed}" |
| 158 | + locations=("${arr[@]}") |
| 159 | + for ((index = 0; index < ${#locations[@]}; index++)); do |
| 160 | + [[ ${locations[index]} != "null" ]] || continue |
| 161 | + set -e |
| 162 | + IFS=$'\t' read -r location _kernel_location _kernel_cmdline <<<"${locations[index]}" |
| 163 | + set +e # Disable 'set -e' to avoid exiting on error for the next assignment. |
| 164 | + cache_key=$( |
| 165 | + set -e # Enable 'set -e' for the next command. |
| 166 | + amazonlinux_cache_key_for_image_kernel "${location}" |
| 167 | + ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition. |
| 168 | + # shellcheck disable=2181 |
| 169 | + [[ $? -eq 0 ]] || continue |
| 170 | + image_entry=$( |
| 171 | + set -e # Enable 'set -e' for the next command. |
| 172 | + if [[ -v image_entry_cache[${cache_key}] ]]; then |
| 173 | + echo "${image_entry_cache[${cache_key}]}" |
| 174 | + else |
| 175 | + amazonlinux_image_entry_for_image_kernel "${location}" |
| 176 | + fi |
| 177 | + ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition. |
| 178 | + # shellcheck disable=2181 |
| 179 | + [[ $? -eq 0 ]] || continue |
| 180 | + set -e |
| 181 | + image_entry_cache[${cache_key}]="${image_entry}" |
| 182 | + if [[ -n ${image_entry} ]]; then |
| 183 | + echo "${image_entry}" | jq |
| 184 | + limactl edit --log-level error --set " |
| 185 | + .images[${index}] = ${image_entry}| |
| 186 | + (.images[${index}] | ..) style = \"double\" |
| 187 | + " "${template}" |
| 188 | + fi |
| 189 | + done |
| 190 | +done |
0 commit comments