github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/vmware/iso/step_register.go (about) 1 package iso 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/mitchellh/multistep" 8 vmwcommon "github.com/mitchellh/packer/builder/vmware/common" 9 "github.com/mitchellh/packer/packer" 10 ) 11 12 type StepRegister struct { 13 registeredPath string 14 Format string 15 } 16 17 func (s *StepRegister) Run(state multistep.StateBag) multistep.StepAction { 18 driver := state.Get("driver").(vmwcommon.Driver) 19 ui := state.Get("ui").(packer.Ui) 20 vmxPath := state.Get("vmx_path").(string) 21 22 if remoteDriver, ok := driver.(RemoteDriver); ok { 23 ui.Say("Registering remote VM...") 24 if err := remoteDriver.Register(vmxPath); err != nil { 25 err := fmt.Errorf("Error registering VM: %s", err) 26 state.Put("error", err) 27 ui.Error(err.Error()) 28 return multistep.ActionHalt 29 } 30 31 s.registeredPath = vmxPath 32 } 33 34 return multistep.ActionContinue 35 } 36 37 func (s *StepRegister) Cleanup(state multistep.StateBag) { 38 if s.registeredPath == "" { 39 return 40 } 41 42 driver := state.Get("driver").(vmwcommon.Driver) 43 ui := state.Get("ui").(packer.Ui) 44 config := state.Get("config").(*Config) 45 46 _, cancelled := state.GetOk(multistep.StateCancelled) 47 _, halted := state.GetOk(multistep.StateHalted) 48 if (config.KeepRegistered) && (!cancelled && !halted) { 49 ui.Say("Keeping virtual machine registered with ESX host (keep_registered = true)") 50 return 51 } 52 53 if remoteDriver, ok := driver.(RemoteDriver); ok { 54 if s.Format == "" { 55 ui.Say("Unregistering virtual machine...") 56 if err := remoteDriver.Unregister(s.registeredPath); err != nil { 57 ui.Error(fmt.Sprintf("Error unregistering VM: %s", err)) 58 } 59 60 s.registeredPath = "" 61 } else { 62 ui.Say("Destroying virtual machine...") 63 if err := remoteDriver.Destroy(); err != nil { 64 ui.Error(fmt.Sprintf("Error destroying VM: %s", err)) 65 } 66 // Wait for the machine to actually destroy 67 for { 68 destroyed, _ := remoteDriver.IsDestroyed() 69 if destroyed { 70 break 71 } 72 time.Sleep(150 * time.Millisecond) 73 } 74 } 75 } 76 }