From be4ca4b666ade6ccb31df2a6f0c21e21146b2121 Mon Sep 17 00:00:00 2001 From: "William L. Brooks" Date: Sat, 10 Aug 2019 15:03:08 -0400 Subject: [PATCH] Move hyper-v host ip determination earlier in steps, connect to Hyper-V vm prior to issuing the start operation. --- builder/hyperv/common/step_run.go | 31 ++++++++++++++----- .../hyperv/common/step_type_boot_command.go | 13 +------- builder/hyperv/iso/builder.go | 3 +- builder/hyperv/vmcx/builder.go | 3 +- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/builder/hyperv/common/step_run.go b/builder/hyperv/common/step_run.go index a897b203e..3c8a988b9 100644 --- a/builder/hyperv/common/step_run.go +++ b/builder/hyperv/common/step_run.go @@ -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 } diff --git a/builder/hyperv/common/step_type_boot_command.go b/builder/hyperv/common/step_type_boot_command.go index 0d296ce8c..2d4a9942a 100644 --- a/builder/hyperv/common/step_type_boot_command.go +++ b/builder/hyperv/common/step_type_boot_command.go @@ -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, diff --git a/builder/hyperv/iso/builder.go b/builder/hyperv/iso/builder.go index a064bc0e7..e97c50162 100644 --- a/builder/hyperv/iso/builder.go +++ b/builder/hyperv/iso/builder.go @@ -449,7 +449,8 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack }, &hypervcommon.StepRun{ - Headless: b.config.Headless, + Headless: b.config.Headless, + SwitchName: b.config.SwitchName, }, &hypervcommon.StepTypeBootCommand{ diff --git a/builder/hyperv/vmcx/builder.go b/builder/hyperv/vmcx/builder.go index 7e9d37694..94dc77644 100644 --- a/builder/hyperv/vmcx/builder.go +++ b/builder/hyperv/vmcx/builder.go @@ -486,7 +486,8 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack }, &hypervcommon.StepRun{ - Headless: b.config.Headless, + Headless: b.config.Headless, + SwitchName: b.config.SwitchName, }, &hypervcommon.StepTypeBootCommand{