-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.go
239 lines (199 loc) · 5.89 KB
/
control.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
package parallels
// Large part of this source is taken from
// https://github.com/mitchellh/packer/blob/master/builder/parallels/common
import (
"bytes"
"errors"
"fmt"
"github.com/sirupsen/logrus"
"io/ioutil"
"os/exec"
"regexp"
"runtime"
"strconv"
"strings"
"time"
)
type StatusType string
const (
NotFound StatusType = "notfound"
Invalid StatusType = "invalid"
Stopped StatusType = "stopped"
Suspended StatusType = "suspended"
Running StatusType = "running"
// TODO: more statuses
)
const (
prlctlPath = "prlctl"
dhcpLeases = "/Library/Preferences/Parallels/parallels_dhcp_leases"
)
func PrlctlOutput(args ...string) (string, error) {
if runtime.GOOS != "darwin" {
return "", fmt.Errorf("Parallels works only on \"darwin\" platform")
}
var stdout, stderr bytes.Buffer
logrus.Debugf("Executing PrlctlOutput: %#v", args)
cmd := exec.Command(prlctlPath, args...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
stderrString := strings.TrimSpace(stderr.String())
if _, ok := err.(*exec.ExitError); ok {
err = fmt.Errorf("PrlctlOutput error: %s", stderrString)
}
return stdout.String(), err
}
func Prlctl(args ...string) error {
_, err := PrlctlOutput(args...)
return err
}
func Exec(vmName string, args ...string) (string, error) {
args2 := append([]string{"exec", vmName}, args...)
return PrlctlOutput(args2...)
}
func Version() (string, error) {
out, err := PrlctlOutput("--version")
if err != nil {
return "", err
}
versionRe := regexp.MustCompile(`prlctl version (\d+\.\d+.\d+)`)
matches := versionRe.FindStringSubmatch(string(out))
if matches == nil {
return "", fmt.Errorf(
"Could not find Parallels Desktop version in output:\n%s", string(out))
}
version := matches[1]
logrus.Debugf("Parallels Desktop version: %s", version)
return version, nil
}
func Exist(name string) bool {
err := Prlctl("list", name, "--no-header", "--output", "status")
if err != nil {
return false
}
return true
}
func CreateTemplate(vmName, templateName string) error {
return Prlctl("clone", vmName, "--name", templateName, "--template", "--linked")
}
func CreateOsVM(vmName, templateName string) error {
return Prlctl("create", vmName, "--ostemplate", templateName)
}
func CreateSnapshot(vmName, snapshotName string) error {
return Prlctl("snapshot", vmName, "--name", snapshotName)
}
func GetDefaultSnapshot(vmName string) (string, error) {
output, err := PrlctlOutput("snapshot-list", vmName)
if err != nil {
return "", err
}
lines := strings.Split(output, "\n")
for _, line := range lines {
pos := strings.Index(line, " *")
if pos >= 0 {
snapshot := line[pos+2:]
snapshot = strings.TrimSpace(snapshot)
if len(snapshot) > 0 { // It uses UUID so it should be 38
return snapshot, nil
}
}
}
return "", errors.New("No snapshot")
}
func RevertToSnapshot(vmName, snapshotID string) error {
return Prlctl("snapshot-switch", vmName, "--id", snapshotID)
}
func Start(vmName string) error {
return Prlctl("start", vmName)
}
func Status(vmName string) (StatusType, error) {
output, err := PrlctlOutput("list", vmName, "--no-header", "--output", "status")
if err != nil {
return NotFound, err
}
return StatusType(strings.TrimSpace(output)), nil
}
func WaitForStatus(vmName string, vmStatus StatusType, seconds int) error {
var status StatusType
var err error
for i := 0; i < seconds; i++ {
status, err = Status(vmName)
if err != nil {
return err
}
if status == vmStatus {
return nil
}
time.Sleep(time.Second)
}
return errors.New("VM " + vmName + " is in " + string(status) + " where it should be in " + string(vmStatus))
}
func TryExec(vmName string, seconds int, cmd ...string) error {
var err error
for i := 0; i < seconds; i++ {
_, err = Exec(vmName, cmd...)
if err == nil {
return nil
}
time.Sleep(time.Second)
}
return err
}
func Kill(vmName string) error {
return Prlctl("stop", vmName, "--kill")
}
func Delete(vmName string) error {
return Prlctl("delete", vmName)
}
func Unregister(vmName string) error {
return Prlctl("unregister", vmName)
}
func Mac(vmName string) (string, error) {
output, err := PrlctlOutput("list", "-i", vmName)
if err != nil {
return "", err
}
stdoutString := strings.TrimSpace(output)
re := regexp.MustCompile("net0.* mac=([0-9A-F]{12}) card=.*")
macMatch := re.FindAllStringSubmatch(stdoutString, 1)
if len(macMatch) != 1 {
return "", fmt.Errorf("MAC address for NIC: nic0 on Virtual Machine: %s not found", vmName)
}
mac := macMatch[0][1]
logrus.Debugf("Found MAC address for NIC: net0 - %s\n", mac)
return mac, nil
}
// IPAddress finds the IP address of a VM connected that uses DHCP by its MAC address
//
// Parses the file /Library/Preferences/Parallels/parallels_dhcp_leases
// file contain a list of DHCP leases given by Parallels Desktop
// Example line:
// 10.211.55.181="1418921112,1800,001c42f593fb,ff42f593fb000100011c25b9ff001c42f593fb"
// IP Address ="Lease expiry, Lease time, MAC, MAC or DUID"
func IPAddress(mac string) (string, error) {
if len(mac) != 12 {
return "", fmt.Errorf("Not a valid MAC address: %s. It should be exactly 12 digits", mac)
}
leases, err := ioutil.ReadFile(dhcpLeases)
if err != nil {
return "", err
}
re := regexp.MustCompile("(.*)=\"(.*),(.*)," + strings.ToLower(mac) + ",.*\"")
mostRecentIP := ""
mostRecentLease := uint64(0)
for _, l := range re.FindAllStringSubmatch(string(leases), -1) {
ip := l[1]
expiry, _ := strconv.ParseUint(l[2], 10, 64)
leaseTime, _ := strconv.ParseUint(l[3], 10, 32)
logrus.Debugf("Found lease: %s for MAC: %s, expiring at %d, leased for %d s.\n", ip, mac, expiry, leaseTime)
if mostRecentLease <= expiry-leaseTime {
mostRecentIP = ip
mostRecentLease = expiry - leaseTime
}
}
if len(mostRecentIP) == 0 {
return "", fmt.Errorf("IP lease not found for MAC address %s in: %s", mac, dhcpLeases)
}
logrus.Debugf("Found IP lease: %s for MAC address %s\n", mostRecentIP, mac)
return mostRecentIP, nil
}