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"
|
2016-06-23 01:36:51 +03:00
|
|
|
"path/filepath"
|
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"
|
2016-04-22 00:51:28 +03:00
|
|
|
cliflags "github.com/docker/docker/cli/flags"
|
2015-04-22 15:06:58 +03:00
|
|
|
"github.com/docker/docker/cliconfig"
|
2016-04-22 01:37:08 +03:00
|
|
|
"github.com/docker/docker/cliconfig/configfile"
|
2016-02-08 03:55:17 +03:00
|
|
|
"github.com/docker/docker/cliconfig/credentials"
|
2015-12-03 07:53:06 +03:00
|
|
|
"github.com/docker/docker/dockerversion"
|
2016-06-22 20:08:04 +03:00
|
|
|
dopts "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"
|
2016-02-04 02:41:26 +03:00
|
|
|
"github.com/docker/go-connections/sockets"
|
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
|
2016-04-22 01:37:08 +03:00
|
|
|
configFile *configfile.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
|
2016-06-23 02:34:01 +03:00
|
|
|
// state holds the terminal input state
|
|
|
|
inState *term.State
|
|
|
|
// outState holds the terminal output state
|
|
|
|
outState *term.State
|
2014-08-10 08:31:59 +04:00
|
|
|
}
|
|
|
|
|
2016-04-19 19:59:48 +03:00
|
|
|
// Client returns the APIClient
|
|
|
|
func (cli *DockerCli) Client() client.APIClient {
|
|
|
|
return cli.client
|
|
|
|
}
|
|
|
|
|
|
|
|
// Out returns the writer used for stdout
|
|
|
|
func (cli *DockerCli) Out() io.Writer {
|
|
|
|
return cli.out
|
|
|
|
}
|
|
|
|
|
|
|
|
// Err returns the writer used for stderr
|
|
|
|
func (cli *DockerCli) Err() io.Writer {
|
|
|
|
return cli.err
|
|
|
|
}
|
|
|
|
|
2016-06-01 02:49:32 +03:00
|
|
|
// In returns the reader used for stdin
|
|
|
|
func (cli *DockerCli) In() io.ReadCloser {
|
|
|
|
return cli.in
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigFile returns the ConfigFile
|
|
|
|
func (cli *DockerCli) ConfigFile() *configfile.ConfigFile {
|
|
|
|
return cli.configFile
|
|
|
|
}
|
|
|
|
|
2016-06-20 16:27:56 +03:00
|
|
|
// IsTerminalIn returns true if the clients stdin is a TTY
|
|
|
|
func (cli *DockerCli) IsTerminalIn() bool {
|
|
|
|
return cli.isTerminalIn
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsTerminalOut returns true if the clients stdout is a TTY
|
2016-06-01 02:49:32 +03:00
|
|
|
func (cli *DockerCli) IsTerminalOut() bool {
|
|
|
|
return cli.isTerminalOut
|
|
|
|
}
|
|
|
|
|
2016-06-01 08:19:13 +03:00
|
|
|
// OutFd returns the fd for the stdout stream
|
|
|
|
func (cli *DockerCli) OutFd() uintptr {
|
|
|
|
return cli.outFd
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-05-25 20:19:17 +03:00
|
|
|
eText := "the input device is not a TTY"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return errors.New(eText + ". If you are using mintty, try prefixing the command with 'winpty'")
|
|
|
|
}
|
|
|
|
return errors.New(eText)
|
2014-12-06 03:50:56 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-21 00:18:36 +03:00
|
|
|
func (cli *DockerCli) setRawTerminal() error {
|
2016-06-23 02:34:01 +03:00
|
|
|
if os.Getenv("NORAW") == "" {
|
|
|
|
if cli.isTerminalIn {
|
|
|
|
state, err := term.SetRawTerminal(cli.inFd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cli.inState = state
|
|
|
|
}
|
|
|
|
if cli.isTerminalOut {
|
|
|
|
state, err := term.SetRawTerminalOutput(cli.outFd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cli.outState = state
|
2016-01-21 00:18:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *DockerCli) restoreTerminal(in io.Closer) error {
|
2016-06-23 02:34:01 +03:00
|
|
|
if cli.inState != nil {
|
|
|
|
term.RestoreTerminal(cli.inFd, cli.inState)
|
|
|
|
}
|
|
|
|
if cli.outState != nil {
|
|
|
|
term.RestoreTerminal(cli.outFd, cli.outState)
|
2016-01-21 00:18:36 +03:00
|
|
|
}
|
2016-02-02 04:28:40 +03: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-21 00:18:36 +03:00
|
|
|
return in.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-22 20:08:04 +03:00
|
|
|
// Initialize the dockerCli runs initialization that must happen after command
|
|
|
|
// line flags are parsed.
|
|
|
|
func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions) error {
|
|
|
|
cli.configFile = LoadDefaultConfigFile(cli.err)
|
|
|
|
|
|
|
|
client, err := NewAPIClientFromFlags(opts.Common, cli.configFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cli.client = client
|
|
|
|
|
|
|
|
if cli.in != nil {
|
|
|
|
cli.inFd, cli.isTerminalIn = term.GetFdInfo(cli.in)
|
|
|
|
}
|
|
|
|
if cli.out != nil {
|
|
|
|
cli.outFd, cli.isTerminalOut = term.GetFdInfo(cli.out)
|
|
|
|
}
|
2016-06-23 01:36:51 +03:00
|
|
|
|
|
|
|
if opts.Common.TrustKey == "" {
|
|
|
|
cli.keyFile = filepath.Join(cliconfig.ConfigDir(), cliflags.DefaultTrustKeyFile)
|
|
|
|
} else {
|
|
|
|
cli.keyFile = opts.Common.TrustKey
|
|
|
|
}
|
|
|
|
|
2016-06-22 20:08:04 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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.
|
2016-06-23 01:36:51 +03:00
|
|
|
func NewDockerCli(in io.ReadCloser, out, err io.Writer) *DockerCli {
|
|
|
|
return &DockerCli{
|
2016-06-22 20:08:04 +03:00
|
|
|
in: in,
|
|
|
|
out: out,
|
|
|
|
err: err,
|
2014-09-10 17:35:48 +04:00
|
|
|
}
|
2014-03-29 03:21:55 +04:00
|
|
|
}
|
2015-12-03 07:53:06 +03:00
|
|
|
|
2016-04-19 19:59:48 +03:00
|
|
|
// LoadDefaultConfigFile attempts to load the default config file and returns
|
|
|
|
// an initialized ConfigFile struct if none is found.
|
|
|
|
func LoadDefaultConfigFile(err io.Writer) *configfile.ConfigFile {
|
|
|
|
configFile, e := cliconfig.Load(cliconfig.ConfigDir())
|
|
|
|
if e != nil {
|
|
|
|
fmt.Fprintf(err, "WARNING: Error loading config file:%v\n", e)
|
|
|
|
}
|
|
|
|
if !configFile.ContainsAuth() {
|
|
|
|
credentials.DetectDefaultStore(configFile)
|
|
|
|
}
|
|
|
|
return configFile
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAPIClientFromFlags creates a new APIClient from command line flags
|
2016-06-22 20:08:04 +03:00
|
|
|
func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.ConfigFile) (client.APIClient, error) {
|
|
|
|
host, err := getServerHost(opts.Hosts, opts.TLSOptions)
|
2016-04-19 19:59:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return &client.Client{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
customHeaders := configFile.HTTPHeaders
|
|
|
|
if customHeaders == nil {
|
|
|
|
customHeaders = map[string]string{}
|
|
|
|
}
|
|
|
|
customHeaders["User-Agent"] = clientUserAgent()
|
|
|
|
|
|
|
|
verStr := api.DefaultVersion
|
|
|
|
if tmpStr := os.Getenv("DOCKER_API_VERSION"); tmpStr != "" {
|
|
|
|
verStr = tmpStr
|
|
|
|
}
|
|
|
|
|
2016-06-22 20:08:04 +03:00
|
|
|
httpClient, err := newHTTPClient(host, opts.TLSOptions)
|
2016-04-19 19:59:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return &client.Client{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.NewClient(host, verStr, httpClient, customHeaders)
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2016-06-22 20:08:04 +03:00
|
|
|
host, err = dopts.ParseHost(tlsOptions != nil, host)
|
2015-12-03 07:53:06 +03:00
|
|
|
return
|
|
|
|
}
|
2015-12-17 04:32:17 +03:00
|
|
|
|
2016-02-04 02:41:26 +03:00
|
|
|
func newHTTPClient(host string, tlsOptions *tlsconfig.Options) (*http.Client, error) {
|
2015-12-17 04:32:17 +03:00
|
|
|
if tlsOptions == nil {
|
2016-02-04 02:41:26 +03:00
|
|
|
// let the api client configure the default transport.
|
|
|
|
return nil, nil
|
2015-12-17 04:32:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
config, err := tlsconfig.Client(*tlsOptions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-04 02:41:26 +03:00
|
|
|
tr := &http.Transport{
|
2015-12-17 04:32:17 +03:00
|
|
|
TLSClientConfig: config,
|
2016-02-04 02:41:26 +03: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-17 04:32:17 +03:00
|
|
|
}, nil
|
|
|
|
}
|
2016-03-19 00:42:40 +03:00
|
|
|
|
|
|
|
func clientUserAgent() string {
|
|
|
|
return "Docker-Client/" + dockerversion.Version + " (" + runtime.GOOS + ")"
|
|
|
|
}
|