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-08-18 17:23:10 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-11-02 20:43:32 -04:00
|
|
|
"github.com/docker/docker/api/types/versions"
|
2016-12-25 14:31:52 -05:00
|
|
|
cliconfig "github.com/docker/docker/cli/config"
|
|
|
|
"github.com/docker/docker/cli/config/configfile"
|
|
|
|
"github.com/docker/docker/cli/config/credentials"
|
2016-04-21 17:51:28 -04:00
|
|
|
cliflags "github.com/docker/docker/cli/flags"
|
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-11-17 13:54:10 -05:00
|
|
|
"github.com/spf13/cobra"
|
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
|
|
|
|
}
|
|
|
|
|
2016-11-17 13:54:10 -05:00
|
|
|
// ShowHelp shows the command help.
|
|
|
|
func (cli *DockerCli) ShowHelp(cmd *cobra.Command, args []string) error {
|
|
|
|
cmd.SetOutput(cli.err)
|
|
|
|
cmd.HelpFunc()(cmd, args)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-31 19:49:32 -04:00
|
|
|
// ConfigFile returns the ConfigFile
|
|
|
|
func (cli *DockerCli) ConfigFile() *configfile.ConfigFile {
|
|
|
|
return cli.configFile
|
|
|
|
}
|
|
|
|
|
2016-08-18 17:23:10 -04:00
|
|
|
// GetAllCredentials returns all of the credentials stored in all of the
|
|
|
|
// configured credential stores.
|
|
|
|
func (cli *DockerCli) GetAllCredentials() (map[string]types.AuthConfig, error) {
|
|
|
|
auths := make(map[string]types.AuthConfig)
|
|
|
|
for registry := range cli.configFile.CredentialHelpers {
|
|
|
|
helper := cli.CredentialsStore(registry)
|
|
|
|
newAuths, err := helper.GetAll()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
addAll(auths, newAuths)
|
|
|
|
}
|
|
|
|
defaultStore := cli.CredentialsStore("")
|
|
|
|
newAuths, err := defaultStore.GetAll()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
addAll(auths, newAuths)
|
|
|
|
return auths, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func addAll(to, from map[string]types.AuthConfig) {
|
|
|
|
for reg, ac := range from {
|
|
|
|
to[reg] = ac
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-09 10:49:52 -04:00
|
|
|
// CredentialsStore returns a new credentials store based
|
2016-08-18 17:23:10 -04:00
|
|
|
// on the settings provided in the configuration file. Empty string returns
|
|
|
|
// the default credential store.
|
|
|
|
func (cli *DockerCli) CredentialsStore(serverAddress string) credentials.Store {
|
|
|
|
if helper := getConfiguredCredentialStore(cli.configFile, serverAddress); helper != "" {
|
|
|
|
return credentials.NewNativeStore(cli.configFile, helper)
|
2016-09-09 10:49:52 -04:00
|
|
|
}
|
|
|
|
return credentials.NewFileStore(cli.configFile)
|
|
|
|
}
|
|
|
|
|
2016-08-18 17:23:10 -04:00
|
|
|
// getConfiguredCredentialStore returns the credential helper configured for the
|
|
|
|
// given registry, the default credsStore, or the empty string if neither are
|
|
|
|
// configured.
|
|
|
|
func getConfiguredCredentialStore(c *configfile.ConfigFile, serverAddress string) string {
|
|
|
|
if c.CredentialHelpers != nil && serverAddress != "" {
|
|
|
|
if helper, exists := c.CredentialHelpers[serverAddress]; exists {
|
|
|
|
return helper
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c.CredentialsStore
|
|
|
|
}
|
|
|
|
|
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 == "" {
|
2016-12-25 14:31:52 -05:00
|
|
|
cli.keyFile = filepath.Join(cliconfig.Dir(), cliflags.DefaultTrustKeyFile)
|
2016-06-22 18:36:51 -04:00
|
|
|
} 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 {
|
2016-12-25 14:31:52 -05:00
|
|
|
configFile, e := cliconfig.Load(cliconfig.Dir())
|
2016-04-19 12:59:48 -04:00
|
|
|
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 + ")"
|
|
|
|
}
|