2017-01-31 05:05:49 -05:00
|
|
|
package common
|
2013-12-24 16:39:52 -05:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-12-24 16:39:52 -05:00
|
|
|
"fmt"
|
2019-04-23 15:55:59 -04:00
|
|
|
"log"
|
2015-02-13 05:13:58 -05:00
|
|
|
"time"
|
2013-12-24 16:39:52 -05:00
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-12-24 16:39:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepRegister struct {
|
|
|
|
registeredPath string
|
2015-02-13 05:13:58 -05:00
|
|
|
Format string
|
2017-01-31 05:05:49 -05:00
|
|
|
KeepRegistered bool
|
2018-10-25 17:17:35 -04:00
|
|
|
SkipExport bool
|
2013-12-24 16:39:52 -05:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepRegister) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-01-31 05:05:49 -05:00
|
|
|
driver := state.Get("driver").(Driver)
|
2013-12-24 16:39:52 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2018-10-25 17:17:35 -04:00
|
|
|
|
2013-12-24 16:39:52 -05:00
|
|
|
vmxPath := state.Get("vmx_path").(string)
|
|
|
|
|
2017-01-31 05:05:49 -05:00
|
|
|
if remoteDriver, ok := driver.(RemoteDriver); ok {
|
2013-12-24 16:39:52 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-01-31 05:05:49 -05:00
|
|
|
driver := state.Get("driver").(Driver)
|
2013-12-24 16:39:52 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2016-03-16 18:17:35 -04:00
|
|
|
|
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
2017-01-31 05:05:49 -05:00
|
|
|
if (s.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
|
|
|
|
2017-01-31 05:05:49 -05:00
|
|
|
if remoteDriver, ok := driver.(RemoteDriver); ok {
|
2018-11-02 14:10:51 -04:00
|
|
|
if s.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
|
2019-04-23 15:55:59 -04:00
|
|
|
start := time.Now()
|
2015-02-13 05:13:58 -05:00
|
|
|
for {
|
2019-04-23 15:55:59 -04:00
|
|
|
destroyed, err := remoteDriver.IsDestroyed()
|
2015-11-03 12:43:42 -05:00
|
|
|
if destroyed {
|
2015-02-13 05:13:58 -05:00
|
|
|
break
|
|
|
|
}
|
2019-04-23 15:55:59 -04:00
|
|
|
log.Printf("error destroying vm: %s", err)
|
2018-11-02 14:10:51 -04:00
|
|
|
time.Sleep(1 * time.Second)
|
2019-04-23 15:55:59 -04:00
|
|
|
if time.Since(start) >= time.Duration(30*time.Minute) {
|
|
|
|
ui.Error("Error unregistering VM; timed out. You may " +
|
|
|
|
"need to manually clean up your machine")
|
|
|
|
break
|
|
|
|
}
|
2015-02-13 05:13:58 -05:00
|
|
|
}
|
|
|
|
}
|
2013-12-24 16:39:52 -05:00
|
|
|
}
|
|
|
|
}
|