cli.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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/cliconfig/credentials"
  13. "github.com/docker/docker/dockerversion"
  14. "github.com/docker/docker/opts"
  15. "github.com/docker/docker/pkg/term"
  16. "github.com/docker/engine-api/client"
  17. "github.com/docker/go-connections/sockets"
  18. "github.com/docker/go-connections/tlsconfig"
  19. )
  20. // DockerCli represents the docker command line client.
  21. // Instances of the client can be returned from NewDockerCli.
  22. type DockerCli struct {
  23. // initializing closure
  24. init func() error
  25. // configFile has the client configuration file
  26. configFile *cliconfig.ConfigFile
  27. // in holds the input stream and closer (io.ReadCloser) for the client.
  28. in io.ReadCloser
  29. // out holds the output stream (io.Writer) for the client.
  30. out io.Writer
  31. // err holds the error stream (io.Writer) for the client.
  32. err io.Writer
  33. // keyFile holds the key file as a string.
  34. keyFile string
  35. // inFd holds the file descriptor of the client's STDIN (if valid).
  36. inFd uintptr
  37. // outFd holds file descriptor of the client's STDOUT (if valid).
  38. outFd uintptr
  39. // isTerminalIn indicates whether the client's STDIN is a TTY
  40. isTerminalIn bool
  41. // isTerminalOut indicates whether the client's STDOUT is a TTY
  42. isTerminalOut bool
  43. // client is the http client that performs all API operations
  44. client client.APIClient
  45. // state holds the terminal state
  46. state *term.State
  47. }
  48. // Initialize calls the init function that will setup the configuration for the client
  49. // such as the TLS, tcp and other parameters used to run the client.
  50. func (cli *DockerCli) Initialize() error {
  51. if cli.init == nil {
  52. return nil
  53. }
  54. return cli.init()
  55. }
  56. // CheckTtyInput checks if we are trying to attach to a container tty
  57. // from a non-tty client input stream, and if so, returns an error.
  58. func (cli *DockerCli) CheckTtyInput(attachStdin, ttyMode bool) error {
  59. // In order to attach to a container tty, input stream for the client must
  60. // be a tty itself: redirecting or piping the client standard input is
  61. // incompatible with `docker run -t`, `docker exec -t` or `docker attach`.
  62. if ttyMode && attachStdin && !cli.isTerminalIn {
  63. return errors.New("cannot enable tty mode on non tty input")
  64. }
  65. return nil
  66. }
  67. // PsFormat returns the format string specified in the configuration.
  68. // String contains columns and format specification, for example {{ID}}\t{{Name}}.
  69. func (cli *DockerCli) PsFormat() string {
  70. return cli.configFile.PsFormat
  71. }
  72. // ImagesFormat returns the format string specified in the configuration.
  73. // String contains columns and format specification, for example {{ID}}\t{{Name}}.
  74. func (cli *DockerCli) ImagesFormat() string {
  75. return cli.configFile.ImagesFormat
  76. }
  77. func (cli *DockerCli) setRawTerminal() error {
  78. if cli.isTerminalIn && os.Getenv("NORAW") == "" {
  79. state, err := term.SetRawTerminal(cli.inFd)
  80. if err != nil {
  81. return err
  82. }
  83. cli.state = state
  84. }
  85. return nil
  86. }
  87. func (cli *DockerCli) restoreTerminal(in io.Closer) error {
  88. if cli.state != nil {
  89. term.RestoreTerminal(cli.inFd, cli.state)
  90. }
  91. // WARNING: DO NOT REMOVE THE OS CHECK !!!
  92. // For some reason this Close call blocks on darwin..
  93. // As the client exists right after, simply discard the close
  94. // until we find a better solution.
  95. if in != nil && runtime.GOOS != "darwin" {
  96. return in.Close()
  97. }
  98. return nil
  99. }
  100. // NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.
  101. // The key file, protocol (i.e. unix) and address are passed in as strings, along with the tls.Config. If the tls.Config
  102. // is set the client scheme will be set to https.
  103. // The client will be given a 32-second timeout (see https://github.com/docker/docker/pull/8035).
  104. func NewDockerCli(in io.ReadCloser, out, err io.Writer, clientFlags *cli.ClientFlags) *DockerCli {
  105. cli := &DockerCli{
  106. in: in,
  107. out: out,
  108. err: err,
  109. keyFile: clientFlags.Common.TrustKey,
  110. }
  111. cli.init = func() error {
  112. clientFlags.PostParse()
  113. configFile, e := cliconfig.Load(cliconfig.ConfigDir())
  114. if e != nil {
  115. fmt.Fprintf(cli.err, "WARNING: Error loading config file:%v\n", e)
  116. }
  117. if !configFile.ContainsAuth() {
  118. credentials.DetectDefaultStore(configFile)
  119. }
  120. cli.configFile = configFile
  121. host, err := getServerHost(clientFlags.Common.Hosts, clientFlags.Common.TLSOptions)
  122. if err != nil {
  123. return err
  124. }
  125. customHeaders := cli.configFile.HTTPHeaders
  126. if customHeaders == nil {
  127. customHeaders = map[string]string{}
  128. }
  129. customHeaders["User-Agent"] = clientUserAgent()
  130. verStr := api.DefaultVersion.String()
  131. if tmpStr := os.Getenv("DOCKER_API_VERSION"); tmpStr != "" {
  132. verStr = tmpStr
  133. }
  134. httpClient, err := newHTTPClient(host, clientFlags.Common.TLSOptions)
  135. if err != nil {
  136. return err
  137. }
  138. client, err := client.NewClient(host, verStr, httpClient, customHeaders)
  139. if err != nil {
  140. return err
  141. }
  142. cli.client = client
  143. if cli.in != nil {
  144. cli.inFd, cli.isTerminalIn = term.GetFdInfo(cli.in)
  145. }
  146. if cli.out != nil {
  147. cli.outFd, cli.isTerminalOut = term.GetFdInfo(cli.out)
  148. }
  149. return nil
  150. }
  151. return cli
  152. }
  153. func getServerHost(hosts []string, tlsOptions *tlsconfig.Options) (host string, err error) {
  154. switch len(hosts) {
  155. case 0:
  156. host = os.Getenv("DOCKER_HOST")
  157. case 1:
  158. host = hosts[0]
  159. default:
  160. return "", errors.New("Please specify only one -H")
  161. }
  162. host, err = opts.ParseHost(tlsOptions != nil, host)
  163. return
  164. }
  165. func newHTTPClient(host string, tlsOptions *tlsconfig.Options) (*http.Client, error) {
  166. if tlsOptions == nil {
  167. // let the api client configure the default transport.
  168. return nil, nil
  169. }
  170. config, err := tlsconfig.Client(*tlsOptions)
  171. if err != nil {
  172. return nil, err
  173. }
  174. tr := &http.Transport{
  175. TLSClientConfig: config,
  176. }
  177. proto, addr, _, err := client.ParseHost(host)
  178. if err != nil {
  179. return nil, err
  180. }
  181. sockets.ConfigureTransport(tr, proto, addr)
  182. return &http.Client{
  183. Transport: tr,
  184. }, nil
  185. }
  186. func clientUserAgent() string {
  187. return "Docker-Client/" + dockerversion.Version + " (" + runtime.GOOS + ")"
  188. }