| 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 macos_print_help() { |
| 16 | cat <<HELP |
| 17 | $(basename "${BASH_SOURCE[0]}"): Update the macOS image location in the specified templates |
| 18 | |
| 19 | Usage: |
| 20 | $(basename "${BASH_SOURCE[0]}") <template.yaml>... |
| 21 | |
| 22 | Description: |
| 23 | This script updates the macOS image location in the specified templates. |
| 24 | Image location format: |
| 25 | |
| 26 | https://updates.cdn-apple.com/.../UniversalMac_<version>_<build>_Restore.ipsw |
| 27 | |
| 28 | Published macOS image information (URL and SHA256 digest) is fetched from |
| 29 | the ipsw.me API: |
| 30 | |
| 31 | https://api.ipsw.me/v4/device/VirtualMac2,1 |
| 32 | |
| 33 | The downloaded JSON will be cached in the Lima cache directory. |
| 34 | |
| 35 | Examples: |
| 36 | Update the macOS image location in templates/_images/macos-*.yaml: |
| 37 | $ $(basename "${BASH_SOURCE[0]}") templates/_images/macos-*.yaml |
| 38 | |
| 39 | Flags: |
| 40 | -h, --help Print this help message |
| 41 | HELP |
| 42 | } |
| 43 | |
| 44 | |
| 45 | |
| 46 | readonly macos_ipsw_me_device_url="https://api.ipsw.me/v4/device/VirtualMac2,1" |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | function macos_url_spec_from_location() { |
| 59 | local location=$1 jq_filter url_spec |
| 60 | jq_filter='capture(" |
| 61 | ^https://updates\\.cdn-apple\\.com/(?:[^/]+/)+ |
| 62 | UniversalMac_(?<version>(?<major_version>\\d+)(?:\\.\\d+)+)_(?<build>[^_]+)_Restore\\.ipsw$ |
| 63 | ";"x") |
| 64 | ' |
| 65 | url_spec=$(jq -e -r "${jq_filter}" <<<"\"${location}\"") |
| 66 | echo "${url_spec}" |
| 67 | } |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | function macos_latest_image_entry_for_url_spec() { |
| 76 | local url_spec=$1 major_version ipsw_me_file latest_entry location digest arch="aarch64" |
| 77 | major_version=$(jq -r '.major_version' <<<"${url_spec}") |
| 78 | ipsw_me_file=$(download_to_cache "${macos_ipsw_me_device_url}") |
| 79 | latest_entry=$(jq -e -r --arg major "${major_version}" ' |
| 80 | .firmwares | |
| 81 | [.[] | select(.version | test("^" + $major + "\\."))] | |
| 82 | sort_by(.releasedate) | |
| 83 | last |
| 84 | ' "${ipsw_me_file}") |
| 85 | [[ -n ${latest_entry} && ${latest_entry} != "null" ]] || |
| 86 | error_exit "Failed to get the latest macOS ${major_version} image from ${macos_ipsw_me_device_url}" |
| 87 | location=$(jq -r '.url' <<<"${latest_entry}") |
| 88 | digest=$(jq -r '.sha256sum // "" | if . != "" then "sha256:" + . else "" end' <<<"${latest_entry}") |
| 89 | json_vars location arch digest |
| 90 | } |
| 91 | |
| 92 | function macos_cache_key_for_image_kernel() { |
| 93 | local location=$1 url_spec |
| 94 | url_spec=$(macos_url_spec_from_location "${location}") |
| 95 | jq -r '["macos", .major_version] | join(":")' <<<"${url_spec}" |
| 96 | } |
| 97 | |
| 98 | function macos_image_entry_for_image_kernel() { |
| 99 | local location=$1 kernel_is_not_supported=$2 url_spec image_entry='' |
| 100 | [[ ${kernel_is_not_supported} == "null" ]] || echo "Updating kernel information is not supported on macOS" >&2 |
| 101 | url_spec=$(macos_url_spec_from_location "${location}") |
| 102 | image_entry=$(macos_latest_image_entry_for_url_spec "${url_spec}") |
| 103 | |
| 104 | if [[ -z ${image_entry} ]]; then |
| 105 | error_exit "Failed to get the image location for ${location}" |
| 106 | elif jq -e ".location == \"${location}\"" <<<"${image_entry}" >/dev/null; then |
| 107 | echo "Image location is up-to-date: ${location}" >&2 |
| 108 | else |
| 109 | echo "${image_entry}" |
| 110 | fi |
| 111 | } |
| 112 | |
| 113 | |
| 114 | |
| 115 | if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then |
| 116 | scriptdir=$(dirname "${BASH_SOURCE[0]}") |
| 117 | |
| 118 | . "${scriptdir}/cache-common-inc.sh" |
| 119 | |
| 120 | |
| 121 | . "${scriptdir}/update-template.sh" |
| 122 | else |
| 123 | |
| 124 | if [[ -v SUPPORTED_DISTRIBUTIONS ]]; then |
| 125 | SUPPORTED_DISTRIBUTIONS+=("macos") |
| 126 | else |
| 127 | declare -a SUPPORTED_DISTRIBUTIONS=("macos") |
| 128 | fi |
| 129 | return 0 |
| 130 | fi |
| 131 | |
| 132 | declare -a templates=() |
| 133 | while [[ $# -gt 0 ]]; do |
| 134 | case "$1" in |
| 135 | -h | --help) |
| 136 | macos_print_help |
| 137 | exit 0 |
| 138 | ;; |
| 139 | -d | --debug) set -x ;; |
| 140 | *.yaml) templates+=("$1") ;; |
| 141 | *) |
| 142 | error_exit "Unknown argument: $1" |
| 143 | ;; |
| 144 | esac |
| 145 | shift |
| 146 | done |
| 147 | |
| 148 | if [[ ${#templates[@]} -eq 0 ]]; then |
| 149 | macos_print_help |
| 150 | exit 0 |
| 151 | fi |
| 152 | |
| 153 | declare -A image_entry_cache=() |
| 154 | |
| 155 | for template in "${templates[@]}"; do |
| 156 | echo "Processing ${template}" |
| 157 | |
| 158 | yq_filter=" |
| 159 | .images[] | [.location, .kernel.location, .kernel.cmdline] | @tsv |
| 160 | " |
| 161 | parsed=$(yq eval "${yq_filter}" "${template}") |
| 162 | |
| 163 | |
| 164 | arr=() |
| 165 | while IFS= read -r line; do arr+=("${line}"); done <<<"${parsed}" |
| 166 | locations=("${arr[@]}") |
| 167 | for ((index = 0; index < ${#locations[@]}; index++)); do |
| 168 | [[ ${locations[index]} != "null" ]] || continue |
| 169 | set -e |
| 170 | IFS=$'\t' read -r location kernel_location kernel_cmdline <<<"${locations[index]}" |
| 171 | set +e |
| 172 | cache_key=$( |
| 173 | set -e |
| 174 | macos_cache_key_for_image_kernel "${location}" "${kernel_location}" |
| 175 | ) |
| 176 | |
| 177 | [[ $? -eq 0 ]] || continue |
| 178 | image_entry=$( |
| 179 | set -e |
| 180 | if [[ -v image_entry_cache[${cache_key}] ]]; then |
| 181 | echo "${image_entry_cache[${cache_key}]}" |
| 182 | else |
| 183 | macos_image_entry_for_image_kernel "${location}" "${kernel_location}" |
| 184 | fi |
| 185 | ) |
| 186 | |
| 187 | [[ $? -eq 0 ]] || continue |
| 188 | set -e |
| 189 | image_entry_cache[${cache_key}]="${image_entry}" |
| 190 | if [[ -n ${image_entry} ]]; then |
| 191 | [[ ${kernel_cmdline} != "null" ]] && |
| 192 | jq -e 'has("kernel")' <<<"${image_entry}" >/dev/null && |
| 193 | image_entry=$(jq ".kernel.cmdline = \"${kernel_cmdline}\"" <<<"${image_entry}") |
| 194 | echo "${image_entry}" | jq |
| 195 | limactl edit --log-level error --set " |
| 196 | .images[${index}] *= ${image_entry}| |
| 197 | (.images[${index}] | ..) style = \"double\" |
| 198 | " "${template}" |
| 199 | fi |
| 200 | done |
| 201 | done |