From 9c5a65263fe3caecfa9659b8b0f538c2b7e74a44 Mon Sep 17 00:00:00 2001 From: Ali Rizvi-Santiago Date: Thu, 4 Jun 2020 19:04:09 -0500 Subject: [PATCH] Added @SwampDragons' suggestion to the `CommHost()` implementation for the vmware builders which uses the communicator config's `Config.Port()` function to determine the communication port-number independent of the configured protocol. --- builder/vmware/common/driver_esx5.go | 5 +---- builder/vmware/common/ssh.go | 22 +++++++++------------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/builder/vmware/common/driver_esx5.go b/builder/vmware/common/driver_esx5.go index a2fc1a181..83e1109e5 100644 --- a/builder/vmware/common/driver_esx5.go +++ b/builder/vmware/common/driver_esx5.go @@ -438,10 +438,7 @@ func (ESX5Driver) UpdateVMX(_, password string, port int, data map[string]string func (d *ESX5Driver) CommHost(state multistep.StateBag) (string, error) { sshc := state.Get("sshConfig").(*SSHConfig).Comm - port := sshc.SSHPort - if sshc.Type == "winrm" { - port = sshc.WinRMPort - } + port := sshc.Port() if address, ok := state.GetOk("vm_address"); ok { return address.(string), nil diff --git a/builder/vmware/common/ssh.go b/builder/vmware/common/ssh.go index 84aae9941..6a473fb3a 100644 --- a/builder/vmware/common/ssh.go +++ b/builder/vmware/common/ssh.go @@ -14,15 +14,10 @@ func CommHost(config *SSHConfig) func(multistep.StateBag) (string, error) { driver := state.Get("driver").(Driver) comm := config.Comm - // Figure out which protocol we're using to communicate with the - // guest, and determine the correct port to handshake with. - port := comm.SSHPort - if comm.SSHHost != "" { - return comm.SSHHost, nil - } - if comm.Type == "winrm" { - port = comm.WinRMPort - } + // Snag the port from the communicator config. This way we can use it + // to perform a 3-way handshake with all of the hosts we suspect in + // order to determine which one of the hosts is the correct one. + port := comm.Port() // Get the list of potential addresses that the guest might use. hosts, err := driver.PotentialGuestIP(state) @@ -36,9 +31,10 @@ func CommHost(config *SSHConfig) func(multistep.StateBag) (string, error) { return "", errors.New("IP is blank") } - // Iterate through our list of addresses and dial each one. This way we - // can dial up each one to see which lease is actually correct and has - // ssh up. + // Iterate through our list of addresses and dial up each one similar to + // a really inefficient port-scan. This way we can determine which of + // the leases that we've parsed was the correct one and actually has our + // target ssh/winrm service bound to a tcp port. for index, host := range hosts { conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", host, port)) @@ -53,7 +49,7 @@ func CommHost(config *SSHConfig) func(multistep.StateBag) (string, error) { } // Otherwise we need to iterate to the next entry and keep hoping. - log.Printf("Ignoring entry %d at %s:%d due to host being down.", index, host, port) + log.Printf("Skipping lease entry #%d due to being unable to connect to the host (%s) with tcp port (%d).", 1+index, host, port) } return "", errors.New("Host is not up")