| 1 | |
| 2 | |
| 3 | |
| 4 | package limatype |
| 5 | |
| 6 | import ( |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | |
| 12 | "github.com/lima-vm/lima/v2/pkg/limatype/filenames" |
| 13 | ) |
| 14 | |
| 15 | type Status = string |
| 16 | |
| 17 | const ( |
| 18 | StatusUnknown Status = "" |
| 19 | StatusUninitialized Status = "Uninitialized" |
| 20 | StatusInstalling Status = "Installing" |
| 21 | StatusBroken Status = "Broken" |
| 22 | StatusStopped Status = "Stopped" |
| 23 | StatusRunning Status = "Running" |
| 24 | ) |
| 25 | |
| 26 | type Instance struct { |
| 27 | Name string `json:"name"` |
| 28 | |
| 29 | Hostname string `json:"hostname"` |
| 30 | Status Status `json:"status"` |
| 31 | Dir string `json:"dir"` |
| 32 | VMType VMType `json:"vmType"` |
| 33 | Arch Arch `json:"arch"` |
| 34 | CPUs int `json:"cpus,omitempty"` |
| 35 | Memory int64 `json:"memory,omitempty"` |
| 36 | Disk int64 `json:"disk,omitempty"` |
| 37 | Message string `json:"message,omitempty"` |
| 38 | AdditionalDisks []Disk `json:"additionalDisks,omitempty"` |
| 39 | Networks []Network `json:"network,omitempty"` |
| 40 | SSHLocalPort int `json:"sshLocalPort,omitempty"` |
| 41 | SSHConfigFile string `json:"sshConfigFile,omitempty"` |
| 42 | HostAgentPID int `json:"hostAgentPID,omitempty"` |
| 43 | DriverPID int `json:"driverPID,omitempty"` |
| 44 | Errors []error `json:"errors,omitempty"` |
| 45 | Config *LimaYAML `json:"config,omitempty"` |
| 46 | SSHAddress string `json:"sshAddress,omitempty"` |
| 47 | Protected bool `json:"protected"` |
| 48 | LimaVersion string `json:"limaVersion"` |
| 49 | Param map[string]string `json:"param,omitempty"` |
| 50 | AutoStartedIdentifier string `json:"autoStartedIdentifier,omitempty"` |
| 51 | } |
| 52 | |
| 53 | |
| 54 | |
| 55 | func (inst *Instance) Protect() error { |
| 56 | protected := filepath.Join(inst.Dir, filenames.Protected) |
| 57 | TODO: |
| 58 | |
| 59 | if err := os.WriteFile(protected, nil, 0o400); err != nil { |
| 60 | return err |
| 61 | } |
| 62 | inst.Protected = true |
| 63 | return nil |
| 64 | } |
| 65 | |
| 66 | |
| 67 | |
| 68 | func (inst *Instance) Unprotect() error { |
| 69 | protected := filepath.Join(inst.Dir, filenames.Protected) |
| 70 | if err := os.RemoveAll(protected); err != nil { |
| 71 | return err |
| 72 | } |
| 73 | inst.Protected = false |
| 74 | return nil |
| 75 | } |
| 76 | |
| 77 | func (inst *Instance) MarshalJSON() ([]byte, error) { |
| 78 | type Alias Instance |
| 79 | errorsAsStrings := make([]string, len(inst.Errors)) |
| 80 | for i, err := range inst.Errors { |
| 81 | if err != nil { |
| 82 | errorsAsStrings[i] = err.Error() |
| 83 | } |
| 84 | } |
| 85 | return json.Marshal(&struct { |
| 86 | *Alias |
| 87 | Errors []string `json:"errors,omitempty"` |
| 88 | }{ |
| 89 | Alias: (*Alias)(inst), |
| 90 | Errors: errorsAsStrings, |
| 91 | }) |
| 92 | } |
| 93 | |
| 94 | func (inst *Instance) UnmarshalJSON(data []byte) error { |
| 95 | type Alias Instance |
| 96 | aux := &struct { |
| 97 | *Alias |
| 98 | Errors []string `json:"errors,omitempty"` |
| 99 | }{ |
| 100 | Alias: (*Alias)(inst), |
| 101 | } |
| 102 | if err := json.Unmarshal(data, &aux); err != nil { |
| 103 | return err |
| 104 | } |
| 105 | inst.Errors = nil |
| 106 | for _, msg := range aux.Errors { |
| 107 | inst.Errors = append(inst.Errors, errors.New(msg)) |
| 108 | } |
| 109 | return nil |
| 110 | } |