github.com/kimor79/packer@v0.8.7-0.20151221212622-d507b18eb4cf/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 45 if remoteDriver, ok := driver.(RemoteDriver); ok { 46 if s.Format == "" { 47 ui.Say("Unregistering virtual machine...") 48 if err := remoteDriver.Unregister(s.registeredPath); err != nil { 49 ui.Error(fmt.Sprintf("Error unregistering VM: %s", err)) 50 } 51 52 s.registeredPath = "" 53 } else { 54 ui.Say("Destroying virtual machine...") 55 if err := remoteDriver.Destroy(); err != nil { 56 ui.Error(fmt.Sprintf("Error destroying VM: %s", err)) 57 } 58 // Wait for the machine to actually destroy 59 for { 60 exists, _ := remoteDriver.IsDestroyed() 61 if !exists { 62 break 63 } 64 time.Sleep(150 * time.Millisecond) 65 } 66 } 67 } 68 }