跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

update-template-almalinux.sh

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