Commit f23d66a1 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/vmware/vmx: clone vmx

parent 82fad98b
...@@ -13,6 +13,11 @@ import ( ...@@ -13,6 +13,11 @@ import (
// A driver is able to talk to VMware, control virtual machines, etc. // A driver is able to talk to VMware, control virtual machines, etc.
type Driver interface { type Driver interface {
// Clone clones the VMX and the disk to the destination path. The
// destination is a path to the VMX file. The disk will be copied
// to that same directory.
Clone(dst string, src string) error
// CompactDisk compacts a virtual disk. // CompactDisk compacts a virtual disk.
CompactDisk(string) error CompactDisk(string) error
......
package common package common
import ( import (
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
...@@ -20,6 +21,10 @@ type Fusion5Driver struct { ...@@ -20,6 +21,10 @@ type Fusion5Driver struct {
SSHConfig *SSHConfig SSHConfig *SSHConfig
} }
func (d *Fusion5Driver) Clone(dst, src string) error {
return errors.New("Cloning is not supported with Fusion 5. Please use Fusion 6+.")
}
func (d *Fusion5Driver) CompactDisk(diskPath string) error { func (d *Fusion5Driver) CompactDisk(diskPath string) error {
defragCmd := exec.Command(d.vdiskManagerPath(), "-d", diskPath) defragCmd := exec.Command(d.vdiskManagerPath(), "-d", diskPath)
if _, _, err := runAndLog(defragCmd); err != nil { if _, _, err := runAndLog(defragCmd); err != nil {
...@@ -111,7 +116,8 @@ func (d *Fusion5Driver) Verify() error { ...@@ -111,7 +116,8 @@ func (d *Fusion5Driver) Verify() error {
if _, err := os.Stat(d.vmrunPath()); err != nil { if _, err := os.Stat(d.vmrunPath()); err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return fmt.Errorf("Critical application 'vmrun' not found at path: %s", d.vmrunPath()) return fmt.Errorf(
"Critical application 'vmrun' not found at path: %s", d.vmrunPath())
} }
return err return err
...@@ -119,7 +125,9 @@ func (d *Fusion5Driver) Verify() error { ...@@ -119,7 +125,9 @@ func (d *Fusion5Driver) Verify() error {
if _, err := os.Stat(d.vdiskManagerPath()); err != nil { if _, err := os.Stat(d.vdiskManagerPath()); err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return fmt.Errorf("Critical application vdisk manager not found at path: %s", d.vdiskManagerPath()) return fmt.Errorf(
"Critical application vdisk manager not found at path: %s",
d.vdiskManagerPath())
} }
return err return err
......
...@@ -9,6 +9,11 @@ import ( ...@@ -9,6 +9,11 @@ import (
type DriverMock struct { type DriverMock struct {
sync.Mutex sync.Mutex
CloneCalled bool
CloneDst string
CloneSrc string
CloneErr error
CompactDiskCalled bool CompactDiskCalled bool
CompactDiskPath string CompactDiskPath string
CompactDiskErr error CompactDiskErr error
...@@ -54,6 +59,13 @@ type DriverMock struct { ...@@ -54,6 +59,13 @@ type DriverMock struct {
VerifyErr error VerifyErr error
} }
func (d *DriverMock) Clone(dst string, src string) error {
d.CloneCalled = true
d.CloneDst = dst
d.CloneSrc = src
return d.CloneErr
}
func (d *DriverMock) CompactDisk(path string) error { func (d *DriverMock) CompactDisk(path string) error {
d.CompactDiskCalled = true d.CompactDiskCalled = true
d.CompactDiskPath = path d.CompactDiskPath = path
......
package common package common
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
...@@ -21,6 +22,10 @@ type Player5LinuxDriver struct { ...@@ -21,6 +22,10 @@ type Player5LinuxDriver struct {
SSHConfig *SSHConfig SSHConfig *SSHConfig
} }
func (d *Player5LinuxDriver) Clone(dst, src string) error {
return errors.New("Cloning is not supported with Player 5. Please use Player 6+.")
}
func (d *Player5LinuxDriver) CompactDisk(diskPath string) error { func (d *Player5LinuxDriver) CompactDisk(diskPath string) error {
if d.QemuImgPath != "" { if d.QemuImgPath != "" {
return d.qemuCompactDisk(diskPath) return d.qemuCompactDisk(diskPath)
......
package common package common
import ( import (
"errors"
"fmt" "fmt"
"log" "log"
"os" "os"
...@@ -22,6 +23,10 @@ type Workstation9Driver struct { ...@@ -22,6 +23,10 @@ type Workstation9Driver struct {
SSHConfig *SSHConfig SSHConfig *SSHConfig
} }
func (d *Workstation9Driver) Clone(dst, src string) error {
return errors.New("Cloning is not supported with WS 9. Please use WS 10+.")
}
func (d *Workstation9Driver) CompactDisk(diskPath string) error { func (d *Workstation9Driver) CompactDisk(diskPath string) error {
defragCmd := exec.Command(d.VdiskManagerPath, "-d", diskPath) defragCmd := exec.Command(d.VdiskManagerPath, "-d", diskPath)
if _, _, err := runAndLog(defragCmd); err != nil { if _, _, err := runAndLog(defragCmd); err != nil {
......
package vmx package vmx
import ( import (
"io"
"log" "log"
"os"
"path/filepath" "path/filepath"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
vmwcommon "github.com/mitchellh/packer/builder/vmware/common"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
) )
...@@ -18,29 +17,16 @@ type StepCloneVMX struct { ...@@ -18,29 +17,16 @@ type StepCloneVMX struct {
} }
func (s *StepCloneVMX) Run(state multistep.StateBag) multistep.StepAction { func (s *StepCloneVMX) Run(state multistep.StateBag) multistep.StepAction {
driver := state.Get("driver").(vmwcommon.Driver)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
vmxPath := filepath.Join(s.OutputDir, s.VMName+".vmx") vmxPath := filepath.Join(s.OutputDir, s.VMName+".vmx")
ui.Say("Cloning VMX...") ui.Say("Cloning source VM...")
log.Printf("Cloning from: %s", s.Path) log.Printf("Cloning from: %s", s.Path)
log.Printf("Cloning to: %s", vmxPath) log.Printf("Cloning to: %s", vmxPath)
from, err := os.Open(s.Path) if err := driver.Clone(vmxPath, s.Path); err != nil {
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
defer from.Close()
to, err := os.Create(vmxPath)
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
defer to.Close()
if _, err := io.Copy(to, from); err != nil {
state.Put("error", err) state.Put("error", err)
return multistep.ActionHalt return multistep.ActionHalt
} }
......
package vmx package vmx
import ( import (
"io/ioutil"
"os"
"path/filepath"
"testing" "testing"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
vmwcommon "github.com/mitchellh/packer/builder/vmware/common"
) )
func TestStepCloneVMX_impl(t *testing.T) { func TestStepCloneVMX_impl(t *testing.T) {
...@@ -14,25 +12,14 @@ func TestStepCloneVMX_impl(t *testing.T) { ...@@ -14,25 +12,14 @@ func TestStepCloneVMX_impl(t *testing.T) {
} }
func TestStepCloneVMX(t *testing.T) { func TestStepCloneVMX(t *testing.T) {
// Setup some state
td, err := ioutil.TempDir("", "packer")
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.RemoveAll(td)
// Create the source
sourcePath := filepath.Join(td, "source.vmx")
if err := ioutil.WriteFile(sourcePath, []byte("foo"), 0644); err != nil {
t.Fatalf("err: %s", err)
}
state := testState(t) state := testState(t)
step := new(StepCloneVMX) step := new(StepCloneVMX)
step.OutputDir = td step.OutputDir = "/foo"
step.Path = sourcePath step.Path = "/bar/bar.vmx"
step.VMName = "foo" step.VMName = "foo"
driver := state.Get("driver").(*vmwcommon.DriverMock)
// Test the run // Test the run
if action := step.Run(state); action != multistep.ActionContinue { if action := step.Run(state); action != multistep.ActionContinue {
t.Fatalf("bad action: %#v", action) t.Fatalf("bad action: %#v", action)
...@@ -41,16 +28,7 @@ func TestStepCloneVMX(t *testing.T) { ...@@ -41,16 +28,7 @@ func TestStepCloneVMX(t *testing.T) {
t.Fatal("should NOT have error") t.Fatal("should NOT have error")
} }
// Test we have our VMX if !driver.CloneCalled {
if _, err := os.Stat(filepath.Join(td, "foo.vmx")); err != nil { t.Fatal("clone should be called")
t.Fatalf("err: %s", err)
}
data, err := ioutil.ReadFile(filepath.Join(td, "foo.vmx"))
if err != nil {
t.Fatalf("err: %s", err)
}
if string(data) != "foo" {
t.Fatalf("bad: %#v", string(data))
} }
} }
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