-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmutils.go
106 lines (97 loc) · 2.49 KB
/
vmutils.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
package vmutils
import (
"bytes"
"net"
"os/exec"
"strings"
"time"
)
type (
// VM is the structure for a VM configuration
VM struct {
controller string
vmx string
vmStarted bool
}
)
// NewVM creates a new VM, with controller being the location of the vmrun utility and vmx being the location of the vmx to start.
func NewVM(controller, vmx string) (vm *VM) {
vm = &VM{controller: controller, vmx: vmx}
return
}
// IPTimeoutInMS waits for the IP for the VM, timing out in timeoutInMS, returning an empty string, or the IP if the timeout isn't reached.
func (vm *VM) IPTimeoutInMS(timeoutInMS uint64) (result string) {
timeout := timeoutInMS
result = vm.IPTimeout(time.Duration(timeout) * time.Millisecond)
return
}
// IPTimeout waits for the specified timeout, or returns the IP for the VM
func (vm *VM) IPTimeout(timeout time.Duration) (result string) {
var (
ipAddr string
elapsed time.Duration
)
start := time.Now()
timeoutInMS := uint64(timeout) / uint64(time.Millisecond)
for ok := true; ok; ok = result == "" {
cmd := exec.Command(vm.controller, "-T", "ws", "readVariable", vm.vmx, "guestVar", "ip")
var out bytes.Buffer
cmd.Stdout = &out
cmd.Run()
ipAddr = strings.TrimSuffix(out.String(), "\n")
ipAddr = strings.TrimSuffix(ipAddr, "\r")
if ipAddr != "" {
ip := net.ParseIP(ipAddr)
if ip != nil {
result = ipAddr
}
}
elapsed = time.Since(start)
elapsedMS := uint64(elapsed / time.Millisecond)
if elapsedMS > timeoutInMS {
return
}
}
return
}
// IP returns the IP for a VM, waiting infinitely if it doesn't get an IP. Requires the VM to be started.
func (vm *VM) IP() (result string) {
if !vm.vmStarted {
vm.Start()
}
for ok := true; ok; ok = result == "" {
cmd := exec.Command(vm.controller, "-T", "ws", "readVariable", vm.vmx, "guestVar", "ip")
var out bytes.Buffer
cmd.Stdout = &out
cmd.Run()
ipAddr := strings.TrimSuffix(out.String(), "\n")
ipAddr = strings.TrimSuffix(ipAddr, "\r")
if ipAddr != "" {
ip := net.ParseIP(ipAddr)
if ip != nil {
result = ipAddr
}
}
}
return
}
func (vm *VM) getWSCmd(cmd string) (result *exec.Cmd) {
result = exec.Command(vm.controller, "-T", "ws", cmd, vm.vmx)
return
}
// Start starts the VM
func (vm *VM) Start() {
cmd := vm.getWSCmd("start")
var out bytes.Buffer
cmd.Stdout = &out
cmd.Run()
vm.vmStarted = true
}
// Stop stops the currently running VM
func (vm *VM) Stop() {
cmd := vm.getWSCmd("stop")
var out bytes.Buffer
cmd.Stdout = &out
cmd.Run()
vm.vmStarted = false
}