diff --git a/builder/vmware/iso/builder.go b/builder/vmware/iso/builder.go index 95395c73c..b70f6b800 100755 --- a/builder/vmware/iso/builder.go +++ b/builder/vmware/iso/builder.go @@ -46,6 +46,7 @@ type Config struct { Version string `mapstructure:"version"` VMName string `mapstructure:"vm_name"` BootCommand []string `mapstructure:"boot_command"` + KeepRegistered bool `mapstructure:"keep_registered"` SkipCompaction bool `mapstructure:"skip_compaction"` VMXTemplatePath string `mapstructure:"vmx_template_path"` VMXDiskTemplatePath string `mapstructure:"vmx_disk_template_path"` @@ -147,7 +148,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { if b.config.RemotePort == 0 { b.config.RemotePort = 22 } - if b.config.VMXTemplatePath != "" { if err := b.validateVMXTemplatePath(); err != nil { errs = packer.MultiErrorAppend( diff --git a/builder/vmware/iso/step_register.go b/builder/vmware/iso/step_register.go index 96528ee5c..744b11844 100644 --- a/builder/vmware/iso/step_register.go +++ b/builder/vmware/iso/step_register.go @@ -41,6 +41,14 @@ func (s *StepRegister) Cleanup(state multistep.StateBag) { driver := state.Get("driver").(vmwcommon.Driver) ui := state.Get("ui").(packer.Ui) + config := state.Get("config").(*Config) + + _, cancelled := state.GetOk(multistep.StateCancelled) + _, halted := state.GetOk(multistep.StateHalted) + if (config.KeepRegistered == true) && (!cancelled && !halted) { + ui.Say("Keeping virtual machine registered with ESX host (keep_registered = true)") + return + } if remoteDriver, ok := driver.(RemoteDriver); ok { if s.Format == "" { diff --git a/builder/vmware/iso/step_register_test.go b/builder/vmware/iso/step_register_test.go index c654f836d..86977431f 100644 --- a/builder/vmware/iso/step_register_test.go +++ b/builder/vmware/iso/step_register_test.go @@ -32,6 +32,10 @@ func TestStepRegister_remoteDriver(t *testing.T) { step := new(StepRegister) driver := new(RemoteDriverMock) + var config Config + config.KeepRegistered = false + state.Put("config", &config) + state.Put("driver", driver) state.Put("vmx_path", "foo") @@ -63,3 +67,29 @@ func TestStepRegister_remoteDriver(t *testing.T) { t.Fatal("should unregister proper path") } } +func TestStepRegister_WithoutUnregister_remoteDriver(t *testing.T) { + state := testState(t) + step := new(StepRegister) + + driver := new(RemoteDriverMock) + var config Config + config.KeepRegistered = true + state.Put("config", &config) + + state.Put("driver", driver) + state.Put("vmx_path", "foo") + + // Test the run + if action := step.Run(state); action != multistep.ActionContinue { + t.Fatalf("bad action: %#v", action) + } + if _, ok := state.GetOk("error"); ok { + t.Fatal("should NOT have error") + } + + // cleanup + step.Cleanup(state) + if driver.UnregisterCalled { + t.Fatal("unregister should not be called") + } +}