2013-12-24 16:39:52 -05:00
|
|
|
package iso
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-02-13 05:13:58 -05:00
|
|
|
"time"
|
2013-12-24 16:39:52 -05:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
vmwcommon "github.com/hashicorp/packer/builder/vmware/common"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-12-24 16:39:52 -05:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StepRegister struct {
|
|
|
|
registeredPath string
|
2015-02-13 05:13:58 -05:00
|
|
|
Format string
|
2013-12-24 16:39:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepRegister) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
driver := state.Get("driver").(vmwcommon.Driver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vmxPath := state.Get("vmx_path").(string)
|
|
|
|
|
|
|
|
if remoteDriver, ok := driver.(RemoteDriver); ok {
|
|
|
|
ui.Say("Registering remote VM...")
|
|
|
|
if err := remoteDriver.Register(vmxPath); err != nil {
|
|
|
|
err := fmt.Errorf("Error registering VM: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
s.registeredPath = vmxPath
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepRegister) Cleanup(state multistep.StateBag) {
|
|
|
|
if s.registeredPath == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
driver := state.Get("driver").(vmwcommon.Driver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2016-03-16 18:17:35 -04:00
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
|
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
2016-03-17 17:14:03 -04:00
|
|
|
if (config.KeepRegistered) && (!cancelled && !halted) {
|
2016-03-16 18:17:35 -04:00
|
|
|
ui.Say("Keeping virtual machine registered with ESX host (keep_registered = true)")
|
|
|
|
return
|
|
|
|
}
|
2013-12-24 16:39:52 -05:00
|
|
|
|
|
|
|
if remoteDriver, ok := driver.(RemoteDriver); ok {
|
2017-11-08 13:57:34 -05:00
|
|
|
if s.Format == "" || config.SkipExport {
|
2015-02-13 05:13:58 -05:00
|
|
|
ui.Say("Unregistering virtual machine...")
|
|
|
|
if err := remoteDriver.Unregister(s.registeredPath); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error unregistering VM: %s", err))
|
|
|
|
}
|
2013-12-24 16:39:52 -05:00
|
|
|
|
2015-02-13 05:13:58 -05:00
|
|
|
s.registeredPath = ""
|
|
|
|
} else {
|
|
|
|
ui.Say("Destroying virtual machine...")
|
|
|
|
if err := remoteDriver.Destroy(); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error destroying VM: %s", err))
|
|
|
|
}
|
|
|
|
// Wait for the machine to actually destroy
|
|
|
|
for {
|
2015-11-03 12:43:42 -05:00
|
|
|
destroyed, _ := remoteDriver.IsDestroyed()
|
|
|
|
if destroyed {
|
2015-02-13 05:13:58 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(150 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
2013-12-24 16:39:52 -05:00
|
|
|
}
|
|
|
|
}
|