builder/vmware: move driver out of ISO

This commit is contained in:
Mitchell Hashimoto 2013-12-24 11:31:57 -07:00
parent d73cbd3744
commit 458bfd186f
18 changed files with 60 additions and 67 deletions

View File

@ -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)),

View File

@ -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,
},

View File

@ -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:

View File

@ -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) {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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")

View File

@ -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

View File

@ -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)

View File

@ -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.

View File

@ -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)

View File

@ -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)

View File

@ -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)