Merge pull request #3127 from bhcleek/master

listen on system chosen port
This commit is contained in:
Chris Bednarski 2016-01-29 15:37:45 -08:00
commit 28b045e546
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 {
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 {
@ -149,19 +151,28 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
config.AddHostKey(private)
localListener, err := func() (net.Listener, error) {
port, _ := strconv.ParseUint(p.config.LocalPort, 10, 16)
if port == 0 {
port = 2200
port, err := strconv.ParseUint(p.config.LocalPort, 10, 16)
if err != nil {
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
}
ui.Say(err.Error())
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())
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")
}()
@ -205,6 +216,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
}
return nil
}
func (p *Provisioner) Cancel() {

View File

@ -51,10 +51,12 @@ Required Parameters:
Optional Parameters:
- `local_port` (string) - The port on which to
attempt to listen for SSH connections. This value is a starting point.
The provisioner will attempt listen for SSH connections on the first
available of ten ports, starting at `local_port`. The default value is 2200.
- `local_port` (string) - The port on which to attempt to listen for SSH
connections. This value is a starting point. The provisioner will attempt
listen for SSH connections on the first available of ten ports, starting at
`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 protocol that Ansible will use to transfer files. The command should