diff --git a/builder/vmware/step_create_disk.go b/builder/vmware/step_create_disk.go index 8129f1cf9..529938003 100644 --- a/builder/vmware/step_create_disk.go +++ b/builder/vmware/step_create_disk.go @@ -8,6 +8,14 @@ import ( "path/filepath" ) +// This step creates the virtual disks for the VM. +// +// Uses: +// config *config +// ui packer.Ui +// +// Produces: +// type stepCreateDisk struct{} func (stepCreateDisk) Run(state map[string]interface{}) multistep.StepAction { diff --git a/builder/vmware/step_create_vmx.go b/builder/vmware/step_create_vmx.go index eec05707d..7253aceee 100644 --- a/builder/vmware/step_create_vmx.go +++ b/builder/vmware/step_create_vmx.go @@ -24,6 +24,7 @@ type vmxTemplateData struct { // ui packer.Ui // // Produces: +// vmx_path string - The path to the VMX file. // vnc_port uint - The port the VM is configured to listen on for VNC. type stepCreateVMX struct{} @@ -51,6 +52,7 @@ func (stepCreateVMX) Run(state map[string]interface{}) multistep.StepAction { t := template.Must(template.New("vmx").Parse(DefaultVMXTemplate)) t.Execute(f, tplData) + state["vmx_path"] = vmx_path state["vnc_port"] = vncPort return multistep.ActionContinue diff --git a/builder/vmware/step_run.go b/builder/vmware/step_run.go new file mode 100644 index 000000000..35eaa578a --- /dev/null +++ b/builder/vmware/step_run.go @@ -0,0 +1,54 @@ +package vmware + +import ( + "fmt" + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" + "os/exec" +) + +// This step runs the created virtual machine. +// +// Uses: +// ui packer.Ui +// vmx_path string +// +// Produces: +// +type stepRun struct{ + vmxPath string +} + +func (s *stepRun) Run(state map[string]interface{}) multistep.StepAction { + ui := state["ui"].(packer.Ui) + vmxPath := state["vmx_path"].(string) + + vmrun_path := "/Applications/VMware Fusion.app/Contents/Library/vmrun" + + ui.Say("Starting virtual machine...") + cmd := exec.Command(vmrun_path, "-T", "fusion", "start", vmxPath, "gui") + if err := cmd.Run(); err != nil { + ui.Error(fmt.Sprintf("Error starting VM: %s", err)) + return multistep.ActionHalt + } + + // Set the VMX path so that we know we started the machine + s.vmxPath = vmxPath + + return multistep.ActionContinue +} + +func (s *stepRun) Cleanup(state map[string]interface{}) { + ui := state["ui"].(packer.Ui) + + vmrun_path := "/Applications/VMware Fusion.app/Contents/Library/vmrun" + + // If we started the machine... stop it. + if s.vmxPath != "" { + ui.Say("Stopping virtual machine...") + cmd := exec.Command(vmrun_path, "-T", "fusion", "start", s.vmxPath, "gui") + if err := cmd.Run(); err != nil { + ui.Error(fmt.Sprintf("Error stopping VM: %s", err)) + } + } +}