2016-09-08 13:11:39 -04:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
|
2017-05-09 12:38:23 -04:00
|
|
|
"github.com/docker/cli/cli"
|
2017-04-17 18:07:56 -04:00
|
|
|
cliconfig "github.com/docker/cli/cli/config"
|
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
|
|
|
"github.com/docker/cli/cli/config/credentials"
|
|
|
|
cliflags "github.com/docker/cli/cli/flags"
|
2016-09-08 13:11:39 -04: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"
|
2017-05-08 13:51:30 -04:00
|
|
|
"github.com/docker/docker/client"
|
2016-09-08 13:11:39 -04:00
|
|
|
dopts "github.com/docker/docker/opts"
|
|
|
|
"github.com/docker/go-connections/sockets"
|
|
|
|
"github.com/docker/go-connections/tlsconfig"
|
2017-02-25 15:17:23 -05:00
|
|
|
"github.com/docker/notary/passphrase"
|
2017-03-27 21:21:59 -04:00
|
|
|
"github.com/pkg/errors"
|
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"
|
2016-09-08 13:11:39 -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
|
|
|
|
}
|
|
|
|
|
2016-12-25 16:23:35 -05:00
|
|
|
// Cli represents the docker command line client.
|
|
|
|
type Cli interface {
|
|
|
|
Client() client.APIClient
|
|
|
|
Out() *OutStream
|
|
|
|
Err() io.Writer
|
|
|
|
In() *InStream
|
2017-03-30 20:21:14 -04:00
|
|
|
SetIn(in *InStream)
|
2016-11-07 00:54:40 -05:00
|
|
|
ConfigFile() *configfile.ConfigFile
|
2017-03-30 20:21:14 -04:00
|
|
|
CredentialsStore(serverAddress string) credentials.Store
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// DockerCli is an instance the docker command line client.
|
2016-09-08 13:11:39 -04:00
|
|
|
// Instances of the client can be returned from NewDockerCli.
|
|
|
|
type DockerCli struct {
|
2017-03-14 17:53:29 -04:00
|
|
|
configFile *configfile.ConfigFile
|
|
|
|
in *InStream
|
|
|
|
out *OutStream
|
|
|
|
err io.Writer
|
|
|
|
client client.APIClient
|
|
|
|
defaultVersion string
|
|
|
|
server ServerInfo
|
2016-10-06 10:09:54 -04:00
|
|
|
}
|
|
|
|
|
2017-03-14 17:53:29 -04:00
|
|
|
// DefaultVersion returns api.defaultVersion or DOCKER_API_VERSION if specified.
|
2016-11-02 20:43:32 -04:00
|
|
|
func (cli *DockerCli) DefaultVersion() string {
|
|
|
|
return cli.defaultVersion
|
2016-09-08 13:11:39 -04: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() *OutStream {
|
|
|
|
return cli.out
|
|
|
|
}
|
|
|
|
|
|
|
|
// Err returns the writer used for stderr
|
|
|
|
func (cli *DockerCli) Err() io.Writer {
|
|
|
|
return cli.err
|
|
|
|
}
|
|
|
|
|
2017-03-30 20:21:14 -04:00
|
|
|
// SetIn sets the reader used for stdin
|
|
|
|
func (cli *DockerCli) SetIn(in *InStream) {
|
|
|
|
cli.in = in
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
// In returns the reader used for stdin
|
|
|
|
func (cli *DockerCli) In() *InStream {
|
|
|
|
return cli.in
|
|
|
|
}
|
|
|
|
|
2016-11-17 13:54:10 -05:00
|
|
|
// ShowHelp shows the command help.
|
2017-05-03 17:58:52 -04:00
|
|
|
func ShowHelp(err io.Writer) func(*cobra.Command, []string) error {
|
|
|
|
return func(cmd *cobra.Command, args []string) error {
|
|
|
|
cmd.SetOutput(err)
|
|
|
|
cmd.HelpFunc()(cmd, args)
|
|
|
|
return nil
|
|
|
|
}
|
2016-11-17 13:54:10 -05:00
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
// ConfigFile returns the ConfigFile
|
|
|
|
func (cli *DockerCli) ConfigFile() *configfile.ConfigFile {
|
|
|
|
return cli.configFile
|
|
|
|
}
|
|
|
|
|
2017-03-14 17:53:29 -04:00
|
|
|
// ServerInfo returns the server version details for the host this client is
|
|
|
|
// connected to
|
|
|
|
func (cli *DockerCli) ServerInfo() ServerInfo {
|
|
|
|
return cli.server
|
|
|
|
}
|
|
|
|
|
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-09-08 13:11:39 -04: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)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
cli.client, err = NewAPIClientFromFlags(opts.Common, cli.configFile)
|
2017-02-25 15:17:23 -05:00
|
|
|
if tlsconfig.IsErrEncryptedKey(err) {
|
|
|
|
var (
|
|
|
|
passwd string
|
|
|
|
giveup bool
|
|
|
|
)
|
|
|
|
passRetriever := passphrase.PromptRetrieverWithInOut(cli.In(), cli.Out(), nil)
|
|
|
|
|
|
|
|
for attempts := 0; tlsconfig.IsErrEncryptedKey(err); attempts++ {
|
|
|
|
// some code and comments borrowed from notary/trustmanager/keystore.go
|
|
|
|
passwd, giveup, err = passRetriever("private", "encrypted TLS private", false, attempts)
|
|
|
|
// Check if the passphrase retriever got an error or if it is telling us to give up
|
|
|
|
if giveup || err != nil {
|
|
|
|
return errors.Wrap(err, "private key is encrypted, but could not get passphrase")
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.Common.TLSOptions.Passphrase = passwd
|
|
|
|
cli.client, err = NewAPIClientFromFlags(opts.Common, cli.configFile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-25 15:17:23 -05:00
|
|
|
|
2016-11-02 20:43:32 -04:00
|
|
|
cli.defaultVersion = cli.client.ClientVersion()
|
|
|
|
|
|
|
|
if ping, err := cli.client.Ping(context.Background()); err == nil {
|
2017-03-14 17:53:29 -04:00
|
|
|
cli.server = ServerInfo{
|
|
|
|
HasExperimental: ping.Experimental,
|
|
|
|
OSType: ping.OSType,
|
|
|
|
}
|
2016-11-02 20:43:32 -04:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
2017-03-14 17:53:29 -04:00
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-14 17:53:29 -04:00
|
|
|
// ServerInfo stores details about the supported features and platform of the
|
|
|
|
// server
|
|
|
|
type ServerInfo struct {
|
|
|
|
HasExperimental bool
|
|
|
|
OSType string
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
// NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.
|
|
|
|
func NewDockerCli(in io.ReadCloser, out, err io.Writer) *DockerCli {
|
|
|
|
return &DockerCli{in: NewInStream(in), out: NewOutStream(out), err: err}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-09-08 13:11:39 -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
|
|
|
|
func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.ConfigFile) (client.APIClient, error) {
|
|
|
|
host, err := getServerHost(opts.Hosts, opts.TLSOptions)
|
|
|
|
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-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
verStr := api.DefaultVersion
|
|
|
|
if tmpStr := os.Getenv("DOCKER_API_VERSION"); tmpStr != "" {
|
|
|
|
verStr = tmpStr
|
|
|
|
}
|
|
|
|
|
|
|
|
httpClient, err := newHTTPClient(host, opts.TLSOptions)
|
|
|
|
if err != nil {
|
|
|
|
return &client.Client{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.NewClient(host, verStr, httpClient, customHeaders)
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
host, err = dopts.ParseHost(tlsOptions != nil, host)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func newHTTPClient(host string, tlsOptions *tlsconfig.Options) (*http.Client, error) {
|
|
|
|
if tlsOptions == nil {
|
|
|
|
// let the api client configure the default transport.
|
|
|
|
return nil, nil
|
|
|
|
}
|
2017-03-09 13:45:15 -05:00
|
|
|
opts := *tlsOptions
|
|
|
|
opts.ExclusiveRootPools = true
|
|
|
|
config, err := tlsconfig.Client(opts)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tr := &http.Transport{
|
|
|
|
TLSClientConfig: config,
|
|
|
|
}
|
|
|
|
proto, addr, _, err := client.ParseHost(host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sockets.ConfigureTransport(tr, proto, addr)
|
|
|
|
|
|
|
|
return &http.Client{
|
|
|
|
Transport: tr,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-08-29 14:45:29 -04:00
|
|
|
// UserAgent returns the user agent string used for making API requests
|
|
|
|
func UserAgent() string {
|
2017-05-09 12:38:23 -04:00
|
|
|
return "Docker-Client/" + cli.Version + " (" + runtime.GOOS + ")"
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|