2014-03-28 19:21:55 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2014-12-05 19:50:56 -05:00
|
|
|
"errors"
|
2014-03-28 19:21:55 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-10-12 22:26:42 -04:00
|
|
|
"net/http"
|
2015-06-30 16:59:51 -04:00
|
|
|
"net/url"
|
2015-05-05 00:18:28 -04:00
|
|
|
"os"
|
2014-03-28 19:21:55 -04:00
|
|
|
"strings"
|
|
|
|
|
2015-05-05 00:18:28 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2015-04-22 08:06:58 -04:00
|
|
|
"github.com/docker/docker/cliconfig"
|
2015-05-05 00:18:28 -04:00
|
|
|
"github.com/docker/docker/opts"
|
2015-05-27 18:21:18 -04:00
|
|
|
"github.com/docker/docker/pkg/sockets"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/pkg/term"
|
2015-05-05 00:18:28 -04:00
|
|
|
"github.com/docker/docker/pkg/tlsconfig"
|
2014-03-28 19:21:55 -04:00
|
|
|
)
|
|
|
|
|
2015-04-14 09:14:33 -04:00
|
|
|
// DockerCli represents the docker command line client.
|
|
|
|
// Instances of the client can be returned from NewDockerCli.
|
2014-08-10 00:31:59 -04:00
|
|
|
type DockerCli struct {
|
2015-05-05 00:18:28 -04:00
|
|
|
// initializing closure
|
|
|
|
init func() error
|
|
|
|
|
2015-04-14 09:14:33 -04:00
|
|
|
// proto holds the client protocol i.e. unix.
|
|
|
|
proto string
|
|
|
|
// addr holds the client address.
|
|
|
|
addr string
|
2015-06-30 16:59:51 -04:00
|
|
|
// basePath holds the path to prepend to the requests
|
|
|
|
basePath string
|
2015-04-22 08:06:58 -04:00
|
|
|
|
|
|
|
// configFile has the client configuration file
|
|
|
|
configFile *cliconfig.ConfigFile
|
2015-04-14 09:14:33 -04:00
|
|
|
// in holds the input stream and closer (io.ReadCloser) for the client.
|
|
|
|
in io.ReadCloser
|
|
|
|
// out holds the output stream (io.Writer) for the client.
|
|
|
|
out io.Writer
|
|
|
|
// err holds the error stream (io.Writer) for the client.
|
|
|
|
err io.Writer
|
|
|
|
// keyFile holds the key file as a string.
|
|
|
|
keyFile string
|
|
|
|
// tlsConfig holds the TLS configuration for the client, and will
|
|
|
|
// set the scheme to https in NewDockerCli if present.
|
|
|
|
tlsConfig *tls.Config
|
|
|
|
// scheme holds the scheme of the client i.e. https.
|
|
|
|
scheme string
|
|
|
|
// inFd holds the file descriptor of the client's STDIN (if valid).
|
2014-09-10 09:35:48 -04:00
|
|
|
inFd uintptr
|
2015-04-14 09:14:33 -04:00
|
|
|
// outFd holds file descriptor of the client's STDOUT (if valid).
|
2014-09-10 09:35:48 -04:00
|
|
|
outFd uintptr
|
2015-04-14 09:14:33 -04:00
|
|
|
// isTerminalIn indicates whether the client's STDIN is a TTY
|
2014-09-10 09:35:48 -04:00
|
|
|
isTerminalIn bool
|
2015-09-10 08:28:38 -04:00
|
|
|
// isTerminalOut indicates whether the client's STDOUT is a TTY
|
2014-09-10 09:35:48 -04:00
|
|
|
isTerminalOut bool
|
2015-04-14 09:14:33 -04:00
|
|
|
// transport holds the client transport instance.
|
|
|
|
transport *http.Transport
|
2014-08-10 00:31:59 -04:00
|
|
|
}
|
|
|
|
|
2015-07-20 20:55:30 -04:00
|
|
|
// Initialize calls the init function that will setup the configuration for the client
|
|
|
|
// such as the TLS, tcp and other parameters used to run the client.
|
2015-05-05 00:18:28 -04:00
|
|
|
func (cli *DockerCli) Initialize() error {
|
|
|
|
if cli.init == nil {
|
|
|
|
return nil
|
2014-03-28 19:21:55 -04:00
|
|
|
}
|
2015-05-05 00:18:28 -04:00
|
|
|
return cli.init()
|
2014-03-28 19:21:55 -04:00
|
|
|
}
|
|
|
|
|
2015-04-22 02:45:18 -04:00
|
|
|
// CheckTtyInput checks if we are trying to attach to a container tty
|
|
|
|
// from a non-tty client input stream, and if so, returns an error.
|
2014-12-05 19:50:56 -05:00
|
|
|
func (cli *DockerCli) CheckTtyInput(attachStdin, ttyMode bool) error {
|
|
|
|
// In order to attach to a container tty, input stream for the client must
|
|
|
|
// be a tty itself: redirecting or piping the client standard input is
|
|
|
|
// incompatible with `docker run -t`, `docker exec -t` or `docker attach`.
|
|
|
|
if ttyMode && attachStdin && !cli.isTerminalIn {
|
|
|
|
return errors.New("cannot enable tty mode on non tty input")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-20 20:55:30 -04:00
|
|
|
// PsFormat returns the format string specified in the configuration.
|
|
|
|
// String contains columns and format specification, for example {{ID}\t{{Name}}.
|
2015-05-01 17:23:27 -04:00
|
|
|
func (cli *DockerCli) PsFormat() string {
|
|
|
|
return cli.configFile.PsFormat
|
|
|
|
}
|
|
|
|
|
2015-04-14 09:14:33 -04:00
|
|
|
// NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.
|
|
|
|
// The key file, protocol (i.e. unix) and address are passed in as strings, along with the tls.Config. If the tls.Config
|
|
|
|
// is set the client scheme will be set to https.
|
|
|
|
// The client will be given a 32-second timeout (see https://github.com/docker/docker/pull/8035).
|
2015-05-05 00:18:28 -04:00
|
|
|
func NewDockerCli(in io.ReadCloser, out, err io.Writer, clientFlags *cli.ClientFlags) *DockerCli {
|
|
|
|
cli := &DockerCli{
|
|
|
|
in: in,
|
|
|
|
out: out,
|
|
|
|
err: err,
|
|
|
|
keyFile: clientFlags.Common.TrustKey,
|
2014-09-10 09:35:48 -04:00
|
|
|
}
|
|
|
|
|
2015-05-05 00:18:28 -04:00
|
|
|
cli.init = func() error {
|
2015-07-29 17:47:30 -04:00
|
|
|
|
2015-05-05 00:18:28 -04:00
|
|
|
clientFlags.PostParse()
|
2014-03-28 19:21:55 -04:00
|
|
|
|
2015-05-05 00:18:28 -04:00
|
|
|
hosts := clientFlags.Common.Hosts
|
2014-09-10 09:35:48 -04:00
|
|
|
|
2015-05-05 00:18:28 -04:00
|
|
|
switch len(hosts) {
|
|
|
|
case 0:
|
2015-10-11 23:45:17 -04:00
|
|
|
hosts = []string{os.Getenv("DOCKER_HOST")}
|
2015-05-05 00:18:28 -04:00
|
|
|
case 1:
|
|
|
|
// only accept one host to talk to
|
|
|
|
default:
|
|
|
|
return errors.New("Please specify only one -H")
|
|
|
|
}
|
2014-10-12 22:26:42 -04:00
|
|
|
|
2015-08-21 09:28:49 -04:00
|
|
|
var e error
|
2015-10-11 23:45:17 -04:00
|
|
|
if hosts[0], e = opts.ParseHost(hosts[0]); e != nil {
|
2015-08-21 09:28:49 -04:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2015-05-05 00:18:28 -04:00
|
|
|
protoAddrParts := strings.SplitN(hosts[0], "://", 2)
|
|
|
|
cli.proto, cli.addr = protoAddrParts[0], protoAddrParts[1]
|
2015-04-01 18:39:37 -04:00
|
|
|
|
2015-05-05 00:18:28 -04:00
|
|
|
if cli.proto == "tcp" {
|
|
|
|
// error is checked in pkg/parsers already
|
|
|
|
parsed, _ := url.Parse("tcp://" + cli.addr)
|
|
|
|
cli.addr = parsed.Host
|
|
|
|
cli.basePath = parsed.Path
|
|
|
|
}
|
2015-06-30 16:59:51 -04:00
|
|
|
|
2015-05-05 00:18:28 -04:00
|
|
|
if clientFlags.Common.TLSOptions != nil {
|
|
|
|
cli.scheme = "https"
|
|
|
|
var e error
|
|
|
|
cli.tlsConfig, e = tlsconfig.Client(*clientFlags.Common.TLSOptions)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cli.scheme = "http"
|
|
|
|
}
|
|
|
|
|
|
|
|
if cli.in != nil {
|
|
|
|
cli.inFd, cli.isTerminalIn = term.GetFdInfo(cli.in)
|
|
|
|
}
|
|
|
|
if cli.out != nil {
|
|
|
|
cli.outFd, cli.isTerminalOut = term.GetFdInfo(cli.out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The transport is created here for reuse during the client session.
|
|
|
|
cli.transport = &http.Transport{
|
|
|
|
TLSClientConfig: cli.tlsConfig,
|
|
|
|
}
|
|
|
|
sockets.ConfigureTCPTransport(cli.transport, cli.proto, cli.addr)
|
|
|
|
|
|
|
|
configFile, e := cliconfig.Load(cliconfig.ConfigDir())
|
|
|
|
if e != nil {
|
|
|
|
fmt.Fprintf(cli.err, "WARNING: Error loading config file:%v\n", e)
|
|
|
|
}
|
|
|
|
cli.configFile = configFile
|
|
|
|
|
|
|
|
return nil
|
2014-03-28 19:21:55 -04:00
|
|
|
}
|
2015-05-05 00:18:28 -04:00
|
|
|
|
|
|
|
return cli
|
2014-03-28 19:21:55 -04:00
|
|
|
}
|