option to keep the VM registered with esxi

This commit is contained in:
kopernikus 2016-03-16 23:17:35 +01:00
parent b50036c817
commit 84bd2ff754
3 changed files with 39 additions and 1 deletions

View File

@ -46,6 +46,7 @@ type Config struct {
Version string `mapstructure:"version"` Version string `mapstructure:"version"`
VMName string `mapstructure:"vm_name"` VMName string `mapstructure:"vm_name"`
BootCommand []string `mapstructure:"boot_command"` BootCommand []string `mapstructure:"boot_command"`
KeepRegistered bool `mapstructure:"keep_registered"`
SkipCompaction bool `mapstructure:"skip_compaction"` SkipCompaction bool `mapstructure:"skip_compaction"`
VMXTemplatePath string `mapstructure:"vmx_template_path"` VMXTemplatePath string `mapstructure:"vmx_template_path"`
VMXDiskTemplatePath string `mapstructure:"vmx_disk_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 { if b.config.RemotePort == 0 {
b.config.RemotePort = 22 b.config.RemotePort = 22
} }
if b.config.VMXTemplatePath != "" { if b.config.VMXTemplatePath != "" {
if err := b.validateVMXTemplatePath(); err != nil { if err := b.validateVMXTemplatePath(); err != nil {
errs = packer.MultiErrorAppend( errs = packer.MultiErrorAppend(

View File

@ -41,6 +41,14 @@ func (s *StepRegister) Cleanup(state multistep.StateBag) {
driver := state.Get("driver").(vmwcommon.Driver) driver := state.Get("driver").(vmwcommon.Driver)
ui := state.Get("ui").(packer.Ui) 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 remoteDriver, ok := driver.(RemoteDriver); ok {
if s.Format == "" { if s.Format == "" {

View File

@ -32,6 +32,10 @@ func TestStepRegister_remoteDriver(t *testing.T) {
step := new(StepRegister) step := new(StepRegister)
driver := new(RemoteDriverMock) driver := new(RemoteDriverMock)
var config Config
config.KeepRegistered = false
state.Put("config", &config)
state.Put("driver", driver) state.Put("driver", driver)
state.Put("vmx_path", "foo") state.Put("vmx_path", "foo")
@ -63,3 +67,29 @@ func TestStepRegister_remoteDriver(t *testing.T) {
t.Fatal("should unregister proper path") 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")
}
}