2014-03-28 19:21:55 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2014-12-05 19:50:56 -05:00
|
|
|
"errors"
|
2014-03-28 19:21:55 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-12-16 20:32:17 -05:00
|
|
|
"net/http"
|
2015-05-05 00:18:28 -04:00
|
|
|
"os"
|
2015-12-02 23:53:06 -05:00
|
|
|
"runtime"
|
2014-03-28 19:21:55 -04:00
|
|
|
|
2015-12-14 12:06:42 -05:00
|
|
|
"github.com/docker/docker/api"
|
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"
|
2016-02-07 19:55:17 -05:00
|
|
|
"github.com/docker/docker/cliconfig/credentials"
|
2015-12-02 23:53:06 -05:00
|
|
|
"github.com/docker/docker/dockerversion"
|
2015-05-05 00:18:28 -04:00
|
|
|
"github.com/docker/docker/opts"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/pkg/term"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/client"
|
2016-02-03 18:41:26 -05:00
|
|
|
"github.com/docker/go-connections/sockets"
|
2015-12-29 19:27:12 -05:00
|
|
|
"github.com/docker/go-connections/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-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
|
|
|
|
// 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-12-02 23:53:06 -05:00
|
|
|
// client is the http client that performs all API operations
|
2016-01-07 19:47:12 -05:00
|
|
|
client client.APIClient
|
2016-01-20 16:18:36 -05:00
|
|
|
// state holds the terminal state
|
|
|
|
state *term.State
|
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.
|
2015-12-18 08:03:41 -05:00
|
|
|
// 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-12-18 08:03:41 -05:00
|
|
|
// ImagesFormat returns the format string specified in the configuration.
|
|
|
|
// String contains columns and format specification, for example {{ID}}\t{{Name}}.
|
|
|
|
func (cli *DockerCli) ImagesFormat() string {
|
|
|
|
return cli.configFile.ImagesFormat
|
|
|
|
}
|
|
|
|
|
2016-01-20 16:18:36 -05:00
|
|
|
func (cli *DockerCli) setRawTerminal() error {
|
|
|
|
if cli.isTerminalIn && os.Getenv("NORAW") == "" {
|
|
|
|
state, err := term.SetRawTerminal(cli.inFd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cli.state = state
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *DockerCli) restoreTerminal(in io.Closer) error {
|
|
|
|
if cli.state != nil {
|
|
|
|
term.RestoreTerminal(cli.inFd, cli.state)
|
|
|
|
}
|
2016-02-01 20:28:40 -05:00
|
|
|
// WARNING: DO NOT REMOVE THE OS CHECK !!!
|
|
|
|
// For some reason this Close call blocks on darwin..
|
|
|
|
// As the client exists right after, simply discard the close
|
|
|
|
// until we find a better solution.
|
|
|
|
if in != nil && runtime.GOOS != "darwin" {
|
2016-01-20 16:18:36 -05:00
|
|
|
return in.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
clientFlags.PostParse()
|
2015-12-02 23:53:06 -05:00
|
|
|
configFile, e := cliconfig.Load(cliconfig.ConfigDir())
|
|
|
|
if e != nil {
|
|
|
|
fmt.Fprintf(cli.err, "WARNING: Error loading config file:%v\n", e)
|
2015-05-05 00:18:28 -04:00
|
|
|
}
|
2016-02-07 19:55:17 -05:00
|
|
|
if !configFile.ContainsAuth() {
|
|
|
|
credentials.DetectDefaultStore(configFile)
|
|
|
|
}
|
2015-12-02 23:53:06 -05:00
|
|
|
cli.configFile = configFile
|
2014-10-12 22:26:42 -04:00
|
|
|
|
2015-12-02 23:53:06 -05:00
|
|
|
host, err := getServerHost(clientFlags.Common.Hosts, clientFlags.Common.TLSOptions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-10-19 09:17:37 -04:00
|
|
|
}
|
|
|
|
|
2015-12-02 23:53:06 -05:00
|
|
|
customHeaders := cli.configFile.HTTPHeaders
|
|
|
|
if customHeaders == nil {
|
|
|
|
customHeaders = map[string]string{}
|
2015-08-21 09:28:49 -04:00
|
|
|
}
|
2016-03-18 17:42:40 -04:00
|
|
|
customHeaders["User-Agent"] = clientUserAgent()
|
2015-08-21 09:28:49 -04:00
|
|
|
|
2016-04-19 10:56:54 -04:00
|
|
|
verStr := api.DefaultVersion
|
2015-08-31 17:45:27 -04:00
|
|
|
if tmpStr := os.Getenv("DOCKER_API_VERSION"); tmpStr != "" {
|
|
|
|
verStr = tmpStr
|
|
|
|
}
|
|
|
|
|
2016-02-03 18:41:26 -05:00
|
|
|
httpClient, err := newHTTPClient(host, clientFlags.Common.TLSOptions)
|
2015-12-16 20:32:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-03 18:41:26 -05:00
|
|
|
client, err := client.NewClient(host, verStr, httpClient, customHeaders)
|
2015-12-02 23:53:06 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-05-05 00:18:28 -04:00
|
|
|
}
|
2015-12-02 23:53:06 -05:00
|
|
|
cli.client = client
|
2015-06-30 16:59:51 -04:00
|
|
|
|
2015-05-05 00:18:28 -04:00
|
|
|
if cli.in != nil {
|
|
|
|
cli.inFd, cli.isTerminalIn = term.GetFdInfo(cli.in)
|
|
|
|
}
|
|
|
|
if cli.out != nil {
|
|
|
|
cli.outFd, cli.isTerminalOut = term.GetFdInfo(cli.out)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2015-12-02 23:53:06 -05:00
|
|
|
|
|
|
|
func getServerHost(hosts []string, tlsOptions *tlsconfig.Options) (host string, err error) {
|
|
|
|
switch len(hosts) {
|
|
|
|
case 0:
|
|
|
|
host = os.Getenv("DOCKER_HOST")
|
|
|
|
case 1:
|
|
|
|
host = hosts[0]
|
|
|
|
default:
|
|
|
|
return "", errors.New("Please specify only one -H")
|
|
|
|
}
|
|
|
|
|
2016-01-30 21:45:49 -05:00
|
|
|
host, err = opts.ParseHost(tlsOptions != nil, host)
|
2015-12-02 23:53:06 -05:00
|
|
|
return
|
|
|
|
}
|
2015-12-16 20:32:17 -05:00
|
|
|
|
2016-02-03 18:41:26 -05:00
|
|
|
func newHTTPClient(host string, tlsOptions *tlsconfig.Options) (*http.Client, error) {
|
2015-12-16 20:32:17 -05:00
|
|
|
if tlsOptions == nil {
|
2016-02-03 18:41:26 -05:00
|
|
|
// let the api client configure the default transport.
|
|
|
|
return nil, nil
|
2015-12-16 20:32:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
config, err := tlsconfig.Client(*tlsOptions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-03 18:41:26 -05:00
|
|
|
tr := &http.Transport{
|
2015-12-16 20:32:17 -05:00
|
|
|
TLSClientConfig: config,
|
2016-02-03 18:41:26 -05:00
|
|
|
}
|
|
|
|
proto, addr, _, err := client.ParseHost(host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sockets.ConfigureTransport(tr, proto, addr)
|
|
|
|
|
|
|
|
return &http.Client{
|
|
|
|
Transport: tr,
|
2015-12-16 20:32:17 -05:00
|
|
|
}, nil
|
|
|
|
}
|
2016-03-18 17:42:40 -04:00
|
|
|
|
|
|
|
func clientUserAgent() string {
|
|
|
|
return "Docker-Client/" + dockerversion.Version + " (" + runtime.GOOS + ")"
|
|
|
|
}
|