2015-06-22 05:47:18 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
|
2021-02-23 16:43:20 -06:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/communicator"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
|
2015-06-22 05:47:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type SSHConfig struct {
|
|
|
|
Comm communicator.Config `mapstructure:",squash"`
|
|
|
|
|
|
|
|
SSHHostPortMin uint `mapstructure:"ssh_host_port_min"`
|
|
|
|
SSHHostPortMax uint `mapstructure:"ssh_host_port_max"`
|
|
|
|
SSHSkipNatMapping bool `mapstructure:"ssh_skip_nat_mapping"`
|
|
|
|
|
|
|
|
// These are deprecated, but we keep them around for BC
|
|
|
|
// TODO(@mitchellh): remove
|
|
|
|
SSHKeyPath string `mapstructure:"ssh_key_path"`
|
|
|
|
SSHWaitTimeout time.Duration `mapstructure:"ssh_wait_timeout"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *SSHConfig) Prepare(ctx *interpolate.Context) []error {
|
|
|
|
if c.SSHHostPortMin == 0 {
|
|
|
|
c.SSHHostPortMin = 2222
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.SSHHostPortMax == 0 {
|
|
|
|
c.SSHHostPortMax = 4444
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: backwards compatibility, write fixer instead
|
|
|
|
if c.SSHKeyPath != "" {
|
2021-01-03 16:08:24 -06:00
|
|
|
c.Comm.SSHPrivateKeyFile = c.SSHKeyPath
|
2015-06-22 05:47:18 -05:00
|
|
|
}
|
|
|
|
if c.SSHWaitTimeout != 0 {
|
|
|
|
c.Comm.SSHTimeout = c.SSHWaitTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
errs := c.Comm.Prepare(ctx)
|
|
|
|
if c.SSHHostPortMin > c.SSHHostPortMax {
|
|
|
|
errs = append(errs,
|
|
|
|
errors.New("ssh_host_port_min must be less than ssh_host_port_max"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return errs
|
|
|
|
}
|