Add retries for SSH dial timeout

Signed-off-by: Nathan LeClaire <nathan.leclaire@gmail.com>
This commit is contained in:
Nathan LeClaire 2015-05-06 12:59:46 -07:00
parent c3f15e9c3e
commit aa6325dadb
1 changed files with 21 additions and 5 deletions

View File

@ -2,12 +2,14 @@ package ssh
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"github.com/docker/docker/pkg/term"
"github.com/docker/machine/log"
"golang.org/x/crypto/ssh"
)
@ -17,6 +19,10 @@ type Client struct {
Port int
}
const (
maxDialAttempts = 10
)
func NewClient(user string, host string, port int, auth *Auth) (*Client, error) {
config, err := NewConfig(user, auth)
if err != nil {
@ -58,16 +64,26 @@ func NewConfig(user string, auth *Auth) (*ssh.ClientConfig, error) {
}
func (client *Client) Run(command string) (Output, error) {
var output Output
var (
output Output
conn *ssh.Client
err error
)
conn, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", client.Hostname, client.Port), client.Config)
for i := 0; ; i++ {
conn, err = ssh.Dial("tcp", fmt.Sprintf("%s:%d", client.Hostname, client.Port), client.Config)
if err != nil {
return output, err
log.Errorf("Error dialing TCP: %s", err)
if i == maxDialAttempts {
return output, errors.New("Max SSH/TCP dial attempts exceeded")
}
}
break
}
session, err := conn.NewSession()
if err != nil {
return output, err
return output, fmt.Errorf("Error getting new session: %s", err)
}
defer session.Close()