2016-09-08 13:11:39 -04:00
|
|
|
package command
|
2014-03-28 19:21:55 -04:00
|
|
|
|
|
|
|
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"
|
2016-06-22 18:36:51 -04:00
|
|
|
"path/filepath"
|
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"
|
2016-11-02 20:43:32 -04:00
|
|
|
"github.com/docker/docker/api/types/versions"
|
2016-04-21 17:51:28 -04:00
|
|
|
cliflags "github.com/docker/docker/cli/flags"
|
2015-04-22 08:06:58 -04:00
|
|
|
"github.com/docker/docker/cliconfig"
|
2016-04-21 18:37:08 -04:00
|
|
|
"github.com/docker/docker/cliconfig/configfile"
|
2016-02-07 19:55:17 -05:00
|
|
|
"github.com/docker/docker/cliconfig/credentials"
|
2016-09-06 14:46:37 -04:00
|
|
|
"github.com/docker/docker/client"
|
2015-12-02 23:53:06 -05:00
|
|
|
"github.com/docker/docker/dockerversion"
|
2016-06-22 13:08:04 -04:00
|
|
|
dopts "github.com/docker/docker/opts"
|
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"
|
2016-10-06 10:09:54 -04:00
|
|
|
"golang.org/x/net/context"
|
2014-03-28 19:21:55 -04:00
|
|
|
)
|
|
|
|
|
2016-08-29 14:45:29 -04:00
|
|
|
// Streams is an interface which exposes the standard input and output streams
|
|
|
|
type Streams interface {
|
|
|
|
In() *InStream
|
|
|
|
Out() *OutStream
|
|
|
|
Err() io.Writer
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-11-02 20:43:32 -04:00
|
|
|
configFile *configfile.ConfigFile
|
|
|
|
in *InStream
|
|
|
|
out *OutStream
|
|
|
|
err io.Writer
|
|
|
|
keyFile string
|
|
|
|
client client.APIClient
|
|
|
|
hasExperimental bool
|
|
|
|
defaultVersion string
|
2016-10-06 10:09:54 -04:00
|
|
|
}
|
|
|
|
|
2016-11-02 20:43:32 -04:00
|
|
|
// HasExperimental returns true if experimental features are accessible.
|
2016-10-06 10:09:54 -04:00
|
|
|
func (cli *DockerCli) HasExperimental() bool {
|
2016-11-02 20:43:32 -04:00
|
|
|
return cli.hasExperimental
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultVersion returns api.defaultVersion of DOCKER_API_VERSION if specified.
|
|
|
|
func (cli *DockerCli) DefaultVersion() string {
|
|
|
|
return cli.defaultVersion
|
2014-08-10 00:31:59 -04:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:59:48 -04:00
|
|
|
// Client returns the APIClient
|
|
|
|
func (cli *DockerCli) Client() client.APIClient {
|
|
|
|
return cli.client
|
|
|
|
}
|
|
|
|
|
|
|
|
// Out returns the writer used for stdout
|
2016-08-29 11:11:29 -04:00
|
|
|
func (cli *DockerCli) Out() *OutStream {
|
2016-04-19 12:59:48 -04:00
|
|
|
return cli.out
|
|
|
|
}
|
|
|
|
|
|
|
|
// Err returns the writer used for stderr
|
|
|
|
func (cli *DockerCli) Err() io.Writer {
|
|
|
|
return cli.err
|
|
|
|
}
|
|
|
|
|
2016-05-31 19:49:32 -04:00
|
|
|
// In returns the reader used for stdin
|
2016-08-29 11:52:05 -04:00
|
|
|
func (cli *DockerCli) In() *InStream {
|
2016-05-31 19:49:32 -04:00
|
|
|
return cli.in
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigFile returns the ConfigFile
|
|
|
|
func (cli *DockerCli) ConfigFile() *configfile.ConfigFile {
|
|
|
|
return cli.configFile
|
|
|
|
}
|
|
|
|
|
2016-09-09 10:49:52 -04:00
|
|
|
// CredentialsStore returns a new credentials store based
|
|
|
|
// on the settings provided in the configuration file.
|
|
|
|
func (cli *DockerCli) CredentialsStore() credentials.Store {
|
|
|
|
if cli.configFile.CredentialsStore != "" {
|
|
|
|
return credentials.NewNativeStore(cli.configFile)
|
|
|
|
}
|
|
|
|
return credentials.NewFileStore(cli.configFile)
|
|
|
|
}
|
|
|
|
|
2016-06-22 13:08:04 -04:00
|
|
|
// Initialize the dockerCli runs initialization that must happen after command
|
|
|
|
// line flags are parsed.
|
2016-08-29 13:36:29 -04:00
|
|
|
func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions) error {
|
2016-06-22 13:08:04 -04:00
|
|
|
cli.configFile = LoadDefaultConfigFile(cli.err)
|
|
|
|
|
2016-08-29 13:36:29 -04:00
|
|
|
var err error
|
2016-08-29 11:11:29 -04:00
|
|
|
cli.client, err = NewAPIClientFromFlags(opts.Common, cli.configFile)
|
2016-06-22 13:08:04 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-02 20:43:32 -04:00
|
|
|
|
|
|
|
cli.defaultVersion = cli.client.ClientVersion()
|
|
|
|
|
2016-06-22 18:36:51 -04:00
|
|
|
if opts.Common.TrustKey == "" {
|
|
|
|
cli.keyFile = filepath.Join(cliconfig.ConfigDir(), cliflags.DefaultTrustKeyFile)
|
|
|
|
} else {
|
|
|
|
cli.keyFile = opts.Common.TrustKey
|
|
|
|
}
|
|
|
|
|
2016-11-02 20:43:32 -04:00
|
|
|
if ping, err := cli.client.Ping(context.Background()); err == nil {
|
|
|
|
cli.hasExperimental = ping.Experimental
|
|
|
|
|
|
|
|
// since the new header was added in 1.25, assume server is 1.24 if header is not present.
|
|
|
|
if ping.APIVersion == "" {
|
|
|
|
ping.APIVersion = "1.24"
|
|
|
|
}
|
|
|
|
|
|
|
|
// if server version is lower than the current cli, downgrade
|
|
|
|
if versions.LessThan(ping.APIVersion, cli.client.ClientVersion()) {
|
|
|
|
cli.client.UpdateClientVersion(ping.APIVersion)
|
|
|
|
}
|
|
|
|
}
|
2016-06-22 13:08:04 -04:00
|
|
|
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.
|
2016-06-22 18:36:51 -04:00
|
|
|
func NewDockerCli(in io.ReadCloser, out, err io.Writer) *DockerCli {
|
2016-08-29 11:52:05 -04:00
|
|
|
return &DockerCli{in: NewInStream(in), out: NewOutStream(out), err: err}
|
2014-03-28 19:21:55 -04:00
|
|
|
}
|
2015-12-02 23:53:06 -05:00
|
|
|
|
2016-04-19 12:59:48 -04: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 13:08:04 -04:00
|
|
|
func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.ConfigFile) (client.APIClient, error) {
|
|
|
|
host, err := getServerHost(opts.Hosts, opts.TLSOptions)
|
2016-04-19 12:59:48 -04:00
|
|
|
if err != nil {
|
|
|
|
return &client.Client{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
customHeaders := configFile.HTTPHeaders
|
|
|
|
if customHeaders == nil {
|
|
|
|
customHeaders = map[string]string{}
|
|
|
|
}
|
2016-08-29 14:45:29 -04:00
|
|
|
customHeaders["User-Agent"] = UserAgent()
|
2016-04-19 12:59:48 -04:00
|
|
|
|
|
|
|
verStr := api.DefaultVersion
|
|
|
|
if tmpStr := os.Getenv("DOCKER_API_VERSION"); tmpStr != "" {
|
|
|
|
verStr = tmpStr
|
|
|
|
}
|
|
|
|
|
2016-06-22 13:08:04 -04:00
|
|
|
httpClient, err := newHTTPClient(host, opts.TLSOptions)
|
2016-04-19 12:59:48 -04:00
|
|
|
if err != nil {
|
|
|
|
return &client.Client{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.NewClient(host, verStr, httpClient, customHeaders)
|
|
|
|
}
|
|
|
|
|
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-06-22 13:08:04 -04:00
|
|
|
host, err = dopts.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
|
|
|
|
2016-08-29 14:45:29 -04:00
|
|
|
// UserAgent returns the user agent string used for making API requests
|
|
|
|
func UserAgent() string {
|
2016-03-18 17:42:40 -04:00
|
|
|
return "Docker-Client/" + dockerversion.Version + " (" + runtime.GOOS + ")"
|
|
|
|
}
|