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-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) {
|
2020-01-30 10:21:04 -05:00
|
|
|
return commandconn.New(ctx, "ssh", 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
|
|
|
|
}
|