| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | |
| 8 | "github.com/docker/go-units" |
| 9 | "github.com/sylvinus/agent-vm/internal/cage" |
| 10 | "github.com/sylvinus/agent-vm/internal/containerenv" |
| 11 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 12 | "go.yaml.in/yaml/v3" |
| 13 | ) |
| 14 | |
| 15 | func (s *Server) instanceDir() string { |
| 16 | if s.windowsGuest != nil { |
| 17 | |
| 18 | |
| 19 | if current, err := guestos.LoadWindowsProfile(s.profile); err == nil && current != nil && current.VMID == s.windowsGuest.VMID && current.GUIUserSID == s.windowsGuest.GUIUserSID { |
| 20 | return current.InstanceDirectory |
| 21 | } |
| 22 | return s.windowsGuest.InstanceDirectory |
| 23 | } |
| 24 | if s.macosGuest != nil { |
| 25 | return filepath.Join(s.profile, "state", "lima", s.macosGuest.Instance) |
| 26 | } |
| 27 | if containerenv.Configured(s.profile) { |
| 28 | return filepath.Join(s.profile, "state", "container", "environment") |
| 29 | } |
| 30 | return filepath.Join(s.profile, "state", "lima", cage.InstanceName(s.profile, s.environmentDir())) |
| 31 | } |
| 32 | |
| 33 | func (s *Server) resourceStatus(w http.ResponseWriter, r *http.Request) { |
| 34 | if s.windowsGuest != nil { |
| 35 | s.windowsResourceStatus(w, r) |
| 36 | return |
| 37 | } |
| 38 | if s.macosGuest != nil { |
| 39 | s.macOSResourceStatus(w, r) |
| 40 | return |
| 41 | } |
| 42 | if containerenv.Configured(s.profile) { |
| 43 | environment, err := containerenv.Load(s.profile) |
| 44 | if err != nil { |
| 45 | requestError(w, err) |
| 46 | return |
| 47 | } |
| 48 | state, err := environment.State(r.Context()) |
| 49 | if err != nil { |
| 50 | requestError(w, err) |
| 51 | return |
| 52 | } |
| 53 | config := environment.Config() |
| 54 | jsonReply(w, map[string]any{"running": state.Running, "automatic": false, "runtime": "container", |
| 55 | "recommended_memory_bytes": config.MemoryBytes, "recommended_cpus": config.CPUs, |
| 56 | "configured_memory_bytes": state.MemoryBytes, "configured_cpus": state.CPUs, |
| 57 | "resources_verified": state.ResourcesVerified, "resource_error": state.ResourceError, "pids_limit": state.PIDs, |
| 58 | "restart_required": false, "observation": nil}) |
| 59 | return |
| 60 | } |
| 61 | a, err := cage.LoadResourceAllocation(s.configPath) |
| 62 | if err != nil { |
| 63 | requestError(w, err) |
| 64 | return |
| 65 | } |
| 66 | memory, cpus, err := a.Limits() |
| 67 | allocationError := "" |
| 68 | if err != nil { |
| 69 | allocationError = err.Error() |
| 70 | memory, cpus = a.MemoryGiB, a.CPUs |
| 71 | } |
| 72 | maxMemory, maxCPUs := cage.ResourceLimits() |
| 73 | running := s.vmRunning() |
| 74 | var configured struct { |
| 75 | Memory string `yaml:"memory"` |
| 76 | CPUs int `yaml:"cpus"` |
| 77 | } |
| 78 | if b, err := os.ReadFile(filepath.Join(s.instanceDir(), "lima.yaml")); err == nil { |
| 79 | _ = yaml.Unmarshal(b, &configured) |
| 80 | } |
| 81 | maximum, _ := units.RAMInBytes(configured.Memory) |
| 82 | var observation *cage.ResourceStatus |
| 83 | status, err := cage.ReadResourceStatus(s.instanceDir()) |
| 84 | if running && err == nil { |
| 85 | observation = &status |
| 86 | } |
| 87 | jsonReply(w, map[string]any{ |
| 88 | "running": running, "automatic": a.Automatic, |
| 89 | "memory_gib": memory, "cpus": cpus, |
| 90 | "max_memory_gib": maxMemory, "max_cpus": maxCPUs, |
| 91 | "resource_error": allocationError, |
| 92 | "recommended_memory_bytes": uint64(maxMemory) << 30, "recommended_cpus": maxCPUs, |
| 93 | "configured_memory_bytes": maximum, "configured_cpus": configured.CPUs, |
| 94 | "restart_required": running && (maximum != int64(memory)<<30 || configured.CPUs != cpus || observation == nil), |
| 95 | "observation": observation, |
| 96 | }) |
| 97 | } |
| 98 | |
| 99 | func (s *Server) applyResources(w http.ResponseWriter, r *http.Request) { |
| 100 | if s.windowsGuest != nil { |
| 101 | s.applyWindowsResources(w, r) |
| 102 | return |
| 103 | } |
| 104 | var input struct { |
| 105 | Allocation *cage.ResourceAllocation `json:"allocation"` |
| 106 | } |
| 107 | if !readJSON(w, r, &input) { |
| 108 | return |
| 109 | } |
| 110 | if containerenv.Configured(s.profile) { |
| 111 | http.Error(w, "容器资源由服务器部署配置管理", http.StatusBadRequest) |
| 112 | return |
| 113 | } |
| 114 | if input.Allocation != nil { |
| 115 | if _, _, err := s.resourceAllocationLimits(*input.Allocation); err != nil { |
| 116 | requestError(w, err) |
| 117 | return |
| 118 | } |
| 119 | if !s.op.TryLock() { |
| 120 | http.Error(w, "另一个操作仍在进行,请稍候", http.StatusConflict) |
| 121 | return |
| 122 | } |
| 123 | err := writePrivate(cage.ResourceAllocationPath(s.configPath), input.Allocation) |
| 124 | s.op.Unlock() |
| 125 | if err != nil { |
| 126 | requestError(w, err) |
| 127 | return |
| 128 | } |
| 129 | jsonReply(w, map[string]bool{"ok": true}) |
| 130 | return |
| 131 | } |
| 132 | s.launch(w, "resources", func() error { |
| 133 | allocation, err := cage.LoadResourceAllocation(s.configPath) |
| 134 | if err != nil { |
| 135 | return err |
| 136 | } |
| 137 | if _, _, err := s.resourceAllocationLimits(allocation); err != nil { |
| 138 | return err |
| 139 | } |
| 140 | if s.vmRunning() { |
| 141 | if err := s.stopEnvironment(); err != nil { |
| 142 | return err |
| 143 | } |
| 144 | } |
| 145 | if err := s.operationContext().Err(); err != nil { |
| 146 | return err |
| 147 | } |
| 148 | return s.startEnvironment(false) |
| 149 | }) |
| 150 | } |
| 151 | |
| 152 | type networkCheckError struct{ error } |
| 153 | |
| 154 | func (e networkCheckError) Unwrap() error { return e.error } |
| 155 | |
| 156 | func (s *Server) checkNetworkIfWanted() error { |
| 157 | s.mu.Lock() |
| 158 | paused := s.networkPaused |
| 159 | s.mu.Unlock() |
| 160 | if paused || s.macSetupPending() { |
| 161 | return nil |
| 162 | } |
| 163 | var err error |
| 164 | if s.macosGuest != nil { |
| 165 | err = s.checkMacOSNetwork() |
| 166 | } else { |
| 167 | err = s.runSafetyCheck() |
| 168 | } |
| 169 | if err != nil { |
| 170 | return networkCheckError{err} |
| 171 | } |
| 172 | return nil |
| 173 | } |