2015-06-27 17:36:39 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2015-06-27 17:36:39 -04:00
|
|
|
"fmt"
|
2018-01-22 18:32:33 -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"
|
2015-06-27 17:36:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepRun struct {
|
2018-05-05 17:54:58 -04:00
|
|
|
Headless bool
|
|
|
|
vmName string
|
2015-06-27 17:36:39 -04:00
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepRun) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-06-27 17:36:39 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vmName := state.Get("vmName").(string)
|
|
|
|
|
|
|
|
ui.Say("Starting the virtual machine...")
|
|
|
|
|
|
|
|
err := driver.Start(vmName)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error starting vm: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
s.vmName = vmName
|
|
|
|
|
2018-05-05 17:54:58 -04:00
|
|
|
if !s.Headless {
|
|
|
|
ui.Say("Connecting to vmconnect...")
|
|
|
|
cancel := driver.Connect(vmName)
|
|
|
|
state.Put("guiCancelFunc", cancel)
|
|
|
|
}
|
2015-06-27 17:36:39 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepRun) Cleanup(state multistep.StateBag) {
|
|
|
|
if s.vmName == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2018-05-05 17:54:58 -04:00
|
|
|
guiCancelFunc := state.Get("guiCancelFunc").(context.CancelFunc)
|
|
|
|
|
|
|
|
if guiCancelFunc != nil {
|
|
|
|
ui.Say("Disconnecting from vmconnect...")
|
|
|
|
guiCancelFunc()
|
|
|
|
}
|
2015-06-27 17:36:39 -04:00
|
|
|
|
|
|
|
if running, _ := driver.IsRunning(s.vmName); running {
|
|
|
|
if err := driver.Stop(s.vmName); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error shutting down VM: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|