跳到主要内容

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

浏览文件

update-template-centos-stream.sh

274 行 · 10.3 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 centos_print_help() {
16 cat <<HELP
17$(basename "${BASH_SOURCE[0]}"): Update the CentOS Stream image location in the specified templates
18
19Usage:
20 $(basename "${BASH_SOURCE[0]}") [--version <version>] <template.yaml>...
21
22Description:
23 This script updates the CentOS Stream 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 Image location basename format: CentOS-Stream-GenericCloud-<version>-[latest|<date>.0].<arch>.qcow2
27
28 Published CentOS Stream image information is fetched from the following URLs:
29
30 https://cloud.centos.org/centos/<major version>-stream/<arch>/images/
31
32 To parsing html, this script requires 'htmlq' or 'pup' command.
33 The downloaded files will be cached in the Lima cache directory.
34
35Examples:
36 Update the CentOS Stream image location in templates/**.yaml:
37 $ $(basename "${BASH_SOURCE[0]}") templates/**.yaml
38
39 Update the CentOS Stream image location in ~/.lima/centos/lima.yaml:
40 $ $(basename "${BASH_SOURCE[0]}") ~/.lima/centos/lima.yaml
41 $ limactl factory-reset centos
42
43 Update the CentOS Stream image location to 9-Stream in ~/.lima/centos/lima.yaml:
44 $ $(basename "${BASH_SOURCE[0]}") --version 9-stream ~/.lima/centos/lima.yaml
45 $ limactl factory-reset centos
46
47Flags:
48 --version <version> Use the specified version. The version must be 8 or later.
49 -h, --help Print this help message
50HELP
51}
52
53# print the URL spec for the given location
54function centos_url_spec_from_location() {
55 local location=$1 jq_filter url_spec
56 jq_filter='capture(
57 "^https://cloud\\.centos\\.org/centos/(?<path_version>\\d+)-stream/(?<path_arch>[^/]+)/images/" +
58 "CentOS-Stream-(?<target_vendor>.*)-(?<version>\\d+(\\.[.\\d]+)?)-" +
59 "(latest|(?<date_and_ci_job_id>\\d{8}\\.\\d+))\\.(?<arch>[^.]+).(?<file_extension>.*)$"
60 ;"x")'
61 url_spec=$(jq -e -r "${jq_filter}" <<<"\"${location}\"")
62 jq -e '.path_version == .version' <<<"${url_spec}" >/dev/null ||
63 error_exit "Validation failed: .path_version != .version: ${location}"
64 jq -e '.path_arch == .arch' <<<"${url_spec}" >/dev/null ||
65 error_exit "Validation failed: .path_arch != .arch: ${location}"
66 echo "${url_spec}"
67}
68
69readonly centos_jq_filter_directory='"https://cloud.centos.org/centos/\(.version)-stream/\(.path_arch)/images/"'
70readonly centos_jq_filter_filename='"CentOS-Stream-\(.target_vendor)-\(.version)-\(.date_and_ci_job_id // "latest").\(.arch).\(.file_extension)"'
71
72# print the location for the given URL spec
73function centos_location_from_url_spec() {
74 local -r url_spec=$1
75 jq -e -r "${centos_jq_filter_directory} + ${centos_jq_filter_filename}" <<<"${url_spec}" ||
76 error_exit "Failed to get the location for ${url_spec}"
77}
78
79function centos_image_directory_from_url_spec() {
80 local -r url_spec=$1
81 jq -e -r "${centos_jq_filter_directory}" <<<"${url_spec}" ||
82 error_exit "Failed to get the image directory for ${url_spec}"
83}
84
85function centos_image_filename_from_url_spec() {
86 local -r url_spec=$1
87 jq -e -r "${centos_jq_filter_filename}" <<<"${url_spec}" ||
88 error_exit "Failed to get the image filename for ${url_spec}"
89}
90
91#
92function centos_latest_image_entry_for_url_spec() {
93 local url_spec=$1 version arch image_directory downloaded_page links_in_page latest_info
94 version=$(jq -r '.version' <<<"${url_spec}")
95 arch=$(jq -r '.arch' <<<"${url_spec}")
96 image_directory=$(centos_image_directory_from_url_spec "${url_spec}")
97 downloaded_page=$(download_to_cache "${image_directory}")
98 if command -v htmlq >/dev/null; then
99 links_in_page=$(htmlq 'td.indexcolname a' --attribute href <"${downloaded_page}")
100 elif command -v pup >/dev/null; then
101 links_in_page=$(pup 'td[class=indexcolname] a attr{href}' <"${downloaded_page}")
102 else
103 error_exit "Please install 'htmlq' or 'pup' to list images from https://cloud.centos.org/centos/${version}/${arch}/images/"
104 fi
105 latest_info=$(jq -e -Rrs --argjson spec "${url_spec}" '
106 [
107 split("\n").[] |
108 capture(
109 "^CentOS-Stream-\($spec.target_vendor)-\($spec.version)-(?<date_and_ci_job_id>\\d{8}\\.\\d+)\\.\($spec.arch)\\.\($spec.file_extension)$"
110 ;"x"
111 )
112 ] | sort_by(.date_and_ci_job_id) | last
113 ' <<<"${links_in_page}")
114 [[ -n ${latest_info} ]] || return
115 local newer_url_spec location sha256sum_location downloaded_sha256sum filename digest
116 newer_url_spec=$(jq -e -r ". + ${latest_info}" <<<"${url_spec}")
117 location=$(centos_location_from_url_spec "${newer_url_spec}")
118 sha256sum_location="${location}.SHA256SUM"
119 downloaded_sha256sum=$(download_to_cache "${sha256sum_location}")
120 filename=$(centos_image_filename_from_url_spec "${newer_url_spec}")
121 digest="sha256:$(awk "/SHA256 \(${filename}\) =/{print \$4}" "${downloaded_sha256sum}")"
122 [[ -n ${digest} ]] || error_exit "Failed to get the SHA256 digest for ${filename}"
123 json_vars location arch digest
124}
125
126function centos_cache_key_for_image_kernel() {
127 local location=$1 url_spec
128 url_spec=$(centos_url_spec_from_location "${location}")
129 jq -r '["centos", .version, .target_vendor,
130 if .date_and_ci_job_id then "timestamped" else "latest" end,
131 .arch, .file_extension] | join(":")' <<<"${url_spec}"
132}
133
134function centos_image_entry_for_image_kernel() {
135 local location=$1 kernel_is_not_supported=$2 overriding=${3:-"{}"} url_spec image_entry=''
136 [[ ${kernel_is_not_supported} == "null" ]] || echo "Updating kernel information is not supported on CentOS Stream" >&2
137 url_spec=$(centos_url_spec_from_location "${location}" | jq -r ". + ${overriding}")
138 if jq -e '.date_and_ci_job_id' <<<"${url_spec}" >/dev/null; then
139 image_entry=$(centos_latest_image_entry_for_url_spec "${url_spec}")
140 else
141 image_entry=$(
142 # shellcheck disable=SC2030
143 location=$(centos_location_from_url_spec "${url_spec}")
144 location=$(validate_url_without_redirect "${location}")
145 arch=$(jq -r '.path_arch' <<<"${url_spec}")
146 json_vars location arch
147 )
148 fi
149 # shellcheck disable=SC2031
150 if [[ -z ${image_entry} ]]; then
151 error_exit "Failed to get the ${url_spec} image location for ${location}"
152 elif jq -e ".location == \"${location}\"" <<<"${image_entry}" >/dev/null; then
153 echo "Image location is up-to-date: ${location}" >&2
154 else
155 echo "${image_entry}"
156 fi
157}
158
159# check if the script is executed or sourced
160# shellcheck disable=SC1091
161if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then
162 scriptdir=$(dirname "${BASH_SOURCE[0]}")
163 # shellcheck source=./cache-common-inc.sh
164 . "${scriptdir}/cache-common-inc.sh"
165
166 if ! command -v htmlq >/dev/null && ! command -v pup >/dev/null; then
167 error_exit "Please install 'htmlq' or 'pup' to list images from https://cloud.centos.org/centos/<version>/<arch>/images/"
168 fi
169 # shellcheck source=/dev/null # avoid shellcheck hangs on source looping
170 . "${scriptdir}/update-template.sh"
171else
172 # this script is sourced
173 if ! command -v htmlq >/dev/null && ! command -v pup >/dev/null; then
174 echo "Please install 'htmlq' or 'pup' to list images from https://cloud.centos.org/centos/<version>/<arch>/images/" >&2
175 elif [[ -v SUPPORTED_DISTRIBUTIONS ]]; then
176 SUPPORTED_DISTRIBUTIONS+=("centos")
177 else
178 declare -a SUPPORTED_DISTRIBUTIONS=("centos")
179 fi
180 return 0
181fi
182
183declare -a templates=()
184declare overriding="{}"
185while [[ $# -gt 0 ]]; do
186 case "$1" in
187 -h | --help)
188 centos_print_help
189 exit 0
190 ;;
191 -d | --debug) set -x ;;
192 --version)
193 if [[ -n $2 && $2 != -* ]]; then
194 overriding=$(
195 version="${2%%-*}"
196 [[ ${version} -ge 8 ]] || error_exit "CentOS Stream version must be 8 or later"
197 json_vars version <<<"${overriding}"
198 )
199 shift
200 else
201 error_exit "--version requires a value"
202 fi
203 ;;
204 --version=*)
205 overriding=$(
206 version="${1#*=}"
207 version="${version%%-*}"
208 [[ ${version} -ge 8 ]] || error_exit "CentOS Stream version must be 8 or later"
209 json_vars version <<<"${overriding}"
210 )
211 ;;
212 *.yaml) templates+=("$1") ;;
213 *)
214 error_exit "Unknown argument: $1"
215 ;;
216 esac
217 shift
218 [[ -z ${overriding} ]] && overriding="{}"
219done
220
221if [[ ${#templates[@]} -eq 0 ]]; then
222 centos_print_help
223 exit 0
224fi
225
226declare -A image_entry_cache=()
227
228for template in "${templates[@]}"; do
229 echo "Processing ${template}"
230 # 1. extract location by parsing template using arch
231 yq_filter="
232 .images[] | [.location, .kernel.location, .kernel.cmdline] | @tsv
233 "
234 parsed=$(yq eval "${yq_filter}" "${template}")
235
236 # 3. get the image location
237 arr=()
238 while IFS= read -r line; do arr+=("${line}"); done <<<"${parsed}"
239 locations=("${arr[@]}")
240 for ((index = 0; index < ${#locations[@]}; index++)); do
241 [[ ${locations[index]} != "null" ]] || continue
242 set -e
243 IFS=$'\t' read -r location kernel_location kernel_cmdline <<<"${locations[index]}"
244 set +e # Disable 'set -e' to avoid exiting on error for the next assignment.
245 cache_key=$(
246 set -e # Enable 'set -e' for the next command.
247 centos_cache_key_for_image_kernel "${location}" "${kernel_location}"
248 ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
249 # shellcheck disable=2181
250 [[ $? -eq 0 ]] || continue
251 image_entry=$(
252 set -e # Enable 'set -e' for the next command.
253 if [[ -v image_entry_cache[${cache_key}] ]]; then
254 echo "${image_entry_cache[${cache_key}]}"
255 else
256 centos_image_entry_for_image_kernel "${location}" "${kernel_location}" "${overriding}"
257 fi
258 ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
259 # shellcheck disable=2181
260 [[ $? -eq 0 ]] || continue
261 set -e
262 image_entry_cache[${cache_key}]="${image_entry}"
263 if [[ -n ${image_entry} ]]; then
264 [[ ${kernel_cmdline} != "null" ]] &&
265 jq -e 'has("kernel")' <<<"${image_entry}" >/dev/null &&
266 image_entry=$(jq ".kernel.cmdline = \"${kernel_cmdline}\"" <<<"${image_entry}")
267 echo "${image_entry}" | jq
268 limactl edit --log-level error --set "
269 .images[${index}] *= ${image_entry}|
270 (.images[${index}] | ..) style = \"double\"
271 " "${template}"
272 fi
273 done
274done