From de8ee65b2b3ae16945e6074bb0426fe8cb93dc7f Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Mon, 14 Jul 2014 17:24:22 -0700 Subject: [PATCH] provisioner/remote-exec: Retry SSH connections --- .../remote-exec/resource_provisioner.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/builtin/provisioners/remote-exec/resource_provisioner.go b/builtin/provisioners/remote-exec/resource_provisioner.go index 7b5fa3f713..65630cf1ec 100644 --- a/builtin/provisioners/remote-exec/resource_provisioner.go +++ b/builtin/provisioners/remote-exec/resource_provisioner.go @@ -47,8 +47,8 @@ type SSHConfig struct { Host string Port int Timeout string - ScriptPath string `mapstructure:"script_path"` - TimeoutVal time.Duration + ScriptPath string `mapstructure:"script_path"` + TimeoutVal time.Duration `mapstructure:"-"` } func (p *ResourceProvisioner) Apply(s *terraform.ResourceState, @@ -104,6 +104,7 @@ func (p *ResourceProvisioner) Validate(c *terraform.ResourceConfig) (ws []string func (p *ResourceProvisioner) verifySSH(s *terraform.ResourceState) error { connType := s.ConnInfo.Raw["type"] switch connType { + case nil: case "": case "ssh": default: @@ -256,7 +257,18 @@ func (p *ResourceProvisioner) runScripts(conf *SSHConfig, scripts []io.ReadClose ssh.KeyboardInteractive(helper.PasswordKeyboardInteractive(conf.Password))) } host := fmt.Sprintf("%s:%d", conf.Host, conf.Port) - comm, err := helper.New(host, &helper.Config{SSHConfig: sshConf}) + config := &helper.Config{ + SSHConfig: sshConf, + Connection: helper.ConnectFunc("tcp", host), + } + + // Wait and retry until we establish the SSH connection + var comm *helper.SSHCommunicator + err := retryFunc(conf.TimeoutVal, func() error { + var err error + comm, err = helper.New(host, config) + return err + }) if err != nil { return err }