2016-09-09 01:11:39 +08:00
|
|
|
package command
|
2014-03-29 07:21:55 +08:00
|
|
|
|
|
|
|
|
import (
|
2014-12-06 08:50:56 +08:00
|
|
|
"errors"
|
2014-03-29 07:21:55 +08:00
|
|
|
"fmt"
|
|
|
|
|
"io"
|
2015-12-17 09:32:17 +08:00
|
|
|
"net/http"
|
2015-05-05 12:18:28 +08:00
|
|
|
"os"
|
2016-06-23 06:36:51 +08:00
|
|
|
"path/filepath"
|
2015-12-03 12:53:06 +08:00
|
|
|
"runtime"
|
2014-03-29 07:21:55 +08:00
|
|
|
|
2015-12-15 01:06:42 +08:00
|
|
|
"github.com/docker/docker/api"
|
2016-04-22 05:51:28 +08:00
|
|
|
cliflags "github.com/docker/docker/cli/flags"
|
2015-04-22 20:06:58 +08:00
|
|
|
"github.com/docker/docker/cliconfig"
|
2016-04-22 06:37:08 +08:00
|
|
|
"github.com/docker/docker/cliconfig/configfile"
|
2016-02-08 08:55:17 +08:00
|
|
|
"github.com/docker/docker/cliconfig/credentials"
|
2016-09-07 02:46:37 +08:00
|
|
|
"github.com/docker/docker/client"
|
2015-12-03 12:53:06 +08:00
|
|
|
"github.com/docker/docker/dockerversion"
|
2016-06-23 01:08:04 +08:00
|
|
|
dopts "github.com/docker/docker/opts"
|
2016-02-04 07:41:26 +08:00
|
|
|
"github.com/docker/go-connections/sockets"
|
2015-12-30 08:27:12 +08:00
|
|
|
"github.com/docker/go-connections/tlsconfig"
|
2014-03-29 07:21:55 +08:00
|
|
|
)
|
|
|
|
|
|
2016-08-30 02:45:29 +08: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 21:14:33 +08:00
|
|
|
// DockerCli represents the docker command line client.
|
|
|
|
|
// Instances of the client can be returned from NewDockerCli.
|
2014-08-10 12:31:59 +08:00
|
|
|
type DockerCli struct {
|
2016-04-22 06:37:08 +08:00
|
|
|
configFile *configfile.ConfigFile
|
2016-08-29 23:52:05 +08:00
|
|
|
in *InStream
|
|
|
|
|
out *OutStream
|
|
|
|
|
err io.Writer
|
|
|
|
|
keyFile string
|
|
|
|
|
client client.APIClient
|
2014-08-10 12:31:59 +08:00
|
|
|
}
|
|
|
|
|
|
2016-04-20 00:59:48 +08:00
|
|
|
// Client returns the APIClient
|
|
|
|
|
func (cli *DockerCli) Client() client.APIClient {
|
|
|
|
|
return cli.client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Out returns the writer used for stdout
|
2016-08-29 23:11:29 +08:00
|
|
|
func (cli *DockerCli) Out() *OutStream {
|
2016-04-20 00:59:48 +08:00
|
|
|
return cli.out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Err returns the writer used for stderr
|
|
|
|
|
func (cli *DockerCli) Err() io.Writer {
|
|
|
|
|
return cli.err
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-01 07:49:32 +08:00
|
|
|
// In returns the reader used for stdin
|
2016-08-29 23:52:05 +08:00
|
|
|
func (cli *DockerCli) In() *InStream {
|
2016-06-01 07:49:32 +08:00
|
|
|
return cli.in
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ConfigFile returns the ConfigFile
|
|
|
|
|
func (cli *DockerCli) ConfigFile() *configfile.ConfigFile {
|
|
|
|
|
return cli.configFile
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-09 22:49:52 +08: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-23 01:08:04 +08:00
|
|
|
// Initialize the dockerCli runs initialization that must happen after command
|
|
|
|
|
// line flags are parsed.
|
2016-08-30 01:36:29 +08:00
|
|
|
func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions) error {
|
2016-06-23 01:08:04 +08:00
|
|
|
cli.configFile = LoadDefaultConfigFile(cli.err)
|
|
|
|
|
|
2016-08-30 01:36:29 +08:00
|
|
|
var err error
|
2016-08-29 23:11:29 +08:00
|
|
|
cli.client, err = NewAPIClientFromFlags(opts.Common, cli.configFile)
|
2016-06-23 01:08:04 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2016-06-23 06:36:51 +08:00
|
|
|
if opts.Common.TrustKey == "" {
|
|
|
|
|
cli.keyFile = filepath.Join(cliconfig.ConfigDir(), cliflags.DefaultTrustKeyFile)
|
|
|
|
|
} else {
|
|
|
|
|
cli.keyFile = opts.Common.TrustKey
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-23 01:08:04 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-14 21:14:33 +08:00
|
|
|
// NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.
|
2016-06-23 06:36:51 +08:00
|
|
|
func NewDockerCli(in io.ReadCloser, out, err io.Writer) *DockerCli {
|
2016-08-29 23:52:05 +08:00
|
|
|
return &DockerCli{in: NewInStream(in), out: NewOutStream(out), err: err}
|
2014-03-29 07:21:55 +08:00
|
|
|
}
|
2015-12-03 12:53:06 +08:00
|
|
|
|
2016-04-20 00:59:48 +08: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-23 01:08:04 +08:00
|
|
|
func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.ConfigFile) (client.APIClient, error) {
|
|
|
|
|
host, err := getServerHost(opts.Hosts, opts.TLSOptions)
|
2016-04-20 00:59:48 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return &client.Client{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
customHeaders := configFile.HTTPHeaders
|
|
|
|
|
if customHeaders == nil {
|
|
|
|
|
customHeaders = map[string]string{}
|
|
|
|
|
}
|
2016-08-30 02:45:29 +08:00
|
|
|
customHeaders["User-Agent"] = UserAgent()
|
2016-04-20 00:59:48 +08:00
|
|
|
|
|
|
|
|
verStr := api.DefaultVersion
|
|
|
|
|
if tmpStr := os.Getenv("DOCKER_API_VERSION"); tmpStr != "" {
|
|
|
|
|
verStr = tmpStr
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-23 01:08:04 +08:00
|
|
|
httpClient, err := newHTTPClient(host, opts.TLSOptions)
|
2016-04-20 00:59:48 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return &client.Client{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return client.NewClient(host, verStr, httpClient, customHeaders)
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-03 12:53:06 +08: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-23 01:08:04 +08:00
|
|
|
host, err = dopts.ParseHost(tlsOptions != nil, host)
|
2015-12-03 12:53:06 +08:00
|
|
|
return
|
|
|
|
|
}
|
2015-12-17 09:32:17 +08:00
|
|
|
|
2016-02-04 07:41:26 +08:00
|
|
|
func newHTTPClient(host string, tlsOptions *tlsconfig.Options) (*http.Client, error) {
|
2015-12-17 09:32:17 +08:00
|
|
|
if tlsOptions == nil {
|
2016-02-04 07:41:26 +08:00
|
|
|
// let the api client configure the default transport.
|
|
|
|
|
return nil, nil
|
2015-12-17 09:32:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
config, err := tlsconfig.Client(*tlsOptions)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2016-02-04 07:41:26 +08:00
|
|
|
tr := &http.Transport{
|
2015-12-17 09:32:17 +08:00
|
|
|
TLSClientConfig: config,
|
2016-02-04 07:41:26 +08: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 09:32:17 +08:00
|
|
|
}, nil
|
|
|
|
|
}
|
2016-03-19 05:42:40 +08:00
|
|
|
|
2016-08-30 02:45:29 +08:00
|
|
|
// UserAgent returns the user agent string used for making API requests
|
|
|
|
|
func UserAgent() string {
|
2016-03-19 05:42:40 +08:00
|
|
|
return "Docker-Client/" + dockerversion.Version + " (" + runtime.GOOS + ")"
|
|
|
|
|
}
|