packer-plugin-xenserver/builder/xenserver/step_forward_port_over_ssh.go
Cheng Sun b433754c83 Factor out common port-finding code
Fixes bug where we would infinite-loop trying to find an HTTP server
port when all ports were unavailable (e.g. range [80,80] and no root
permission)

Also fixes bug where SSH port forwarding would try ports up to but not
including HostPortMax.

Also add clarification in error messages for which port range to expand
2014-12-10 16:18:54 +00:00

50 lines
1.3 KiB
Go

package xenserver
import (
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
)
type stepForwardPortOverSSH struct {
RemotePort func(state multistep.StateBag) (uint, error)
RemoteDest func(state multistep.StateBag) (string, error)
HostPortMin uint
HostPortMax uint
ResultKey string
}
func (self *stepForwardPortOverSSH) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(config)
ui := state.Get("ui").(packer.Ui)
// Find a free local port:
l, sshHostPort := FindPort(self.HostPortMin, self.HostPortMax)
if l == nil || sshHostPort == 0 {
ui.Error("Error: unable to find free host port. Try providing a larger range [host_port_min, host_port_max]")
return multistep.ActionHalt
}
l.Close()
ui.Say(fmt.Sprintf("Creating a local port forward over SSH on local port %d", sshHostPort))
remotePort, _ := self.RemotePort(state)
remoteDest, _ := self.RemoteDest(state)
go ssh_port_forward(sshHostPort, remotePort, remoteDest, config.HostIp, config.Username, config.Password)
ui.Say(fmt.Sprintf("Port forward setup. %d ---> %s:%d on %s", sshHostPort, remoteDest, remotePort, config.HostIp))
// Provide the local port to future steps.
state.Put(self.ResultKey, sshHostPort)
return multistep.ActionContinue
}
func (self *stepForwardPortOverSSH) Cleanup(state multistep.StateBag) {}