cli.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. // ImagesFormat returns the format string specified in the configuration.
  69. // String contains columns and format specification, for example {{ID}}\t{{Name}}.
  70. func (cli *DockerCli) ImagesFormat() string {
  71. return cli.configFile.ImagesFormat
  72. }
  73. // NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.
  74. // The key file, protocol (i.e. unix) and address are passed in as strings, along with the tls.Config. If the tls.Config
  75. // is set the client scheme will be set to https.
  76. // The client will be given a 32-second timeout (see https://github.com/docker/docker/pull/8035).
  77. func NewDockerCli(in io.ReadCloser, out, err io.Writer, clientFlags *cli.ClientFlags) *DockerCli {
  78. cli := &DockerCli{
  79. in: in,
  80. out: out,
  81. err: err,
  82. keyFile: clientFlags.Common.TrustKey,
  83. }
  84. cli.init = func() error {
  85. clientFlags.PostParse()
  86. configFile, e := cliconfig.Load(cliconfig.ConfigDir())
  87. if e != nil {
  88. fmt.Fprintf(cli.err, "WARNING: Error loading config file:%v\n", e)
  89. }
  90. cli.configFile = configFile
  91. host, err := getServerHost(clientFlags.Common.Hosts, clientFlags.Common.TLSOptions)
  92. if err != nil {
  93. return err
  94. }
  95. customHeaders := cli.configFile.HTTPHeaders
  96. if customHeaders == nil {
  97. customHeaders = map[string]string{}
  98. }
  99. customHeaders["User-Agent"] = "Docker-Client/" + dockerversion.Version + " (" + runtime.GOOS + ")"
  100. verStr := api.DefaultVersion.String()
  101. if tmpStr := os.Getenv("DOCKER_API_VERSION"); tmpStr != "" {
  102. verStr = tmpStr
  103. }
  104. clientTransport, err := newClientTransport(clientFlags.Common.TLSOptions)
  105. if err != nil {
  106. return err
  107. }
  108. client, err := lib.NewClient(host, verStr, clientTransport, customHeaders)
  109. if err != nil {
  110. return err
  111. }
  112. cli.client = client
  113. if cli.in != nil {
  114. cli.inFd, cli.isTerminalIn = term.GetFdInfo(cli.in)
  115. }
  116. if cli.out != nil {
  117. cli.outFd, cli.isTerminalOut = term.GetFdInfo(cli.out)
  118. }
  119. return nil
  120. }
  121. return cli
  122. }
  123. func getServerHost(hosts []string, tlsOptions *tlsconfig.Options) (host string, err error) {
  124. switch len(hosts) {
  125. case 0:
  126. host = os.Getenv("DOCKER_HOST")
  127. case 1:
  128. host = hosts[0]
  129. default:
  130. return "", errors.New("Please specify only one -H")
  131. }
  132. defaultHost := opts.DefaultTCPHost
  133. if tlsOptions != nil {
  134. defaultHost = opts.DefaultTLSHost
  135. }
  136. host, err = opts.ParseHost(defaultHost, host)
  137. return
  138. }
  139. func newClientTransport(tlsOptions *tlsconfig.Options) (*http.Transport, error) {
  140. if tlsOptions == nil {
  141. return &http.Transport{}, nil
  142. }
  143. config, err := tlsconfig.Client(*tlsOptions)
  144. if err != nil {
  145. return nil, err
  146. }
  147. return &http.Transport{
  148. TLSClientConfig: config,
  149. }, nil
  150. }