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-05-10 07:50:56 -04:00
|
|
|
"log"
|
2018-01-22 18:32:33 -05:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2015-06-27 17:36:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepRun struct {
|
2018-05-10 07:50:56 -04:00
|
|
|
GuiCancelFunc context.CancelFunc
|
|
|
|
Headless bool
|
2019-08-10 15:03:08 -04:00
|
|
|
SwitchName string
|
2018-05-10 07:50:56 -04:00
|
|
|
vmName string
|
2015-06-27 17:36:39 -04:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepRun) Run(ctx 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)
|
|
|
|
|
2019-08-10 15:03:08 -04:00
|
|
|
ui.Say("Determine Host IP for HyperV machine...")
|
|
|
|
hostIp, err := driver.GetHostAdapterIpAddressForSwitch(s.SwitchName)
|
2015-06-27 17:36:39 -04:00
|
|
|
if err != nil {
|
2019-08-10 15:03:08 -04:00
|
|
|
err := fmt.Errorf("Error getting host adapter ip address: %s", err)
|
2015-06-27 17:36:39 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2019-08-10 15:03:08 -04:00
|
|
|
ui.Say(fmt.Sprintf("Host IP for the HyperV machine: %s", hostIp))
|
2020-05-19 05:49:48 -04:00
|
|
|
state.Put("http_ip", hostIp)
|
2015-06-27 17:36:39 -04:00
|
|
|
|
2018-05-05 17:54:58 -04:00
|
|
|
if !s.Headless {
|
2018-05-14 06:53:51 -04:00
|
|
|
ui.Say("Attempting to connect with vmconnect...")
|
2018-05-10 07:50:56 -04:00
|
|
|
s.GuiCancelFunc, err = driver.Connect(vmName)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf(fmt.Sprintf("Non-fatal error starting vmconnect: %s. continuing...", err))
|
|
|
|
}
|
2018-05-05 17:54:58 -04:00
|
|
|
}
|
2019-08-10 15:03:08 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
2018-05-10 07:50:56 -04:00
|
|
|
if !s.Headless && s.GuiCancelFunc != nil {
|
2018-05-05 17:54:58 -04:00
|
|
|
ui.Say("Disconnecting from vmconnect...")
|
2018-05-10 07:50:56 -04:00
|
|
|
s.GuiCancelFunc()
|
2018-05-05 17:54:58 -04:00
|
|
|
}
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|