跳到主要内容

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

浏览文件

fcos.go

160 行 · 3.8 KB历史下载
1package e2eutils
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7
8 "encoding/json"
9 "fmt"
10 "io"
11 "net/http"
12 "runtime"
13
14 "github.com/opencontainers/go-digest"
15
16 "github.com/coreos/stream-metadata-go/fedoracoreos"
17 "github.com/coreos/stream-metadata-go/stream"
18 "github.com/sirupsen/logrus"
19)
20
21type FcosDownload struct {
22 DataDir string
23}
24
25type fcosDownloadInfo struct {
26 Location string
27 Sha256Sum string
28}
29
30type ArtifactFormat struct {
31 Artifact string
32 Format string
33}
34
35func NewFcosDownloader(dataDir string) (*FcosDownload, error) {
36 return &FcosDownload{
37 DataDir: dataDir,
38 }, nil
39}
40
41func imageName(info *fcosDownloadInfo) string {
42 urlSplit := strings.Split(info.Location, "/")
43 return urlSplit[len(urlSplit)-1]
44}
45
46func (downloader *FcosDownload) DownloadImage(artifactType string, formatType string) (string, error) {
47 info, err := getFCOSDownload(artifactType, formatType)
48 if err != nil {
49 return "", err
50 }
51
52 compressedImage := filepath.Join(downloader.DataDir, imageName(info))
53
54 // check if the latest image is already present
55 ok, err := downloader.updateAvailable(info, compressedImage)
56 if err != nil {
57 return "", err
58 }
59 if !ok {
60 if err := DownloadVMImage(info.Location, compressedImage); err != nil {
61 return "", err
62 }
63 }
64
65 uncompressedImage := ""
66 if uncompressedImage, err = Decompress(compressedImage); err != nil {
67 return "", err
68 }
69 return uncompressedImage, nil
70}
71
72func (downloader *FcosDownload) updateAvailable(info *fcosDownloadInfo, compressedImage string) (bool, error) {
73 // check the sha of the local image if it exists
74 // get the sha of the remote image
75 // == dont bother to pull
76 if _, err := os.Stat(compressedImage); os.IsNotExist(err) {
77 return false, nil
78 }
79 fd, err := os.Open(compressedImage)
80 if err != nil {
81 return false, err
82 }
83 defer fd.Close()
84 sum, err := digest.SHA256.FromReader(fd)
85 if err != nil {
86 return false, err
87 }
88 if sum.Encoded() == info.Sha256Sum {
89 return true, nil
90 }
91 return false, nil
92}
93
94// as of 2024-05-28, these are the 4 architectures available in
95// curl https://builds.coreos.fedoraproject.org/streams/next.json
96func CoreosArch() string {
97 switch runtime.GOARCH {
98 case "amd64":
99 return "x86_64"
100 case "arm64":
101 return "aarch64"
102 case "ppc64le":
103 return "ppc64le"
104 case "s390x":
105 return "s390x"
106 }
107 panic(fmt.Sprintf("unknown arch: %s", runtime.GOOS))
108}
109
110// This should get Exported and stay put as it will apply to all fcos downloads
111// getFCOS parses fedoraCoreOS's stream and returns the image download URL and the release version
112func getFCOSDownload(artifactType string, formatType string) (*fcosDownloadInfo, error) {
113 streamurl := fedoracoreos.GetStreamURL(fedoracoreos.StreamNext)
114 resp, err := http.Get(streamurl.String())
115 if err != nil {
116 return nil, err
117 }
118 body, err := io.ReadAll(resp.Body)
119 if err != nil {
120 return nil, err
121 }
122 defer func() {
123 if err := resp.Body.Close(); err != nil {
124 logrus.Error(err)
125 }
126 }()
127
128 var fcosstable stream.Stream
129 if err := json.Unmarshal(body, &fcosstable); err != nil {
130 return nil, err
131 }
132 arch, ok := fcosstable.Architectures[CoreosArch()]
133 if !ok {
134 return nil, fmt.Errorf("unable to pull VM image: no targetArch in stream")
135 }
136 artifacts := arch.Artifacts
137 if artifacts == nil {
138 return nil, fmt.Errorf("unable to pull VM image: no artifact in stream")
139 }
140 artifact, ok := artifacts[artifactType]
141 if !ok {
142 return nil, fmt.Errorf("unable to pull VM image: no qemu artifact in stream")
143 }
144 formats := artifact.Formats
145 if formats == nil {
146 return nil, fmt.Errorf("unable to pull VM image: no formats in stream")
147 }
148 format, ok := formats[formatType]
149 if !ok {
150 return nil, fmt.Errorf("unable to pull VM image: no qcow2.xz format in stream")
151 }
152 disk := format.Disk
153 if disk == nil {
154 return nil, fmt.Errorf("unable to pull VM image: no disk in stream")
155 }
156 return &fcosDownloadInfo{
157 Location: disk.Location,
158 Sha256Sum: disk.Sha256,
159 }, nil
160}