forked from toolshed/abra
		
	fix: custom timeout only for "server add"
This commit is contained in:
		@ -144,6 +144,11 @@ of your ~/.ssh/config. Checks for a valid online domain will be skipped:
 | 
				
			|||||||
			name = internal.ValidateDomain(c)
 | 
								name = internal.ValidateDomain(c)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// NOTE(d1): reasonable 5 second timeout for connections which can't
 | 
				
			||||||
 | 
							// succeed. The connection is attempted twice, so this results in 10
 | 
				
			||||||
 | 
							// seconds.
 | 
				
			||||||
 | 
							timeout := client.WithTimeout(5)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if local {
 | 
							if local {
 | 
				
			||||||
			created, err := createServerDir(name)
 | 
								created, err := createServerDir(name)
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
@ -152,7 +157,7 @@ of your ~/.ssh/config. Checks for a valid online domain will be skipped:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
			logrus.Debugf("attempting to create client for %s", name)
 | 
								logrus.Debugf("attempting to create client for %s", name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if _, err := client.New(name); err != nil {
 | 
								if _, err := client.New(name, timeout); err != nil {
 | 
				
			||||||
				cleanUp(name)
 | 
									cleanUp(name)
 | 
				
			||||||
				logrus.Fatal(err)
 | 
									logrus.Fatal(err)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
@ -184,9 +189,8 @@ of your ~/.ssh/config. Checks for a valid online domain will be skipped:
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		logrus.Debugf("attempting to create client for %s", name)
 | 
							logrus.Debugf("attempting to create client for %s", name)
 | 
				
			||||||
		if _, err := client.New(name); err != nil {
 | 
							if _, err := client.New(name, timeout); err != nil {
 | 
				
			||||||
			cleanUp(name)
 | 
								cleanUp(name)
 | 
				
			||||||
			logrus.Debugf("failed to construct client for %s, saw %s", name, err.Error())
 | 
					 | 
				
			||||||
			logrus.Fatal(sshPkg.Fatal(name, err))
 | 
								logrus.Fatal(sshPkg.Fatal(name, err))
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -16,11 +16,26 @@ import (
 | 
				
			|||||||
	"github.com/sirupsen/logrus"
 | 
						"github.com/sirupsen/logrus"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Conf is a Docker client configuration.
 | 
				
			||||||
 | 
					type Conf struct {
 | 
				
			||||||
 | 
						Timeout int
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Opt is a Docker client option.
 | 
				
			||||||
 | 
					type Opt func(c *Conf)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// WithTimeout specifies a timeout for a Docker client.
 | 
				
			||||||
 | 
					func WithTimeout(timeout int) Opt {
 | 
				
			||||||
 | 
						return func(c *Conf) {
 | 
				
			||||||
 | 
							c.Timeout = timeout
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// New initiates a new Docker client. New client connections are validated so
 | 
					// New initiates a new Docker client. New client connections are validated so
 | 
				
			||||||
// that we ensure connections via SSH to the daemon can succeed. It takes into
 | 
					// that we ensure connections via SSH to the daemon can succeed. It takes into
 | 
				
			||||||
// account that you may only want the local client and not communicate via SSH.
 | 
					// account that you may only want the local client and not communicate via SSH.
 | 
				
			||||||
// For this use-case, please pass "default" as the contextName.
 | 
					// For this use-case, please pass "default" as the contextName.
 | 
				
			||||||
func New(serverName string) (*client.Client, error) {
 | 
					func New(serverName string, opts ...Opt) (*client.Client, error) {
 | 
				
			||||||
	var clientOpts []client.Opt
 | 
						var clientOpts []client.Opt
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if serverName != "default" {
 | 
						if serverName != "default" {
 | 
				
			||||||
@ -34,7 +49,12 @@ func New(serverName string) (*client.Client, error) {
 | 
				
			|||||||
			return nil, err
 | 
								return nil, err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		helper, err := commandconnPkg.NewConnectionHelper(ctxEndpoint)
 | 
							conf := &Conf{}
 | 
				
			||||||
 | 
							for _, opt := range opts {
 | 
				
			||||||
 | 
								opt(conf)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							helper, err := commandconnPkg.NewConnectionHelper(ctxEndpoint, conf.Timeout)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return nil, err
 | 
								return nil, err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
				
			|||||||
@ -19,7 +19,7 @@ func Fatal(hostname string, err error) error {
 | 
				
			|||||||
	} else if strings.Contains(out, "Permission denied") {
 | 
						} else if strings.Contains(out, "Permission denied") {
 | 
				
			||||||
		return fmt.Errorf("ssh auth: permission denied for %s", hostname)
 | 
							return fmt.Errorf("ssh auth: permission denied for %s", hostname)
 | 
				
			||||||
	} else if strings.Contains(out, "Network is unreachable") {
 | 
						} else if strings.Contains(out, "Network is unreachable") {
 | 
				
			||||||
		return fmt.Errorf("unable to connect to %s, network is unreachable?", hostname)
 | 
							return fmt.Errorf("unable to connect to %s, please check your SSH config", hostname)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return err
 | 
						return err
 | 
				
			||||||
 | 
				
			|||||||
@ -2,6 +2,7 @@ package commandconn
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"net"
 | 
						"net"
 | 
				
			||||||
	"net/url"
 | 
						"net/url"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -14,14 +15,17 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// GetConnectionHelper returns Docker-specific connection helper for the given URL.
 | 
					// GetConnectionHelper returns Docker-specific connection helper for the given URL.
 | 
				
			||||||
// GetConnectionHelper returns nil without error when no helper is registered for the scheme.
 | 
					func GetConnectionHelper(daemonURL string, timeout int) (*connhelper.ConnectionHelper, error) {
 | 
				
			||||||
//
 | 
						if timeout != 0 {
 | 
				
			||||||
// ssh://<host> URL requires Docker 18.09 or later on the remote host.
 | 
							return getConnectionHelper(daemonURL, []string{fmt.Sprintf("-o ConnectTimeout=%v", timeout)})
 | 
				
			||||||
func GetConnectionHelper(daemonURL string) (*connhelper.ConnectionHelper, error) {
 | 
						}
 | 
				
			||||||
	return getConnectionHelper(daemonURL)
 | 
					
 | 
				
			||||||
 | 
						return getConnectionHelper(daemonURL, []string{})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func getConnectionHelper(daemonURL string) (*connhelper.ConnectionHelper, error) {
 | 
					// getConnectionHelper generates a connection helper from the underlying Docker
 | 
				
			||||||
 | 
					// libraries.
 | 
				
			||||||
 | 
					func getConnectionHelper(daemonURL string, sshFlags []string) (*connhelper.ConnectionHelper, error) {
 | 
				
			||||||
	url, err := url.Parse(daemonURL)
 | 
						url, err := url.Parse(daemonURL)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
@ -35,7 +39,7 @@ func getConnectionHelper(daemonURL string) (*connhelper.ConnectionHelper, error)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		return &connhelper.ConnectionHelper{
 | 
							return &connhelper.ConnectionHelper{
 | 
				
			||||||
			Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
 | 
								Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
 | 
				
			||||||
				return New(ctx, "ssh", ctxConnDetails.Args("docker", "system", "dial-stdio")...)
 | 
									return New(ctx, "ssh", append(sshFlags, ctxConnDetails.Args("docker", "system", "dial-stdio")...)...)
 | 
				
			||||||
			},
 | 
								},
 | 
				
			||||||
			Host: "http://docker.example.com",
 | 
								Host: "http://docker.example.com",
 | 
				
			||||||
		}, nil
 | 
							}, nil
 | 
				
			||||||
@ -46,8 +50,8 @@ func getConnectionHelper(daemonURL string) (*connhelper.ConnectionHelper, error)
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// NewConnectionHelper creates new connection helper for a remote docker daemon.
 | 
					// NewConnectionHelper creates new connection helper for a remote docker daemon.
 | 
				
			||||||
func NewConnectionHelper(daemonURL string) (*connhelper.ConnectionHelper, error) {
 | 
					func NewConnectionHelper(daemonURL string, timeout int) (*connhelper.ConnectionHelper, error) {
 | 
				
			||||||
	helper, err := GetConnectionHelper(daemonURL)
 | 
						helper, err := GetConnectionHelper(daemonURL, timeout)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user