cli.go 4.7 KB

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