Move hyper-v host ip determination earlier in steps, connect to Hyper-V vm prior to issuing the start operation.

This commit is contained in:
William L. Brooks 2019-08-10 15:03:08 -04:00 committed by Megan Marsh
parent 47e7d1ff56
commit be4ca4b666
4 changed files with 28 additions and 22 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt"
"log"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
@ -12,6 +13,7 @@ import (
type StepRun struct {
GuiCancelFunc context.CancelFunc
Headless bool
SwitchName string
vmName string
}
@ -20,9 +22,29 @@ func (s *StepRun) Run(ctx context.Context, state multistep.StateBag) multistep.S
ui := state.Get("ui").(packer.Ui)
vmName := state.Get("vmName").(string)
ui.Say("Determine Host IP for HyperV machine...")
hostIp, err := driver.GetHostAdapterIpAddressForSwitch(s.SwitchName)
if err != nil {
err := fmt.Errorf("Error getting host adapter ip address: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
ui.Say(fmt.Sprintf("Host IP for the HyperV machine: %s", hostIp))
common.SetHTTPIP(hostIp)
if !s.Headless {
ui.Say("Attempting to connect with vmconnect...")
s.GuiCancelFunc, err = driver.Connect(vmName)
if err != nil {
log.Printf(fmt.Sprintf("Non-fatal error starting vmconnect: %s. continuing...", err))
}
}
ui.Say("Starting the virtual machine...")
err := driver.Start(vmName)
err = driver.Start(vmName)
if err != nil {
err := fmt.Errorf("Error starting vm: %s", err)
state.Put("error", err)
@ -32,13 +54,6 @@ func (s *StepRun) Run(ctx context.Context, state multistep.StateBag) multistep.S
s.vmName = vmName
if !s.Headless {
ui.Say("Attempting to connect with vmconnect...")
s.GuiCancelFunc, err = driver.Connect(vmName)
if err != nil {
log.Printf(fmt.Sprintf("Non-fatal error starting vmconnect: %s. continuing...", err))
}
}
return multistep.ActionContinue
}

View File

@ -33,6 +33,7 @@ func (s *StepTypeBootCommand) Run(ctx context.Context, state multistep.StateBag)
ui := state.Get("ui").(packer.Ui)
driver := state.Get("driver").(Driver)
vmName := state.Get("vmName").(string)
hostIp := common.GetHTTPIP()
// Wait the for the vm to boot.
if int64(s.BootWait) > 0 {
@ -45,18 +46,6 @@ func (s *StepTypeBootCommand) Run(ctx context.Context, state multistep.StateBag)
}
}
hostIp, err := driver.GetHostAdapterIpAddressForSwitch(s.SwitchName)
if err != nil {
err := fmt.Errorf("Error getting host adapter ip address: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
ui.Say(fmt.Sprintf("Host IP for the HyperV machine: %s", hostIp))
common.SetHTTPIP(hostIp)
s.Ctx.Data = &bootCommandTemplateData{
hostIp,
httpPort,

View File

@ -450,6 +450,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
&hypervcommon.StepRun{
Headless: b.config.Headless,
SwitchName: b.config.SwitchName,
},
&hypervcommon.StepTypeBootCommand{

View File

@ -487,6 +487,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
&hypervcommon.StepRun{
Headless: b.config.Headless,
SwitchName: b.config.SwitchName,
},
&hypervcommon.StepTypeBootCommand{