listen on system chosen port

Change the default for LocalPort to "0", so that the listener will be a
system chosen port.
This commit is contained in:
Billie H. Cleek 2016-01-28 22:22:12 -08:00
parent 314aad379a
commit bf3c294326
2 changed files with 29 additions and 15 deletions

View File

@ -92,6 +92,8 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
if _, err := strconv.ParseUint(p.config.LocalPort, 10, 16); err != nil { if _, err := strconv.ParseUint(p.config.LocalPort, 10, 16); err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("local_port: %s must be a valid port", p.config.LocalPort)) errs = packer.MultiErrorAppend(errs, fmt.Errorf("local_port: %s must be a valid port", p.config.LocalPort))
} }
} else {
p.config.LocalPort = "0"
} }
if errs != nil && len(errs.Errors) > 0 { if errs != nil && len(errs.Errors) > 0 {
@ -149,19 +151,28 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
config.AddHostKey(private) config.AddHostKey(private)
localListener, err := func() (net.Listener, error) { localListener, err := func() (net.Listener, error) {
port, _ := strconv.ParseUint(p.config.LocalPort, 10, 16) port, err := strconv.ParseUint(p.config.LocalPort, 10, 16)
if port == 0 { if err != nil {
port = 2200 return nil, err
}
for i := 0; i < 10; i++ {
port++
l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
if err == nil {
p.config.LocalPort = strconv.FormatUint(port, 10)
return l, nil
} }
tries := 1
if port != 0 {
tries = 10
}
for i := 0; i < tries; i++ {
l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
port++
if err != nil {
ui.Say(err.Error()) ui.Say(err.Error())
continue
}
_, p.config.LocalPort, err = net.SplitHostPort(l.Addr().String())
if err != nil {
ui.Say(err.Error())
continue
}
return l, nil
} }
return nil, errors.New("Error setting up SSH proxy connection") return nil, errors.New("Error setting up SSH proxy connection")
}() }()
@ -205,6 +216,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
} }
return nil return nil
} }
func (p *Provisioner) Cancel() { func (p *Provisioner) Cancel() {

View File

@ -51,10 +51,12 @@ Required Parameters:
Optional Parameters: Optional Parameters:
- `local_port` (string) - The port on which to - `local_port` (string) - The port on which to attempt to listen for SSH
attempt to listen for SSH connections. This value is a starting point. connections. This value is a starting point. The provisioner will attempt
The provisioner will attempt listen for SSH connections on the first listen for SSH connections on the first available of ten ports, starting at
available of ten ports, starting at `local_port`. The default value is 2200. `local_port`. When `local_port` is missing or empty, ansible-provisioner will
listen on a system-chosen port.
- `sftp_command` (string) - The command to run on the machine to handle the - `sftp_command` (string) - The command to run on the machine to handle the
SFTP protocol that Ansible will use to transfer files. The command should SFTP protocol that Ansible will use to transfer files. The command should