跳到主要内容

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

浏览文件

update-template-opensuse.sh

364 行 · 13.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 opensuse_print_help() {
16 cat <<HELP
17$(basename "${BASH_SOURCE[0]}"): Update the openSUSE Linux image location in the specified templates
18
19Usage:
20 $(basename "${BASH_SOURCE[0]}") [--version-major-minor (<major>.<minor>|current|stable|tumbleweed)|--version-major <major> --version-minor <minor>] <template.yaml>...
21
22Description:
23 This script updates the openSUSE Linux image location in the specified templates.
24 Image location basename format:
25
26 openSUSE-(Leap-<major minor version>|Tumbleweed)-Minimal-VM.<arch>-Cloud.qcow2
27
28 Published openSUSE Linux image information is fetched from the following URLs:
29
30 Leap:
31 <major>.<minor>: https://download.opensuse.org/distribution/leap/<major>.<minor>/appliances/?jsontable
32 current: https://download.opensuse.org/distribution/openSUSE-current/appliances/?jsontable
33 stable: https://download.opensuse.org/distribution/openSUSE-stable/appliances/?jsontable
34
35 Tumbleweed:
36 x86_64: https://download.opensuse.org/tumbleweed/appliances/?jsontable
37 not x86_64: https://download.opensuse.org/ports/<arch>/tumbleweed/appliances/?jsontable
38
39 The downloaded files will be cached in the Lima cache directory.
40
41Examples:
42 Update the openSUSE Linux image location in templates/**.yaml:
43 $ $(basename "${BASH_SOURCE[0]}") templates/**.yaml
44
45 Update the openSUSE Linux image location to version 15.6 in ~/.lima/opensuse/lima.yaml:
46 $ $(basename "${BASH_SOURCE[0]}") --version-major-minor 15.6 ~/.lima/opensuse/lima.yaml
47 $ limactl factory-reset opensuse
48
49 Update the openSUSE Linux image location to tumbleweed in ~/.lima/opensuse/lima.yaml:
50 $ $(basename "${BASH_SOURCE[0]}") --version-major-minor tumbleweed ~/.lima/opensuse/lima.yaml
51 $ limactl factory-reset opensuse
52
53Flags:
54 --version-major-minor (<major>.<minor>|current|stable|tumbleweed) Use the specified <major>.<minor> version or
55 aliases "current", "stable", or "tumbleweed".
56 The <major>.<minor> version must be 15.0 or later.
57 --version-major <major> --version-minor <minor> Use the specified <major> and <minor> version.
58 -h, --help Print this help message
59HELP
60}
61
62# print the URL spec for the given location
63function opensuse_url_spec_from_location() {
64 local location=$1 jq_filter url_spec
65 jq_filter='capture("
66 ^https://download\\.opensuse\\.org/(?:
67 distribution/(?:
68 leap/(?<path_version_leap>\\d+\\.\\d+)|
69 openSUSE-(?<path_version_leap_alias>current|stable)
70 )|
71 (?:ports/aarch64/)?(?<path_version_tumbleweed>tumbleweed)
72 )/appliances/
73 openSUSE-(?<version>Leap-\\d+\\.\\d+|Tumbleweed)-Minimal-VM
74 \\.(?<arch>[^-]+)(?<major_minor_patch>-\\d+\\.\\d+\\.\\d+)?-(?<target_vendor>.*)(?<build_info>-Build\\d+\\.\\d+)?\\.(?<file_extension>.*)$
75 ";"x") |
76 .path_version = (.path_version_leap // .path_version_leap_alias // .path_version_tumbleweed)
77 '
78 url_spec=$(jq -e -r "${jq_filter}" <<<"\"${location}\"")
79 echo "${url_spec}"
80}
81
82readonly opensuse_jq_filter_directory='"https://download.opensuse.org/\(
83 if .path_version == "tumbleweed" then
84 if .arch != "x86_64" then
85 "ports/\(.arch)/"
86 else
87 ""
88 end + "tumbleweed"
89 else
90 "distribution/" +
91 if .path_version == "current" or .path_version == "stable" then
92 "openSUSE-\(.path_version)"
93 else
94 "leap/\(.path_version)"
95 end
96 end
97)/appliances/"'
98readonly opensuse_jq_filter_filename='
99 "openSUSE-\(
100 if .version == "tumbleweed" then "Tumbleweed" else "Leap-\(.version)" end
101 )-Minimal-VM.\(.arch)\(
102 if .major_minor_patch then .major_minor_patch else "" end
103 )-\(.target_vendor)\(
104 if .build_info then .build_info else "" end
105 ).\(.file_extension)"
106'
107
108# print the location for the given URL spec
109function opensuse_location_from_url_spec() {
110 local -r url_spec=$1
111 jq -e -r "${opensuse_jq_filter_directory} + ${opensuse_jq_filter_filename}" <<<"${url_spec}" ||
112 error_exit "Failed to get the location for ${url_spec}"
113}
114
115function opensuse_image_directory_from_url_spec() {
116 local -r url_spec=$1
117 jq -e -r "${opensuse_jq_filter_directory}" <<<"${url_spec}" ||
118 error_exit "Failed to get the image directory for ${url_spec}"
119}
120
121function opensuse_image_filename_from_url_spec() {
122 local -r url_spec=$1
123 jq -e -r "${opensuse_jq_filter_filename}" <<<"${url_spec}" ||
124 error_exit "Failed to get the image filename for ${url_spec}"
125}
126
127function opensuse_json_url_from_url_spec() {
128 local -r url_spec=$1
129 local json_url
130 json_url="$(opensuse_image_directory_from_url_spec "${url_spec}")?jsontable"
131 echo "${json_url}"
132}
133
134#
135function opensuse_latest_image_entry_for_url_spec() {
136 local url_spec=$1 arch json_url downloaded_json latest_version_info
137 # shellcheck disable=SC2034
138 arch=$(jq -r '.arch' <<<"${url_spec}")
139 json_url="$(opensuse_image_directory_from_url_spec "${url_spec}")?jsontable"
140 downloaded_json=$(download_to_cache "${json_url}")
141 latest_version_info=$(jq -e -r --argjson spec "${url_spec}" '
142 [
143 .data |sort_by(.mtime)|.[].name|
144 if $spec.major_minor_patch then
145 capture(
146 "^openSUSE-(?:Leap-(?<version_leap>\\d+\\.\\d+)|(?<version_tumbleweed>Tumbleweed))-Minimal-VM
147 \\.\($spec.arch)(?<major_minor_patch>-\\d+\\.\\d+\\.\\d+)?-\($spec.target_vendor)(?<build_info>-Build\\d+\\.\\d+)?\\.\($spec.file_extension)$"
148 ;"x"
149 )
150 else
151 capture(
152 "^openSUSE-(?:Leap-(?<version_leap>\\d+\\.\\d+)|(?<version_tumbleweed>Tumbleweed))-Minimal-VM
153 \\.\($spec.arch)-\($spec.target_vendor)\\.\($spec.file_extension)$"
154 ;"x"
155 )
156 end |
157 .version = (.version_leap // (.version_tumbleweed|ascii_downcase)) |
158 .version_number_array = ([.version | scan("\\d+") | tonumber])
159 ] | sort_by(.version_number_array, .image_revision) | last
160 ' <"${downloaded_json}")
161 [[ -n ${latest_version_info} ]] || return
162 local newer_url_spec location
163 # prefer the <major>.<minor> in the path
164 newer_url_spec=$(jq -e -r ". + ${latest_version_info} | .path_version = .version" <<<"${url_spec}")
165 location=$(opensuse_location_from_url_spec "${newer_url_spec}")
166 location=$(validate_url_without_redirect "${location}")
167
168 # Digest is not used here because URLs containing dates are not retained long-term.
169 # Instead, URLs without dates must be used, and their content is often updated without a URL change,
170 # resulting in only the digest being updated. Therefore, recording the digest is not meaningful.
171 #
172 # local sha256sum_location downloaded_sha256sum filename digest
173 # sha256sum_location="${location}.sha256"
174 # downloaded_sha256sum=$(download_to_cache "${sha256sum_location}")
175 # filename=$(opensuse_image_filename_from_url_spec "${newer_url_spec}")
176 # digest="sha256:$(awk '{print $1}' <"${downloaded_sha256sum}")"
177 # [[ -n ${digest} ]] || error_exit "Failed to get the digest for ${filename}"
178 json_vars location arch # digest
179}
180
181function opensuse_cache_key_for_image_kernel() {
182 local location=$1 url_spec
183 url_spec=$(opensuse_url_spec_from_location "${location}")
184 jq -r '["opensuse", .path_version, .target_vendor, .arch, .file_extension] | join(":")' <<<"${url_spec}"
185}
186
187function opensuse_image_entry_for_image_kernel() {
188 local location=$1 kernel_is_not_supported=$2 url_spec path_version overriding image_entry=''
189 [[ ${kernel_is_not_supported} == "null" ]] || echo "Updating kernel information is not supported on openSUSE Linux" >&2
190 url_spec=$(opensuse_url_spec_from_location "${location}")
191 path_version=$(jq -r '.path_version' <<<"${url_spec}")
192 if [[ ${path_version} == "tumbleweed" ]]; then
193 overriding=${3:-'{"path_version":"tumbleweed"}'}
194 else
195 overriding=${3:-'{"path_version":"stable"}'}
196 fi
197 url_spec=$(jq -r '. + '"${overriding}" <<<"${url_spec}")
198 image_entry=$(opensuse_latest_image_entry_for_url_spec "${url_spec}")
199 # shellcheck disable=SC2031
200 if [[ -z ${image_entry} ]]; then
201 error_exit "Failed to get the ${url_spec} image location for ${location}"
202 elif jq -e ".location == \"${location}\"" <<<"${image_entry}" >/dev/null; then
203 echo "Image location is up-to-date: ${location}" >&2
204 else
205 echo "${image_entry}"
206 fi
207}
208
209# check if the script is executed or sourced
210# shellcheck disable=SC1091
211if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then
212 scriptdir=$(dirname "${BASH_SOURCE[0]}")
213 # shellcheck source=./cache-common-inc.sh
214 . "${scriptdir}/cache-common-inc.sh"
215
216 # shellcheck source=/dev/null # avoid shellcheck hangs on source looping
217 . "${scriptdir}/update-template.sh"
218else
219 # this script is sourced
220 if [[ -v SUPPORTED_DISTRIBUTIONS ]]; then
221 SUPPORTED_DISTRIBUTIONS+=("opensuse")
222 else
223 declare -a SUPPORTED_DISTRIBUTIONS=("opensuse")
224 fi
225 return 0
226fi
227
228declare -a templates=()
229declare overriding='{}'
230declare version_major='' version_minor=''
231while [[ $# -gt 0 ]]; do
232 case "$1" in
233 -h | --help)
234 opensuse_print_help
235 exit 0
236 ;;
237 -d | --debug) set -x ;;
238 --version-major-minor)
239 if [[ -n ${2:-} && $2 != -* ]]; then
240 version="$2"
241 shift
242 else
243 error_exit "--version-major-minor requires a value"
244 fi
245 ;&
246 --version-major-minor=*)
247 version=${version:-${1#*=}}
248 overriding=$(
249 version="${version#v}"
250 if [[ ${version} =~ ^v?[0-9]+.[0-9]+ ]]; then
251 version="$(echo "${version}" | cut -d. -f1-2)"
252 [[ ${version%%.*} -ge 15 ]] || error_exit "openSUSE Linux version must be 15.0 or later"
253 path_version="${version}"
254 elif [[ ${version} == "current" || ${version} == "stable" || ${version} == "tumbleweed" ]]; then
255 path_version=${version}
256 else
257 error_exit "--version-major-minor requires a value in the format <major>.<minor>, current, stable, or tumbleweed"
258 fi
259 json_vars path_version <<<"${overriding}"
260 )
261 ;;
262 --version-major)
263 if [[ -n ${2:-} && $2 != -* ]]; then
264 version_major="$2"
265 shift
266 else
267 error_exit "--version-major requires a value"
268 fi
269 ;&
270 --version-major=*)
271 version_major=${version_major:-${1#*=}}
272 [[ ${version_major} =~ ^[0-9]+$ ]] || error_exit "Please specify --version-major in numbers"
273 ;;
274 --version-minor)
275 if [[ -n ${2:-} && $2 != -* ]]; then
276 version_minor="$2"
277 shift
278 else
279 error_exit "--version-minor requires a value"
280 fi
281 ;&
282 --version-minor=*)
283 version_minor=${version_minor:-${1#*=}}
284 [[ ${version_minor} =~ ^[0-9]+$ ]] || error_exit "Please specify --version-minor in numbers"
285 ;;
286 *.yaml) templates+=("$1") ;;
287 *)
288 error_exit "Unknown argument: $1"
289 ;;
290 esac
291 shift
292 [[ -z ${overriding} ]] && overriding="{}"
293done
294
295if ! jq -e '.path_version' <<<"${overriding}" >/dev/null; then # --version-major-minor is not specified
296 if [[ -n ${version_major} && -n ${version_minor} ]]; then
297 [[ ${version_major} -ge 15 ]] || error_exit "openSUSE Linux version must be 15.0 or later"
298 # shellcheck disable=2034
299 path_version="${version_major}.${version_minor}"
300 overriding=$(json_vars path_version <<<"${overriding}")
301 elif [[ -n ${version_major} ]]; then
302 error_exit "--version-minor is required when --version-major is specified"
303 elif [[ -n ${version_minor} ]]; then
304 error_exit "--version-major is required when --version-minor is specified"
305 fi
306elif [[ -n ${version_major} || -n ${version_minor} ]]; then # --version-major-minor is specified
307 echo "Ignoring --version-major and --version-minor because --version-major-minor is specified" >&2
308fi
309[[ ${overriding} == "{}" ]] && overriding=''
310
311if [[ ${#templates[@]} -eq 0 ]]; then
312 opensuse_print_help
313 exit 0
314fi
315
316declare -A image_entry_cache=()
317
318for template in "${templates[@]}"; do
319 echo "Processing ${template}"
320 # 1. extract location by parsing template using arch
321 yq_filter="
322 .images[] | [.location, .kernel.location, .kernel.cmdline] | @tsv
323 "
324 parsed=$(yq eval "${yq_filter}" "${template}")
325
326 # 3. get the image location
327 arr=()
328 while IFS= read -r line; do arr+=("${line}"); done <<<"${parsed}"
329 locations=("${arr[@]}")
330 for ((index = 0; index < ${#locations[@]}; index++)); do
331 [[ ${locations[index]} != "null" ]] || continue
332 set -e
333 IFS=$'\t' read -r location kernel_location kernel_cmdline <<<"${locations[index]}"
334 set +e # Disable 'set -e' to avoid exiting on error for the next assignment.
335 cache_key=$(
336 set -e # Enable 'set -e' for the next command.
337 opensuse_cache_key_for_image_kernel "${location}" "${kernel_location}"
338 ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
339 # shellcheck disable=2181
340 [[ $? -eq 0 ]] || continue
341 image_entry=$(
342 set -e # Enable 'set -e' for the next command.
343 if [[ -v image_entry_cache[${cache_key}] ]]; then
344 echo "${image_entry_cache[${cache_key}]}"
345 else
346 opensuse_image_entry_for_image_kernel "${location}" "${kernel_location}" "${overriding}"
347 fi
348 ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
349 # shellcheck disable=2181
350 [[ $? -eq 0 ]] || continue
351 set -e
352 image_entry_cache[${cache_key}]="${image_entry}"
353 if [[ -n ${image_entry} ]]; then
354 [[ ${kernel_cmdline} != "null" ]] &&
355 jq -e 'has("kernel")' <<<"${image_entry}" >/dev/null &&
356 image_entry=$(jq ".kernel.cmdline = \"${kernel_cmdline}\"" <<<"${image_entry}")
357 echo "${image_entry}" | jq
358 limactl edit --log-level error --set "
359 .images[${index}] *= ${image_entry}|
360 (.images[${index}] | ..) style = \"double\"
361 " "${template}"
362 fi
363 done
364done