From f828b72c10233fb69d304e30e3bbd888612d170a Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Tue, 19 Mar 2019 10:42:08 +0100 Subject: [PATCH] step configure vnc: allow to use ESX5Driver again --- builder/vmware/common/driver_esx5.go | 3 +- builder/vmware/common/step_configure_vnc.go | 33 +++++++++++++++------ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/builder/vmware/common/driver_esx5.go b/builder/vmware/common/driver_esx5.go index b82aac7b2..903203496 100644 --- a/builder/vmware/common/driver_esx5.go +++ b/builder/vmware/common/driver_esx5.go @@ -3,6 +3,7 @@ package common import ( "bufio" "bytes" + "context" "encoding/csv" "errors" "fmt" @@ -359,7 +360,7 @@ func (d *ESX5Driver) GuestAddress(multistep.StateBag) (string, error) { return result, nil } -func (d *ESX5Driver) VNCAddress(_ string, portMin, portMax uint) (string, uint, error) { +func (d *ESX5Driver) VNCAddress(ctx context.Context, _ string, portMin, portMax uint) (string, uint, error) { var vncPort uint //Process ports ESXi is listening on to determine which are available diff --git a/builder/vmware/common/step_configure_vnc.go b/builder/vmware/common/step_configure_vnc.go index b3ef92bf3..7e5e0c3a2 100644 --- a/builder/vmware/common/step_configure_vnc.go +++ b/builder/vmware/common/step_configure_vnc.go @@ -30,10 +30,28 @@ type StepConfigureVNC struct { } type VNCAddressFinder interface { + VNCAddress(context.Context, string, uint, uint) (string, uint, error) + // UpdateVMX, sets driver specific VNC values to VMX data. UpdateVMX(vncAddress, vncPassword string, vncPort uint, vmxData map[string]string) } +func (s *StepConfigureVNC) VNCAddress(ctx context.Context, vncBindAddress string, portMin, portMax uint) (string, uint, error) { + var err error + s.l, err = net.ListenRangeConfig{ + Addr: s.VNCBindAddress, + Min: s.VNCPortMin, + Max: s.VNCPortMax, + Network: "tcp", + }.Listen(ctx) + if err != nil { + return "", 0, err + } + + s.l.Listener.Close() + return s.l.Address, s.l.Port, nil +} + func VNCPassword(skipPassword bool) string { if skipPassword { return "" @@ -78,12 +96,7 @@ func (s *StepConfigureVNC) Run(ctx context.Context, state multistep.StateBag) mu } log.Printf("Looking for available port between %d and %d", s.VNCPortMin, s.VNCPortMax) - s.l, err = net.ListenRangeConfig{ - Addr: s.VNCBindAddress, - Min: s.VNCPortMin, - Max: s.VNCPortMax, - Network: "tcp", - }.Listen(ctx) + vncBindAddress, vncPort, err := vncFinder.VNCAddress(ctx, s.VNCBindAddress, s.VNCPortMin, s.VNCPortMax) if err != nil { state.Put("error", err) @@ -96,7 +109,7 @@ func (s *StepConfigureVNC) Run(ctx context.Context, state multistep.StateBag) mu log.Printf("Found available VNC port: %v", s.l) - vncFinder.UpdateVMX(s.l.Address, vncPassword, s.l.Port, vmxData) + vncFinder.UpdateVMX(vncBindAddress, vncPassword, vncPort, vmxData) if err := WriteVMX(vmxPath, vmxData); err != nil { err := fmt.Errorf("Error writing VMX data: %s", err) @@ -122,7 +135,9 @@ func (StepConfigureVNC) UpdateVMX(address, password string, port uint, data map[ } func (s *StepConfigureVNC) Cleanup(multistep.StateBag) { - if err := s.l.Close(); err != nil { - log.Printf("failed to unlock port lockfile: %v", err) + if s.l != nil { + if err := s.l.Close(); err != nil { + log.Printf("failed to unlock port lockfile: %v", err) + } } }