Commit 16911d75 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/vmware: move drivers out to common

parent bee87940
package common package common
import ( import (
"bytes"
"fmt"
"log"
"os/exec"
"runtime"
"strings"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
) )
...@@ -41,3 +48,76 @@ type Driver interface { ...@@ -41,3 +48,76 @@ type Driver interface {
// return an error. Otherwise, this returns an error. // return an error. Otherwise, this returns an error.
Verify() 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 *SSHConfig) (Driver, error) {
drivers := []Driver{}
switch runtime.GOOS {
case "darwin":
drivers = []Driver{
&Fusion5Driver{
AppPath: "/Applications/VMware Fusion.app",
SSHConfig: config,
},
}
case "linux":
drivers = []Driver{
&Workstation9Driver{
SSHConfig: config,
},
&Player5LinuxDriver{
SSHConfig: config,
},
}
case "windows":
drivers = []Driver{
&Workstation9Driver{
SSHConfig: config,
},
}
default:
return nil, fmt.Errorf("can't find driver for OS: %s", runtime.GOOS)
}
errs := ""
for _, driver := range drivers {
err := driver.Verify()
if err == nil {
return driver, nil
}
errs += "* " + err.Error() + "\n"
}
return nil, fmt.Errorf(
"Unable to initialize any driver for this platform. The errors\n"+
"from each driver are shown below. Please fix at least one driver\n"+
"to continue:\n%s", errs)
}
func runAndLog(cmd *exec.Cmd) (string, string, error) {
var stdout, stderr bytes.Buffer
log.Printf("Executing: %s %v", cmd.Path, cmd.Args[1:])
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
stdoutString := strings.TrimSpace(stdout.String())
stderrString := strings.TrimSpace(stderr.String())
if _, ok := err.(*exec.ExitError); ok {
err = fmt.Errorf("VMware error: %s", stderrString)
}
log.Printf("stdout: %s", stdoutString)
log.Printf("stderr: %s", stderrString)
// Replace these for Windows, we only want to deal with Unix
// style line endings.
returnStdout := strings.Replace(stdout.String(), "\r\n", "\n", -1)
returnStderr := strings.Replace(stderr.String(), "\r\n", "\n", -1)
return returnStdout, returnStderr, err
}
package iso package common
import ( import (
"fmt" "fmt"
...@@ -9,7 +9,6 @@ import ( ...@@ -9,7 +9,6 @@ import (
"strings" "strings"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
vmwcommon "github.com/mitchellh/packer/builder/vmware/common"
) )
// Fusion5Driver is a driver that can run VMWare Fusion 5. // Fusion5Driver is a driver that can run VMWare Fusion 5.
...@@ -18,7 +17,7 @@ type Fusion5Driver struct { ...@@ -18,7 +17,7 @@ type Fusion5Driver struct {
AppPath string AppPath string
// SSHConfig are the SSH settings for the Fusion VM // SSHConfig are the SSH settings for the Fusion VM
SSHConfig *vmwcommon.SSHConfig SSHConfig *SSHConfig
} }
func (d *Fusion5Driver) CompactDisk(diskPath string) error { func (d *Fusion5Driver) CompactDisk(diskPath string) error {
...@@ -66,7 +65,7 @@ func (d *Fusion5Driver) IsRunning(vmxPath string) (bool, error) { ...@@ -66,7 +65,7 @@ func (d *Fusion5Driver) IsRunning(vmxPath string) (bool, error) {
} }
func (d *Fusion5Driver) SSHAddress(state multistep.StateBag) (string, error) { func (d *Fusion5Driver) SSHAddress(state multistep.StateBag) (string, error) {
return vmwcommon.SSHAddressFunc(d.SSHConfig)(state) return SSHAddressFunc(d.SSHConfig)(state)
} }
func (d *Fusion5Driver) Start(vmxPath string, headless bool) error { func (d *Fusion5Driver) Start(vmxPath string, headless bool) error {
......
package iso package common
import ( import (
"fmt" "fmt"
...@@ -8,7 +8,6 @@ import ( ...@@ -8,7 +8,6 @@ import (
"strings" "strings"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
vmwcommon "github.com/mitchellh/packer/builder/vmware/common"
) )
// Player5LinuxDriver is a driver that can run VMware Player 5 on Linux. // Player5LinuxDriver is a driver that can run VMware Player 5 on Linux.
...@@ -19,7 +18,7 @@ type Player5LinuxDriver struct { ...@@ -19,7 +18,7 @@ type Player5LinuxDriver struct {
VmrunPath string VmrunPath string
// SSHConfig are the SSH settings for the Fusion VM // SSHConfig are the SSH settings for the Fusion VM
SSHConfig *vmwcommon.SSHConfig SSHConfig *SSHConfig
} }
func (d *Player5LinuxDriver) CompactDisk(diskPath string) error { func (d *Player5LinuxDriver) CompactDisk(diskPath string) error {
...@@ -93,7 +92,7 @@ func (d *Player5LinuxDriver) IsRunning(vmxPath string) (bool, error) { ...@@ -93,7 +92,7 @@ func (d *Player5LinuxDriver) IsRunning(vmxPath string) (bool, error) {
} }
func (d *Player5LinuxDriver) SSHAddress(state multistep.StateBag) (string, error) { func (d *Player5LinuxDriver) SSHAddress(state multistep.StateBag) (string, error) {
return vmwcommon.SSHAddressFunc(d.SSHConfig)(state) return SSHAddressFunc(d.SSHConfig)(state)
} }
func (d *Player5LinuxDriver) Start(vmxPath string, headless bool) error { func (d *Player5LinuxDriver) Start(vmxPath string, headless bool) error {
......
package iso package common
import ( import (
"fmt" "fmt"
...@@ -9,7 +9,6 @@ import ( ...@@ -9,7 +9,6 @@ import (
"strings" "strings"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
vmwcommon "github.com/mitchellh/packer/builder/vmware/common"
) )
// Workstation9Driver is a driver that can run VMware Workstation 9 // Workstation9Driver is a driver that can run VMware Workstation 9
...@@ -20,7 +19,7 @@ type Workstation9Driver struct { ...@@ -20,7 +19,7 @@ type Workstation9Driver struct {
VmrunPath string VmrunPath string
// SSHConfig are the SSH settings for the Fusion VM // SSHConfig are the SSH settings for the Fusion VM
SSHConfig *vmwcommon.SSHConfig SSHConfig *SSHConfig
} }
func (d *Workstation9Driver) CompactDisk(diskPath string) error { func (d *Workstation9Driver) CompactDisk(diskPath string) error {
...@@ -68,7 +67,7 @@ func (d *Workstation9Driver) IsRunning(vmxPath string) (bool, error) { ...@@ -68,7 +67,7 @@ func (d *Workstation9Driver) IsRunning(vmxPath string) (bool, error) {
} }
func (d *Workstation9Driver) SSHAddress(state multistep.StateBag) (string, error) { func (d *Workstation9Driver) SSHAddress(state multistep.StateBag) (string, error) {
return vmwcommon.SSHAddressFunc(d.SSHConfig)(state) return SSHAddressFunc(d.SSHConfig)(state)
} }
func (d *Workstation9Driver) Start(vmxPath string, headless bool) error { func (d *Workstation9Driver) Start(vmxPath string, headless bool) error {
......
// +build !windows // +build !windows
package iso package common
import ( import (
"errors" "errors"
......
// +build windows // +build windows
package iso package common
import ( import (
"log" "log"
......
package iso package iso
import ( import (
"bytes"
"fmt" "fmt"
"log"
"os/exec"
"runtime"
"strings"
vmwcommon "github.com/mitchellh/packer/builder/vmware/common" vmwcommon "github.com/mitchellh/packer/builder/vmware/common"
) )
...@@ -16,43 +11,18 @@ import ( ...@@ -16,43 +11,18 @@ import (
func NewDriver(config *config) (vmwcommon.Driver, error) { func NewDriver(config *config) (vmwcommon.Driver, error) {
drivers := []vmwcommon.Driver{} drivers := []vmwcommon.Driver{}
if config.RemoteType != "" { if config.RemoteType == "" {
drivers = []vmwcommon.Driver{ return vmwcommon.NewDriver(&config.SSHConfig)
&ESX5Driver{ }
Host: config.RemoteHost,
Port: config.RemotePort, drivers = []vmwcommon.Driver{
Username: config.RemoteUser, &ESX5Driver{
Password: config.RemotePassword, Host: config.RemoteHost,
Datastore: config.RemoteDatastore, Port: config.RemotePort,
}, Username: config.RemoteUser,
} Password: config.RemotePassword,
} else { Datastore: config.RemoteDatastore,
switch runtime.GOOS { },
case "darwin":
drivers = []vmwcommon.Driver{
&Fusion5Driver{
AppPath: "/Applications/VMware Fusion.app",
SSHConfig: &config.SSHConfig,
},
}
case "linux":
drivers = []vmwcommon.Driver{
&Workstation9Driver{
SSHConfig: &config.SSHConfig,
},
&Player5LinuxDriver{
SSHConfig: &config.SSHConfig,
},
}
case "windows":
drivers = []vmwcommon.Driver{
&Workstation9Driver{
SSHConfig: &config.SSHConfig,
},
}
default:
return nil, fmt.Errorf("can't find driver for OS: %s", runtime.GOOS)
}
} }
errs := "" errs := ""
...@@ -69,29 +39,3 @@ func NewDriver(config *config) (vmwcommon.Driver, error) { ...@@ -69,29 +39,3 @@ func NewDriver(config *config) (vmwcommon.Driver, error) {
"from each driver are shown below. Please fix at least one driver\n"+ "from each driver are shown below. Please fix at least one driver\n"+
"to continue:\n%s", errs) "to continue:\n%s", errs)
} }
func runAndLog(cmd *exec.Cmd) (string, string, error) {
var stdout, stderr bytes.Buffer
log.Printf("Executing: %s %v", cmd.Path, cmd.Args[1:])
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
stdoutString := strings.TrimSpace(stdout.String())
stderrString := strings.TrimSpace(stderr.String())
if _, ok := err.(*exec.ExitError); ok {
err = fmt.Errorf("VMware error: %s", stderrString)
}
log.Printf("stdout: %s", stdoutString)
log.Printf("stderr: %s", stderrString)
// Replace these for Windows, we only want to deal with Unix
// style line endings.
returnStdout := strings.Replace(stdout.String(), "\r\n", "\n", -1)
returnStderr := strings.Replace(stderr.String(), "\r\n", "\n", -1)
return returnStdout, returnStderr, err
}
...@@ -8,6 +8,8 @@ import ( ...@@ -8,6 +8,8 @@ import (
"os" "os"
"regexp" "regexp"
"strings" "strings"
vmwcommon "github.com/mitchellh/packer/builder/vmware/common"
) )
// VMnetNatConfIPFinder finds the IP address of the host machine by // VMnetNatConfIPFinder finds the IP address of the host machine by
...@@ -16,7 +18,7 @@ import ( ...@@ -16,7 +18,7 @@ import (
type VMnetNatConfIPFinder struct{} type VMnetNatConfIPFinder struct{}
func (*VMnetNatConfIPFinder) HostIP() (string, error) { func (*VMnetNatConfIPFinder) HostIP() (string, error) {
driver := &Workstation9Driver{} driver := &vmwcommon.Workstation9Driver{}
vmnetnat := driver.VmnetnatConfPath() vmnetnat := driver.VmnetnatConfPath()
if vmnetnat == "" { if vmnetnat == "" {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment