changed local_port to uint representation

This commit is contained in:
xinau 2019-02-12 06:32:19 +00:00
parent c207451f7e
commit 8526244285
3 changed files with 20 additions and 25 deletions

View File

@ -51,7 +51,7 @@ type Config struct {
Backend string `mapstructure:"backend"`
User string `mapstructure:"user"`
Host string `mapstructure:"host"`
LocalPort string `mapstructure:"local_port"`
LocalPort uint `mapstructure:"local_port"`
SSHHostKeyFile string `mapstructure:"ssh_host_key_file"`
SSHAuthorizedKeyFile string `mapstructure:"ssh_authorized_key_file"`
}
@ -109,24 +109,20 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
}
}
if _, ok := SupportedBackends[p.config.Backend]; !ok {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("backend: %s must be a valid backend", p.config.Backend))
}
if p.config.Backend == "" {
p.config.Backend = "ssh"
}
if _, ok := SupportedBackends[p.config.Backend]; !ok {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("backend: %s must be a valid backend", p.config.Backend))
}
if p.config.Host == "" {
p.config.Host = "127.0.0.1"
}
if len(p.config.LocalPort) > 0 {
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 p.config.LocalPort > 65535 {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("local_port: %d must be a valid port", p.config.LocalPort))
}
if len(p.config.AttributesDirectory) > 0 {
@ -245,11 +241,8 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
config.AddHostKey(hostSigner)
localListener, err := func() (net.Listener, error) {
port, err := strconv.ParseUint(p.config.LocalPort, 10, 16)
if err != nil {
return nil, err
}
port := p.config.LocalPort
tries := 1
if port != 0 {
tries = 10
@ -261,11 +254,17 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
ui.Say(err.Error())
continue
}
_, p.config.LocalPort, err = net.SplitHostPort(l.Addr().String())
_, portStr, err := net.SplitHostPort(l.Addr().String())
if err != nil {
ui.Say(err.Error())
continue
}
portUint64, err := strconv.ParseUint(portStr, 10, 0)
if err != nil {
ui.Say(err.Error())
continue
}
p.config.LocalPort = uint(portUint64)
return l, nil
}
return nil, errors.New("Error setting up SSH proxy connection")
@ -331,7 +330,7 @@ func (p *Provisioner) executeInspec(ui packer.Ui, comm packer.Communicator, priv
args = append(args, "--key-files", privKeyFile)
}
args = append(args, "--user", p.config.User)
args = append(args, "--port", p.config.LocalPort)
args = append(args, "--port", strconv.FormatUint(uint64(p.config.LocalPort), 10))
}
args = append(args, "--attrs")

View File

@ -254,13 +254,13 @@ func TestProvisionerPrepare_LocalPort(t *testing.T) {
config["ssh_authorized_key_file"] = publickey_file.Name()
config["profile"] = profile_file.Name()
config["local_port"] = "65537"
config["local_port"] = uint(65537)
err = p.Prepare(config)
if err == nil {
t.Fatal("should have error")
}
config["local_port"] = "22222"
config["local_port"] = uint(22222)
err = p.Prepare(config)
if err != nil {
t.Fatalf("err: %s", err)

View File

@ -45,7 +45,7 @@ Replace the mock `api_token` value with your own.
Required Parameters:
- `profile` - The profile to be executed by InSpec.
- `profile` (string) - The profile to be executed by InSpec.
Optional Parameters:
@ -53,9 +53,7 @@ Optional Parameters:
running InSpec. Usage example:
``` json
{
"inspec_env_vars": [ "FOO=bar" ]
}
```
- `command` (string) - The command to invoke InSpec. Defaults to `inspec`.
@ -65,9 +63,7 @@ Optional Parameters:
not be quoted. Usage example:
``` json
{
"extra_arguments": [ "--sudo", "--reporter", "json" ]
}
```
- `attributes` (array of strings) - Attribute Files used by InSpec which will
@ -89,7 +85,7 @@ Optional Parameters:
- `host` (string) - Host used for by InSpec for connection. Defaults to
localhost.
- `local_port` (string) - The port on which to attempt to listen for SSH
- `local_port` (uint) - The port on which to attempt to listen for SSH
connections. This value is a starting point. The provisioner will attempt to
listen for SSH connections on the first available of ten ports, starting at
`local_port`. A system-chosen port is used when `local_port` is missing or