| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | |
| 10 | "github.com/sylvinus/agent-vm/internal/host" |
| 11 | "github.com/sylvinus/agent-vm/internal/hostimage" |
| 12 | "github.com/sylvinus/agent-vm/internal/hostproxy" |
| 13 | "github.com/sylvinus/agent-vm/internal/limaembed" |
| 14 | ) |
| 15 | |
| 16 | func (s *Server) hostClonePlan(ctx context.Context) (hostimage.Plan, error) { |
| 17 | identity, err := hostimage.Host(ctx) |
| 18 | if err != nil { |
| 19 | return hostimage.Plan{}, err |
| 20 | } |
| 21 | home, err := os.UserHomeDir() |
| 22 | if err != nil { |
| 23 | return hostimage.Plan{}, err |
| 24 | } |
| 25 | var template []byte |
| 26 | if hostimage.IsLinux(identity.System) { |
| 27 | template, err = limaembed.ReadImageTemplate(identity.System) |
| 28 | if err != nil { |
| 29 | return hostimage.Plan{}, err |
| 30 | } |
| 31 | } |
| 32 | cache := filepath.Join(s.profile, "state", "guest-media", identity.System) |
| 33 | transport := hostproxy.NewTransport() |
| 34 | defer transport.CloseIdleConnections() |
| 35 | return hostimage.Resolve(ctx, identity, cache, filepath.Join(home, "Downloads"), template, transport) |
| 36 | } |
| 37 | |
| 38 | func (s *Server) hostClone(w http.ResponseWriter, r *http.Request) { |
| 39 | if !s.hasTrustedView(r) || !s.localVMStorage() { |
| 40 | http.Error(w, "请在本机 Cage 窗口选择系统创建方式", http.StatusForbidden) |
| 41 | return |
| 42 | } |
| 43 | view, err := s.guestInstallView() |
| 44 | if err != nil { |
| 45 | requestError(w, err) |
| 46 | return |
| 47 | } |
| 48 | if !view.Available || (!view.Fresh && view.Pending == nil) { |
| 49 | requestError(w, errors.New("当前环境无法创建新的本机系统")) |
| 50 | return |
| 51 | } |
| 52 | if view.Pending != nil && view.Pending.SourceKind == "linux-image" { |
| 53 | requestError(w, errors.New("请继续已选择的 Ubuntu 安装,不能改为自动匹配本机系统版本")) |
| 54 | return |
| 55 | } |
| 56 | var plan hostimage.Plan |
| 57 | if view.Pending != nil && view.Pending.Source.Host.System != "" { |
| 58 | plan = view.Pending.Source |
| 59 | } else { |
| 60 | plan, err = s.hostClonePlan(r.Context()) |
| 61 | if err != nil { |
| 62 | requestError(w, err) |
| 63 | return |
| 64 | } |
| 65 | } |
| 66 | minimumGiB := 32 |
| 67 | if hostimage.IsLinux(plan.Host.System) { |
| 68 | minimumGiB = 16 |
| 69 | } |
| 70 | started := view.Pending != nil && !view.Reselect |
| 71 | if started { |
| 72 | minimumGiB = 0 |
| 73 | } |
| 74 | downloadBytes := plan.RemainingBytes(filepath.Join(s.profile, "state", "guest-media", plan.Host.System)) |
| 75 | minimumGiB += int((downloadBytes + (1 << 30) - 1) / (1 << 30)) |
| 76 | freeGiB, measured := host.FreeGiB(filepath.Join(s.profile, "state")) |
| 77 | jsonReply(w, map[string]any{ |
| 78 | "plan": plan, "key": plan.Key(), "download_bytes": downloadBytes, |
| 79 | "minimum_free_gib": minimumGiB, "free_gib": freeGiB, |
| 80 | "installation_started": started, |
| 81 | "enough_space": !measured || freeGiB >= minimumGiB, |
| 82 | }) |
| 83 | } |