forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.go
55 lines (51 loc) · 919 Bytes
/
vm.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
package virtualbox
import (
"bufio"
"io"
"strconv"
"strings"
)
type VirtualBoxVM struct {
CPUs int
Memory int
}
func parseVMInfo(r io.Reader) (*VirtualBoxVM, error) {
s := bufio.NewScanner(r)
vm := &VirtualBoxVM{}
for s.Scan() {
line := s.Text()
if line == "" {
continue
}
res := reEqualLine.FindStringSubmatch(line)
if res == nil {
continue
}
switch key, val := res[1], res[2]; key {
case "cpus":
v, err := strconv.Atoi(val)
if err != nil {
return nil, err
}
vm.CPUs = v
case "memory":
v, err := strconv.Atoi(val)
if err != nil {
return nil, err
}
vm.Memory = v
}
}
if err := s.Err(); err != nil {
return nil, err
}
return vm, nil
}
func getVMInfo(name string) (*VirtualBoxVM, error) {
out, err := vbmOut("showvminfo", name, "--machinereadable")
if err != nil {
return nil, err
}
r := strings.NewReader(out)
return parseVMInfo(r)
}