From 458bfd186fd54d46818933897311965b2c4bc324 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 24 Dec 2013 11:31:57 -0700 Subject: [PATCH] builder/vmware: move driver out of ISO --- builder/vmware/common/ssh.go | 4 +- builder/vmware/iso/builder.go | 2 +- builder/vmware/iso/driver.go | 53 +++----------------- builder/vmware/iso/driver_esx5_test.go | 3 +- builder/vmware/iso/driver_fusion5.go | 9 +++- builder/vmware/iso/driver_player5.go | 9 +++- builder/vmware/iso/driver_workstation9.go | 9 +++- builder/vmware/iso/guest_ip.go | 4 +- builder/vmware/iso/remote_driver.go | 6 ++- builder/vmware/iso/step_compact_disk.go | 3 +- builder/vmware/iso/step_configure_vnc.go | 2 +- builder/vmware/iso/step_create_disk.go | 3 +- builder/vmware/iso/step_prepare_tools.go | 3 +- builder/vmware/iso/step_remote_upload.go | 3 +- builder/vmware/iso/step_run.go | 5 +- builder/vmware/iso/step_shutdown.go | 3 +- builder/vmware/iso/step_suppress_messages.go | 3 +- builder/vmware/iso/step_type_boot_command.go | 3 +- 18 files changed, 60 insertions(+), 67 deletions(-) diff --git a/builder/vmware/common/ssh.go b/builder/vmware/common/ssh.go index 7923dc376..7bf8bac72 100644 --- a/builder/vmware/common/ssh.go +++ b/builder/vmware/common/ssh.go @@ -12,7 +12,7 @@ import ( "github.com/mitchellh/packer/communicator/ssh" ) -func sshAddress(config *SSHConfig) func(multistep.StateBag) (string, error) { +func SSHAddressFunc(config *SSHConfig) func(multistep.StateBag) (string, error) { return func(state multistep.StateBag) (string, error) { driver := state.Get("driver").(Driver) vmxPath := state.Get("vmx_path").(string) @@ -61,7 +61,7 @@ func sshAddress(config *SSHConfig) func(multistep.StateBag) (string, error) { } } -func sshConfig(config *SSHConfig) func(multistep.StateBag) (*gossh.ClientConfig, error) { +func SSHConfigFunc(config *SSHConfig) func(multistep.StateBag) (*gossh.ClientConfig, error) { return func(state multistep.StateBag) (*gossh.ClientConfig, error) { auth := []gossh.ClientAuth{ gossh.ClientAuthPassword(ssh.Password(config.SSHPassword)), diff --git a/builder/vmware/iso/builder.go b/builder/vmware/iso/builder.go index 173a8fc7c..ea19bf24a 100644 --- a/builder/vmware/iso/builder.go +++ b/builder/vmware/iso/builder.go @@ -399,7 +399,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &stepTypeBootCommand{}, &common.StepConnectSSH{ SSHAddress: driver.SSHAddress, - SSHConfig: sshConfig, + SSHConfig: vmwcommon.SSHConfigFunc(&b.config.SSHConfig), SSHWaitTimeout: b.config.SSHWaitTimeout, NoPty: b.config.SSHSkipRequestPty, }, diff --git a/builder/vmware/iso/driver.go b/builder/vmware/iso/driver.go index 3e52af5a8..0fcc77fc6 100644 --- a/builder/vmware/iso/driver.go +++ b/builder/vmware/iso/driver.go @@ -3,58 +3,21 @@ package iso import ( "bytes" "fmt" - "github.com/mitchellh/multistep" "log" "os/exec" "runtime" "strings" + + vmwcommon "github.com/mitchellh/packer/builder/vmware/common" ) -// A driver is able to talk to VMware, control virtual machines, etc. -type Driver interface { - // CompactDisk compacts a virtual disk. - CompactDisk(string) error - - // CreateDisk creates a virtual disk with the given size. - CreateDisk(string, string, string) error - - // Checks if the VMX file at the given path is running. - IsRunning(string) (bool, error) - - // SSHAddress returns the SSH address for the VM that is being - // managed by this driver. - SSHAddress(multistep.StateBag) (string, 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 - - // Get the path to the DHCP leases file for the given device. - DhcpLeasesPath(string) string - - // 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 - // return an error. Otherwise, this returns an error. - Verify() error -} - // NewDriver returns a new driver implementation for this operating // system, or an error if the driver couldn't be initialized. -func NewDriver(config *config) (Driver, error) { - drivers := []Driver{} +func NewDriver(config *config) (vmwcommon.Driver, error) { + drivers := []vmwcommon.Driver{} if config.RemoteType != "" { - drivers = []Driver{ + drivers = []vmwcommon.Driver{ &ESX5Driver{ Host: config.RemoteHost, Port: config.RemotePort, @@ -66,18 +29,18 @@ func NewDriver(config *config) (Driver, error) { } else { switch runtime.GOOS { case "darwin": - drivers = []Driver{ + drivers = []vmwcommon.Driver{ &Fusion5Driver{ AppPath: "/Applications/VMware Fusion.app", }, } case "linux": - drivers = []Driver{ + drivers = []vmwcommon.Driver{ new(Workstation9Driver), new(Player5LinuxDriver), } case "windows": - drivers = []Driver{ + drivers = []vmwcommon.Driver{ new(Workstation9Driver), } default: diff --git a/builder/vmware/iso/driver_esx5_test.go b/builder/vmware/iso/driver_esx5_test.go index 577e68d71..c7e4f776d 100644 --- a/builder/vmware/iso/driver_esx5_test.go +++ b/builder/vmware/iso/driver_esx5_test.go @@ -1,11 +1,12 @@ package iso import ( + vmwcommon "github.com/mitchellh/packer/builder/vmware/common" "testing" ) func TestESX5Driver_implDriver(t *testing.T) { - var _ Driver = new(ESX5Driver) + var _ vmwcommon.Driver = new(ESX5Driver) } func TestESX5Driver_implRemoteDriver(t *testing.T) { diff --git a/builder/vmware/iso/driver_fusion5.go b/builder/vmware/iso/driver_fusion5.go index 5674acafe..9366c1755 100644 --- a/builder/vmware/iso/driver_fusion5.go +++ b/builder/vmware/iso/driver_fusion5.go @@ -2,18 +2,23 @@ package iso import ( "fmt" - "github.com/mitchellh/multistep" "io/ioutil" "os" "os/exec" "path/filepath" "strings" + + "github.com/mitchellh/multistep" + vmwcommon "github.com/mitchellh/packer/builder/vmware/common" ) // Fusion5Driver is a driver that can run VMWare Fusion 5. type Fusion5Driver struct { // This is the path to the "VMware Fusion.app" AppPath string + + // SSHConfig are the SSH settings for the Fusion VM + SSHConfig *vmwcommon.SSHConfig } func (d *Fusion5Driver) CompactDisk(diskPath string) error { @@ -61,7 +66,7 @@ func (d *Fusion5Driver) IsRunning(vmxPath string) (bool, error) { } func (d *Fusion5Driver) SSHAddress(state multistep.StateBag) (string, error) { - return sshAddress(state) + return vmwcommon.SSHAddressFunc(d.SSHConfig)(state) } func (d *Fusion5Driver) Start(vmxPath string, headless bool) error { diff --git a/builder/vmware/iso/driver_player5.go b/builder/vmware/iso/driver_player5.go index 56a8b0b06..a317d0c59 100644 --- a/builder/vmware/iso/driver_player5.go +++ b/builder/vmware/iso/driver_player5.go @@ -2,11 +2,13 @@ package iso import ( "fmt" - "github.com/mitchellh/multistep" "os" "os/exec" "path/filepath" "strings" + + "github.com/mitchellh/multistep" + vmwcommon "github.com/mitchellh/packer/builder/vmware/common" ) // Player5LinuxDriver is a driver that can run VMware Player 5 on Linux. @@ -15,6 +17,9 @@ type Player5LinuxDriver struct { VdiskManagerPath string QemuImgPath string VmrunPath string + + // SSHConfig are the SSH settings for the Fusion VM + SSHConfig *vmwcommon.SSHConfig } func (d *Player5LinuxDriver) CompactDisk(diskPath string) error { @@ -88,7 +93,7 @@ func (d *Player5LinuxDriver) IsRunning(vmxPath string) (bool, error) { } func (d *Player5LinuxDriver) SSHAddress(state multistep.StateBag) (string, error) { - return sshAddress(state) + return vmwcommon.SSHAddressFunc(d.SSHConfig)(state) } func (d *Player5LinuxDriver) Start(vmxPath string, headless bool) error { diff --git a/builder/vmware/iso/driver_workstation9.go b/builder/vmware/iso/driver_workstation9.go index 101572ada..e87c3b2e2 100644 --- a/builder/vmware/iso/driver_workstation9.go +++ b/builder/vmware/iso/driver_workstation9.go @@ -2,12 +2,14 @@ package iso import ( "fmt" - "github.com/mitchellh/multistep" "log" "os" "os/exec" "path/filepath" "strings" + + "github.com/mitchellh/multistep" + vmwcommon "github.com/mitchellh/packer/builder/vmware/common" ) // Workstation9Driver is a driver that can run VMware Workstation 9 @@ -16,6 +18,9 @@ type Workstation9Driver struct { AppPath string VdiskManagerPath string VmrunPath string + + // SSHConfig are the SSH settings for the Fusion VM + SSHConfig *vmwcommon.SSHConfig } func (d *Workstation9Driver) CompactDisk(diskPath string) error { @@ -63,7 +68,7 @@ func (d *Workstation9Driver) IsRunning(vmxPath string) (bool, error) { } func (d *Workstation9Driver) SSHAddress(state multistep.StateBag) (string, error) { - return sshAddress(state) + return vmwcommon.SSHAddressFunc(d.SSHConfig)(state) } func (d *Workstation9Driver) Start(vmxPath string, headless bool) error { diff --git a/builder/vmware/iso/guest_ip.go b/builder/vmware/iso/guest_ip.go index 2b9ca53fe..9d9713096 100644 --- a/builder/vmware/iso/guest_ip.go +++ b/builder/vmware/iso/guest_ip.go @@ -8,6 +8,8 @@ import ( "regexp" "strings" "time" + + vmwcommon "github.com/mitchellh/packer/builder/vmware/common" ) // Interface to help find the IP address of a running virtual machine. @@ -19,7 +21,7 @@ type GuestIPFinder interface { // lease information from the VMware network devices. type DHCPLeaseGuestLookup struct { // Driver that is being used (to find leases path) - Driver Driver + Driver vmwcommon.Driver // Device that the guest is connected to. Device string diff --git a/builder/vmware/iso/remote_driver.go b/builder/vmware/iso/remote_driver.go index 4a88ed1e6..d020cb800 100644 --- a/builder/vmware/iso/remote_driver.go +++ b/builder/vmware/iso/remote_driver.go @@ -1,7 +1,11 @@ package iso +import ( + vmwcommon "github.com/mitchellh/packer/builder/vmware/common" +) + type RemoteDriver interface { - Driver + vmwcommon.Driver // UploadISO uploads a local ISO to the remote side and returns the // new path that should be used in the VMX along with an error if it diff --git a/builder/vmware/iso/step_compact_disk.go b/builder/vmware/iso/step_compact_disk.go index 352ecf56e..8b4cf6593 100644 --- a/builder/vmware/iso/step_compact_disk.go +++ b/builder/vmware/iso/step_compact_disk.go @@ -3,6 +3,7 @@ package iso import ( "fmt" "github.com/mitchellh/multistep" + vmwcommon "github.com/mitchellh/packer/builder/vmware/common" "github.com/mitchellh/packer/packer" "log" ) @@ -22,7 +23,7 @@ type stepCompactDisk struct{} func (stepCompactDisk) Run(state multistep.StateBag) multistep.StepAction { config := state.Get("config").(*config) - driver := state.Get("driver").(Driver) + driver := state.Get("driver").(vmwcommon.Driver) ui := state.Get("ui").(packer.Ui) full_disk_path := state.Get("full_disk_path").(string) diff --git a/builder/vmware/iso/step_configure_vnc.go b/builder/vmware/iso/step_configure_vnc.go index 676439213..1f194be76 100644 --- a/builder/vmware/iso/step_configure_vnc.go +++ b/builder/vmware/iso/step_configure_vnc.go @@ -47,7 +47,7 @@ func (stepConfigureVNC) VNCAddress(portMin, portMax uint) (string, uint) { func (s *stepConfigureVNC) Run(state multistep.StateBag) multistep.StepAction { config := state.Get("config").(*config) - driver := state.Get("driver").(Driver) + driver := state.Get("driver").(vmwcommon.Driver) ui := state.Get("ui").(packer.Ui) vmxPath := state.Get("vmx_path").(string) diff --git a/builder/vmware/iso/step_create_disk.go b/builder/vmware/iso/step_create_disk.go index 8544313c3..cade63f75 100644 --- a/builder/vmware/iso/step_create_disk.go +++ b/builder/vmware/iso/step_create_disk.go @@ -3,6 +3,7 @@ package iso import ( "fmt" "github.com/mitchellh/multistep" + vmwcommon "github.com/mitchellh/packer/builder/vmware/common" "github.com/mitchellh/packer/packer" "path/filepath" ) @@ -20,7 +21,7 @@ type stepCreateDisk struct{} func (stepCreateDisk) Run(state multistep.StateBag) multistep.StepAction { config := state.Get("config").(*config) - driver := state.Get("driver").(Driver) + driver := state.Get("driver").(vmwcommon.Driver) ui := state.Get("ui").(packer.Ui) ui.Say("Creating virtual machine disk") diff --git a/builder/vmware/iso/step_prepare_tools.go b/builder/vmware/iso/step_prepare_tools.go index 3dc1e3482..69ff81690 100644 --- a/builder/vmware/iso/step_prepare_tools.go +++ b/builder/vmware/iso/step_prepare_tools.go @@ -3,6 +3,7 @@ package iso import ( "fmt" "github.com/mitchellh/multistep" + vmwcommon "github.com/mitchellh/packer/builder/vmware/common" "os" ) @@ -10,7 +11,7 @@ type stepPrepareTools struct{} func (*stepPrepareTools) Run(state multistep.StateBag) multistep.StepAction { config := state.Get("config").(*config) - driver := state.Get("driver").(Driver) + driver := state.Get("driver").(vmwcommon.Driver) if config.ToolsUploadFlavor == "" { return multistep.ActionContinue diff --git a/builder/vmware/iso/step_remote_upload.go b/builder/vmware/iso/step_remote_upload.go index 00aca6e09..717b3dc13 100644 --- a/builder/vmware/iso/step_remote_upload.go +++ b/builder/vmware/iso/step_remote_upload.go @@ -3,6 +3,7 @@ package iso import ( "fmt" "github.com/mitchellh/multistep" + vmwcommon "github.com/mitchellh/packer/builder/vmware/common" "github.com/mitchellh/packer/packer" "log" ) @@ -15,7 +16,7 @@ type stepRemoteUpload struct { } func (s *stepRemoteUpload) Run(state multistep.StateBag) multistep.StepAction { - driver := state.Get("driver").(Driver) + driver := state.Get("driver").(vmwcommon.Driver) ui := state.Get("ui").(packer.Ui) remote, ok := driver.(RemoteDriver) diff --git a/builder/vmware/iso/step_run.go b/builder/vmware/iso/step_run.go index 66ed76377..835ff06ab 100644 --- a/builder/vmware/iso/step_run.go +++ b/builder/vmware/iso/step_run.go @@ -3,6 +3,7 @@ package iso import ( "fmt" "github.com/mitchellh/multistep" + vmwcommon "github.com/mitchellh/packer/builder/vmware/common" "github.com/mitchellh/packer/packer" "time" ) @@ -26,7 +27,7 @@ type stepRun struct { func (s *stepRun) Run(state multistep.StateBag) multistep.StepAction { config := state.Get("config").(*config) - driver := state.Get("driver").(Driver) + driver := state.Get("driver").(vmwcommon.Driver) ui := state.Get("ui").(packer.Ui) vmxPath := state.Get("vmx_path").(string) vncIp := state.Get("vnc_ip").(string) @@ -84,7 +85,7 @@ func (s *stepRun) Run(state multistep.StateBag) multistep.StepAction { } func (s *stepRun) Cleanup(state multistep.StateBag) { - driver := state.Get("driver").(Driver) + driver := state.Get("driver").(vmwcommon.Driver) ui := state.Get("ui").(packer.Ui) // If we started the machine... stop it. diff --git a/builder/vmware/iso/step_shutdown.go b/builder/vmware/iso/step_shutdown.go index 67c6a29f4..fea2f129a 100644 --- a/builder/vmware/iso/step_shutdown.go +++ b/builder/vmware/iso/step_shutdown.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "github.com/mitchellh/multistep" + vmwcommon "github.com/mitchellh/packer/builder/vmware/common" "github.com/mitchellh/packer/packer" "log" "path/filepath" @@ -30,7 +31,7 @@ type stepShutdown struct{} func (s *stepShutdown) Run(state multistep.StateBag) multistep.StepAction { comm := state.Get("communicator").(packer.Communicator) config := state.Get("config").(*config) - driver := state.Get("driver").(Driver) + driver := state.Get("driver").(vmwcommon.Driver) ui := state.Get("ui").(packer.Ui) vmxPath := state.Get("vmx_path").(string) diff --git a/builder/vmware/iso/step_suppress_messages.go b/builder/vmware/iso/step_suppress_messages.go index a936a5e6e..e2044fdcd 100644 --- a/builder/vmware/iso/step_suppress_messages.go +++ b/builder/vmware/iso/step_suppress_messages.go @@ -3,6 +3,7 @@ package iso import ( "fmt" "github.com/mitchellh/multistep" + vmwcommon "github.com/mitchellh/packer/builder/vmware/common" "github.com/mitchellh/packer/packer" "log" ) @@ -11,7 +12,7 @@ import ( type stepSuppressMessages struct{} func (s *stepSuppressMessages) Run(state multistep.StateBag) multistep.StepAction { - driver := state.Get("driver").(Driver) + driver := state.Get("driver").(vmwcommon.Driver) ui := state.Get("ui").(packer.Ui) vmxPath := state.Get("vmx_path").(string) diff --git a/builder/vmware/iso/step_type_boot_command.go b/builder/vmware/iso/step_type_boot_command.go index c72beecb7..89e7ac432 100644 --- a/builder/vmware/iso/step_type_boot_command.go +++ b/builder/vmware/iso/step_type_boot_command.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/mitchellh/go-vnc" "github.com/mitchellh/multistep" + vmwcommon "github.com/mitchellh/packer/builder/vmware/common" "github.com/mitchellh/packer/packer" "log" "net" @@ -36,7 +37,7 @@ type stepTypeBootCommand struct{} func (s *stepTypeBootCommand) Run(state multistep.StateBag) multistep.StepAction { config := state.Get("config").(*config) - driver := state.Get("driver").(Driver) + driver := state.Get("driver").(vmwcommon.Driver) httpPort := state.Get("http_port").(uint) ui := state.Get("ui").(packer.Ui) vncIp := state.Get("vnc_ip").(string)