cli.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. if in != nil {
  90. return in.Close()
  91. }
  92. return nil
  93. }
  94. // NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.
  95. // The key file, protocol (i.e. unix) and address are passed in as strings, along with the tls.Config. If the tls.Config
  96. // is set the client scheme will be set to https.
  97. // The client will be given a 32-second timeout (see https://github.com/docker/docker/pull/8035).
  98. func NewDockerCli(in io.ReadCloser, out, err io.Writer, clientFlags *cli.ClientFlags) *DockerCli {
  99. cli := &DockerCli{
  100. in: in,
  101. out: out,
  102. err: err,
  103. keyFile: clientFlags.Common.TrustKey,
  104. }
  105. cli.init = func() error {
  106. clientFlags.PostParse()
  107. configFile, e := cliconfig.Load(cliconfig.ConfigDir())
  108. if e != nil {
  109. fmt.Fprintf(cli.err, "WARNING: Error loading config file:%v\n", e)
  110. }
  111. cli.configFile = configFile
  112. host, err := getServerHost(clientFlags.Common.Hosts, clientFlags.Common.TLSOptions)
  113. if err != nil {
  114. return err
  115. }
  116. customHeaders := cli.configFile.HTTPHeaders
  117. if customHeaders == nil {
  118. customHeaders = map[string]string{}
  119. }
  120. customHeaders["User-Agent"] = "Docker-Client/" + dockerversion.Version + " (" + runtime.GOOS + ")"
  121. verStr := api.DefaultVersion.String()
  122. if tmpStr := os.Getenv("DOCKER_API_VERSION"); tmpStr != "" {
  123. verStr = tmpStr
  124. }
  125. clientTransport, err := newClientTransport(clientFlags.Common.TLSOptions)
  126. if err != nil {
  127. return err
  128. }
  129. client, err := client.NewClient(host, verStr, clientTransport, customHeaders)
  130. if err != nil {
  131. return err
  132. }
  133. cli.client = client
  134. if cli.in != nil {
  135. cli.inFd, cli.isTerminalIn = term.GetFdInfo(cli.in)
  136. }
  137. if cli.out != nil {
  138. cli.outFd, cli.isTerminalOut = term.GetFdInfo(cli.out)
  139. }
  140. return nil
  141. }
  142. return cli
  143. }
  144. func getServerHost(hosts []string, tlsOptions *tlsconfig.Options) (host string, err error) {
  145. switch len(hosts) {
  146. case 0:
  147. host = os.Getenv("DOCKER_HOST")
  148. case 1:
  149. host = hosts[0]
  150. default:
  151. return "", errors.New("Please specify only one -H")
  152. }
  153. defaultHost := opts.DefaultTCPHost
  154. if tlsOptions != nil {
  155. defaultHost = opts.DefaultTLSHost
  156. }
  157. host, err = opts.ParseHost(defaultHost, 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. }