From c559ec7d713f92ad57e0b7a337e4c8f850f644fc Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 6 Jun 2013 16:14:07 -0700 Subject: [PATCH] builder/vmware: check if running prior to shutting down --- builder/vmware/artifact_test.go | 2 +- builder/vmware/builder.go | 21 +++++++++++---------- builder/vmware/driver.go | 22 ++++++++++++++++++++++ builder/vmware/step_run.go | 10 +++++++--- 4 files changed, 41 insertions(+), 14 deletions(-) diff --git a/builder/vmware/artifact_test.go b/builder/vmware/artifact_test.go index 3cf90c8e4..ea96fe569 100644 --- a/builder/vmware/artifact_test.go +++ b/builder/vmware/artifact_test.go @@ -1,8 +1,8 @@ package vmware import ( - "testing" "github.com/mitchellh/packer/packer" + "testing" ) func TestArtifact_Impl(t *testing.T) { diff --git a/builder/vmware/builder.go b/builder/vmware/builder.go index 935f15597..ee577a30b 100644 --- a/builder/vmware/builder.go +++ b/builder/vmware/builder.go @@ -21,16 +21,17 @@ type Builder struct { } type config struct { - DiskName string `mapstructure:"vmdk_name"` - ISOUrl string `mapstructure:"iso_url"` - VMName string `mapstructure:"vm_name"` - OutputDir string `mapstructure:"output_directory"` - HTTPDir string `mapstructure:"http_directory"` - BootCommand []string `mapstructure:"boot_command"` - BootWait time.Duration - SSHUser string `mapstructure:"ssh_username"` - SSHPassword string `mapstructure:"ssh_password"` - SSHWaitTimeout time.Duration + DiskName string `mapstructure:"vmdk_name"` + ISOUrl string `mapstructure:"iso_url"` + VMName string `mapstructure:"vm_name"` + OutputDir string `mapstructure:"output_directory"` + HTTPDir string `mapstructure:"http_directory"` + BootCommand []string `mapstructure:"boot_command"` + BootWait time.Duration + ShutdownCommand string `mapstructure:"shutdown_command"` + SSHUser string `mapstructure:"ssh_username"` + SSHPassword string `mapstructure:"ssh_password"` + SSHWaitTimeout time.Duration RawBootWait string `mapstructure:"boot_wait"` RawSSHWaitTimeout string `mapstructure:"ssh_wait_timeout"` diff --git a/builder/vmware/driver.go b/builder/vmware/driver.go index 4f07d1e28..67aa59e01 100644 --- a/builder/vmware/driver.go +++ b/builder/vmware/driver.go @@ -1,8 +1,10 @@ package vmware import ( + "bytes" "os/exec" "path/filepath" + "strings" ) // A driver is able to talk to VMware, control virtual machines, etc. @@ -10,6 +12,9 @@ type Driver interface { // CreateDisk creates a virtual disk with the given size. CreateDisk(string, string) error + // Checks if the VMX file at the given path is running. + IsRunning(string) (bool, error) + // Start starts a VM specified by the path to the VMX given. Start(string) error @@ -33,6 +38,23 @@ func (d *Fusion5Driver) CreateDisk(output string, size string) error { return nil } +func (d *Fusion5Driver) IsRunning(vmxPath string) (bool, error) { + stdout := new(bytes.Buffer) + cmd := exec.Command(d.vmrunPath(), "-T", "fusion", "list") + cmd.Stdout = stdout + if err := cmd.Run(); err != nil { + return false, err + } + + for _, line := range strings.Split(stdout.String(), "\n") { + if line == vmxPath { + return true, nil + } + } + + return false, nil +} + func (d *Fusion5Driver) Start(vmxPath string) error { cmd := exec.Command(d.vmrunPath(), "-T", "fusion", "start", vmxPath, "gui") if err := cmd.Run(); err != nil { diff --git a/builder/vmware/step_run.go b/builder/vmware/step_run.go index 7d099462f..5af7afaff 100644 --- a/builder/vmware/step_run.go +++ b/builder/vmware/step_run.go @@ -62,9 +62,13 @@ func (s *stepRun) Cleanup(state map[string]interface{}) { time.Sleep(sleepTime) } - ui.Say("Stopping virtual machine...") - if err := driver.Stop(s.vmxPath); err != nil { - ui.Error(fmt.Sprintf("Error stopping VM: %s", err)) + // See if it is running + running, _ := driver.IsRunning(s.vmxPath) + if running { + ui.Say("Stopping virtual machine...") + if err := driver.Stop(s.vmxPath); err != nil { + ui.Error(fmt.Sprintf("Error stopping VM: %s", err)) + } } } }