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