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 9, 2024
1 parent 3059905 commit 82753c2
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 151 deletions.
3 changes: 1 addition & 2 deletions builder/vmware/common/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ func NewDriver(dconfig *DriverConfig, config *SSHConfig, vmName string) (Driver,
drivers = []Driver{
NewWorkstation10Driver(config),
NewWorkstation9Driver(config),
NewPlayer6Driver(config),
NewPlayer5Driver(config),
NewPlayerDriver(config),
}
default:
return nil, fmt.Errorf("can't find driver for OS: %s", runtime.GOOS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package common

import (
"errors"
"fmt"
"log"
"os"
Expand All @@ -15,30 +14,46 @@ import (
"github.com/hashicorp/packer-plugin-sdk/multistep"
)

// Player5Driver is a driver that can run VMware Player 5 on Linux.
type Player5Driver struct {
// PlayerDriver is a driver for VMware Workstation Player.
type PlayerDriver struct {
VmwareDriver

AppPath string
VdiskManagerPath string
QemuImgPath string
VmrunPath string

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

func NewPlayer5Driver(config *SSHConfig) Driver {
return &Player5Driver{
func NewPlayerDriver(config *SSHConfig) Driver {
return &PlayerDriver{
SSHConfig: config,
}
}

func (d *Player5Driver) Clone(dst, src string, linked bool, snapshot string) error {
return errors.New("linked clones are not supported on this version of VMware Player, please upgrade")
func (d *PlayerDriver) 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 *Player5Driver) CompactDisk(diskPath string) error {
func (d *PlayerDriver) CompactDisk(diskPath string) error {
if d.QemuImgPath != "" {
return d.qemuCompactDisk(diskPath)
}
Expand All @@ -56,7 +71,7 @@ func (d *Player5Driver) CompactDisk(diskPath string) error {
return nil
}

func (d *Player5Driver) qemuCompactDisk(diskPath string) error {
func (d *PlayerDriver) qemuCompactDisk(diskPath string) error {
cmd := exec.Command(d.QemuImgPath, "convert", "-f", "vmdk", "-O", "vmdk", "-o", "compat6", diskPath, diskPath+".new")
if _, _, err := runAndLog(cmd); err != nil {
return err
Expand All @@ -73,7 +88,7 @@ func (d *Player5Driver) qemuCompactDisk(diskPath string) error {
return nil
}

func (d *Player5Driver) CreateDisk(output string, size string, adapter_type string, type_id string) error {
func (d *PlayerDriver) CreateDisk(output string, size string, adapter_type string, type_id string) error {
var cmd *exec.Cmd
if d.QemuImgPath != "" {
cmd = exec.Command(d.QemuImgPath, "create", "-f", "vmdk", "-o", "compat6", output, size)
Expand All @@ -87,13 +102,13 @@ func (d *Player5Driver) CreateDisk(output string, size string, adapter_type stri
return nil
}

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

func (d *Player5Driver) IsRunning(vmxPath string) (bool, error) {
func (d *PlayerDriver) IsRunning(vmxPath string) (bool, error) {
vmxPath, err := filepath.Abs(vmxPath)
if err != nil {
return false, err
Expand All @@ -114,11 +129,11 @@ func (d *Player5Driver) IsRunning(vmxPath string) (bool, error) {
return false, nil
}

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

func (d *Player5Driver) Start(vmxPath string, headless bool) error {
func (d *PlayerDriver) Start(vmxPath string, headless bool) error {
guiArgument := "gui"
if headless {
guiArgument = "nogui"
Expand All @@ -132,7 +147,7 @@ func (d *Player5Driver) Start(vmxPath string, headless bool) error {
return nil
}

func (d *Player5Driver) Stop(vmxPath string) error {
func (d *PlayerDriver) Stop(vmxPath string) error {
cmd := exec.Command(d.VmrunPath, "-T", "player", "stop", vmxPath, "hard")
if _, _, err := runAndLog(cmd); err != nil {
return err
Expand All @@ -141,11 +156,11 @@ func (d *Player5Driver) Stop(vmxPath string) error {
return nil
}

func (d *Player5Driver) SuppressMessages(vmxPath string) error {
func (d *PlayerDriver) SuppressMessages(vmxPath string) error {
return nil
}

func (d *Player5Driver) Verify() error {
func (d *PlayerDriver) Verify() error {
var err error
if d.AppPath == "" {
if d.AppPath, err = playerFindVMware(); err != nil {
Expand Down Expand Up @@ -239,14 +254,14 @@ func (d *Player5Driver) Verify() error {
return nil
}

func (d *Player5Driver) ToolsIsoPath(flavor string) string {
func (d *PlayerDriver) ToolsIsoPath(flavor string) string {
return playerToolsIsoPath(flavor)
}

func (d *Player5Driver) ToolsInstall() error {
func (d *PlayerDriver) ToolsInstall() error {
return nil
}

func (d *Player5Driver) GetVmwareDriver() VmwareDriver {
func (d *PlayerDriver) GetVmwareDriver() VmwareDriver {
return d.VmwareDriver
}
59 changes: 0 additions & 59 deletions builder/vmware/common/driver_player6.go

This file was deleted.

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

This file was deleted.

44 changes: 22 additions & 22 deletions builder/vmware/common/driver_player_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

//go:build !windows

// These functions are compatible with WS 9 and 10 on *NIX
package common

import (
Expand All @@ -17,6 +16,8 @@ import (
"runtime"
)

// VMware Workstation Player for Linux.

func playerFindVdiskManager() (string, error) {
return exec.LookPath("vmware-vdiskmanager")
}
Expand All @@ -37,25 +38,25 @@ func playerToolsIsoPath(flavor string) string {
return "/usr/lib/vmware/isoimages/" + flavor + ".iso"
}

// return the base path to vmware's config on the host
// Return the base path to configuration files.
func playerVMwareRoot() (s string, err error) {
return "/etc/vmware", nil
}

func playerDhcpLeasesPath(device string) string {
base, err := playerVMwareRoot()
if err != nil {
log.Printf("Error finding VMware root: %s", err)
log.Printf("Error finding configuration root path: %s", err)
return ""
}

// Build the base path to VMware configuration for specified device: `/etc/vmware/${device}`
// Build the base path to configuration for specified device:
// `/etc/vmware/${device}`
devicebase := filepath.Join(base, device)

// Walk through a list of paths searching for the correct permutation...
// ...as it appears that in >= WS14 and < WS14, the leases file may be labelled differently.

// Docs say we should expect: dhcpd/dhcpd.leases
// Iterate through a list of paths searching for the correct permutation.
// The default is dhcpd.leases, per the product documentation, but this
// will check for a few variations.
paths := []string{"dhcpd/dhcpd.leases", "dhcpd/dhcp.leases", "dhcp/dhcpd.leases", "dhcp/dhcp.leases"}
for _, p := range paths {
fp := filepath.Join(devicebase, p)
Expand All @@ -64,24 +65,24 @@ func playerDhcpLeasesPath(device string) string {
}
}

log.Printf("Error finding VMware DHCP Server Leases (dhcpd.leases) under device path: %s", devicebase)
log.Printf("Error finding 'dhcpd.leases' in device path: %s", devicebase)
return ""
}

func playerVmDhcpConfPath(device string) string {
base, err := playerVMwareRoot()
if err != nil {
log.Printf("Error finding VMware root: %s", err)
log.Printf("Error finding the configuration root path: %s", err)
return ""
}

// Build the base path to VMware configuration for specified device: `/etc/vmware/${device}`
// Build the base path to configuration for specified device:
// `/etc/vmware/${device}`
devicebase := filepath.Join(base, device)

// Walk through a list of paths searching for the correct permutation...
// ...as it appears that in >= WS14 and < WS14, the dhcp config may be labelled differently.

// Docs say we should expect: dhcp/dhcp.conf
// Iterate through a list of paths searching for the correct permutation.
// The default is dhcp/dhcp.conf, per the product documentation, but this
// will check for a few variations.
paths := []string{"dhcp/dhcp.conf", "dhcp/dhcpd.conf", "dhcpd/dhcp.conf", "dhcpd/dhcpd.conf"}
for _, p := range paths {
fp := filepath.Join(devicebase, p)
Expand All @@ -90,14 +91,14 @@ func playerVmDhcpConfPath(device string) string {
}
}

log.Printf("Error finding VMware DHCP Server Configuration (dhcp.conf) under device path: %s", devicebase)
log.Printf("Error finding 'dhcp.conf' in device path: %s", devicebase)
return ""
}

func playerVmnetnatConfPath(device string) string {
base, err := playerVMwareRoot()
if err != nil {
log.Printf("Error finding VMware root: %s", err)
log.Printf("Error finding the configuration root path: %s", err)
return ""
}
return filepath.Join(base, device, "nat/nat.conf")
Expand All @@ -106,18 +107,17 @@ func playerVmnetnatConfPath(device string) string {
func playerNetmapConfPath() string {
base, err := playerVMwareRoot()
if err != nil {
log.Printf("Error finding VMware root: %s", err)
log.Printf("Error finding the configuration root path: %s", err)
return ""
}
return filepath.Join(base, "netmap.conf")
}

func playerVerifyVersion(version string) error {
if runtime.GOOS != "linux" {
return fmt.Errorf("driver is only supported on Linux and Windows, not %s", runtime.GOOS)
return fmt.Errorf("driver is only supported on linux and windows, not %s", runtime.GOOS)
}

//TODO: Is there is a better way to find this?
// Using the default.
vmxpath := "/usr/lib/vmware/bin/vmware-vmx"

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

return compareVersions(matches[1], version, "Player")
}
Loading

0 comments on commit 82753c2

Please sign in to comment.