generated from hashicorp/packer-plugin-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathdriver.go
673 lines (558 loc) · 19.3 KB
/
driver.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package common
import (
"bytes"
"errors"
"fmt"
"io"
"log"
"net"
"os"
"os/exec"
"regexp"
"runtime"
"strconv"
"strings"
"time"
"github.com/hashicorp/packer-plugin-sdk/multistep"
)
// A driver is able to talk to VMware, control virtual machines, etc.
type Driver interface {
// Clone clones the VMX and the disk to the destination path. The
// destination is a path to the VMX file. The disk will be copied
// to that same directory.
Clone(dst string, src string, cloneType bool, snapshot string) error
// CompactDisk compacts a virtual disk.
CompactDisk(string) error
// CreateDisk creates a virtual disk with the given size.
CreateDisk(string, string, string, string) error
// CreateSnapshot creates a snapshot of the supplied .vmx file with
// the given name
CreateSnapshot(string, string) error
// Checks if the VMX file at the given path is running.
IsRunning(string) (bool, error)
// Start starts a VM specified by the path to the VMX given.
Start(string, bool) error
// Stop stops a VM specified by the path to the VMX given.
Stop(string) error
// SuppressMessages modifies the VMX or surrounding directory so that
// VMware doesn't show any annoying messages.
SuppressMessages(string) error
// Get the path to the VMware ISO for the given flavor.
ToolsIsoPath(string) string
// Attach the VMware tools ISO
ToolsInstall() error
// Verify checks to make sure that this driver should function
// properly. This should check that all the files it will use
// appear to exist and so on. If everything is okay, this doesn't
// return an error. Otherwise, this returns an error. Each vmware
// driver should assign the VmwareMachine callback functions for locating
// paths within this function.
Verify() error
/// This is to establish a connection to the guest
CommHost(multistep.StateBag) (string, error)
/// These methods are generally implemented by the VmwareDriver
/// structure within this file. A driver implementation can
/// reimplement these, though, if it wants.
GetVmwareDriver() VmwareDriver
// Get the guest hw address for the vm
GuestAddress(multistep.StateBag) (string, error)
// Get the guest ip address for the vm
PotentialGuestIP(multistep.StateBag) ([]string, error)
// Get the host hw address for the vm
HostAddress(multistep.StateBag) (string, error)
// Get the host ip address for the vm
HostIP(multistep.StateBag) (string, error)
// Export the vm to ovf or ova format using ovftool
Export([]string) error
// OvfTool
VerifyOvfTool(bool, bool) error
}
// NewDriver returns a new driver implementation for this operating
// system, or an error if the driver couldn't be initialized.
func NewDriver(dconfig *DriverConfig, config *SSHConfig, vmName string) (Driver, error) {
var drivers []Driver
if dconfig.RemoteType != "" {
esx5Driver, err := NewESX5Driver(dconfig, config, vmName)
if err != nil {
return nil, err
}
drivers = []Driver{esx5Driver}
} else {
switch runtime.GOOS {
case "darwin":
drivers = []Driver{
NewFusion6Driver(dconfig, config),
NewFusion5Driver(dconfig, config),
}
case "linux":
fallthrough
case "windows":
drivers = []Driver{
NewWorkstation10Driver(config),
NewWorkstation9Driver(config),
NewPlayer6Driver(config),
NewPlayer5Driver(config),
}
default:
return nil, fmt.Errorf("can't find driver for OS: %s", runtime.GOOS)
}
}
errs := ""
for _, driver := range drivers {
err := driver.Verify()
log.Printf("Testing against vmware driver %T, Success: %t", driver, err == nil)
if err == nil {
return driver, nil
}
log.Printf("skipping %T because it failed with the following error %s", driver, err)
errs += "* " + err.Error() + "\n"
}
return nil, fmt.Errorf("driver initialization failed. fix at least one driver to continue:\n%s", errs)
}
func runAndLog(cmd *exec.Cmd) (string, string, error) {
var stdout, stderr bytes.Buffer
log.Printf("Executing: %s %s", cmd.Path, strings.Join(cmd.Args[1:], " "))
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
stdoutString := strings.TrimSpace(stdout.String())
stderrString := strings.TrimSpace(stderr.String())
if _, ok := err.(*exec.ExitError); ok {
message := stderrString
if message == "" {
message = stdoutString
}
err = fmt.Errorf("VMware error: %s", message)
// If "unknown error" is in there, add some additional notes
re := regexp.MustCompile(`(?i)unknown error`)
if re.MatchString(message) {
err = fmt.Errorf(
"%s\n\n%s", err,
"Packer detected a VMware 'Unknown Error'. Unfortunately VMware\n"+
"often has extremely vague error messages such as this and Packer\n"+
"itself can't do much about that. Please check the vmware.log files\n"+
"created by VMware when a VM is started (in the directory of the\n"+
"vmx file), which often contains more detailed error information.\n\n"+
"You may need to set the command line flag --on-error=abort to\n\n"+
"prevent Packer from cleaning up the vmx file directory.")
}
}
log.Printf("stdout: %s", stdoutString)
log.Printf("stderr: %s", stderrString)
// Replace these for Windows, we only want to deal with Unix
// style line endings.
returnStdout := strings.Replace(stdout.String(), "\r\n", "\n", -1)
returnStderr := strings.Replace(stderr.String(), "\r\n", "\n", -1)
return returnStdout, returnStderr, err
}
func normalizeVersion(version string) (string, error) {
i, err := strconv.Atoi(version)
if err != nil {
return "", fmt.Errorf("returned a non-integer version %q: %s", version, err)
}
return fmt.Sprintf("%02d", i), nil
}
func compareVersions(versionFound string, versionWanted string, product string) error {
found, err := normalizeVersion(versionFound)
if err != nil {
return err
}
wanted, err := normalizeVersion(versionWanted)
if err != nil {
return err
}
if found < wanted {
return fmt.Errorf("requires %s or later, found %s", versionWanted, versionFound)
}
return nil
}
// helper functions that read configuration information from a file
// read the network<->device configuration out of the specified path
func ReadNetmapConfig(path string) (NetworkMap, error) {
fd, err := os.Open(path)
if err != nil {
return nil, err
}
defer fd.Close()
return ReadNetworkMap(fd)
}
// read the dhcp configuration out of the specified path
func ReadDhcpConfig(path string) (DhcpConfiguration, error) {
fd, err := os.Open(path)
if err != nil {
return nil, err
}
defer fd.Close()
return ReadDhcpConfiguration(fd)
}
// read the VMX configuration from the specified path
func readVMXConfig(path string) (map[string]string, error) {
f, err := os.Open(path)
if err != nil {
return map[string]string{}, err
}
defer f.Close()
vmxBytes, err := io.ReadAll(f)
if err != nil {
return map[string]string{}, err
}
return ParseVMX(string(vmxBytes)), nil
}
// read the connection type out of a vmx configuration
func readCustomDeviceName(vmxData map[string]string) (string, error) {
connectionType, ok := vmxData["ethernet0.connectiontype"]
if !ok || connectionType != "custom" {
return "", fmt.Errorf("unable to determine the device name for the connection type : %s", connectionType)
}
device, ok := vmxData["ethernet0.vnet"]
if !ok || device == "" {
return "", fmt.Errorf("unable to determine the device name for the connection type \"%s\" : %s", connectionType, device)
}
return device, nil
}
// This VmwareDriver is a base class that contains default methods
// that a Driver can use or implement themselves.
type VmwareDriver struct {
/// These methods define paths that are utilized by the driver
/// A driver must overload these in order to point to the correct
/// files so that the address detection (ip and ethernet) machinery
/// works.
DhcpLeasesPath func(string) string
DhcpConfPath func(string) string
VmnetnatConfPath func(string) string
/// This method returns an object with the NetworkNameMapper interface
/// that maps network to device and vice-versa.
NetworkMapper func() (NetworkNameMapper, error)
}
func (d *VmwareDriver) GuestAddress(state multistep.StateBag) (string, error) {
vmxPath := state.Get("vmx_path").(string)
log.Println("Lookup up IP information...")
vmxData, err := readVMXConfig(vmxPath)
if err != nil {
return "", err
}
var ok bool
macAddress := ""
if macAddress, ok = vmxData["ethernet0.address"]; !ok || macAddress == "" {
if macAddress, ok = vmxData["ethernet0.generatedaddress"]; !ok || macAddress == "" {
return "", errors.New("unable to determine MAC address")
}
}
log.Printf("GuestAddress discovered MAC address: %s", macAddress)
res, err := net.ParseMAC(macAddress)
if err != nil {
return "", err
}
return res.String(), nil
}
func (d *VmwareDriver) PotentialGuestIP(state multistep.StateBag) ([]string, error) {
// grab network mapper
netmap, err := d.NetworkMapper()
if err != nil {
return []string{}, err
}
// convert the stashed network to a device
network := state.Get("vmnetwork").(string)
devices, err := netmap.NameIntoDevices(network)
// log them to see what was detected
for _, device := range devices {
log.Printf("GuestIP discovered device matching %s: %s", network, device)
}
// we were unable to find the device, maybe it's a custom one...
// so, check to see if it's in the .vmx configuration
if err != nil || network == "custom" {
vmxPath := state.Get("vmx_path").(string)
vmxData, err := readVMXConfig(vmxPath)
if err != nil {
return []string{}, err
}
var device string
device, err = readCustomDeviceName(vmxData)
devices = append(devices, device)
if err != nil {
return []string{}, err
}
log.Printf("GuestIP discovered custom device matching %s: %s", network, device)
}
// figure out our MAC address for looking up the guest address
MACAddress, err := d.GuestAddress(state)
if err != nil {
return []string{}, err
}
// iterate through all of the devices and collect all the dhcp lease entries
// that we possibly cacn.
var available_lease_entries []dhcpLeaseEntry
for _, device := range devices {
// figure out the correct dhcp leases
dhcpLeasesPath := d.DhcpLeasesPath(device)
log.Printf("Trying DHCP leases path: %s", dhcpLeasesPath)
if dhcpLeasesPath == "" {
return []string{}, fmt.Errorf("no DHCP leases path found for device %s", device)
}
// open up the path to the dhcpd leases
fh, err := os.Open(dhcpLeasesPath)
if err != nil {
log.Printf("Error reading DHCP lease path file %s: %s", dhcpLeasesPath, err.Error())
continue
}
defer fh.Close()
// and then read its contents
leaseEntries, err := ReadDhcpdLeaseEntries(fh)
if err != nil {
return []string{}, err
}
// Parse our MAC address again. There's no need to check for an
// error because we've already parsed this successfully.
hwaddr, _ := net.ParseMAC(MACAddress)
// Go through our available lease entries and see which ones are within
// scope, and that match to our hardware address.
results := make([]dhcpLeaseEntry, 0)
for _, entry := range leaseEntries {
// First check for leases that are still valid. The timestamp for
// each lease should be in UTC according to the documentation at
// the top of VMware's dhcpd.leases file.
now := time.Now().UTC()
if !(now.After(entry.starts) && now.Before(entry.ends)) {
continue
}
// Next check for any where the hardware address matches.
if !bytes.Equal(hwaddr, entry.ether) {
continue
}
// This entry fits within our constraints, so store it so we can
// check it out later.
results = append(results, entry)
}
// If we weren't able to grab any results, then we'll do a "loose"-match
// where we only look for anything where the hardware address matches.
if len(results) == 0 {
log.Printf("Unable to find an exact match for DHCP lease. Falling back loose matching for a hardware address %v", MACAddress)
for _, entry := range leaseEntries {
if bytes.Equal(hwaddr, entry.ether) {
results = append(results, entry)
}
}
}
// If we found something, then we need to add it to our current list
// of lease entries.
if len(results) > 0 {
available_lease_entries = append(available_lease_entries, results...)
}
// Now we need to map our results to get the address so we can return it.iterate through our results and figure out which one
// is actually up...and should be relevant.
}
// Check if we found any lease entries that correspond to us. If so, then we
// need to map() them in order to extract the address field to return to the
// caller.
if len(available_lease_entries) > 0 {
addrs := make([]string, 0)
for _, entry := range available_lease_entries {
addrs = append(addrs, entry.address)
}
return addrs, nil
}
if runtime.GOOS == "darwin" {
// We have match no vmware DHCP lease for this MAC. We'll try to match it in Apple DHCP leases.
// As a remember, VMware is no longer able to rely on its own dhcpd server on MacOS BigSur and is
// forced to use Apple DHCPD server instead.
// https://communities.vmware.com/t5/VMware-Fusion-Discussions/Big-Sur-hosts-with-Fusion-Is-vmnet-dhcpd-vmnet8-leases-file/m-p/2298927/highlight/true#M140003
// set the apple dhcp leases path
appleDhcpLeasesPath := "/var/db/dhcpd_leases"
log.Printf("Trying Apple DHCP leases path: %s", appleDhcpLeasesPath)
// open up the path to the apple dhcpd leases
fh, err := os.Open(appleDhcpLeasesPath)
if err != nil {
log.Printf("Error while reading apple DHCP lease path file %s: %s", appleDhcpLeasesPath, err.Error())
} else {
defer fh.Close()
// and then read its contents
leaseEntries, err := ReadAppleDhcpdLeaseEntries(fh)
if err != nil {
return []string{}, err
}
// Parse our MAC address again. There's no need to check for an
// error because we've already parsed this successfully.
hwaddr, _ := net.ParseMAC(MACAddress)
// Go through our available lease entries and see which ones are within
// scope, and that match to our hardware address.
available_lease_entries := make([]appleDhcpLeaseEntry, 0)
for _, entry := range leaseEntries {
// Next check for any where the hardware address matches.
if bytes.Equal(hwaddr, entry.hwAddress) {
available_lease_entries = append(available_lease_entries, entry)
}
}
// Check if we found any lease entries that correspond to us. If so, then we
// need to map() them in order to extract the address field to return to the
// caller.
if len(available_lease_entries) > 0 {
addrs := make([]string, 0)
for _, entry := range available_lease_entries {
addrs = append(addrs, entry.ipAddress)
}
return addrs, nil
}
}
}
return []string{}, fmt.Errorf("none of the found device(s) %v have a DHCP lease for MAC address %s", devices, MACAddress)
}
func (d *VmwareDriver) HostAddress(state multistep.StateBag) (string, error) {
// grab mapper for converting network<->device
netmap, err := d.NetworkMapper()
if err != nil {
return "", err
}
// convert network to name
network := state.Get("vmnetwork").(string)
devices, err := netmap.NameIntoDevices(network)
// log them to see what was detected
for _, device := range devices {
log.Printf("HostAddress discovered device matching %s: %s", network, device)
}
// we were unable to find the device, maybe it's a custom one...
// so, check to see if it's in the .vmx configuration
if err != nil || network == "custom" {
vmxPath := state.Get("vmx_path").(string)
vmxData, err := readVMXConfig(vmxPath)
if err != nil {
return "", err
}
var device string
device, err = readCustomDeviceName(vmxData)
devices = append(devices, device)
if err != nil {
return "", err
}
log.Printf("HostAddress discovered custom device matching %s: %s", network, device)
}
var lastError error
for _, device := range devices {
// parse dhcpd configuration
pathDhcpConfig := d.DhcpConfPath(device)
if _, err := os.Stat(pathDhcpConfig); err != nil {
return "", fmt.Errorf("unable to find vmnetdhcp conf file: %s", pathDhcpConfig)
}
config, err := ReadDhcpConfig(pathDhcpConfig)
if err != nil {
lastError = err
continue
}
// find the entry configured in the dhcpd
interfaceConfig, err := config.HostByName(device)
if err != nil {
lastError = err
continue
}
// finally grab the hardware address
address, err := interfaceConfig.Hardware()
if err == nil {
return address.String(), nil
}
// we didn't find it, so search through our interfaces for the device name
interfaceList, err := net.Interfaces()
if err == nil {
return "", err
}
names := make([]string, 0)
for _, intf := range interfaceList {
if strings.HasSuffix(strings.ToLower(intf.Name), device) {
return intf.HardwareAddr.String(), nil
}
names = append(names, intf.Name)
}
}
return "", fmt.Errorf("unable to find host address from devices %v, last error: %s", devices, lastError)
}
func (d *VmwareDriver) HostIP(state multistep.StateBag) (string, error) {
// grab mapper for converting network<->device
netmap, err := d.NetworkMapper()
if err != nil {
return "", err
}
// convert network to name
network := state.Get("vmnetwork").(string)
devices, err := netmap.NameIntoDevices(network)
// log them to see what was detected
for _, device := range devices {
log.Printf("HostIP discovered device matching %s: %s", network, device)
}
// we were unable to find the device, maybe it's a custom one...
// so, check to see if it's in the .vmx configuration
if err != nil || network == "custom" {
vmxPath := state.Get("vmx_path").(string)
vmxData, err := readVMXConfig(vmxPath)
if err != nil {
return "", err
}
var device string
device, err = readCustomDeviceName(vmxData)
devices = append(devices, device)
if err != nil {
return "", err
}
log.Printf("HostIP discovered custom device matching %s: %s", network, device)
}
var lastError error
for _, device := range devices {
// parse dhcpd configuration
pathDhcpConfig := d.DhcpConfPath(device)
if _, err := os.Stat(pathDhcpConfig); err != nil {
return "", fmt.Errorf("unable to find vmnetdhcp conf file: %s", pathDhcpConfig)
}
config, err := ReadDhcpConfig(pathDhcpConfig)
if err != nil {
lastError = err
continue
}
// find the entry configured in the dhcpd
interfaceConfig, err := config.HostByName(device)
if err != nil {
lastError = err
continue
}
address, err := interfaceConfig.IP4()
if err != nil {
lastError = err
continue
}
return address.String(), nil
}
return "", fmt.Errorf("unable to find host IP from devices %v, last error: %s", devices, lastError)
}
func GetOVFTool() string {
ovftool := "ovftool"
if runtime.GOOS == "windows" {
ovftool = "ovftool.exe"
}
if _, err := exec.LookPath(ovftool); err != nil {
return ""
}
return ovftool
}
func (d *VmwareDriver) Export(args []string) error {
ovftool := GetOVFTool()
if ovftool == "" {
return fmt.Errorf("error finding ovftool in path")
}
cmd := exec.Command(ovftool, args...)
if _, _, err := runAndLog(cmd); err != nil {
return err
}
return nil
}
func (d *VmwareDriver) VerifyOvfTool(SkipExport, _ bool) error {
if SkipExport {
return nil
}
log.Printf("Verifying that ovftool exists...")
// Validate that tool exists, but no need to validate credentials.
ovftool := GetOVFTool()
if ovftool != "" {
return nil
} else {
return fmt.Errorf("ovftool not found in path. either set " +
"'skip_export = true', remove 'format' option, or install ovftool")
}
}