-
Notifications
You must be signed in to change notification settings - Fork 649
/
Copy pathinstance.go
447 lines (420 loc) · 12.6 KB
/
instance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package store
import (
"context"
"errors"
"fmt"
"io"
"os"
"os/user"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
"text/tabwriter"
"text/template"
"time"
"github.com/docker/go-units"
hostagentclient "github.com/lima-vm/lima/pkg/hostagent/api/client"
"github.com/lima-vm/lima/pkg/identifierutil"
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/lima-vm/lima/pkg/store/dirnames"
"github.com/lima-vm/lima/pkg/store/filenames"
"github.com/lima-vm/lima/pkg/textutil"
"github.com/lima-vm/lima/pkg/version/versionutil"
"github.com/sirupsen/logrus"
)
type Status = string
const (
StatusUnknown Status = ""
StatusUninitialized Status = "Uninitialized"
StatusInstalling Status = "Installing"
StatusBroken Status = "Broken"
StatusStopped Status = "Stopped"
StatusRunning Status = "Running"
)
type Instance struct {
Name string `json:"name"`
// Hostname, not HostName (corresponds to SSH's naming convention)
Hostname string `json:"hostname"`
Status Status `json:"status"`
Saved bool `json:"saved"`
Dir string `json:"dir"`
VMType limayaml.VMType `json:"vmType"`
Arch limayaml.Arch `json:"arch"`
CPUType string `json:"cpuType"`
CPUs int `json:"cpus,omitempty"`
Memory int64 `json:"memory,omitempty"` // bytes
Disk int64 `json:"disk,omitempty"` // bytes
Message string `json:"message,omitempty"`
AdditionalDisks []limayaml.Disk `json:"additionalDisks,omitempty"`
Networks []limayaml.Network `json:"network,omitempty"`
SSHLocalPort int `json:"sshLocalPort,omitempty"`
SSHConfigFile string `json:"sshConfigFile,omitempty"`
HostAgentPID int `json:"hostAgentPID,omitempty"`
DriverPID int `json:"driverPID,omitempty"`
Errors []error `json:"errors,omitempty"`
Config *limayaml.LimaYAML `json:"config,omitempty"`
SSHAddress string `json:"sshAddress,omitempty"`
Protected bool `json:"protected"`
LimaVersion string `json:"limaVersion"`
Param map[string]string `json:"param,omitempty"`
}
// Inspect returns err only when the instance does not exist (os.ErrNotExist).
// Other errors are returned as *Instance.Errors.
func Inspect(instName string) (*Instance, error) {
inst := &Instance{
Name: instName,
// TODO: support customizing hostname
Hostname: identifierutil.HostnameFromInstName(instName),
Status: StatusUnknown,
}
// InstanceDir validates the instName but does not check whether the instance exists
instDir, err := InstanceDir(instName)
if err != nil {
return nil, err
}
// Make sure inst.Dir is set, even when YAML validation fails
inst.Dir = instDir
yamlPath := filepath.Join(instDir, filenames.LimaYAML)
y, err := LoadYAMLByFilePath(yamlPath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil, err
}
inst.Errors = append(inst.Errors, err)
return inst, nil
}
inst.Config = y
inst.Arch = *y.Arch
inst.VMType = *y.VMType
inst.CPUType = y.CPUType[*y.Arch]
inst.SSHAddress = "127.0.0.1"
inst.SSHLocalPort = *y.SSH.LocalPort // maybe 0
inst.SSHConfigFile = filepath.Join(instDir, filenames.SSHConfig)
inst.HostAgentPID, err = ReadPIDFile(filepath.Join(instDir, filenames.HostAgentPID))
if err != nil {
inst.Status = StatusBroken
inst.Errors = append(inst.Errors, err)
}
if inst.HostAgentPID != 0 {
haSock := filepath.Join(instDir, filenames.HostAgentSock)
haClient, err := hostagentclient.NewHostAgentClient(haSock)
if err != nil {
inst.Status = StatusBroken
inst.Errors = append(inst.Errors, fmt.Errorf("failed to connect to %q: %w", haSock, err))
} else {
ctx, cancel := context.WithTimeout(context.TODO(), 3*time.Second)
defer cancel()
info, err := haClient.Info(ctx)
if err != nil {
inst.Status = StatusBroken
inst.Errors = append(inst.Errors, fmt.Errorf("failed to get Info from %q: %w", haSock, err))
} else {
inst.SSHLocalPort = info.SSHLocalPort
}
}
}
inst.CPUs = *y.CPUs
memory, err := units.RAMInBytes(*y.Memory)
if err == nil {
inst.Memory = memory
}
disk, err := units.RAMInBytes(*y.Disk)
if err == nil {
inst.Disk = disk
}
inst.AdditionalDisks = y.AdditionalDisks
inst.Networks = y.Networks
// 0 out values since not configurable on WSL2
if inst.VMType == limayaml.WSL2 {
inst.Memory = 0
inst.CPUs = 0
inst.Disk = 0
}
protected := filepath.Join(instDir, filenames.Protected)
if _, err := os.Lstat(protected); !errors.Is(err, os.ErrNotExist) {
inst.Protected = true
}
inspectStatus(instDir, inst, y)
_, err = os.Stat(filepath.Join(instDir, filenames.VzMachineState))
if err == nil {
inst.Saved = true
} else if errors.Is(err, os.ErrNotExist) {
inst.Saved = false
} else {
inst.Errors = append(inst.Errors, fmt.Errorf("cannot determine whether the instance is saved: %w", err))
}
tmpl, err := template.New("format").Parse(y.Message)
if err != nil {
inst.Errors = append(inst.Errors, fmt.Errorf("message %q is not a valid template: %w", y.Message, err))
inst.Status = StatusBroken
} else {
data, err := AddGlobalFields(inst)
if err != nil {
inst.Errors = append(inst.Errors, fmt.Errorf("cannot add global fields to instance data: %w", err))
inst.Status = StatusBroken
} else {
var message strings.Builder
err = tmpl.Execute(&message, data)
if err != nil {
inst.Errors = append(inst.Errors, fmt.Errorf("cannot execute template %q: %w", y.Message, err))
inst.Status = StatusBroken
} else {
inst.Message = message.String()
}
}
}
limaVersionFile := filepath.Join(instDir, filenames.LimaVersion)
if version, err := os.ReadFile(limaVersionFile); err == nil {
inst.LimaVersion = strings.TrimSpace(string(version))
if _, err = versionutil.Parse(inst.LimaVersion); err != nil {
logrus.Warnf("treating lima version %q from %q as very latest release", inst.LimaVersion, limaVersionFile)
}
} else if !errors.Is(err, os.ErrNotExist) {
inst.Errors = append(inst.Errors, err)
}
inst.Param = y.Param
return inst, nil
}
func inspectStatusWithPIDFiles(instDir string, inst *Instance, y *limayaml.LimaYAML) {
var err error
inst.DriverPID, err = ReadPIDFile(filepath.Join(instDir, filenames.PIDFile(*y.VMType)))
if err != nil {
inst.Status = StatusBroken
inst.Errors = append(inst.Errors, err)
}
if inst.Status == StatusUnknown {
switch {
case inst.HostAgentPID > 0 && inst.DriverPID > 0:
inst.Status = StatusRunning
case inst.HostAgentPID == 0 && inst.DriverPID == 0:
inst.Status = StatusStopped
case inst.HostAgentPID > 0 && inst.DriverPID == 0:
inst.Errors = append(inst.Errors, errors.New("host agent is running but driver is not"))
inst.Status = StatusBroken
default:
inst.Errors = append(inst.Errors, fmt.Errorf("%s driver is running but host agent is not", inst.VMType))
inst.Status = StatusBroken
}
}
}
// ReadPIDFile returns 0 if the PID file does not exist or the process has already terminated
// (in which case the PID file will be removed).
func ReadPIDFile(path string) (int, error) {
b, err := os.ReadFile(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return 0, nil
}
return 0, err
}
pid, err := strconv.Atoi(strings.TrimSpace(string(b)))
if err != nil {
return 0, err
}
proc, err := os.FindProcess(pid)
if err != nil {
return 0, err
}
// os.FindProcess will only return running processes on Windows, exit early
if runtime.GOOS == "windows" {
return pid, nil
}
err = proc.Signal(syscall.Signal(0))
if err != nil {
if errors.Is(err, os.ErrProcessDone) {
_ = os.Remove(path)
return 0, nil
}
// We may not have permission to send the signal (e.g. to network daemon running as root).
// But if we get a permissions error, it means the process is still running.
if !errors.Is(err, os.ErrPermission) {
return 0, err
}
}
return pid, nil
}
type FormatData struct {
Instance
HostOS string
HostArch string
LimaHome string
IdentityFile string
}
var FormatHelp = "\n" +
"These functions are available to go templates:\n\n" +
textutil.IndentString(2,
strings.Join(textutil.FuncHelp, "\n")+"\n")
func AddGlobalFields(inst *Instance) (FormatData, error) {
var data FormatData
data.Instance = *inst
// Add HostOS
data.HostOS = runtime.GOOS
// Add HostArch
data.HostArch = limayaml.NewArch(runtime.GOARCH)
// Add IdentityFile
configDir, err := dirnames.LimaConfigDir()
if err != nil {
return FormatData{}, err
}
data.IdentityFile = filepath.Join(configDir, filenames.UserPrivateKey)
// Add LimaHome
data.LimaHome, err = dirnames.LimaDir()
if err != nil {
return FormatData{}, err
}
return data, nil
}
type PrintOptions struct {
AllFields bool
TerminalWidth int
}
// PrintInstances prints instances in a requested format to a given io.Writer.
// Supported formats are "json", "yaml", "table", or a go template.
func PrintInstances(w io.Writer, instances []*Instance, format string, options *PrintOptions) error {
switch format {
case "json":
format = "{{json .}}"
case "yaml":
format = "{{yaml .}}"
case "table":
types := map[string]int{}
archs := map[string]int{}
for _, instance := range instances {
types[instance.VMType]++
archs[instance.Arch]++
}
all := options != nil && options.AllFields
width := 0
if options != nil {
width = options.TerminalWidth
}
columnWidth := 8
hideType := false
hideArch := false
hideDir := false
columns := 1 // NAME
columns += 2 // STATUS
columns += 2 // SSH
// can we still fit the remaining columns (7)
if width == 0 || (columns+7)*columnWidth > width && !all {
hideType = len(types) == 1
}
if !hideType {
columns++ // VMTYPE
}
// only hide arch if it is the same as the host arch
goarch := limayaml.NewArch(runtime.GOARCH)
// can we still fit the remaining columns (6)
if width == 0 || (columns+6)*columnWidth > width && !all {
hideArch = len(archs) == 1 && instances[0].Arch == goarch
}
if !hideArch {
columns++ // ARCH
}
columns++ // CPUS
columns++ // MEMORY
columns++ // DISK
// can we still fit the remaining columns (2)
if width != 0 && (columns+2)*columnWidth > width && !all {
hideDir = true
}
if !hideDir {
columns += 2 // DIR
}
_ = columns
w := tabwriter.NewWriter(w, 4, 8, 4, ' ', 0)
fmt.Fprint(w, "NAME\tSTATUS\tSSH")
if !hideType {
fmt.Fprint(w, "\tVMTYPE")
}
if !hideArch {
fmt.Fprint(w, "\tARCH")
}
fmt.Fprint(w, "\tCPUS\tMEMORY\tDISK")
if !hideDir {
fmt.Fprint(w, "\tDIR")
}
fmt.Fprintln(w)
u, err := user.Current()
if err != nil {
return err
}
homeDir := u.HomeDir
for _, instance := range instances {
dir := instance.Dir
if strings.HasPrefix(dir, homeDir) {
dir = strings.Replace(dir, homeDir, "~", 1)
}
fmt.Fprintf(w, "%s\t%s\t%s",
instance.Name,
instance.Status,
fmt.Sprintf("%s:%d", instance.SSHAddress, instance.SSHLocalPort),
)
if !hideType {
fmt.Fprintf(w, "\t%s",
instance.VMType,
)
}
if !hideArch {
fmt.Fprintf(w, "\t%s",
instance.Arch,
)
}
fmt.Fprintf(w, "\t%d\t%s\t%s",
instance.CPUs,
units.BytesSize(float64(instance.Memory)),
units.BytesSize(float64(instance.Disk)),
)
if !hideDir {
fmt.Fprintf(w, "\t%s",
dir,
)
}
fmt.Fprint(w, "\n")
}
return w.Flush()
default:
// NOP
}
tmpl, err := template.New("format").Funcs(textutil.TemplateFuncMap).Parse(format)
if err != nil {
return fmt.Errorf("invalid go template: %w", err)
}
for _, instance := range instances {
data, err := AddGlobalFields(instance)
if err != nil {
return err
}
data.Message = strings.TrimSuffix(instance.Message, "\n")
err = tmpl.Execute(w, data)
if err != nil {
return err
}
fmt.Fprintln(w)
}
return nil
}
// Protect protects the instance to prohibit accidental removal.
// Protect does not return an error even when the instance is already protected.
func (inst *Instance) Protect() error {
protected := filepath.Join(inst.Dir, filenames.Protected)
// TODO: Do an equivalent of `chmod +a "everyone deny delete,delete_child,file_inherit,directory_inherit"`
// https://github.com/lima-vm/lima/issues/1595
if err := os.WriteFile(protected, nil, 0o400); err != nil {
return err
}
inst.Protected = true
return nil
}
// Unprotect unprotects the instance.
// Unprotect does not return an error even when the instance is already unprotected.
func (inst *Instance) Unprotect() error {
protected := filepath.Join(inst.Dir, filenames.Protected)
if err := os.RemoveAll(protected); err != nil {
return err
}
inst.Protected = false
return nil
}