cli.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package client
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "os"
  8. "runtime"
  9. "github.com/docker/docker/api"
  10. "github.com/docker/docker/cli"
  11. "github.com/docker/docker/cliconfig"
  12. "github.com/docker/docker/dockerversion"
  13. "github.com/docker/docker/opts"
  14. "github.com/docker/docker/pkg/term"
  15. "github.com/docker/engine-api/client"
  16. "github.com/docker/go-connections/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 client.APIClient
  43. // state holds the terminal state
  44. state *term.State
  45. }
  46. // Initialize calls the init function that will setup the configuration for the client
  47. // such as the TLS, tcp and other parameters used to run the client.
  48. func (cli *DockerCli) Initialize() error {
  49. if cli.init == nil {
  50. return nil
  51. }
  52. return cli.init()
  53. }
  54. // CheckTtyInput checks if we are trying to attach to a container tty
  55. // from a non-tty client input stream, and if so, returns an error.
  56. func (cli *DockerCli) CheckTtyInput(attachStdin, ttyMode bool) error {
  57. // In order to attach to a container tty, input stream for the client must
  58. // be a tty itself: redirecting or piping the client standard input is
  59. // incompatible with `docker run -t`, `docker exec -t` or `docker attach`.
  60. if ttyMode && attachStdin && !cli.isTerminalIn {
  61. return errors.New("cannot enable tty mode on non tty input")
  62. }
  63. return nil
  64. }
  65. // PsFormat returns the format string specified in the configuration.
  66. // String contains columns and format specification, for example {{ID}}\t{{Name}}.
  67. func (cli *DockerCli) PsFormat() string {
  68. return cli.configFile.PsFormat
  69. }
  70. // ImagesFormat returns the format string specified in the configuration.
  71. // String contains columns and format specification, for example {{ID}}\t{{Name}}.
  72. func (cli *DockerCli) ImagesFormat() string {
  73. return cli.configFile.ImagesFormat
  74. }
  75. func (cli *DockerCli) setRawTerminal() error {
  76. if cli.isTerminalIn && os.Getenv("NORAW") == "" {
  77. state, err := term.SetRawTerminal(cli.inFd)
  78. if err != nil {
  79. return err
  80. }
  81. cli.state = state
  82. }
  83. return nil
  84. }
  85. func (cli *DockerCli) restoreTerminal(in io.Closer) error {
  86. if cli.state != nil {
  87. term.RestoreTerminal(cli.inFd, cli.state)
  88. }
  89. // WARNING: DO NOT REMOVE THE OS CHECK !!!
  90. // For some reason this Close call blocks on darwin..
  91. // As the client exists right after, simply discard the close
  92. // until we find a better solution.
  93. if in != nil && runtime.GOOS != "darwin" {
  94. return in.Close()
  95. }
  96. return nil
  97. }
  98. // NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.
  99. // The key file, protocol (i.e. unix) and address are passed in as strings, along with the tls.Config. If the tls.Config
  100. // is set the client scheme will be set to https.
  101. // The client will be given a 32-second timeout (see https://github.com/docker/docker/pull/8035).
  102. func NewDockerCli(in io.ReadCloser, out, err io.Writer, clientFlags *cli.ClientFlags) *DockerCli {
  103. cli := &DockerCli{
  104. in: in,
  105. out: out,
  106. err: err,
  107. keyFile: clientFlags.Common.TrustKey,
  108. }
  109. cli.init = func() error {
  110. clientFlags.PostParse()
  111. configFile, e := cliconfig.Load(cliconfig.ConfigDir())
  112. if e != nil {
  113. fmt.Fprintf(cli.err, "WARNING: Error loading config file:%v\n", e)
  114. }
  115. cli.configFile = configFile
  116. host, err := getServerHost(clientFlags.Common.Hosts, clientFlags.Common.TLSOptions)
  117. if err != nil {
  118. return err
  119. }
  120. customHeaders := cli.configFile.HTTPHeaders
  121. if customHeaders == nil {
  122. customHeaders = map[string]string{}
  123. }
  124. customHeaders["User-Agent"] = "Docker-Client/" + dockerversion.Version + " (" + runtime.GOOS + ")"
  125. verStr := api.DefaultVersion.String()
  126. if tmpStr := os.Getenv("DOCKER_API_VERSION"); tmpStr != "" {
  127. verStr = tmpStr
  128. }
  129. clientTransport, err := newClientTransport(clientFlags.Common.TLSOptions)
  130. if err != nil {
  131. return err
  132. }
  133. client, err := client.NewClient(host, verStr, clientTransport, customHeaders)
  134. if err != nil {
  135. return err
  136. }
  137. cli.client = client
  138. if cli.in != nil {
  139. cli.inFd, cli.isTerminalIn = term.GetFdInfo(cli.in)
  140. }
  141. if cli.out != nil {
  142. cli.outFd, cli.isTerminalOut = term.GetFdInfo(cli.out)
  143. }
  144. return nil
  145. }
  146. return cli
  147. }
  148. func getServerHost(hosts []string, tlsOptions *tlsconfig.Options) (host string, err error) {
  149. switch len(hosts) {
  150. case 0:
  151. host = os.Getenv("DOCKER_HOST")
  152. case 1:
  153. host = hosts[0]
  154. default:
  155. return "", errors.New("Please specify only one -H")
  156. }
  157. host, err = opts.ParseHost(tlsOptions != nil, host)
  158. return
  159. }
  160. func newClientTransport(tlsOptions *tlsconfig.Options) (*http.Transport, error) {
  161. if tlsOptions == nil {
  162. return &http.Transport{}, nil
  163. }
  164. config, err := tlsconfig.Client(*tlsOptions)
  165. if err != nil {
  166. return nil, err
  167. }
  168. return &http.Transport{
  169. TLSClientConfig: config,
  170. }, nil
  171. }