cli.go 5.0 KB

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