cli.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package client
  2. import (
  3. "crypto/tls"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "os"
  9. "runtime"
  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 *lib.Client
  43. // DEPRECATED OPTIONS TO MAKE THE CLIENT COMPILE
  44. // TODO: Remove
  45. // proto holds the client protocol i.e. unix.
  46. proto string
  47. // addr holds the client address.
  48. addr string
  49. // basePath holds the path to prepend to the requests
  50. basePath string
  51. // tlsConfig holds the TLS configuration for the client, and will
  52. // set the scheme to https in NewDockerCli if present.
  53. tlsConfig *tls.Config
  54. // scheme holds the scheme of the client i.e. https.
  55. scheme string
  56. // transport holds the client transport instance.
  57. transport *http.Transport
  58. }
  59. // Initialize calls the init function that will setup the configuration for the client
  60. // such as the TLS, tcp and other parameters used to run the client.
  61. func (cli *DockerCli) Initialize() error {
  62. if cli.init == nil {
  63. return nil
  64. }
  65. return cli.init()
  66. }
  67. // CheckTtyInput checks if we are trying to attach to a container tty
  68. // from a non-tty client input stream, and if so, returns an error.
  69. func (cli *DockerCli) CheckTtyInput(attachStdin, ttyMode bool) error {
  70. // In order to attach to a container tty, input stream for the client must
  71. // be a tty itself: redirecting or piping the client standard input is
  72. // incompatible with `docker run -t`, `docker exec -t` or `docker attach`.
  73. if ttyMode && attachStdin && !cli.isTerminalIn {
  74. return errors.New("cannot enable tty mode on non tty input")
  75. }
  76. return nil
  77. }
  78. // PsFormat returns the format string specified in the configuration.
  79. // String contains columns and format specification, for example {{ID}\t{{Name}}.
  80. func (cli *DockerCli) PsFormat() string {
  81. return cli.configFile.PsFormat
  82. }
  83. // NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.
  84. // The key file, protocol (i.e. unix) and address are passed in as strings, along with the tls.Config. If the tls.Config
  85. // is set the client scheme will be set to https.
  86. // The client will be given a 32-second timeout (see https://github.com/docker/docker/pull/8035).
  87. func NewDockerCli(in io.ReadCloser, out, err io.Writer, clientFlags *cli.ClientFlags) *DockerCli {
  88. cli := &DockerCli{
  89. in: in,
  90. out: out,
  91. err: err,
  92. keyFile: clientFlags.Common.TrustKey,
  93. }
  94. cli.init = func() error {
  95. clientFlags.PostParse()
  96. configFile, e := cliconfig.Load(cliconfig.ConfigDir())
  97. if e != nil {
  98. fmt.Fprintf(cli.err, "WARNING: Error loading config file:%v\n", e)
  99. }
  100. cli.configFile = configFile
  101. host, err := getServerHost(clientFlags.Common.Hosts, clientFlags.Common.TLSOptions)
  102. if err != nil {
  103. return err
  104. }
  105. customHeaders := cli.configFile.HTTPHeaders
  106. if customHeaders == nil {
  107. customHeaders = map[string]string{}
  108. }
  109. customHeaders["User-Agent"] = "Docker-Client/" + dockerversion.Version + " (" + runtime.GOOS + ")"
  110. client, err := lib.NewClient(host, clientFlags.Common.TLSOptions, customHeaders)
  111. if err != nil {
  112. return err
  113. }
  114. cli.client = client
  115. // FIXME: Deprecated, only to keep the old code running.
  116. cli.transport = client.HTTPClient.Transport.(*http.Transport)
  117. cli.basePath = client.BasePath
  118. cli.addr = client.Addr
  119. cli.scheme = client.Scheme
  120. if cli.in != nil {
  121. cli.inFd, cli.isTerminalIn = term.GetFdInfo(cli.in)
  122. }
  123. if cli.out != nil {
  124. cli.outFd, cli.isTerminalOut = term.GetFdInfo(cli.out)
  125. }
  126. return nil
  127. }
  128. return cli
  129. }
  130. func getServerHost(hosts []string, tlsOptions *tlsconfig.Options) (host string, err error) {
  131. switch len(hosts) {
  132. case 0:
  133. host = os.Getenv("DOCKER_HOST")
  134. case 1:
  135. host = hosts[0]
  136. default:
  137. return "", errors.New("Please specify only one -H")
  138. }
  139. defaultHost := opts.DefaultTCPHost
  140. if tlsOptions != nil {
  141. defaultHost = opts.DefaultTLSHost
  142. }
  143. host, err = opts.ParseHost(defaultHost, host)
  144. return
  145. }