Repository navigation
Expand file tree
/
Copy pathlist.go
More file actions
190 lines (172 loc) · 4.81 KB
/
Copy pathlist.go
File metadata and controls
190 lines (172 loc) · 4.81 KB
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
package main
import (
"encoding/json"
"flag"
"fmt"
"maps"
"os"
"slices"
"strconv"
"strings"
"sync"
"text/tabwriter"
"time"
)
const listUsage = `List dev VMs with their IP and SSH hostname.
Usage: devvm list
- Shows each VM that ~/.config/dev-vm/state.json records, with its Lima
status, its guest IP and its SSH hostname. The hostname comes from the ssh
config that Lima generates.
- CPUS, MEM and DISK come from Lima. Thus they show the real size of the
instance, not the flags that created it.
- The command reads IP from the guest, the only source of the IP. Lima does
not report it. If a VM is not running or does not answer in time, IP shows
"-".
- There are no port forwards. Connect to a service in the guest at that IP.
- Connect with: ssh -F ~/.lima/<name>/ssh.config <hostname>
- If a VM is in the state file but has no Lima instance, its status is Missing.
`
// guestIPTimeout bounds the single guest query per VM, so a wedged guest
// cannot hang list or status.
const guestIPTimeout = 5 * time.Second
type limaInstance struct {
Name string `json:"name"`
Hostname string `json:"hostname"`
Status string `json:"status"`
CPUs int `json:"cpus"`
Memory int64 `json:"memory"` // bytes
Disk int64 `json:"disk"` // bytes
Network []struct {
Interface string `json:"interface"`
} `json:"network"`
}
// iface is the guest network interface Lima assigns to the VM's own IP.
func (inst limaInstance) iface() string {
for _, n := range inst.Network {
if n.Interface != "" {
return n.Interface
}
}
return "lima0"
}
func cmdList(argv []string) {
fs := flag.NewFlagSet("list", flag.ExitOnError)
fs.Usage = func() {
fmt.Fprint(os.Stderr, listUsage)
}
fs.Parse(argv)
if fs.NArg() > 0 {
die("unexpected argument %q", fs.Arg(0))
}
vms, _ := loadState()["vms"].(map[string]any)
if len(vms) == 0 {
fmt.Println("no VMs recorded; run: dev-vm create")
return
}
names := slices.Sorted(maps.Keys(vms))
instances := limaInstances()
ips := guestIPs(instances, names)
w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
fmt.Fprintln(w, "NAME\tSTATUS\tCPUS\tMEM\tDISK\tIP\tSSH")
for _, name := range names {
status, host := "Missing", "-"
cpus, mem, disk := "-", "-", "-"
if inst, ok := instances[name]; ok {
status, host = inst.Status, inst.Hostname
if inst.CPUs > 0 {
cpus = strconv.Itoa(inst.CPUs)
}
mem, disk = gib(inst.Memory), gib(inst.Disk)
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
name, status, cpus, mem, disk, dash(ips[name]), host)
}
w.Flush()
}
// gib renders a Lima byte count as GiB, or "-" when unknown.
func gib(n int64) string {
if n <= 0 {
return "-"
}
return fmt.Sprintf("%dGiB", n>>30)
}
// dash renders an empty value as "-", like the size columns.
func dash(s string) string {
if s == "" {
return "-"
}
return s
}
// limaInstances indexes `limactl list --format json` output, one JSON object
// per line, by instance name.
func limaInstances() map[string]limaInstance {
dec := json.NewDecoder(strings.NewReader(limactl("list", "--format", "json")))
instances := map[string]limaInstance{}
for dec.More() {
var inst limaInstance
if err := dec.Decode(&inst); err != nil {
die("cannot parse limactl list output: %v", err)
}
instances[inst.Name] = inst
}
return instances
}
// guestIPs asks every running VM for its own IP, one attempt each and all in
// parallel. Names without a running instance, and queries that fail, are left
// out of the map.
func guestIPs(instances map[string]limaInstance, names []string) map[string]string {
var (
mu sync.Mutex
wg sync.WaitGroup
ips = map[string]string{}
)
for _, name := range names {
inst, ok := instances[name]
if !ok || inst.Status != "Running" {
continue
}
wg.Go(func() {
ip := guestIP(inst)
if ip == "" {
return
}
mu.Lock()
defer mu.Unlock()
ips[name] = ip
})
}
wg.Wait()
return ips
}
// guestIP reads the VM's own IP from inside the guest. Lima reports no guest
// IP: `limactl list --format json` carries only the MAC, and the macOS DHCP
// lease file keeps stale rows, so the guest is the only source of truth.
func guestIP(inst limaInstance) string {
out, err := limactlTry(guestIPTimeout,
"shell", inst.Name, "ip", "-4", "-json", "addr", "show", inst.iface())
if err != nil {
return ""
}
return parseGuestIP(out)
}
// parseGuestIP picks the first IPv4 address out of `ip -4 -json addr` output,
// or returns "" when there is none.
func parseGuestIP(out []byte) string {
var links []struct {
AddrInfo []struct {
Family string `json:"family"`
Local string `json:"local"`
} `json:"addr_info"`
}
if err := json.Unmarshal(out, &links); err != nil {
return ""
}
for _, link := range links {
for _, addr := range link.AddrInfo {
if addr.Family == "inet" && addr.Local != "" {
return addr.Local
}
}
}
return ""
}