Changes requested in PR #6243

- Logging error if vmconnect.exe fails.
- Using StepRun struct rather than StateBag for command Cancel function
- Better handling in Disconnect when headless is true or vmconnect failed in Start
This commit is contained in:
Unknown 2018-05-10 21:50:56 +10:00 committed by Cameron Jack
parent fc734b6bd9
commit 29c4b4436d
5 changed files with 22 additions and 14 deletions

View File

@ -115,7 +115,7 @@ type Driver interface {
UnmountFloppyDrive(string) error UnmountFloppyDrive(string) error
// Connect connects to a VM specified by the name given. // Connect connects to a VM specified by the name given.
Connect(string) context.CancelFunc Connect(string) (context.CancelFunc, error)
// Disconnect disconnects to a VM specified by the context cancel function. // Disconnect disconnects to a VM specified by the context cancel function.
Disconnect(context.CancelFunc) Disconnect(context.CancelFunc)

View File

@ -248,6 +248,7 @@ type DriverMock struct {
Connect_Called bool Connect_Called bool
Connect_VmName string Connect_VmName string
Connect_Cancel context.CancelFunc Connect_Cancel context.CancelFunc
Connect_Err error
Disconnect_Called bool Disconnect_Called bool
Disconnect_Cancel context.CancelFunc Disconnect_Cancel context.CancelFunc
@ -565,10 +566,10 @@ func (d *DriverMock) UnmountFloppyDrive(vmName string) error {
return d.UnmountFloppyDrive_Err return d.UnmountFloppyDrive_Err
} }
func (d *DriverMock) Connect(vmName string) context.CancelFunc { func (d *DriverMock) Connect(vmName string) (context.CancelFunc, error) {
d.Connect_Called = true d.Connect_Called = true
d.Connect_VmName = vmName d.Connect_VmName = vmName
return d.Connect_Cancel return d.Connect_Cancel, d.Connect_Err
} }
func (d *DriverMock) Disconnect(cancel context.CancelFunc) { func (d *DriverMock) Disconnect(cancel context.CancelFunc) {

View File

@ -350,7 +350,7 @@ func (d *HypervPS4Driver) verifyHypervPermissions() error {
} }
// Connect connects to a VM specified by the name given. // Connect connects to a VM specified by the name given.
func (d *HypervPS4Driver) Connect(vmName string) context.CancelFunc { func (d *HypervPS4Driver) Connect(vmName string) (context.CancelFunc, error) {
return hyperv.ConnectVirtualMachine(vmName) return hyperv.ConnectVirtualMachine(vmName)
} }

View File

@ -3,12 +3,14 @@ package common
import ( import (
"context" "context"
"fmt" "fmt"
"log"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
) )
type StepRun struct { type StepRun struct {
GuiCancelFunc context.CancelFunc
Headless bool Headless bool
vmName string vmName string
} }
@ -32,8 +34,10 @@ func (s *StepRun) Run(_ context.Context, state multistep.StateBag) multistep.Ste
if !s.Headless { if !s.Headless {
ui.Say("Connecting to vmconnect...") ui.Say("Connecting to vmconnect...")
cancel := driver.Connect(vmName) s.GuiCancelFunc, err = driver.Connect(vmName)
state.Put("guiCancelFunc", cancel) if err != nil {
log.Printf(fmt.Sprintf("Non-fatal error starting vmconnect: %s. continuing...", err))
}
} }
return multistep.ActionContinue return multistep.ActionContinue
} }
@ -45,11 +49,10 @@ func (s *StepRun) Cleanup(state multistep.StateBag) {
driver := state.Get("driver").(Driver) driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
guiCancelFunc := state.Get("guiCancelFunc").(context.CancelFunc)
if guiCancelFunc != nil { if !s.Headless && s.GuiCancelFunc != nil {
ui.Say("Disconnecting from vmconnect...") ui.Say("Disconnecting from vmconnect...")
guiCancelFunc() s.GuiCancelFunc()
} }
if running, _ := driver.IsRunning(s.vmName); running { if running, _ := driver.IsRunning(s.vmName); running {

View File

@ -1247,11 +1247,15 @@ param([string]$vmName, [string]$scanCodes)
return err return err
} }
func ConnectVirtualMachine(vmName string) context.CancelFunc { func ConnectVirtualMachine(vmName string) (context.CancelFunc, error) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
cmd := exec.CommandContext(ctx, "vmconnect.exe", "localhost", vmName) cmd := exec.CommandContext(ctx, "vmconnect.exe", "localhost", vmName)
cmd.Start() err := cmd.Start()
return cancel if err != nil {
// Failed to start so cancel function not required
cancel = nil
}
return cancel, err
} }
func DisconnectVirtualMachine(cancel context.CancelFunc) { func DisconnectVirtualMachine(cancel context.CancelFunc) {