2018-02-19 04:31:29 -05:00
|
|
|
// Package connhelper provides helpers for connecting to a remote daemon host with custom logic.
|
|
|
|
package connhelper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
2019-10-10 12:53:03 -04:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2018-02-19 04:31:29 -05:00
|
|
|
|
2019-10-10 12:53:03 -04:00
|
|
|
"github.com/docker/cli/cli/config"
|
2019-03-01 09:34:36 -05:00
|
|
|
"github.com/docker/cli/cli/connhelper/commandconn"
|
2018-02-19 04:31:29 -05:00
|
|
|
"github.com/docker/cli/cli/connhelper/ssh"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ConnectionHelper allows to connect to a remote host with custom stream provider binary.
|
|
|
|
type ConnectionHelper struct {
|
|
|
|
Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
|
|
|
|
Host string // dummy URL used for HTTP requests. e.g. "http://docker"
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetConnectionHelper returns Docker-specific connection helper for the given URL.
|
|
|
|
// GetConnectionHelper returns nil without error when no helper is registered for the scheme.
|
2019-03-01 09:34:36 -05:00
|
|
|
//
|
|
|
|
// ssh://<user>@<host> URL requires Docker 18.09 or later on the remote host.
|
2018-02-19 04:31:29 -05:00
|
|
|
func GetConnectionHelper(daemonURL string) (*ConnectionHelper, error) {
|
|
|
|
u, err := url.Parse(daemonURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch scheme := u.Scheme; scheme {
|
|
|
|
case "ssh":
|
2019-03-01 09:34:36 -05:00
|
|
|
sp, err := ssh.ParseURL(daemonURL)
|
2018-02-19 04:31:29 -05:00
|
|
|
if err != nil {
|
2019-03-01 09:34:36 -05:00
|
|
|
return nil, errors.Wrap(err, "ssh host connection is not valid")
|
2018-02-19 04:31:29 -05:00
|
|
|
}
|
|
|
|
return &ConnectionHelper{
|
|
|
|
Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
2019-10-10 12:53:03 -04:00
|
|
|
return commandconn.New(ctx, "ssh", append(multiplexingArgs(), append(sp.Args(), []string{"--", "docker", "system", "dial-stdio"}...)...)...)
|
2018-02-19 04:31:29 -05:00
|
|
|
},
|
|
|
|
Host: "http://docker",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
// Future version may support plugins via ~/.docker/config.json. e.g. "dind"
|
|
|
|
// See docker/cli#889 for the previous discussion.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-01 09:34:36 -05:00
|
|
|
// GetCommandConnectionHelper returns Docker-specific connection helper constructed from an arbitrary command.
|
2019-01-31 12:50:58 -05:00
|
|
|
func GetCommandConnectionHelper(cmd string, flags ...string) (*ConnectionHelper, error) {
|
|
|
|
return &ConnectionHelper{
|
|
|
|
Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
2019-03-01 09:34:36 -05:00
|
|
|
return commandconn.New(ctx, cmd, flags...)
|
2019-01-31 12:50:58 -05:00
|
|
|
},
|
|
|
|
Host: "http://docker",
|
|
|
|
}, nil
|
|
|
|
}
|
2019-10-10 12:53:03 -04:00
|
|
|
|
|
|
|
func multiplexingArgs() []string {
|
|
|
|
if v := os.Getenv("DOCKER_SSH_NO_MUX"); v != "" {
|
|
|
|
if b, err := strconv.ParseBool(v); err == nil && b {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(config.Dir(), 0700); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
args := []string{"-o", "ControlMaster=auto", "-o", "ControlPath=" + config.Dir() + "/%r@%h:%p"}
|
|
|
|
if v := os.Getenv("DOCKER_SSH_MUX_PERSIST"); v != "" {
|
|
|
|
args = append(args, "-o", "ControlPersist="+v)
|
|
|
|
}
|
|
|
|
return args
|
|
|
|
}
|