Skip to content

Commit

Permalink
refactor: consolidate workstation driver
Browse files Browse the repository at this point in the history
- Consolidates `Workstation9Driver` and `Workstation10Driver` to `WorkstationrDriver` within `driver_workstation.go`.
- Addresses  the deprecation of `syscall.StringToUTF16Ptr` with `windows.UTF16PtrFromString` in `readRegString`.

Signed-off-by: Ryan Johnson <ryan@tenthirtyam.org>
  • Loading branch information
tenthirtyam committed Sep 6, 2024
1 parent 6d0b1ec commit 789045f
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 210 deletions.
3 changes: 1 addition & 2 deletions builder/vmware/common/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ func NewDriver(dconfig *DriverConfig, config *SSHConfig, vmName string) (Driver,
fallthrough
case "windows":
drivers = []Driver{
NewWorkstation10Driver(config),
NewWorkstation9Driver(config),
NewWorkstationDriver(config),
NewPlayer6Driver(config),
NewPlayer5Driver(config),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,45 @@ type NetmapConfig struct {
Device string
}

// Workstation9Driver is a driver that can run VMware Workstation 9
type Workstation9Driver struct {
// WorkstationDriver is a driver for VMware Workstation.
type WorkstationDriver struct {
VmwareDriver

AppPath string
VdiskManagerPath string
VmrunPath string

// SSHConfig are the SSH settings for the Fusion VM
SSHConfig *SSHConfig
}

func NewWorkstation9Driver(config *SSHConfig) Driver {
return &Workstation9Driver{
func NewWorkstationDriver(config *SSHConfig) Driver {
return &WorkstationDriver{
SSHConfig: config,
}
}

func (d *Workstation9Driver) Clone(dst, src string, linked bool, snapshot string) error {
return errors.New("linked clones are not supported on this version")
func (d *WorkstationDriver) Clone(dst, src string, linked bool, snapshot string) error {

var cloneType string
if linked {
cloneType = "linked"
} else {
cloneType = "full"
}

args := []string{"-T", "ws", "clone", src, dst, cloneType}
if snapshot != "" {
args = append(args, "-snapshot", snapshot)
}
cmd := exec.Command(d.VmrunPath, args...)
if _, _, err := runAndLog(cmd); err != nil {
return err
}

return nil
}

func (d *Workstation9Driver) CompactDisk(diskPath string) error {
func (d *WorkstationDriver) CompactDisk(diskPath string) error {
defragCmd := exec.Command(d.VdiskManagerPath, "-d", diskPath)
if _, _, err := runAndLog(defragCmd); err != nil {
return err
Expand All @@ -88,7 +104,7 @@ func (d *Workstation9Driver) CompactDisk(diskPath string) error {
return nil
}

func (d *Workstation9Driver) CreateDisk(output string, size string, adapter_type string, type_id string) error {
func (d *WorkstationDriver) CreateDisk(output string, size string, adapter_type string, type_id string) error {
cmd := exec.Command(d.VdiskManagerPath, "-c", "-s", size, "-a", adapter_type, "-t", type_id, output)
if _, _, err := runAndLog(cmd); err != nil {
return err
Expand All @@ -97,13 +113,13 @@ func (d *Workstation9Driver) CreateDisk(output string, size string, adapter_type
return nil
}

func (d *Workstation9Driver) CreateSnapshot(vmxPath string, snapshotName string) error {
func (d *WorkstationDriver) CreateSnapshot(vmxPath string, snapshotName string) error {
cmd := exec.Command(d.VmrunPath, "-T", "ws", "snapshot", vmxPath, snapshotName)
_, _, err := runAndLog(cmd)
return err
}

func (d *Workstation9Driver) IsRunning(vmxPath string) (bool, error) {
func (d *WorkstationDriver) IsRunning(vmxPath string) (bool, error) {
vmxPath, err := filepath.Abs(vmxPath)
if err != nil {
return false, err
Expand All @@ -124,11 +140,11 @@ func (d *Workstation9Driver) IsRunning(vmxPath string) (bool, error) {
return false, nil
}

func (d *Workstation9Driver) CommHost(state multistep.StateBag) (string, error) {
func (d *WorkstationDriver) CommHost(state multistep.StateBag) (string, error) {
return CommHost(d.SSHConfig)(state)
}

func (d *Workstation9Driver) Start(vmxPath string, headless bool) error {
func (d *WorkstationDriver) Start(vmxPath string, headless bool) error {
guiArgument := "gui"
if headless {
guiArgument = "nogui"
Expand All @@ -142,7 +158,7 @@ func (d *Workstation9Driver) Start(vmxPath string, headless bool) error {
return nil
}

func (d *Workstation9Driver) Stop(vmxPath string) error {
func (d *WorkstationDriver) Stop(vmxPath string) error {
cmd := exec.Command(d.VmrunPath, "-T", "ws", "stop", vmxPath, "hard")
if _, _, err := runAndLog(cmd); err != nil {
return err
Expand All @@ -151,11 +167,11 @@ func (d *Workstation9Driver) Stop(vmxPath string) error {
return nil
}

func (d *Workstation9Driver) SuppressMessages(vmxPath string) error {
func (d *WorkstationDriver) SuppressMessages(vmxPath string) error {
return nil
}

func (d *Workstation9Driver) Verify() error {
func (d *WorkstationDriver) Verify() error {
var err error
if d.AppPath == "" {
if d.AppPath, err = workstationFindVMware(); err != nil {
Expand Down Expand Up @@ -242,15 +258,15 @@ func (d *Workstation9Driver) Verify() error {
return nil
}

func (d *Workstation9Driver) ToolsIsoPath(flavor string) string {
func (d *WorkstationDriver) ToolsIsoPath(flavor string) string {
return workstationToolsIsoPath(flavor)
}

func (d *Workstation9Driver) ToolsInstall() error {
func (d *WorkstationDriver) ToolsInstall() error {
return nil
}

func (d *Workstation9Driver) GetVmwareDriver() VmwareDriver {
func (d *WorkstationDriver) GetVmwareDriver() VmwareDriver {
return d.VmwareDriver
}

Expand Down
58 changes: 0 additions & 58 deletions builder/vmware/common/driver_workstation10.go

This file was deleted.

38 changes: 0 additions & 38 deletions builder/vmware/common/driver_workstation10_windows.go

This file was deleted.

8 changes: 4 additions & 4 deletions builder/vmware/common/driver_workstation_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

//go:build !windows

// These functions are compatible with WS 9 and 10 on *NIX
// VMware Workstation on Linux

package common

import (
Expand Down Expand Up @@ -127,7 +128,6 @@ func workstationVerifyVersion(version string) error {
return fmt.Errorf("driver is only supported on Linux or Windows, not %s", runtime.GOOS)
}

//TODO(pmyjavec) there is a better way to find this, how?
//the default will suffice for now.
vmxpath := "/usr/lib/vmware/bin/vmware-vmx"

Expand All @@ -144,9 +144,9 @@ func workstationTestVersion(wanted, versionOutput string) error {
versionRe := regexp.MustCompile(`(?i)VMware Workstation (\d+)\.`)
matches := versionRe.FindStringSubmatch(versionOutput)
if matches == nil {
return fmt.Errorf("error parsing version output: %s", wanted)
return fmt.Errorf("error parsing version from output: %s", wanted)
}
log.Printf("Detected VMware Workstation version: %s", matches[1])
log.Printf("VMware Workstation: %s", matches[1])

return compareVersions(matches[1], wanted, "Workstation")
}
18 changes: 0 additions & 18 deletions builder/vmware/common/driver_workstation_unix_test.go

This file was deleted.

Loading

0 comments on commit 789045f

Please sign in to comment.