Skip to content

Commit

Permalink
refactor: consolidate player driver
Browse files Browse the repository at this point in the history
Consolidates `Player5Driver` and `Player6Driver` to `PlayerDriver` within `driver_player.go`.

Signed-off-by: Ryan Johnson <ryan@tenthirtyam.org>
  • Loading branch information
tenthirtyam committed Aug 28, 2024
1 parent a50a401 commit 56e5c45
Show file tree
Hide file tree
Showing 7 changed files with 386 additions and 420 deletions.
80 changes: 69 additions & 11 deletions builder/vmware/common/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,57 @@ import (
)

const (
// OVF Tool.
ovfToolDownloadURL = "https://developer.broadcom.com/tools/open-virtualization-format-ovf-tool/latest"
ovfToolMinVersion = "4.6.0"

// Operating systems.
osLinux = "linux"

Check failure on line 31 in builder/vmware/common/driver.go

View workflow job for this annotation

GitHub Actions / Lint check

const `osLinux` is unused (unused)

// Clone types.
cloneTypeLinked = "linked"
cloneTypeFull = "full"

// GUI arguments.
guiArgumentNoGUI = "nogui"
guiArgumentGUI = "gui"

// Application binary names.
appPlayer = "vmplayer"
appVdiskManager = "vmware-vdiskmanager"
appVmrun = "vmrun"
appVmx = "vmware-vmx"
appQemuImg = "qemu-img"

// Version Regular Expressions.
productPreviewRegex = `(?i)VMware [a-z0-9-]+ e\.x\.p `
productVersionRegex = `(?i)VMware [a-z0-9-]+ (\d+\.\d+\.\d+)`
ovfToolVersionRegex = `\d+\.\d+\.\d+`

// File Names.
dhcpVmnetConfFile = "vmnetdhcp.conf"

Check failure on line 54 in builder/vmware/common/driver.go

View workflow job for this annotation

GitHub Actions / Lint check

const `dhcpVmnetConfFile` is unused (unused)
dhcpVmnetLeasesFile = "vmnetdhcp.leases"

Check failure on line 55 in builder/vmware/common/driver.go

View workflow job for this annotation

GitHub Actions / Lint check

const `dhcpVmnetLeasesFile` is unused (unused)
natVmnetConfFile = "vmnetnat.conf"

Check failure on line 56 in builder/vmware/common/driver.go

View workflow job for this annotation

GitHub Actions / Lint check

const `natVmnetConfFile` is unused (unused)
netmapConfFile = "netmap.conf"

Check failure on line 57 in builder/vmware/common/driver.go

View workflow job for this annotation

GitHub Actions / Lint check

const `netmapConfFile` is unused (unused)
)

var dhcpLeasesPaths = []string{
"dhcp/dhcp.leases",
"dhcp/dhcpd.leases",
"dhcpd/dhcp.leases",
"dhcpd/dhcpd.leases",
}

var dhcpConfPaths = []string{
"dhcp/dhcp.conf",
"dhcp/dhcpd.conf",
"dhcpd/dhcp.conf",
"dhcpd/dhcpd.conf",
}

// The minimum recommended version of the VMware OVF Tool.
var ovfToolMinRecommended = version.Must(version.NewVersion(ovfToolMinVersion))

// A regex to match the version of the VMware OVF Tool.
var ovfToolVersionRegex = regexp.MustCompile(`\d+\.\d+\.\d+`)

// 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
Expand Down Expand Up @@ -120,17 +161,16 @@ func NewDriver(dconfig *DriverConfig, config *SSHConfig, vmName string) (Driver,
switch runtime.GOOS {
case "darwin":
drivers = []Driver{
NewFusion6Driver(dconfig, config),
NewFusion5Driver(dconfig, config),
NewFusion6Driver(dconfig, config),
}
case "linux":
fallthrough
case "windows":
drivers = []Driver{
NewWorkstation10Driver(config),
NewWorkstation9Driver(config),
NewPlayer6Driver(config),
NewPlayer5Driver(config),
NewPlayerDriver(config),
}
default:
return nil, fmt.Errorf("error finding a driver for %s", runtime.GOOS)
Expand All @@ -141,12 +181,12 @@ func NewDriver(dconfig *DriverConfig, config *SSHConfig, vmName string) (Driver,
for _, driver := range drivers {
err := driver.Verify()

log.Printf("Testing against driver %T, Success: %t", driver, err == nil)
log.Printf("Using 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)
log.Printf("Skipping %T because it failed with the following error %s", driver, err)
errs += "* " + err.Error() + "\n"
}

Expand Down Expand Up @@ -200,6 +240,7 @@ func runAndLog(cmd *exec.Cmd) (string, string, error) {
return returnStdout, returnStderr, err
}

// Still used for Workstation and Player until conversion.
func normalizeVersion(version string) (string, error) {
i, err := strconv.Atoi(version)
if err != nil {
Expand All @@ -209,6 +250,7 @@ func normalizeVersion(version string) (string, error) {
return fmt.Sprintf("%02d", i), nil
}

// Still used for Workstation and Player until conversion.
func compareVersions(versionFound string, versionWanted string, product string) error {
found, err := normalizeVersion(versionFound)
if err != nil {
Expand All @@ -227,6 +269,13 @@ func compareVersions(versionFound string, versionWanted string, product string)
return nil
}

func compareVersionObjects(versionFound *version.Version, versionWanted *version.Version, product string) error {

Check failure on line 272 in builder/vmware/common/driver.go

View workflow job for this annotation

GitHub Actions / Lint check

func `compareVersionObjects` is unused (unused)
if versionFound.LessThan(versionWanted) {
return fmt.Errorf("requires %s or later, found %s", versionWanted.String(), versionFound.String())
}
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) {
Expand Down Expand Up @@ -362,7 +411,7 @@ func (d *VmwareDriver) PotentialGuestIP(state multistep.StateBag) ([]string, err
}

// iterate through all of the devices and collect all the dhcp lease entries
// that we possibly cacn.
// that we possibly can.
var available_lease_entries []dhcpLeaseEntry

for _, device := range devices {
Expand Down Expand Up @@ -450,7 +499,6 @@ func (d *VmwareDriver) PotentialGuestIP(state multistep.StateBag) ([]string, err
// 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"
Expand Down Expand Up @@ -644,6 +692,16 @@ func (d *VmwareDriver) HostIP(state multistep.StateBag) (string, error) {
return "", fmt.Errorf("unable to find host IP from devices %v, last error: %s", devices, lastError)
}

// GetDhcpLeasesPaths returns a copy of the DHCP leases paths.
func GetDhcpLeasesPaths() []string {
return append([]string(nil), dhcpLeasesPaths...)
}

// GetDhcpConfPaths returns a copy of the DHCP configuration paths.
func GetDhcpConfPaths() []string {
return append([]string(nil), dhcpConfPaths...)
}

func GetOvfTool() string {
ovftool := "ovftool"
if runtime.GOOS == "windows" {
Expand All @@ -667,7 +725,7 @@ func CheckOvfToolVersion(ovftoolPath string) error {
versionOutput := string(output)
log.Printf("Returned ovftool version: %s.", versionOutput)

versionString := ovfToolVersionRegex.FindString(versionOutput)
versionString := regexp.MustCompile(ovfToolVersionRegex).FindString(versionOutput)
if versionString == "" {
return errors.New("unable to determine the version of ovftool")
}
Expand Down
Loading

0 comments on commit 56e5c45

Please sign in to comment.