跳到主要内容

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

浏览文件

update-template-freebsd.sh

255 行 · 9.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 freebsd_print_help() {
16 cat <<HELP
17$(basename "${BASH_SOURCE[0]}"): Update the FreeBSD image location in the specified templates
18
19Usage:
20 $(basename "${BASH_SOURCE[0]}") [--version <major>.<minor>] <template.yaml>...
21
22Description:
23 This script updates the FreeBSD image location in the specified templates.
24 Image location basename format:
25
26 FreeBSD-<version>-RELEASE-<arch>[-BASIC-CLOUDINIT]-<fs>.<format>.xz
27
28 Published FreeBSD image information is fetched from the following URL:
29
30 ${freebsd_archive_url}
31
32 The downloaded files will be cached in the Lima cache directory.
33
34Examples:
35 Update the FreeBSD image location in templates/**.yaml:
36 $ $(basename "${BASH_SOURCE[0]}") templates/**.yaml
37
38 Update the FreeBSD image location to version 14.3 in ~/.lima/freebsd/lima.yaml:
39 $ $(basename "${BASH_SOURCE[0]}") --version 14.3 ~/.lima/freebsd/lima.yaml
40 $ limactl factory-reset freebsd
41
42Flags:
43 --version <major>.<minor> Use the specified <major>.<minor> version.
44 -h, --help Print this help message
45HELP
46}
47
48# ftp-archive.freebsd.org doesn't seem to support HTTPS
49readonly freebsd_archive_url='http://ftp-archive.freebsd.org/pub/FreeBSD-Archive/old-releases/VM-IMAGES/'
50
51# freebsd_url_spec_from_location prints the URL spec for the given location.
52# If the location is not supported, it returns 1.
53# e.g.
54# ```console
55# freebsd_url_spec_from_location http://ftp-archive.freebsd.org/pub/FreeBSD-Archive/old-releases/VM-IMAGES/15.0-RELEASE/amd64/Latest/FreeBSD-15.0-RELEASE-amd64-BASIC-CLOUDINIT-zfs.raw.xz
56# {"version":"15.0","dir_arch":"amd64","filename_arch":"amd64","cloudinit":true,"fs":"zfs","format":"raw"}
57# freebsd_url_spec_from_location http://ftp-archive.freebsd.org/pub/FreeBSD-Archive/old-releases/VM-IMAGES/15.0-RELEASE/aarch64/Latest/FreeBSD-15.0-RELEASE-arm64-aarch64-BASIC-CLOUDINIT-zfs.raw.xz
58# {"version":"15.0","dir_arch":"aarch64","filename_arch":"arm64-aarch64","cloudinit":true,"fs":"zfs","format":"raw"}
59# ```
60function freebsd_url_spec_from_location() {
61 local location=$1 jq_filter url_spec
62 jq_filter='capture("
63 ^http://ftp-archive\\.freebsd\\.org/pub/FreeBSD-Archive/old-releases/VM-IMAGES/
64 (?<version>\\d+\\.\\d+)-RELEASE/(?<dir_arch>[^/]+)/Latest/
65 FreeBSD-\\d+\\.\\d+-RELEASE-(?<filename_arch>arm64-aarch64|riscv-riscv64|amd64)
66 (?<cloudinit>-BASIC-CLOUDINIT)?-(?<fs>zfs|ufs)\\.(?<format>raw|qcow2)\\.xz$
67 ";"x") | .cloudinit = (.cloudinit != null)
68 '
69 url_spec=$(jq -e -r "${jq_filter}" <<<"\"${location}\"")
70 echo "${url_spec}"
71}
72
73readonly freebsd_jq_filter_directory='"http://ftp-archive.freebsd.org/pub/FreeBSD-Archive/old-releases/VM-IMAGES/\(.version)-RELEASE/\(.dir_arch)/Latest/"'
74readonly freebsd_jq_filter_filename='
75 "FreeBSD-\(.version)-RELEASE-\(.filename_arch)\(if .cloudinit then "-BASIC-CLOUDINIT" else "" end)-\(.fs).\(.format).xz"
76'
77
78# freebsd_location_from_url_spec prints the location for the given URL spec.
79function freebsd_location_from_url_spec() {
80 local url_spec=$1
81 jq -e -r "${freebsd_jq_filter_directory} + ${freebsd_jq_filter_filename}" <<<"${url_spec}" ||
82 error_exit "Failed to get the location for ${url_spec}"
83}
84
85function freebsd_directory_from_url_spec() {
86 local url_spec=$1
87 jq -e -r "${freebsd_jq_filter_directory}" <<<"${url_spec}" ||
88 error_exit "Failed to get the directory for ${url_spec}"
89}
90
91function freebsd_filename_from_url_spec() {
92 local url_spec=$1
93 jq -e -r "${freebsd_jq_filter_filename}" <<<"${url_spec}" ||
94 error_exit "Failed to get the filename for ${url_spec}"
95}
96
97function freebsd_latest_image_entry_for_url_spec() {
98 local url_spec=$1 releases_page latest_version newer_url_spec location filename checksum_url downloaded_checksum digest arch
99 releases_page=$(download_to_cache "${freebsd_archive_url}")
100
101 # Find the latest RELEASE version with the same major version as in url_spec
102 latest_version=$(jq -e -Rrs --argjson spec "${url_spec}" '
103 [
104 split("\n").[] |
105 select(test("href=\"\\d+\\.\\d+-RELEASE/\"")) |
106 capture("href=\"(?<ver>\\d+\\.\\d+)-RELEASE/\"") |
107 .ver |
108 select((split(".")[0] | tonumber) == ($spec.version | split(".")[0] | tonumber))
109 ] |
110 sort_by(split(".") | map(tonumber)) |
111 last
112 ' <"${releases_page}") ||
113 error_exit "No RELEASE found for FreeBSD $(jq -r '.version | split(".")[0]' <<<"${url_spec}").x in ${freebsd_archive_url}"
114 [[ -n ${latest_version} ]] || return
115 newer_url_spec=$(jq -e -r ". + {version: \"${latest_version}\"}" <<<"${url_spec}")
116 filename=$(freebsd_filename_from_url_spec "${newer_url_spec}")
117 checksum_url="$(freebsd_directory_from_url_spec "${newer_url_spec}")CHECKSUM.SHA256"
118 downloaded_checksum=$(download_to_cache "${checksum_url}")
119 digest="sha256:$(awk "/SHA256 \\(${filename}\\) =/{print \$4}" "${downloaded_checksum}")"
120 [[ -n ${digest} ]] || error_exit "Failed to get the digest for ${filename}"
121 location=$(freebsd_location_from_url_spec "${newer_url_spec}")
122 location=$(validate_url "${location}")
123 arch=$(jq -e -r '.dir_arch' <<<"${newer_url_spec}")
124 arch=$(limayaml_arch "${arch}")
125 json_vars location arch digest
126}
127
128function freebsd_cache_key_for_image_kernel() {
129 local location=$1 url_spec
130 url_spec=$(freebsd_url_spec_from_location "${location}")
131 jq -r '["freebsd", (.version | split(".")[0]), .dir_arch, (if .cloudinit then "cloudinit" else "" end), .fs, .format] | join(":")' <<<"${url_spec}"
132}
133
134function freebsd_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 FreeBSD" >&2
137 url_spec=$(freebsd_url_spec_from_location "${location}" | jq -r ". + ${overriding}")
138 image_entry=$(freebsd_latest_image_entry_for_url_spec "${url_spec}")
139 # shellcheck disable=SC2031
140 if [[ -z ${image_entry} ]]; then
141 error_exit "Failed to get the ${url_spec} image location for ${location}"
142 elif jq -e ".location == \"${location}\"" <<<"${image_entry}" >/dev/null; then
143 echo "Image location is up-to-date: ${location}" >&2
144 else
145 echo "${image_entry}"
146 fi
147}
148
149# check if the script is executed or sourced
150# shellcheck disable=SC1091
151if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then
152 scriptdir=$(dirname "${BASH_SOURCE[0]}")
153 # shellcheck source=./cache-common-inc.sh
154 . "${scriptdir}/cache-common-inc.sh"
155
156 # shellcheck source=/dev/null # avoid shellcheck hangs on source looping
157 . "${scriptdir}/update-template.sh"
158else
159 # this script is sourced
160 if [[ -v SUPPORTED_DISTRIBUTIONS ]]; then
161 SUPPORTED_DISTRIBUTIONS+=("freebsd")
162 else
163 declare -a SUPPORTED_DISTRIBUTIONS=("freebsd")
164 fi
165 return 0
166fi
167
168declare -a templates=()
169declare overriding='{}'
170while [[ $# -gt 0 ]]; do
171 case "$1" in
172 -h | --help)
173 freebsd_print_help
174 exit 0
175 ;;
176 -d | --debug) set -x ;;
177 --version)
178 if [[ -n ${2:-} && $2 != -* ]]; then
179 version="$2"
180 shift
181 else
182 error_exit "--version requires a value"
183 fi
184 ;&
185 --version=*)
186 version=${version:-${1#*=}}
187 overriding=$(
188 [[ ${version} =~ ^[0-9]+\.[0-9]+$ ]] ||
189 error_exit "The version must be in the format <major>.<minor>"
190 json_vars version <<<"${overriding}"
191 )
192 ;;
193 *.yaml) templates+=("$1") ;;
194 *)
195 error_exit "Unknown argument: $1"
196 ;;
197 esac
198 shift
199 [[ -z ${overriding} ]] && overriding="{}"
200done
201
202if [[ ${#templates[@]} -eq 0 ]]; then
203 freebsd_print_help
204 exit 0
205fi
206
207declare -A image_entry_cache=()
208
209for template in "${templates[@]}"; do
210 echo "Processing ${template}"
211 # 1. extract location by parsing template using arch
212 yq_filter="
213 .images[] | [.location, .kernel.location, .kernel.cmdline] | @tsv
214 "
215 parsed=$(yq eval "${yq_filter}" "${template}")
216
217 # 3. get the image location
218 arr=()
219 while IFS= read -r line; do arr+=("${line}"); done <<<"${parsed}"
220 locations=("${arr[@]}")
221 for ((index = 0; index < ${#locations[@]}; index++)); do
222 [[ ${locations[index]} != "null" ]] || continue
223 set -e
224 IFS=$'\t' read -r location kernel_location kernel_cmdline <<<"${locations[index]}"
225 set +e # Disable 'set -e' to avoid exiting on error for the next assignment.
226 cache_key=$(
227 set -e # Enable 'set -e' for the next command.
228 freebsd_cache_key_for_image_kernel "${location}" "${kernel_location}"
229 ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
230 # shellcheck disable=2181
231 [[ $? -eq 0 ]] || continue
232 image_entry=$(
233 set -e # Enable 'set -e' for the next command.
234 if [[ -v image_entry_cache[${cache_key}] ]]; then
235 echo "${image_entry_cache[${cache_key}]}"
236 else
237 freebsd_image_entry_for_image_kernel "${location}" "${kernel_location}" "${overriding}"
238 fi
239 ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
240 # shellcheck disable=2181
241 [[ $? -eq 0 ]] || continue
242 set -e
243 image_entry_cache[${cache_key}]="${image_entry}"
244 if [[ -n ${image_entry} ]]; then
245 [[ ${kernel_cmdline} != "null" ]] &&
246 jq -e 'has("kernel")' <<<"${image_entry}" >/dev/null &&
247 image_entry=$(jq ".kernel.cmdline = \"${kernel_cmdline}\"" <<<"${image_entry}")
248 echo "${image_entry}" | jq
249 limactl edit --log-level error --set "
250 .images[${index}] *= ${image_entry}|
251 (.images[${index}] | ..) style = \"double\"
252 " "${template}"
253 fi
254 done
255done