github.com/walkingsparrow/docker@v1.4.2-0.20151218153551-b708a2249bfa/api/client/cli.go (about)

     1  package client
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io"
     7  	"net/http"
     8  	"os"
     9  	"runtime"
    10  
    11  	"github.com/docker/docker/api"
    12  	"github.com/docker/docker/api/client/lib"
    13  	"github.com/docker/docker/cli"
    14  	"github.com/docker/docker/cliconfig"
    15  	"github.com/docker/docker/dockerversion"
    16  	"github.com/docker/docker/opts"
    17  	"github.com/docker/docker/pkg/term"
    18  	"github.com/docker/docker/pkg/tlsconfig"
    19  )
    20  
    21  // DockerCli represents the docker command line client.
    22  // Instances of the client can be returned from NewDockerCli.
    23  type DockerCli struct {
    24  	// initializing closure
    25  	init func() error
    26  
    27  	// configFile has the client configuration file
    28  	configFile *cliconfig.ConfigFile
    29  	// in holds the input stream and closer (io.ReadCloser) for the client.
    30  	in io.ReadCloser
    31  	// out holds the output stream (io.Writer) for the client.
    32  	out io.Writer
    33  	// err holds the error stream (io.Writer) for the client.
    34  	err io.Writer
    35  	// keyFile holds the key file as a string.
    36  	keyFile string
    37  	// inFd holds the file descriptor of the client's STDIN (if valid).
    38  	inFd uintptr
    39  	// outFd holds file descriptor of the client's STDOUT (if valid).
    40  	outFd uintptr
    41  	// isTerminalIn indicates whether the client's STDIN is a TTY
    42  	isTerminalIn bool
    43  	// isTerminalOut indicates whether the client's STDOUT is a TTY
    44  	isTerminalOut bool
    45  	// client is the http client that performs all API operations
    46  	client apiClient
    47  }
    48  
    49  // Initialize calls the init function that will setup the configuration for the client
    50  // such as the TLS, tcp and other parameters used to run the client.
    51  func (cli *DockerCli) Initialize() error {
    52  	if cli.init == nil {
    53  		return nil
    54  	}
    55  	return cli.init()
    56  }
    57  
    58  // CheckTtyInput checks if we are trying to attach to a container tty
    59  // from a non-tty client input stream, and if so, returns an error.
    60  func (cli *DockerCli) CheckTtyInput(attachStdin, ttyMode bool) error {
    61  	// In order to attach to a container tty, input stream for the client must
    62  	// be a tty itself: redirecting or piping the client standard input is
    63  	// incompatible with `docker run -t`, `docker exec -t` or `docker attach`.
    64  	if ttyMode && attachStdin && !cli.isTerminalIn {
    65  		return errors.New("cannot enable tty mode on non tty input")
    66  	}
    67  	return nil
    68  }
    69  
    70  // PsFormat returns the format string specified in the configuration.
    71  // String contains columns and format specification, for example {{ID}\t{{Name}}.
    72  func (cli *DockerCli) PsFormat() string {
    73  	return cli.configFile.PsFormat
    74  }
    75  
    76  // NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.
    77  // The key file, protocol (i.e. unix) and address are passed in as strings, along with the tls.Config. If the tls.Config
    78  // is set the client scheme will be set to https.
    79  // The client will be given a 32-second timeout (see https://github.com/docker/docker/pull/8035).
    80  func NewDockerCli(in io.ReadCloser, out, err io.Writer, clientFlags *cli.ClientFlags) *DockerCli {
    81  	cli := &DockerCli{
    82  		in:      in,
    83  		out:     out,
    84  		err:     err,
    85  		keyFile: clientFlags.Common.TrustKey,
    86  	}
    87  
    88  	cli.init = func() error {
    89  		clientFlags.PostParse()
    90  		configFile, e := cliconfig.Load(cliconfig.ConfigDir())
    91  		if e != nil {
    92  			fmt.Fprintf(cli.err, "WARNING: Error loading config file:%v\n", e)
    93  		}
    94  		cli.configFile = configFile
    95  
    96  		host, err := getServerHost(clientFlags.Common.Hosts, clientFlags.Common.TLSOptions)
    97  		if err != nil {
    98  			return err
    99  		}
   100  
   101  		customHeaders := cli.configFile.HTTPHeaders
   102  		if customHeaders == nil {
   103  			customHeaders = map[string]string{}
   104  		}
   105  		customHeaders["User-Agent"] = "Docker-Client/" + dockerversion.Version + " (" + runtime.GOOS + ")"
   106  
   107  		verStr := string(api.DefaultVersion)
   108  		if tmpStr := os.Getenv("DOCKER_API_VERSION"); tmpStr != "" {
   109  			verStr = tmpStr
   110  		}
   111  
   112  		clientTransport, err := newClientTransport(clientFlags.Common.TLSOptions)
   113  		if err != nil {
   114  			return err
   115  		}
   116  
   117  		client, err := lib.NewClient(host, verStr, clientTransport, customHeaders)
   118  		if err != nil {
   119  			return err
   120  		}
   121  		cli.client = client
   122  
   123  		if cli.in != nil {
   124  			cli.inFd, cli.isTerminalIn = term.GetFdInfo(cli.in)
   125  		}
   126  		if cli.out != nil {
   127  			cli.outFd, cli.isTerminalOut = term.GetFdInfo(cli.out)
   128  		}
   129  
   130  		return nil
   131  	}
   132  
   133  	return cli
   134  }
   135  
   136  func getServerHost(hosts []string, tlsOptions *tlsconfig.Options) (host string, err error) {
   137  	switch len(hosts) {
   138  	case 0:
   139  		host = os.Getenv("DOCKER_HOST")
   140  	case 1:
   141  		host = hosts[0]
   142  	default:
   143  		return "", errors.New("Please specify only one -H")
   144  	}
   145  
   146  	defaultHost := opts.DefaultTCPHost
   147  	if tlsOptions != nil {
   148  		defaultHost = opts.DefaultTLSHost
   149  	}
   150  
   151  	host, err = opts.ParseHost(defaultHost, host)
   152  	return
   153  }
   154  
   155  func newClientTransport(tlsOptions *tlsconfig.Options) (*http.Transport, error) {
   156  	if tlsOptions == nil {
   157  		return &http.Transport{}, nil
   158  	}
   159  
   160  	config, err := tlsconfig.Client(*tlsOptions)
   161  	if err != nil {
   162  		return nil, err
   163  	}
   164  	return &http.Transport{
   165  		TLSClientConfig: config,
   166  	}, nil
   167  }