2016-09-08 13:11:39 -04:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2017-08-02 17:35:03 -04:00
|
|
|
"net"
|
2016-09-08 13:11:39 -04:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
2017-08-02 17:35:03 -04:00
|
|
|
"time"
|
2016-09-08 13:11:39 -04:00
|
|
|
|
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"
|
|
|
|
cliflags "github.com/docker/cli/cli/flags"
|
2017-09-12 12:39:13 -04:00
|
|
|
"github.com/docker/cli/cli/trust"
|
2017-05-15 08:45:19 -04:00
|
|
|
dopts "github.com/docker/cli/opts"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api"
|
2017-05-08 13:51:30 -04:00
|
|
|
"github.com/docker/docker/client"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/go-connections/sockets"
|
|
|
|
"github.com/docker/go-connections/tlsconfig"
|
2017-09-20 16:13:03 -04:00
|
|
|
"github.com/docker/notary"
|
2017-09-12 12:39:13 -04:00
|
|
|
notaryclient "github.com/docker/notary/client"
|
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-06-27 10:31:38 -04:00
|
|
|
ServerInfo() ServerInfo
|
2017-09-12 12:39:13 -04:00
|
|
|
NotaryClient(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (notaryclient.Repository, error)
|
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-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 {
|
2017-06-12 14:36:49 -04:00
|
|
|
cli.configFile = cliconfig.LoadDefaultConfigFile(cli.err)
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
var err error
|
|
|
|
cli.client, err = NewAPIClientFromFlags(opts.Common, cli.configFile)
|
2017-02-25 15:17:23 -05:00
|
|
|
if tlsconfig.IsErrEncryptedKey(err) {
|
|
|
|
passRetriever := passphrase.PromptRetrieverWithInOut(cli.In(), cli.Out(), nil)
|
2017-09-20 16:13:03 -04:00
|
|
|
newClient := func(password string) (client.APIClient, error) {
|
|
|
|
opts.Common.TLSOptions.Passphrase = password
|
|
|
|
return NewAPIClientFromFlags(opts.Common, cli.configFile)
|
2017-02-25 15:17:23 -05:00
|
|
|
}
|
2017-09-20 16:13:03 -04:00
|
|
|
cli.client, err = getClientWithPassword(passRetriever, newClient)
|
2017-02-25 15:17:23 -05:00
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-20 16:13:03 -04:00
|
|
|
cli.initializeFromClient()
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-25 15:17:23 -05:00
|
|
|
|
2017-09-20 16:13:03 -04:00
|
|
|
func (cli *DockerCli) initializeFromClient() {
|
2016-11-02 20:43:32 -04:00
|
|
|
cli.defaultVersion = cli.client.ClientVersion()
|
|
|
|
|
2017-09-20 16:13:03 -04:00
|
|
|
ping, err := cli.client.Ping(context.Background())
|
|
|
|
if err != nil {
|
2017-06-14 16:25:24 -04:00
|
|
|
// Default to true if we fail to connect to daemon
|
|
|
|
cli.server = ServerInfo{HasExperimental: true}
|
2017-09-20 16:13:03 -04:00
|
|
|
|
|
|
|
if ping.APIVersion != "" {
|
|
|
|
cli.client.NegotiateAPIVersionPing(ping)
|
|
|
|
}
|
|
|
|
return
|
2016-11-02 20:43:32 -04:00
|
|
|
}
|
2017-03-14 17:53:29 -04:00
|
|
|
|
2017-09-20 16:13:03 -04:00
|
|
|
cli.server = ServerInfo{
|
|
|
|
HasExperimental: ping.Experimental,
|
|
|
|
OSType: ping.OSType,
|
|
|
|
}
|
|
|
|
cli.client.NegotiateAPIVersionPing(ping)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getClientWithPassword(passRetriever notary.PassRetriever, newClient func(password string) (client.APIClient, error)) (client.APIClient, error) {
|
|
|
|
for attempts := 0; ; attempts++ {
|
|
|
|
passwd, giveup, err := passRetriever("private", "encrypted TLS private", false, attempts)
|
|
|
|
if giveup || err != nil {
|
|
|
|
return nil, errors.Wrap(err, "private key is encrypted, but could not get passphrase")
|
|
|
|
}
|
|
|
|
|
|
|
|
apiclient, err := newClient(passwd)
|
|
|
|
if !tlsconfig.IsErrEncryptedKey(err) {
|
|
|
|
return apiclient, err
|
|
|
|
}
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2017-09-12 12:39:13 -04:00
|
|
|
// NotaryClient provides a Notary Repository to interact with signed metadata for an image
|
|
|
|
func (cli *DockerCli) NotaryClient(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (notaryclient.Repository, error) {
|
|
|
|
return trust.GetNotaryRepository(cli.In(), cli.Out(), UserAgent(), imgRefAndAuth.RepoInfo(), imgRefAndAuth.AuthConfig(), actions...)
|
|
|
|
}
|
|
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2017-10-12 11:44:03 -04:00
|
|
|
func getServerHost(hosts []string, tlsOptions *tlsconfig.Options) (string, error) {
|
|
|
|
var host string
|
2016-09-08 13:11:39 -04:00
|
|
|
switch len(hosts) {
|
|
|
|
case 0:
|
|
|
|
host = os.Getenv("DOCKER_HOST")
|
|
|
|
case 1:
|
|
|
|
host = hosts[0]
|
|
|
|
default:
|
|
|
|
return "", errors.New("Please specify only one -H")
|
|
|
|
}
|
|
|
|
|
2017-10-12 11:44:03 -04:00
|
|
|
return dopts.ParseHost(tlsOptions != nil, host)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2017-08-02 17:35:03 -04:00
|
|
|
DialContext: (&net.Dialer{
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
}).DialContext,
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
proto, addr, _, err := client.ParseHost(host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sockets.ConfigureTransport(tr, proto, addr)
|
|
|
|
|
|
|
|
return &http.Client{
|
2017-05-16 11:48:00 -04:00
|
|
|
Transport: tr,
|
|
|
|
CheckRedirect: client.CheckRedirect,
|
2016-09-08 13:11:39 -04:00
|
|
|
}, 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
|
|
|
}
|