| 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 print_help() { |
| 16 | cat <<HELP |
| 17 | $(basename "${BASH_SOURCE[0]}"): Update the image location in the specified templates |
| 18 | |
| 19 | Usage: |
| 20 | $(basename "${BASH_SOURCE[0]}") <template.yaml>... |
| 21 | |
| 22 | Description: |
| 23 | This script updates the image location in the specified templates. |
| 24 | If the image location in the template contains a release date in the URL, the script replaces it with the latest available date. |
| 25 | |
| 26 | Examples: |
| 27 | Update the Ubuntu image location in templates/**.yaml: |
| 28 | $ $(basename "${BASH_SOURCE[0]}") templates/**.yaml |
| 29 | |
| 30 | Update the Ubuntu image location in ~/.lima/ubuntu/lima.yaml: |
| 31 | $ $(basename "${BASH_SOURCE[0]}") ~/.lima/ubuntu/lima.yaml |
| 32 | $ limactl factory-reset ubuntu |
| 33 | |
| 34 | Flags: |
| 35 | -h, --help Print this help message |
| 36 | HELP |
| 37 | } |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | function json() { |
| 48 | local args=() pattern='^(\[.*\]|\{.*\}|true|false|[0-9]+)$' value |
| 49 | [[ ! -p /dev/stdin ]] && args+=(--null-input) |
| 50 | while [[ $# -gt 0 ]]; do |
| 51 | value="${2-}" |
| 52 | if [[ ${value} =~ ${pattern} ]]; then |
| 53 | args+=(--argjson "${1}" "${value}") |
| 54 | elif [[ -n ${value} ]]; then |
| 55 | args+=(--arg "${1}" "${value}") |
| 56 | fi |
| 57 | shift |
| 58 | shift |
| 59 | done |
| 60 | jq -c "${args[@]}" '. + $ARGS.named | if . == {} then empty else . end' |
| 61 | } |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | function json_vars() { |
| 73 | local args=() var |
| 74 | for var in "$@"; do |
| 75 | [[ -v ${var} ]] || error_exit "${var} is not set" |
| 76 | args+=("${var}" "${!var}") |
| 77 | done |
| 78 | json "${args[@]}" |
| 79 | } |
| 80 | |
| 81 | |
| 82 | function limayaml_arch() { |
| 83 | local arch=$1 |
| 84 | arch=${arch/amd64/x86_64} |
| 85 | arch=${arch/arm64/aarch64} |
| 86 | arch=${arch/armhf/armv7l} |
| 87 | arch=${arch/ppc64el/ppc64le} |
| 88 | echo "${arch}" |
| 89 | } |
| 90 | |
| 91 | function validate_boolean() { |
| 92 | local value=$1 |
| 93 | case "${value}" in |
| 94 | '') ;; |
| 95 | true | 1) echo true ;; |
| 96 | false | 0) echo false ;; |
| 97 | *) error_exit "Invalid boolean value: ${value}" ;; |
| 98 | esac |
| 99 | } |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | function validate_url() { |
| 109 | local url=$1 |
| 110 | code_location=$(curl -sSL -o /dev/null -I -w "%{http_code}\t%{url_effective}" "${url}") |
| 111 | read -r code location <<<"${code_location}" |
| 112 | [[ ${code} -eq 200 ]] || error_exit "[${code}]: The image is not available for download from ${location}" |
| 113 | echo "${location}" |
| 114 | } |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | function validate_url_without_redirect() { |
| 125 | local url=$1 location |
| 126 | location=$(validate_url "${url}") |
| 127 | [[ -n ${location} ]] || error_exit "The image is not available for download from ${url}" |
| 128 | echo "${url}" |
| 129 | } |
| 130 | |
| 131 | |
| 132 | |
| 133 | if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then |
| 134 | scriptdir=$(dirname "${BASH_SOURCE[0]}") |
| 135 | |
| 136 | . "${scriptdir}/cache-common-inc.sh" |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | declare -a SUPPORTED_DISTRIBUTIONS=() |
| 150 | |
| 151 | |
| 152 | . "${scriptdir}/update-template-ubuntu.sh" |
| 153 | |
| 154 | . "${scriptdir}/update-template-debian.sh" |
| 155 | |
| 156 | . "${scriptdir}/update-template-archlinux.sh" |
| 157 | |
| 158 | . "${scriptdir}/update-template-centos-stream.sh" |
| 159 | |
| 160 | . "${scriptdir}/update-template-almalinux.sh" |
| 161 | |
| 162 | . "${scriptdir}/update-template-almalinux-kitten.sh" |
| 163 | |
| 164 | . "${scriptdir}/update-template-rocky.sh" |
| 165 | |
| 166 | . "${scriptdir}/update-template-alpine.sh" |
| 167 | |
| 168 | . "${scriptdir}/update-template-oraclelinux.sh" |
| 169 | |
| 170 | . "${scriptdir}/update-template-fedora.sh" |
| 171 | |
| 172 | . "${scriptdir}/update-template-opensuse.sh" |
| 173 | |
| 174 | . "${scriptdir}/update-template-freebsd.sh" |
| 175 | |
| 176 | . "${scriptdir}/update-template-macos.sh" |
| 177 | else |
| 178 | |
| 179 | return 0 |
| 180 | fi |
| 181 | |
| 182 | declare -a templates=() |
| 183 | while [[ $# -gt 0 ]]; do |
| 184 | case "$1" in |
| 185 | -h | --help) |
| 186 | print_help |
| 187 | exit 0 |
| 188 | ;; |
| 189 | -d | --debug) set -x ;; |
| 190 | *.yaml) templates+=("$1") ;; |
| 191 | *) |
| 192 | error_exit "Unknown argument: $1" |
| 193 | ;; |
| 194 | esac |
| 195 | shift |
| 196 | done |
| 197 | |
| 198 | if [[ ${#templates[@]} -eq 0 ]]; then |
| 199 | print_help |
| 200 | exit 0 |
| 201 | fi |
| 202 | |
| 203 | declare -a distributions=() |
| 204 | |
| 205 | for distribution in "${SUPPORTED_DISTRIBUTIONS[@]}"; do |
| 206 | if declare -f "${distribution}_cache_key_for_image_kernel" >/dev/null && |
| 207 | declare -f "${distribution}_image_entry_for_image_kernel" >/dev/null; then |
| 208 | distributions+=("${distribution}") |
| 209 | fi |
| 210 | done |
| 211 | [[ ${#distributions[@]} -gt 0 ]] || error_exit "No supported distributions found" |
| 212 | |
| 213 | declare -A image_entry_cache=() |
| 214 | |
| 215 | for template in "${templates[@]}"; do |
| 216 | echo "Processing ${template}" |
| 217 | |
| 218 | yq_filter=" |
| 219 | .images[] | [.location, .kernel.location, .kernel.cmdline] | @tsv |
| 220 | " |
| 221 | parsed=$(yq eval "${yq_filter}" "${template}") |
| 222 | |
| 223 | |
| 224 | arr=() |
| 225 | while IFS= read -r line; do arr+=("${line}"); done <<<"${parsed}" |
| 226 | locations=("${arr[@]}") |
| 227 | for ((index = 0; index < ${#locations[@]}; index++)); do |
| 228 | [[ ${locations[index]} != "null" ]] || continue |
| 229 | set -e |
| 230 | IFS=$'\t' read -r location kernel_location kernel_cmdline <<<"${locations[index]}" |
| 231 | for distribution in "${distributions[@]}"; do |
| 232 | set +e |
| 233 | cache_key=$( |
| 234 | set -e |
| 235 | "${distribution}_cache_key_for_image_kernel" "${location}" "${kernel_location}" |
| 236 | ) |
| 237 | |
| 238 | [[ $? -eq 0 ]] || continue |
| 239 | image_entry=$( |
| 240 | set -e |
| 241 | if [[ -v image_entry_cache[${cache_key}] ]]; then |
| 242 | echo "${image_entry_cache[${cache_key}]}" |
| 243 | else |
| 244 | "${distribution}_image_entry_for_image_kernel" "${location}" "${kernel_location}" |
| 245 | fi |
| 246 | ) |
| 247 | |
| 248 | [[ $? -eq 0 ]] || continue |
| 249 | set -e |
| 250 | image_entry_cache[${cache_key}]="${image_entry}" |
| 251 | if [[ -n ${image_entry} ]]; then |
| 252 | [[ ${kernel_cmdline} != "null" ]] && |
| 253 | jq -e 'has("kernel")' <<<"${image_entry}" >/dev/null && |
| 254 | image_entry=$(jq ".kernel.cmdline = \"${kernel_cmdline}\"" <<<"${image_entry}") |
| 255 | echo "${image_entry}" | jq |
| 256 | limactl edit --log-level error --set " |
| 257 | .images[${index}] *= ${image_entry}| |
| 258 | (.images[${index}] | ..) style = \"double\" |
| 259 | " "${template}" |
| 260 | fi |
| 261 | done |
| 262 | done |
| 263 | done |