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"
|
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"
|
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
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
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
|
|
|
}
|
2015-12-02 23:53:06 -05:00
|
|
|
customHeaders["User-Agent"] = "Docker-Client/" + dockerversion.Version + " (" + runtime.GOOS + ")"
|
2015-08-21 09:28:49 -04:00
|
|
|
|
2015-12-18 15:28:17 -05:00
|
|
|
verStr := api.DefaultVersion.String()
|
2015-08-31 17:45:27 -04:00
|
|
|
if tmpStr := os.Getenv("DOCKER_API_VERSION"); tmpStr != "" {
|
|
|
|
verStr = tmpStr
|
|
|
|
}
|
|
|
|
|
2015-12-16 20:32:17 -05:00
|
|
|
clientTransport, err := newClientTransport(clientFlags.Common.TLSOptions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-04 19:05:26 -05:00
|
|
|
client, err := client.NewClient(host, verStr, clientTransport, 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")
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultHost := opts.DefaultTCPHost
|
|
|
|
if tlsOptions != nil {
|
|
|
|
defaultHost = opts.DefaultTLSHost
|
|
|
|
}
|
|
|
|
|
|
|
|
host, err = opts.ParseHost(defaultHost, host)
|
|
|
|
return
|
|
|
|
}
|
2015-12-16 20:32:17 -05:00
|
|
|
|
|
|
|
func newClientTransport(tlsOptions *tlsconfig.Options) (*http.Transport, error) {
|
|
|
|
if tlsOptions == nil {
|
|
|
|
return &http.Transport{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := tlsconfig.Client(*tlsOptions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &http.Transport{
|
|
|
|
TLSClientConfig: config,
|
|
|
|
}, nil
|
|
|
|
}
|