Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: consolidate fusion driver #208

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 49 additions & 10 deletions builder/vmware/common/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,48 @@ import (
)

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

// Architectures.
archAMD64 = "x86_x64"
archARM64 = "arm64"

// Operating systems.
osWindows = "windows"

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

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

// Application binary names.
appVdiskManager = "vmware-vdiskmanager"
appVmrun = "vmrun"
appVmx = "vmware-vmx"

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

// The product version.
var productVersion = regexp.MustCompile(productVersionRegex)

// The technical preview version.
var technicalPreview = regexp.MustCompile(technicalPreviewRegex)

// The VMware OVF Tool version.
var ovfToolVersion = regexp.MustCompile(ovfToolVersionRegex)

// 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,8 +152,7 @@ func NewDriver(dconfig *DriverConfig, config *SSHConfig, vmName string) (Driver,
switch runtime.GOOS {
case "darwin":
drivers = []Driver{
NewFusion6Driver(dconfig, config),
NewFusion5Driver(dconfig, config),
NewFusionDriver(dconfig, config),
}
case "linux":
fallthrough
Expand All @@ -141,12 +172,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)
Dismissed Show dismissed Hide dismissed
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)
Dismissed Show dismissed Hide dismissed
errs += "* " + err.Error() + "\n"
}

Expand Down Expand Up @@ -200,6 +231,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 +241,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 +260,13 @@ func compareVersions(versionFound string, versionWanted string, product string)
return nil
}

func compareVersionObjects(versionFound *version.Version, versionWanted *version.Version, product string) error {
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 +402,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 +490,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 @@ -667,7 +706,7 @@ func CheckOvfToolVersion(ovftoolPath string) error {
versionOutput := string(output)
log.Printf("Returned ovftool version: %s.", versionOutput)

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