From 8d0904e296065a2951ef0856d2ce9b5e9c4b6c7e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 13 Jun 2015 19:39:42 -0400 Subject: [PATCH] helper/communicator: configurable handshake attempts [GH-1988] --- helper/communicator/config.go | 21 +++++++++++++-------- helper/communicator/step_connect_ssh.go | 6 ++++-- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/helper/communicator/config.go b/helper/communicator/config.go index a2d93a480..6a5e7b97b 100644 --- a/helper/communicator/config.go +++ b/helper/communicator/config.go @@ -12,14 +12,15 @@ import ( // Config is the common configuration that communicators allow within // a builder. type Config struct { - Type string `mapstructure:"communicator"` - SSHHost string `mapstructure:"ssh_host"` - SSHPort int `mapstructure:"ssh_port"` - SSHUsername string `mapstructure:"ssh_username"` - SSHPassword string `mapstructure:"ssh_password"` - SSHPrivateKey string `mapstructure:"ssh_private_key_file"` - SSHPty bool `mapstructure:"ssh_pty"` - SSHTimeout time.Duration `mapstructure:"ssh_timeout"` + Type string `mapstructure:"communicator"` + SSHHost string `mapstructure:"ssh_host"` + SSHPort int `mapstructure:"ssh_port"` + SSHUsername string `mapstructure:"ssh_username"` + SSHPassword string `mapstructure:"ssh_password"` + SSHPrivateKey string `mapstructure:"ssh_private_key_file"` + SSHPty bool `mapstructure:"ssh_pty"` + SSHTimeout time.Duration `mapstructure:"ssh_timeout"` + SSHHandshakeAttempts int `mapstructure:"ssh_handshake_attempts"` } func (c *Config) Prepare(ctx *interpolate.Context) []error { @@ -35,6 +36,10 @@ func (c *Config) Prepare(ctx *interpolate.Context) []error { c.SSHTimeout = 5 * time.Minute } + if c.SSHHandshakeAttempts == 0 { + c.SSHHandshakeAttempts = 10 + } + // Validation var errs []error if c.Type == "ssh" { diff --git a/helper/communicator/step_connect_ssh.go b/helper/communicator/step_connect_ssh.go index 0b54bae9d..4b664fe4c 100644 --- a/helper/communicator/step_connect_ssh.go +++ b/helper/communicator/step_connect_ssh.go @@ -149,8 +149,10 @@ func (s *StepConnectSSH) waitForSSH(state multistep.StateBag, cancel <-chan stru handshakeAttempts += 1 } - if handshakeAttempts < 10 { - // Try to connect via SSH a handful of times + if handshakeAttempts < s.Config.SSHHandshakeAttempts { + // Try to connect via SSH a handful of times. We sleep here + // so we don't get a ton of authentication errors back to back. + time.Sleep(2 * time.Second) continue }