mirror of https://github.com/docker/docs.git
Add retries for SSH dial timeout
Signed-off-by: Nathan LeClaire <nathan.leclaire@gmail.com>
This commit is contained in:
parent
c3f15e9c3e
commit
aa6325dadb
|
@ -2,12 +2,14 @@ package ssh
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/docker/docker/pkg/term"
|
"github.com/docker/docker/pkg/term"
|
||||||
|
"github.com/docker/machine/log"
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -17,6 +19,10 @@ type Client struct {
|
||||||
Port int
|
Port int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
maxDialAttempts = 10
|
||||||
|
)
|
||||||
|
|
||||||
func NewClient(user string, host string, port int, auth *Auth) (*Client, error) {
|
func NewClient(user string, host string, port int, auth *Auth) (*Client, error) {
|
||||||
config, err := NewConfig(user, auth)
|
config, err := NewConfig(user, auth)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -58,16 +64,26 @@ func NewConfig(user string, auth *Auth) (*ssh.ClientConfig, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) Run(command string) (Output, 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++ {
|
||||||
if err != nil {
|
conn, err = ssh.Dial("tcp", fmt.Sprintf("%s:%d", client.Hostname, client.Port), client.Config)
|
||||||
return output, err
|
if err != nil {
|
||||||
|
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()
|
session, err := conn.NewSession()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return output, err
|
return output, fmt.Errorf("Error getting new session: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer session.Close()
|
defer session.Close()
|
||||||
|
|
Loading…
Reference in New Issue