2013-12-24 13:00:51 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2013-12-25 17:50:12 -05:00
|
|
|
"bytes"
|
2017-02-27 16:34:53 -05:00
|
|
|
"errors"
|
2013-12-25 17:50:12 -05:00
|
|
|
"fmt"
|
2017-02-27 16:34:53 -05:00
|
|
|
"io/ioutil"
|
2013-12-25 17:50:12 -05:00
|
|
|
"log"
|
2017-02-27 16:34:53 -05:00
|
|
|
"net"
|
2015-11-11 07:39:06 -05:00
|
|
|
"os"
|
2013-12-25 17:50:12 -05:00
|
|
|
"os/exec"
|
2015-06-10 14:31:20 -04:00
|
|
|
"regexp"
|
2013-12-25 17:50:12 -05:00
|
|
|
"runtime"
|
2014-04-06 18:31:49 -04:00
|
|
|
"strconv"
|
2013-12-25 17:50:12 -05:00
|
|
|
"strings"
|
2015-11-11 07:39:06 -05:00
|
|
|
"time"
|
2013-12-25 17:50:12 -05:00
|
|
|
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2013-12-24 13:00:51 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// A driver is able to talk to VMware, control virtual machines, etc.
|
|
|
|
type Driver interface {
|
2013-12-26 16:39:41 -05:00
|
|
|
// 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.
|
2018-06-16 20:38:42 -04:00
|
|
|
Clone(dst string, src string, cloneType bool) error
|
2013-12-26 16:39:41 -05:00
|
|
|
|
2013-12-24 13:00:51 -05:00
|
|
|
// CompactDisk compacts a virtual disk.
|
|
|
|
CompactDisk(string) error
|
|
|
|
|
|
|
|
// CreateDisk creates a virtual disk with the given size.
|
2017-12-25 15:08:08 -05:00
|
|
|
CreateDisk(string, string, string, string) error
|
2013-12-24 13:00:51 -05:00
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2014-04-21 11:25:32 -04:00
|
|
|
// Attach the VMware tools ISO
|
|
|
|
ToolsInstall() error
|
|
|
|
|
2013-12-24 13:00:51 -05:00
|
|
|
// 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
|
2015-11-11 07:39:06 -05:00
|
|
|
// return an error. Otherwise, this returns an error. Each vmware
|
|
|
|
// driver should assign the VmwareMachine callback functions for locating
|
|
|
|
// paths within this function.
|
2013-12-24 13:00:51 -05:00
|
|
|
Verify() error
|
2015-11-11 07:39:06 -05:00
|
|
|
|
|
|
|
/// 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.
|
2017-04-13 18:39:55 -04:00
|
|
|
GetVmwareDriver() VmwareDriver
|
2015-11-11 07:39:06 -05:00
|
|
|
|
|
|
|
// Get the guest hw address for the vm
|
|
|
|
GuestAddress(multistep.StateBag) (string, error)
|
|
|
|
|
|
|
|
// Get the guest ip address for the vm
|
2020-05-28 16:36:45 -04:00
|
|
|
PotentialGuestIP(multistep.StateBag) ([]string, error)
|
2015-11-11 07:39:06 -05:00
|
|
|
|
|
|
|
// 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)
|
2020-08-25 15:18:38 -04:00
|
|
|
|
|
|
|
// Export the vm to ovf or ova format using ovftool
|
|
|
|
Export([]string) error
|
2020-08-26 18:45:59 -04:00
|
|
|
|
|
|
|
// OvfTool
|
|
|
|
VerifyOvfTool(bool, bool) error
|
2013-12-24 13:00:51 -05:00
|
|
|
}
|
2013-12-25 17:50:12 -05:00
|
|
|
|
|
|
|
// NewDriver returns a new driver implementation for this operating
|
|
|
|
// system, or an error if the driver couldn't be initialized.
|
2018-10-29 14:24:26 -04:00
|
|
|
func NewDriver(dconfig *DriverConfig, config *SSHConfig, vmName string) (Driver, error) {
|
2013-12-25 17:50:12 -05:00
|
|
|
drivers := []Driver{}
|
2018-10-25 17:17:35 -04:00
|
|
|
|
2017-01-23 08:58:39 -05:00
|
|
|
if dconfig.RemoteType != "" {
|
2020-09-11 10:46:33 -04:00
|
|
|
esx5Driver, err := NewESX5Driver(dconfig, config, vmName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-01-23 08:58:39 -05:00
|
|
|
}
|
2020-09-11 10:46:33 -04:00
|
|
|
drivers = []Driver{esx5Driver}
|
2017-01-23 08:58:39 -05:00
|
|
|
|
|
|
|
} else {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "darwin":
|
|
|
|
drivers = []Driver{
|
2020-09-17 04:38:33 -04:00
|
|
|
NewFusion6Driver(dconfig, config),
|
|
|
|
NewFusion5Driver(dconfig, config),
|
2017-01-23 08:58:39 -05:00
|
|
|
}
|
|
|
|
case "linux":
|
|
|
|
fallthrough
|
|
|
|
case "windows":
|
|
|
|
drivers = []Driver{
|
2020-09-17 04:38:33 -04:00
|
|
|
NewWorkstation10Driver(config),
|
|
|
|
NewWorkstation9Driver(config),
|
|
|
|
NewPlayer6Driver(config),
|
|
|
|
NewPlayer5Driver(config),
|
2017-01-23 08:58:39 -05:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("can't find driver for OS: %s", runtime.GOOS)
|
2013-12-25 17:50:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errs := ""
|
|
|
|
for _, driver := range drivers {
|
|
|
|
err := driver.Verify()
|
2018-01-26 19:12:43 -05:00
|
|
|
|
2020-04-15 04:34:48 -04:00
|
|
|
log.Printf("Testing against vmware driver %T, Success: %t", driver, err == nil)
|
2013-12-25 17:50:12 -05:00
|
|
|
if err == nil {
|
|
|
|
return driver, nil
|
|
|
|
}
|
2020-04-15 04:34:48 -04:00
|
|
|
|
|
|
|
log.Printf("skipping %T because it failed with the following error %s", driver, err)
|
2013-12-25 17:50:12 -05:00
|
|
|
errs += "* " + err.Error() + "\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Unable to initialize any driver for this platform. The errors\n"+
|
|
|
|
"from each driver are shown below. Please fix at least one driver\n"+
|
|
|
|
"to continue:\n%s", errs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func runAndLog(cmd *exec.Cmd) (string, string, error) {
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
|
2017-11-02 13:45:54 -04:00
|
|
|
log.Printf("Executing: %s %s", cmd.Path, strings.Join(cmd.Args[1:], " "))
|
2013-12-25 17:50:12 -05:00
|
|
|
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 {
|
2014-09-05 14:27:33 -04:00
|
|
|
message := stderrString
|
|
|
|
if message == "" {
|
|
|
|
message = stdoutString
|
|
|
|
}
|
|
|
|
|
|
|
|
err = fmt.Errorf("VMware error: %s", message)
|
2015-06-10 14:31:20 -04:00
|
|
|
|
|
|
|
// 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"+
|
2020-01-24 12:02:30 -05:00
|
|
|
"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.")
|
2015-06-10 14:31:20 -04:00
|
|
|
}
|
2013-12-25 17:50:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2014-04-06 18:31:49 -04:00
|
|
|
|
|
|
|
func normalizeVersion(version string) (string, error) {
|
|
|
|
i, err := strconv.Atoi(version)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf(
|
2014-08-22 04:07:36 -04:00
|
|
|
"VMware version '%s' is not numeric", version)
|
2014-04-06 18:31:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%02d", i), nil
|
|
|
|
}
|
|
|
|
|
2014-09-10 13:14:50 -04:00
|
|
|
func compareVersions(versionFound string, versionWanted string, product string) error {
|
2014-04-06 18:31:49 -04:00
|
|
|
found, err := normalizeVersion(versionFound)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
wanted, err := normalizeVersion(versionWanted)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if found < wanted {
|
|
|
|
return fmt.Errorf(
|
2014-09-10 13:14:50 -04:00
|
|
|
"VMware %s version %s, or greater, is required. Found version: %s", product, versionWanted, versionFound)
|
2014-04-06 18:31:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2016-05-31 15:39:38 -04:00
|
|
|
/// helper functions that read configuration information from a file
|
|
|
|
// read the network<->device configuration out of the specified path
|
2017-02-27 16:34:53 -05:00
|
|
|
func ReadNetmapConfig(path string) (NetworkMap, error) {
|
|
|
|
fd, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-11 07:39:06 -05:00
|
|
|
defer fd.Close()
|
|
|
|
return ReadNetworkMap(fd)
|
|
|
|
}
|
|
|
|
|
2016-05-31 15:39:38 -04:00
|
|
|
// read the dhcp configuration out of the specified path
|
2017-02-27 16:34:53 -05:00
|
|
|
func ReadDhcpConfig(path string) (DhcpConfiguration, error) {
|
|
|
|
fd, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-11 07:39:06 -05:00
|
|
|
defer fd.Close()
|
|
|
|
return ReadDhcpConfiguration(fd)
|
|
|
|
}
|
|
|
|
|
2016-05-31 15:39:38 -04:00
|
|
|
// read the VMX configuration from the specified path
|
2017-02-27 16:34:53 -05:00
|
|
|
func readVMXConfig(path string) (map[string]string, error) {
|
2016-05-31 15:39:38 -04:00
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return map[string]string{}, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
vmxBytes, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return map[string]string{}, err
|
|
|
|
}
|
|
|
|
return ParseVMX(string(vmxBytes)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// read the connection type out of a vmx configuration
|
2017-02-27 16:34:53 -05:00
|
|
|
func readCustomDeviceName(vmxData map[string]string) (string, error) {
|
2016-05-31 15:39:38 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-11-11 07:39:06 -05:00
|
|
|
// 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.
|
2017-02-27 16:34:53 -05:00
|
|
|
DhcpLeasesPath func(string) string
|
|
|
|
DhcpConfPath func(string) string
|
2016-04-05 21:45:58 -04:00
|
|
|
VmnetnatConfPath func(string) string
|
2017-10-23 21:26:16 -04:00
|
|
|
|
|
|
|
/// This method returns an object with the NetworkNameMapper interface
|
|
|
|
/// that maps network to device and vice-versa.
|
|
|
|
NetworkMapper func() (NetworkNameMapper, error)
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
func (d *VmwareDriver) GuestAddress(state multistep.StateBag) (string, error) {
|
2015-11-11 07:39:06 -05:00
|
|
|
vmxPath := state.Get("vmx_path").(string)
|
|
|
|
|
|
|
|
log.Println("Lookup up IP information...")
|
2016-05-31 15:39:38 -04:00
|
|
|
vmxData, err := readVMXConfig(vmxPath)
|
2017-02-27 16:34:53 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-11-11 07:39:06 -05:00
|
|
|
|
|
|
|
var ok bool
|
|
|
|
macAddress := ""
|
|
|
|
if macAddress, ok = vmxData["ethernet0.address"]; !ok || macAddress == "" {
|
|
|
|
if macAddress, ok = vmxData["ethernet0.generatedaddress"]; !ok || macAddress == "" {
|
|
|
|
return "", errors.New("couldn't find MAC address in VMX")
|
|
|
|
}
|
|
|
|
}
|
2018-03-30 19:13:02 -04:00
|
|
|
log.Printf("GuestAddress found MAC address in VMX: %s", macAddress)
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
res, err := net.ParseMAC(macAddress)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
return res.String(), nil
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
|
|
|
|
2020-05-28 16:36:45 -04:00
|
|
|
func (d *VmwareDriver) PotentialGuestIP(state multistep.StateBag) ([]string, error) {
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2017-10-23 21:26:16 -04:00
|
|
|
// grab network mapper
|
|
|
|
netmap, err := d.NetworkMapper()
|
2017-02-27 16:34:53 -05:00
|
|
|
if err != nil {
|
2020-05-28 16:36:45 -04:00
|
|
|
return []string{}, err
|
2017-02-27 16:34:53 -05:00
|
|
|
}
|
2015-11-11 07:39:06 -05:00
|
|
|
|
|
|
|
// convert the stashed network to a device
|
|
|
|
network := state.Get("vmnetwork").(string)
|
2018-03-07 04:59:55 -05:00
|
|
|
devices, err := netmap.NameIntoDevices(network)
|
2016-05-31 15:39:38 -04:00
|
|
|
|
2018-03-29 14:06:41 -04:00
|
|
|
// log them to see what was detected
|
|
|
|
for _, device := range devices {
|
2018-03-30 19:13:02 -04:00
|
|
|
log.Printf("GuestIP discovered device matching %s: %s", network, device)
|
2018-03-29 14:06:41 -04:00
|
|
|
}
|
|
|
|
|
2016-05-31 15:39:38 -04:00
|
|
|
// 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)
|
2017-02-27 16:34:53 -05:00
|
|
|
if err != nil {
|
2020-05-28 16:36:45 -04:00
|
|
|
return []string{}, err
|
2017-02-27 16:34:53 -05:00
|
|
|
}
|
2016-05-31 15:39:38 -04:00
|
|
|
|
2018-03-07 04:59:55 -05:00
|
|
|
var device string
|
2016-05-31 15:39:38 -04:00
|
|
|
device, err = readCustomDeviceName(vmxData)
|
2018-03-07 04:59:55 -05:00
|
|
|
devices = append(devices, device)
|
2017-02-27 16:34:53 -05:00
|
|
|
if err != nil {
|
2020-05-28 16:36:45 -04:00
|
|
|
return []string{}, err
|
2017-02-27 16:34:53 -05:00
|
|
|
}
|
2018-03-30 19:13:02 -04:00
|
|
|
log.Printf("GuestIP discovered custom device matching %s: %s", network, device)
|
2016-05-31 15:39:38 -04:00
|
|
|
}
|
2015-11-11 07:39:06 -05:00
|
|
|
|
|
|
|
// figure out our MAC address for looking up the guest address
|
2017-02-27 16:34:53 -05:00
|
|
|
MACAddress, err := d.GuestAddress(state)
|
|
|
|
if err != nil {
|
2020-05-28 16:36:45 -04:00
|
|
|
return []string{}, err
|
2017-02-27 16:34:53 -05:00
|
|
|
}
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2020-05-28 16:36:45 -04:00
|
|
|
// iterate through all of the devices and collect all the dhcp lease entries
|
|
|
|
// that we possibly cacn.
|
|
|
|
var available_lease_entries []dhcpLeaseEntry
|
|
|
|
|
2018-03-07 04:59:55 -05:00
|
|
|
for _, device := range devices {
|
|
|
|
// figure out the correct dhcp leases
|
|
|
|
dhcpLeasesPath := d.DhcpLeasesPath(device)
|
|
|
|
log.Printf("Trying DHCP leases path: %s", dhcpLeasesPath)
|
|
|
|
if dhcpLeasesPath == "" {
|
2020-05-28 16:36:45 -04:00
|
|
|
return []string{}, fmt.Errorf("no DHCP leases path found for device %s", device)
|
2018-03-07 04:59:55 -05:00
|
|
|
}
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2020-05-28 13:48:19 -04:00
|
|
|
// open up the path to the dhcpd leases
|
2018-03-07 04:59:55 -05:00
|
|
|
fh, err := os.Open(dhcpLeasesPath)
|
|
|
|
if err != nil {
|
2018-06-25 11:28:32 -04:00
|
|
|
log.Printf("Error while reading DHCP lease path file %s: %s", dhcpLeasesPath, err.Error())
|
2018-06-22 21:09:27 -04:00
|
|
|
continue
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
2018-03-07 04:59:55 -05:00
|
|
|
defer fh.Close()
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2020-05-28 13:48:19 -04:00
|
|
|
// and then read its contents
|
|
|
|
leaseEntries, err := ReadDhcpdLeaseEntries(fh)
|
2018-03-07 04:59:55 -05:00
|
|
|
if err != nil {
|
2020-05-28 16:36:45 -04:00
|
|
|
return []string{}, err
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
|
|
|
|
2020-05-28 13:48:19 -04:00
|
|
|
// 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)
|
|
|
|
|
2020-05-28 16:36:45 -04:00
|
|
|
// 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 {
|
2018-03-07 04:59:55 -05:00
|
|
|
|
2020-05-28 16:36:45 -04:00
|
|
|
// 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
|
|
|
|
}
|
2018-03-07 04:59:55 -05:00
|
|
|
|
2020-05-28 16:36:45 -04:00
|
|
|
// Next check for any where the hardware address matches.
|
|
|
|
if !bytes.Equal(hwaddr, entry.ether) {
|
|
|
|
continue
|
|
|
|
}
|
2018-03-07 04:59:55 -05:00
|
|
|
|
2020-05-28 16:36:45 -04:00
|
|
|
// This entry fits within our constraints, so store it so we can
|
|
|
|
// check it out later.
|
|
|
|
results = append(results, entry)
|
|
|
|
}
|
2018-03-07 04:59:55 -05:00
|
|
|
|
2020-05-28 16:36:45 -04:00
|
|
|
// 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 to a loose match for hw address %v", MACAddress)
|
|
|
|
for _, entry := range leaseEntries {
|
|
|
|
if bytes.Equal(hwaddr, entry.ether) {
|
|
|
|
results = append(results, entry)
|
|
|
|
}
|
2018-03-07 04:59:55 -05:00
|
|
|
}
|
|
|
|
}
|
2020-05-28 13:48:19 -04:00
|
|
|
|
2020-05-28 16:36:45 -04:00
|
|
|
// 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)
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
2020-05-28 16:36:45 -04:00
|
|
|
return addrs, nil
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
2018-03-07 04:59:55 -05:00
|
|
|
|
2020-12-13 17:26:53 -05:00
|
|
|
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.
|
2020-12-13 19:00:24 -05:00
|
|
|
if bytes.Equal(hwaddr, entry.hwAddress) {
|
2020-12-13 17:26:53 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 16:36:45 -04:00
|
|
|
return []string{}, fmt.Errorf("None of the found device(s) %v has a DHCP lease for MAC %s", devices, MACAddress)
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
func (d *VmwareDriver) HostAddress(state multistep.StateBag) (string, error) {
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2017-10-23 21:26:16 -04:00
|
|
|
// grab mapper for converting network<->device
|
|
|
|
netmap, err := d.NetworkMapper()
|
2017-02-27 16:34:53 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2016-04-05 21:45:58 -04:00
|
|
|
// convert network to name
|
|
|
|
network := state.Get("vmnetwork").(string)
|
2018-03-07 04:59:55 -05:00
|
|
|
devices, err := netmap.NameIntoDevices(network)
|
2016-05-31 15:39:38 -04:00
|
|
|
|
2018-03-29 14:06:41 -04:00
|
|
|
// log them to see what was detected
|
|
|
|
for _, device := range devices {
|
2018-03-30 19:13:02 -04:00
|
|
|
log.Printf("HostAddress discovered device matching %s: %s", network, device)
|
2018-03-29 14:06:41 -04:00
|
|
|
}
|
|
|
|
|
2016-05-31 15:39:38 -04:00
|
|
|
// 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)
|
2017-02-27 16:34:53 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-05-31 15:39:38 -04:00
|
|
|
|
2018-03-07 04:59:55 -05:00
|
|
|
var device string
|
2016-05-31 15:39:38 -04:00
|
|
|
device, err = readCustomDeviceName(vmxData)
|
2018-03-07 04:59:55 -05:00
|
|
|
devices = append(devices, device)
|
2017-02-27 16:34:53 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-03-30 19:13:02 -04:00
|
|
|
log.Printf("HostAddress discovered custom device matching %s: %s", network, device)
|
2016-05-31 15:39:38 -04:00
|
|
|
}
|
2016-04-05 21:45:58 -04:00
|
|
|
|
2018-03-07 04:59:55 -05:00
|
|
|
var lastError error
|
|
|
|
for _, device := range devices {
|
|
|
|
// parse dhcpd configuration
|
|
|
|
pathDhcpConfig := d.DhcpConfPath(device)
|
|
|
|
if _, err := os.Stat(pathDhcpConfig); err != nil {
|
|
|
|
return "", fmt.Errorf("Could not find vmnetdhcp conf file: %s", pathDhcpConfig)
|
|
|
|
}
|
2016-04-05 21:45:58 -04:00
|
|
|
|
2018-03-07 04:59:55 -05:00
|
|
|
config, err := ReadDhcpConfig(pathDhcpConfig)
|
|
|
|
if err != nil {
|
|
|
|
lastError = err
|
|
|
|
continue
|
|
|
|
}
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2018-03-07 04:59:55 -05:00
|
|
|
// find the entry configured in the dhcpd
|
|
|
|
interfaceConfig, err := config.HostByName(device)
|
|
|
|
if err != nil {
|
|
|
|
lastError = err
|
|
|
|
continue
|
|
|
|
}
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2018-03-07 04:59:55 -05:00
|
|
|
// finally grab the hardware address
|
|
|
|
address, err := interfaceConfig.Hardware()
|
|
|
|
if err == nil {
|
|
|
|
return address.String(), nil
|
|
|
|
}
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2018-03-07 04:59:55 -05:00
|
|
|
// we didn't find it, so search through our interfaces for the device name
|
|
|
|
interfaceList, err := net.Interfaces()
|
|
|
|
if err == nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2018-03-07 04:59:55 -05:00
|
|
|
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)
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
|
|
|
}
|
2018-03-07 04:59:55 -05:00
|
|
|
return "", fmt.Errorf("Unable to find host address from devices %v, last error: %s", devices, lastError)
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
func (d *VmwareDriver) HostIP(state multistep.StateBag) (string, error) {
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2017-10-23 21:26:16 -04:00
|
|
|
// grab mapper for converting network<->device
|
|
|
|
netmap, err := d.NetworkMapper()
|
2017-02-27 16:34:53 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2016-04-05 21:45:58 -04:00
|
|
|
// convert network to name
|
|
|
|
network := state.Get("vmnetwork").(string)
|
2018-03-07 04:59:55 -05:00
|
|
|
devices, err := netmap.NameIntoDevices(network)
|
2016-05-31 15:39:38 -04:00
|
|
|
|
2018-03-29 14:06:41 -04:00
|
|
|
// log them to see what was detected
|
|
|
|
for _, device := range devices {
|
2018-03-30 19:13:02 -04:00
|
|
|
log.Printf("HostIP discovered device matching %s: %s", network, device)
|
2018-03-29 14:06:41 -04:00
|
|
|
}
|
|
|
|
|
2016-05-31 15:39:38 -04:00
|
|
|
// 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)
|
2017-02-27 16:34:53 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-05-31 15:39:38 -04:00
|
|
|
|
2018-03-07 04:59:55 -05:00
|
|
|
var device string
|
2016-05-31 15:39:38 -04:00
|
|
|
device, err = readCustomDeviceName(vmxData)
|
2018-03-07 04:59:55 -05:00
|
|
|
devices = append(devices, device)
|
2017-02-27 16:34:53 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-03-30 19:13:02 -04:00
|
|
|
log.Printf("HostIP discovered custom device matching %s: %s", network, device)
|
2016-05-31 15:39:38 -04:00
|
|
|
}
|
2016-04-05 21:45:58 -04:00
|
|
|
|
2018-03-07 04:59:55 -05:00
|
|
|
var lastError error
|
|
|
|
for _, device := range devices {
|
|
|
|
// parse dhcpd configuration
|
|
|
|
pathDhcpConfig := d.DhcpConfPath(device)
|
|
|
|
if _, err := os.Stat(pathDhcpConfig); err != nil {
|
|
|
|
return "", fmt.Errorf("Could not find vmnetdhcp conf file: %s", pathDhcpConfig)
|
|
|
|
}
|
|
|
|
config, err := ReadDhcpConfig(pathDhcpConfig)
|
|
|
|
if err != nil {
|
|
|
|
lastError = err
|
|
|
|
continue
|
|
|
|
}
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2018-03-07 04:59:55 -05:00
|
|
|
// find the entry configured in the dhcpd
|
|
|
|
interfaceConfig, err := config.HostByName(device)
|
|
|
|
if err != nil {
|
|
|
|
lastError = err
|
|
|
|
continue
|
|
|
|
}
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2018-03-07 04:59:55 -05:00
|
|
|
address, err := interfaceConfig.IP4()
|
|
|
|
if err != nil {
|
|
|
|
lastError = err
|
|
|
|
continue
|
|
|
|
}
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2018-03-07 04:59:55 -05:00
|
|
|
return address.String(), nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("Unable to find host IP from devices %v, last error: %s", devices, lastError)
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
2020-08-25 15:18:38 -04:00
|
|
|
|
|
|
|
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: ovftool not found")
|
|
|
|
}
|
|
|
|
cmd := exec.Command(ovftool, args...)
|
|
|
|
if _, _, err := runAndLog(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-08-26 18:45:59 -04:00
|
|
|
|
|
|
|
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("Couldn't find ovftool in path! Please either " +
|
|
|
|
"set `skip_export = true` and remove the `format` option " +
|
|
|
|
"from your template, or make sure ovftool is installed on " +
|
|
|
|
"your build system. ")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|