| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "strconv" |
| 5 | "strings" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | type SetupProgress struct { |
| 10 | Phase string `json:"phase"` |
| 11 | Label string `json:"label"` |
| 12 | Received *int64 `json:"received,omitempty"` |
| 13 | Total int64 `json:"total,omitempty"` |
| 14 | System string `json:"system,omitempty"` |
| 15 | Operation string `json:"operation,omitempty"` |
| 16 | BytesPerSecond float64 `json:"bytes_per_second,omitempty"` |
| 17 | } |
| 18 | |
| 19 | |
| 20 | |
| 21 | func parseSetupProgress(line string) *SetupProgress { |
| 22 | line = strings.TrimSpace(line) |
| 23 | if strings.HasPrefix(line, "CAGE_HOST_RETRY ") { |
| 24 | fields := strings.Fields(line) |
| 25 | if len(fields) != 3 || (fields[2] != "2" && fields[2] != "3") { |
| 26 | return nil |
| 27 | } |
| 28 | label := macOSDownloadLabel(fields[1]) |
| 29 | if label == "" { |
| 30 | return nil |
| 31 | } |
| 32 | return &SetupProgress{Phase: "image", Label: label + "(准备第 " + fields[2] + "/3 次尝试)", System: "macos", Operation: "download"} |
| 33 | } |
| 34 | if strings.HasPrefix(line, "CAGE_HOST_BYTES ") { |
| 35 | fields := strings.Fields(line) |
| 36 | if len(fields) != 4 { |
| 37 | return nil |
| 38 | } |
| 39 | label := macOSDownloadLabel(fields[1]) |
| 40 | received, err := strconv.ParseInt(fields[2], 10, 64) |
| 41 | total, te := strconv.ParseInt(fields[3], 10, 64) |
| 42 | if label == "" || err != nil || te != nil || received < 0 || total < 0 || total > 0 && received > total { |
| 43 | return nil |
| 44 | } |
| 45 | return &SetupProgress{Phase: "image", Label: label, System: "macos", Operation: "download", Received: &received, Total: total} |
| 46 | } |
| 47 | if strings.HasPrefix(line, "下载 ") { |
| 48 | if label := macOSDownloadLabel(strings.TrimPrefix(line, "下载 ")); label != "" { |
| 49 | return &SetupProgress{Phase: "image", Label: label, System: "macos"} |
| 50 | } |
| 51 | } |
| 52 | if strings.HasPrefix(line, "CAGE_GUEST_BYTES ") { |
| 53 | fields := strings.Fields(line) |
| 54 | if len(fields) != 5 { |
| 55 | return nil |
| 56 | } |
| 57 | name := guestProgressName(fields[1]) |
| 58 | received, err := strconv.ParseInt(fields[3], 10, 64) |
| 59 | total, te := strconv.ParseInt(fields[4], 10, 64) |
| 60 | if name == "" || err != nil || te != nil || received < 0 || total < 0 || (total > 0 && received > total) { |
| 61 | return nil |
| 62 | } |
| 63 | label := "正在下载 " + name + " 系统镜像" |
| 64 | switch fields[2] { |
| 65 | case "download": |
| 66 | case "verify": |
| 67 | label = "正在校验 " + name + " 系统镜像" |
| 68 | default: |
| 69 | return nil |
| 70 | } |
| 71 | return &SetupProgress{Phase: "image", Label: label, Received: &received, Total: total, System: fields[1], Operation: fields[2]} |
| 72 | } |
| 73 | if strings.HasPrefix(line, "CAGE_GUEST_STAGE ") { |
| 74 | fields := strings.Fields(line) |
| 75 | if len(fields) != 3 || guestProgressName(fields[1]) == "" { |
| 76 | return nil |
| 77 | } |
| 78 | p := &SetupProgress{System: fields[1]} |
| 79 | if fields[1] == "windows-11" { |
| 80 | p.Operation = fields[2] |
| 81 | } |
| 82 | switch fields[2] { |
| 83 | case "restore": |
| 84 | p.Phase, p.Label = "packages", "正在安装 "+guestProgressName(fields[1])+" 系统" |
| 85 | case "account", "oobe", "credentials": |
| 86 | if fields[1] != "windows-11" { |
| 87 | return nil |
| 88 | } |
| 89 | p.Phase = "packages" |
| 90 | p.Label = map[string]string{ |
| 91 | "account": "请在安装窗口设置客机本地管理员", |
| 92 | "oobe": "请在虚拟机连接窗口完成 Windows 首次设置", |
| 93 | "credentials": "请在安装窗口输入客机管理员账户", |
| 94 | }[fields[2]] |
| 95 | case "media": |
| 96 | p.Phase, p.Label = "image", "正在下载并校验客机开发组件" |
| 97 | case "tools": |
| 98 | p.Phase, p.Label = "tools", "正在安装客机应用和开发工具" |
| 99 | case "services": |
| 100 | p.Phase, p.Label = "services", "正在完成客机配置" |
| 101 | default: |
| 102 | return nil |
| 103 | } |
| 104 | return p |
| 105 | } |
| 106 | if strings.HasPrefix(line, "CAGE_IMAGE_BYTES ") { |
| 107 | fields := strings.Fields(line) |
| 108 | if len(fields) != 3 { |
| 109 | return nil |
| 110 | } |
| 111 | received, err := strconv.ParseInt(fields[1], 10, 64) |
| 112 | total, te := strconv.ParseInt(fields[2], 10, 64) |
| 113 | if err != nil || te != nil || received < 0 || total < -1 || (total > 0 && received > total) { |
| 114 | return nil |
| 115 | } |
| 116 | return &SetupProgress{Phase: "image", Label: "正在下载 Debian 13 镜像", Received: &received, Total: max(total, 0)} |
| 117 | } |
| 118 | if strings.HasPrefix(line, "CAGE_TOOL_BYTES ") { |
| 119 | fields := strings.Fields(line) |
| 120 | if len(fields) != 3 && len(fields) != 4 { |
| 121 | return nil |
| 122 | } |
| 123 | received, err := strconv.ParseInt(fields[2], 10, 64) |
| 124 | if err != nil || received < 0 { |
| 125 | return nil |
| 126 | } |
| 127 | p := &SetupProgress{Phase: "tools", Received: &received} |
| 128 | if len(fields) == 4 { |
| 129 | total, err := strconv.ParseInt(fields[3], 10, 64) |
| 130 | if err != nil || total <= 0 || received > total { |
| 131 | return nil |
| 132 | } |
| 133 | p.Total = total |
| 134 | } |
| 135 | switch fields[1] { |
| 136 | case "mise.tar.gz": |
| 137 | p.Phase, p.Label = "packages", "正在下载 mise" |
| 138 | case "code-server.deb": |
| 139 | p.Label = "正在下载网页编辑器 code-server" |
| 140 | case "chromium.deb": |
| 141 | p.Phase, p.Label = "packages", "正在下载 Chromium 浏览器" |
| 142 | case "chromium-common.deb": |
| 143 | p.Phase, p.Label = "packages", "正在下载 Chromium 公共资源" |
| 144 | case "claude-code-linux-x64.tgz", "claude-code-linux-arm64.tgz": |
| 145 | p.Label = "正在下载 Claude Code" |
| 146 | default: |
| 147 | return nil |
| 148 | } |
| 149 | return p |
| 150 | } |
| 151 | switch line { |
| 152 | case "CAGE_IMAGE_READY": |
| 153 | return &SetupProgress{Phase: "image", Label: "正在启动 Linux 基础环境"} |
| 154 | case "CAGE_IMAGE_VERIFY": |
| 155 | return &SetupProgress{Phase: "image", Label: "正在校验 Debian 镜像"} |
| 156 | case "Installing packages inside VM...", "Installing base packages...": |
| 157 | return &SetupProgress{Phase: "packages", Label: "正在安装 Linux 基础软件包"} |
| 158 | case "Installing Python 3...": |
| 159 | return &SetupProgress{Phase: "packages", Label: "正在安装 Python"} |
| 160 | case "Installing Chromium...": |
| 161 | return &SetupProgress{Phase: "packages", Label: "正在安装 Chromium 浏览器"} |
| 162 | case "Installing Node.js 24...": |
| 163 | return &SetupProgress{Phase: "packages", Label: "正在安装 Node.js 24"} |
| 164 | case "Installing mise...": |
| 165 | return &SetupProgress{Phase: "packages", Label: "正在安装 mise"} |
| 166 | case "Installing Claude Code...": |
| 167 | return &SetupProgress{Phase: "tools", Label: "正在安装 Claude Code"} |
| 168 | case "Installing code-server...": |
| 169 | return &SetupProgress{Phase: "tools", Label: "正在安装网页编辑器 code-server"} |
| 170 | } |
| 171 | if strings.HasPrefix(line, "Running custom setup from ") { |
| 172 | return &SetupProgress{Phase: "services", Label: "正在配置桌面服务"} |
| 173 | } |
| 174 | return nil |
| 175 | } |
| 176 | |
| 177 | func macOSDownloadLabel(name string) string { |
| 178 | component := map[string]string{ |
| 179 | "editor.tgz": "macOS 网页编辑器", "playwright.tgz": "浏览器控制组件", "node.tgz": "Node.js", |
| 180 | "macfuse.dmg": "macFUSE 文件共享组件", "claude.dmg": "Claude Desktop", "chrome.dmg": "Chrome 浏览器", |
| 181 | "macos-cli-claude": "Claude Code", "macos-cli-uv.tar.gz": "uv 开发工具", |
| 182 | "macos-cli-python.tar.gz": "Python", "macos-cli-git.tar.gz": "Git", |
| 183 | }[name] |
| 184 | if component == "" { |
| 185 | return "" |
| 186 | } |
| 187 | return "正在下载 " + component |
| 188 | } |
| 189 | |
| 190 | func guestProgressName(system string) string { |
| 191 | return map[string]string{"macos": "macOS", "windows-11": "Windows 11", "debian-12": "Debian 12", "debian-13": "Debian 13", "ubuntu-22.04": "Ubuntu 22.04", "ubuntu-24.04": "Ubuntu 24.04"}[system] |
| 192 | } |
| 193 | |
| 194 | func (e *jobExecution) setSetupProgress(p *SetupProgress) { |
| 195 | e.owner.mu.Lock() |
| 196 | defer e.owner.mu.Unlock() |
| 197 | if e.id != "" && e.id == e.owner.state.ID && e.owner.state.Running { |
| 198 | e.owner.state.SetupProgress = p |
| 199 | e.progressUpdated = time.Now() |
| 200 | } |
| 201 | } |