cli.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 dindicates whether the client's STDOUT is a TTY
  51. isTerminalOut bool
  52. // transport holds the client transport instance.
  53. transport *http.Transport
  54. }
  55. func (cli *DockerCli) Initialize() error {
  56. if cli.init == nil {
  57. return nil
  58. }
  59. return cli.init()
  60. }
  61. // CheckTtyInput checks if we are trying to attach to a container tty
  62. // from a non-tty client input stream, and if so, returns an error.
  63. func (cli *DockerCli) CheckTtyInput(attachStdin, ttyMode bool) error {
  64. // In order to attach to a container tty, input stream for the client must
  65. // be a tty itself: redirecting or piping the client standard input is
  66. // incompatible with `docker run -t`, `docker exec -t` or `docker attach`.
  67. if ttyMode && attachStdin && !cli.isTerminalIn {
  68. return errors.New("cannot enable tty mode on non tty input")
  69. }
  70. return nil
  71. }
  72. func (cli *DockerCli) PsFormat() string {
  73. return cli.configFile.PsFormat
  74. }
  75. // NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.
  76. // The key file, protocol (i.e. unix) and address are passed in as strings, along with the tls.Config. If the tls.Config
  77. // is set the client scheme will be set to https.
  78. // The client will be given a 32-second timeout (see https://github.com/docker/docker/pull/8035).
  79. func NewDockerCli(in io.ReadCloser, out, err io.Writer, clientFlags *cli.ClientFlags) *DockerCli {
  80. cli := &DockerCli{
  81. in: in,
  82. out: out,
  83. err: err,
  84. keyFile: clientFlags.Common.TrustKey,
  85. }
  86. cli.init = func() error {
  87. clientFlags.PostParse()
  88. hosts := clientFlags.Common.Hosts
  89. switch len(hosts) {
  90. case 0:
  91. defaultHost := os.Getenv("DOCKER_HOST")
  92. if defaultHost == "" {
  93. defaultHost = opts.DefaultHost
  94. }
  95. defaultHost, err := opts.ValidateHost(defaultHost)
  96. if err != nil {
  97. return err
  98. }
  99. hosts = []string{defaultHost}
  100. case 1:
  101. // only accept one host to talk to
  102. default:
  103. return errors.New("Please specify only one -H")
  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. }