2014-03-29 03:21:55 +04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2014-12-06 03:50:56 +03:00
|
|
|
"errors"
|
2014-03-29 03:21:55 +04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-12-17 04:32:17 +03:00
|
|
|
"net/http"
|
2015-05-05 07:18:28 +03:00
|
|
|
"os"
|
2015-12-03 07:53:06 +03:00
|
|
|
"runtime"
|
2014-03-29 03:21:55 +04:00
|
|
|
|
2015-12-14 20:06:42 +03:00
|
|
|
"github.com/docker/docker/api"
|
2015-05-05 07:18:28 +03:00
|
|
|
"github.com/docker/docker/cli"
|
2015-04-22 15:06:58 +03:00
|
|
|
"github.com/docker/docker/cliconfig"
|
2015-12-03 07:53:06 +03:00
|
|
|
"github.com/docker/docker/dockerversion"
|
2015-05-05 07:18:28 +03:00
|
|
|
"github.com/docker/docker/opts"
|
2014-07-25 02:19:50 +04:00
|
|
|
"github.com/docker/docker/pkg/term"
|
2016-01-05 03:05:26 +03:00
|
|
|
"github.com/docker/engine-api/client"
|
2015-12-30 03:27:12 +03:00
|
|
|
"github.com/docker/go-connections/tlsconfig"
|
2014-03-29 03:21:55 +04:00
|
|
|
)
|
|
|
|
|
2015-04-14 16:14:33 +03:00
|
|
|
// DockerCli represents the docker command line client.
|
|
|
|
// Instances of the client can be returned from NewDockerCli.
|
2014-08-10 08:31:59 +04:00
|
|
|
type DockerCli struct {
|
2015-05-05 07:18:28 +03:00
|
|
|
// initializing closure
|
|
|
|
init func() error
|
|
|
|
|
2015-04-22 15:06:58 +03:00
|
|
|
// configFile has the client configuration file
|
|
|
|
configFile *cliconfig.ConfigFile
|
2015-04-14 16:14:33 +03: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 17:35:48 +04:00
|
|
|
inFd uintptr
|
2015-04-14 16:14:33 +03:00
|
|
|
// outFd holds file descriptor of the client's STDOUT (if valid).
|
2014-09-10 17:35:48 +04:00
|
|
|
outFd uintptr
|
2015-04-14 16:14:33 +03:00
|
|
|
// isTerminalIn indicates whether the client's STDIN is a TTY
|
2014-09-10 17:35:48 +04:00
|
|
|
isTerminalIn bool
|
2015-09-10 15:28:38 +03:00
|
|
|
// isTerminalOut indicates whether the client's STDOUT is a TTY
|
2014-09-10 17:35:48 +04:00
|
|
|
isTerminalOut bool
|
2015-12-03 07:53:06 +03:00
|
|
|
// client is the http client that performs all API operations
|
2016-01-08 03:47:12 +03:00
|
|
|
client client.APIClient
|
2014-08-10 08:31:59 +04:00
|
|
|
}
|
|
|
|
|
2015-07-21 03:55:30 +03: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 07:18:28 +03:00
|
|
|
func (cli *DockerCli) Initialize() error {
|
|
|
|
if cli.init == nil {
|
|
|
|
return nil
|
2014-03-29 03:21:55 +04:00
|
|
|
}
|
2015-05-05 07:18:28 +03:00
|
|
|
return cli.init()
|
2014-03-29 03:21:55 +04:00
|
|
|
}
|
|
|
|
|
2015-04-22 09:45:18 +03: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-06 03:50:56 +03: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-21 03:55:30 +03:00
|
|
|
// PsFormat returns the format string specified in the configuration.
|
2015-12-18 16:03:41 +03:00
|
|
|
// String contains columns and format specification, for example {{ID}}\t{{Name}}.
|
2015-05-02 00:23:27 +03:00
|
|
|
func (cli *DockerCli) PsFormat() string {
|
|
|
|
return cli.configFile.PsFormat
|
|
|
|
}
|
|
|
|
|
2015-12-18 16:03:41 +03: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 16:14:33 +03: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 07:18:28 +03: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 17:35:48 +04:00
|
|
|
}
|
|
|
|
|
2015-05-05 07:18:28 +03:00
|
|
|
cli.init = func() error {
|
|
|
|
clientFlags.PostParse()
|
2015-12-03 07:53:06 +03: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 07:18:28 +03:00
|
|
|
}
|
2015-12-03 07:53:06 +03:00
|
|
|
cli.configFile = configFile
|
2014-10-13 06:26:42 +04:00
|
|
|
|
2015-12-03 07:53:06 +03:00
|
|
|
host, err := getServerHost(clientFlags.Common.Hosts, clientFlags.Common.TLSOptions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-10-19 16:17:37 +03:00
|
|
|
}
|
|
|
|
|
2015-12-03 07:53:06 +03:00
|
|
|
customHeaders := cli.configFile.HTTPHeaders
|
|
|
|
if customHeaders == nil {
|
|
|
|
customHeaders = map[string]string{}
|
2015-08-21 16:28:49 +03:00
|
|
|
}
|
2015-12-03 07:53:06 +03:00
|
|
|
customHeaders["User-Agent"] = "Docker-Client/" + dockerversion.Version + " (" + runtime.GOOS + ")"
|
2015-08-21 16:28:49 +03:00
|
|
|
|
2015-12-18 23:28:17 +03:00
|
|
|
verStr := api.DefaultVersion.String()
|
2015-09-01 00:45:27 +03:00
|
|
|
if tmpStr := os.Getenv("DOCKER_API_VERSION"); tmpStr != "" {
|
|
|
|
verStr = tmpStr
|
|
|
|
}
|
|
|
|
|
2015-12-17 04:32:17 +03:00
|
|
|
clientTransport, err := newClientTransport(clientFlags.Common.TLSOptions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-05 03:05:26 +03:00
|
|
|
client, err := client.NewClient(host, verStr, clientTransport, customHeaders)
|
2015-12-03 07:53:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-05-05 07:18:28 +03:00
|
|
|
}
|
2015-12-03 07:53:06 +03:00
|
|
|
cli.client = client
|
2015-06-30 23:59:51 +03:00
|
|
|
|
2015-05-05 07:18:28 +03: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-29 03:21:55 +04:00
|
|
|
}
|
2015-05-05 07:18:28 +03:00
|
|
|
|
|
|
|
return cli
|
2014-03-29 03:21:55 +04:00
|
|
|
}
|
2015-12-03 07:53:06 +03: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-17 04:32:17 +03: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
|
|
|
|
}
|