cli.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. var e error
  102. if hosts[0], e = opts.ParseHost(hosts[0]); e != nil {
  103. return e
  104. }
  105. protoAddrParts := strings.SplitN(hosts[0], "://", 2)
  106. cli.proto, cli.addr = protoAddrParts[0], protoAddrParts[1]
  107. if cli.proto == "tcp" {
  108. // error is checked in pkg/parsers already
  109. parsed, _ := url.Parse("tcp://" + cli.addr)
  110. cli.addr = parsed.Host
  111. cli.basePath = parsed.Path
  112. }
  113. if clientFlags.Common.TLSOptions != nil {
  114. cli.scheme = "https"
  115. var e error
  116. cli.tlsConfig, e = tlsconfig.Client(*clientFlags.Common.TLSOptions)
  117. if e != nil {
  118. return e
  119. }
  120. } else {
  121. cli.scheme = "http"
  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. // The transport is created here for reuse during the client session.
  130. cli.transport = &http.Transport{
  131. TLSClientConfig: cli.tlsConfig,
  132. }
  133. sockets.ConfigureTCPTransport(cli.transport, cli.proto, cli.addr)
  134. configFile, e := cliconfig.Load(cliconfig.ConfigDir())
  135. if e != nil {
  136. fmt.Fprintf(cli.err, "WARNING: Error loading config file:%v\n", e)
  137. }
  138. cli.configFile = configFile
  139. return nil
  140. }
  141. return cli
  142. }