cli.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package client
  2. import (
  3. "crypto/tls"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "net/url"
  9. "os"
  10. "strings"
  11. "github.com/docker/docker/cli"
  12. "github.com/docker/docker/cliconfig"
  13. "github.com/docker/docker/opts"
  14. "github.com/docker/docker/pkg/sockets"
  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. // proto holds the client protocol i.e. unix.
  24. proto string
  25. // addr holds the client address.
  26. addr string
  27. // basePath holds the path to prepend to the requests
  28. basePath string
  29. // configFile has the client configuration file
  30. configFile *cliconfig.ConfigFile
  31. // in holds the input stream and closer (io.ReadCloser) for the client.
  32. in io.ReadCloser
  33. // out holds the output stream (io.Writer) for the client.
  34. out io.Writer
  35. // err holds the error stream (io.Writer) for the client.
  36. err io.Writer
  37. // keyFile holds the key file as a string.
  38. keyFile string
  39. // tlsConfig holds the TLS configuration for the client, and will
  40. // set the scheme to https in NewDockerCli if present.
  41. tlsConfig *tls.Config
  42. // scheme holds the scheme of the client i.e. https.
  43. scheme string
  44. // inFd holds the file descriptor of the client's STDIN (if valid).
  45. inFd uintptr
  46. // outFd holds file descriptor of the client's STDOUT (if valid).
  47. outFd uintptr
  48. // isTerminalIn indicates whether the client's STDIN is a TTY
  49. isTerminalIn bool
  50. // isTerminalOut indicates whether the client's STDOUT is a TTY
  51. isTerminalOut bool
  52. // transport holds the client transport instance.
  53. transport *http.Transport
  54. }
  55. // Initialize calls the init function that will setup the configuration for the client
  56. // such as the TLS, tcp and other parameters used to run the client.
  57. func (cli *DockerCli) Initialize() error {
  58. if cli.init == nil {
  59. return nil
  60. }
  61. return cli.init()
  62. }
  63. // CheckTtyInput checks if we are trying to attach to a container tty
  64. // from a non-tty client input stream, and if so, returns an error.
  65. func (cli *DockerCli) CheckTtyInput(attachStdin, ttyMode bool) error {
  66. // In order to attach to a container tty, input stream for the client must
  67. // be a tty itself: redirecting or piping the client standard input is
  68. // incompatible with `docker run -t`, `docker exec -t` or `docker attach`.
  69. if ttyMode && attachStdin && !cli.isTerminalIn {
  70. return errors.New("cannot enable tty mode on non tty input")
  71. }
  72. return nil
  73. }
  74. // PsFormat returns the format string specified in the configuration.
  75. // String contains columns and format specification, for example {{ID}\t{{Name}}.
  76. func (cli *DockerCli) PsFormat() string {
  77. return cli.configFile.PsFormat
  78. }
  79. // NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.
  80. // The key file, protocol (i.e. unix) and address are passed in as strings, along with the tls.Config. If the tls.Config
  81. // is set the client scheme will be set to https.
  82. // The client will be given a 32-second timeout (see https://github.com/docker/docker/pull/8035).
  83. func NewDockerCli(in io.ReadCloser, out, err io.Writer, clientFlags *cli.ClientFlags) *DockerCli {
  84. cli := &DockerCli{
  85. in: in,
  86. out: out,
  87. err: err,
  88. keyFile: clientFlags.Common.TrustKey,
  89. }
  90. cli.init = func() error {
  91. clientFlags.PostParse()
  92. hosts := clientFlags.Common.Hosts
  93. switch len(hosts) {
  94. case 0:
  95. hosts = []string{os.Getenv("DOCKER_HOST")}
  96. case 1:
  97. // only accept one host to talk to
  98. default:
  99. return errors.New("Please specify only one -H")
  100. }
  101. defaultHost := opts.DefaultTCPHost
  102. if clientFlags.Common.TLSOptions != nil {
  103. defaultHost = opts.DefaultTLSHost
  104. }
  105. var e error
  106. if hosts[0], e = opts.ParseHost(defaultHost, hosts[0]); e != nil {
  107. return e
  108. }
  109. protoAddrParts := strings.SplitN(hosts[0], "://", 2)
  110. cli.proto, cli.addr = protoAddrParts[0], protoAddrParts[1]
  111. if cli.proto == "tcp" {
  112. // error is checked in pkg/parsers already
  113. parsed, _ := url.Parse("tcp://" + cli.addr)
  114. cli.addr = parsed.Host
  115. cli.basePath = parsed.Path
  116. }
  117. if clientFlags.Common.TLSOptions != nil {
  118. cli.scheme = "https"
  119. var e error
  120. cli.tlsConfig, e = tlsconfig.Client(*clientFlags.Common.TLSOptions)
  121. if e != nil {
  122. return e
  123. }
  124. } else {
  125. cli.scheme = "http"
  126. }
  127. if cli.in != nil {
  128. cli.inFd, cli.isTerminalIn = term.GetFdInfo(cli.in)
  129. }
  130. if cli.out != nil {
  131. cli.outFd, cli.isTerminalOut = term.GetFdInfo(cli.out)
  132. }
  133. // The transport is created here for reuse during the client session.
  134. cli.transport = &http.Transport{
  135. TLSClientConfig: cli.tlsConfig,
  136. }
  137. sockets.ConfigureTCPTransport(cli.transport, cli.proto, cli.addr)
  138. configFile, e := cliconfig.Load(cliconfig.ConfigDir())
  139. if e != nil {
  140. fmt.Fprintf(cli.err, "WARNING: Error loading config file:%v\n", e)
  141. }
  142. cli.configFile = configFile
  143. return nil
  144. }
  145. return cli
  146. }