cli.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package client
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "os"
  8. "path/filepath"
  9. "runtime"
  10. "github.com/docker/docker/api"
  11. cliflags "github.com/docker/docker/cli/flags"
  12. "github.com/docker/docker/cliconfig"
  13. "github.com/docker/docker/cliconfig/configfile"
  14. "github.com/docker/docker/cliconfig/credentials"
  15. "github.com/docker/docker/client"
  16. "github.com/docker/docker/dockerversion"
  17. dopts "github.com/docker/docker/opts"
  18. "github.com/docker/docker/pkg/term"
  19. "github.com/docker/go-connections/sockets"
  20. "github.com/docker/go-connections/tlsconfig"
  21. )
  22. // DockerCli represents the docker command line client.
  23. // Instances of the client can be returned from NewDockerCli.
  24. type DockerCli struct {
  25. // initializing closure
  26. init func() error
  27. // configFile has the client configuration file
  28. configFile *configfile.ConfigFile
  29. // in holds the input stream and closer (io.ReadCloser) for the client.
  30. // TODO: remove
  31. in io.ReadCloser
  32. // err holds the error stream (io.Writer) for the client.
  33. err io.Writer
  34. // keyFile holds the key file as a string.
  35. keyFile string
  36. // inFd holds the file descriptor of the client's STDIN (if valid).
  37. // TODO: remove
  38. inFd uintptr
  39. // isTerminalIn indicates whether the client's STDIN is a TTY
  40. // TODO: remove
  41. isTerminalIn bool
  42. // client is the http client that performs all API operations
  43. client client.APIClient
  44. // inState holds the terminal input state
  45. // TODO: remove
  46. inState *term.State
  47. out *OutStream
  48. }
  49. // Client returns the APIClient
  50. func (cli *DockerCli) Client() client.APIClient {
  51. return cli.client
  52. }
  53. // Out returns the writer used for stdout
  54. func (cli *DockerCli) Out() *OutStream {
  55. return cli.out
  56. }
  57. // Err returns the writer used for stderr
  58. func (cli *DockerCli) Err() io.Writer {
  59. return cli.err
  60. }
  61. // In returns the reader used for stdin
  62. func (cli *DockerCli) In() io.ReadCloser {
  63. return cli.in
  64. }
  65. // ConfigFile returns the ConfigFile
  66. func (cli *DockerCli) ConfigFile() *configfile.ConfigFile {
  67. return cli.configFile
  68. }
  69. // IsTerminalIn returns true if the clients stdin is a TTY
  70. // TODO: remove
  71. func (cli *DockerCli) IsTerminalIn() bool {
  72. return cli.isTerminalIn
  73. }
  74. // CheckTtyInput checks if we are trying to attach to a container tty
  75. // from a non-tty client input stream, and if so, returns an error.
  76. func (cli *DockerCli) CheckTtyInput(attachStdin, ttyMode bool) error {
  77. // In order to attach to a container tty, input stream for the client must
  78. // be a tty itself: redirecting or piping the client standard input is
  79. // incompatible with `docker run -t`, `docker exec -t` or `docker attach`.
  80. if ttyMode && attachStdin && !cli.isTerminalIn {
  81. eText := "the input device is not a TTY"
  82. if runtime.GOOS == "windows" {
  83. return errors.New(eText + ". If you are using mintty, try prefixing the command with 'winpty'")
  84. }
  85. return errors.New(eText)
  86. }
  87. return nil
  88. }
  89. func (cli *DockerCli) setRawTerminal() error {
  90. if os.Getenv("NORAW") == "" {
  91. if cli.isTerminalIn {
  92. state, err := term.SetRawTerminal(cli.inFd)
  93. if err != nil {
  94. return err
  95. }
  96. cli.inState = state
  97. }
  98. if err := cli.out.setRawTerminal(); err != nil {
  99. return err
  100. }
  101. }
  102. return nil
  103. }
  104. func (cli *DockerCli) restoreTerminal(in io.Closer) error {
  105. if cli.inState != nil {
  106. term.RestoreTerminal(cli.inFd, cli.inState)
  107. }
  108. cli.out.restoreTerminal()
  109. // WARNING: DO NOT REMOVE THE OS CHECK !!!
  110. // For some reason this Close call blocks on darwin..
  111. // As the client exists right after, simply discard the close
  112. // until we find a better solution.
  113. if in != nil && runtime.GOOS != "darwin" {
  114. return in.Close()
  115. }
  116. return nil
  117. }
  118. // Initialize the dockerCli runs initialization that must happen after command
  119. // line flags are parsed.
  120. func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions) (err error) {
  121. cli.configFile = LoadDefaultConfigFile(cli.err)
  122. cli.client, err = NewAPIClientFromFlags(opts.Common, cli.configFile)
  123. if err != nil {
  124. return err
  125. }
  126. if cli.in != nil {
  127. cli.inFd, cli.isTerminalIn = term.GetFdInfo(cli.in)
  128. }
  129. if opts.Common.TrustKey == "" {
  130. cli.keyFile = filepath.Join(cliconfig.ConfigDir(), cliflags.DefaultTrustKeyFile)
  131. } else {
  132. cli.keyFile = opts.Common.TrustKey
  133. }
  134. return nil
  135. }
  136. // NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.
  137. func NewDockerCli(in io.ReadCloser, out, err io.Writer) *DockerCli {
  138. return &DockerCli{in: in, out: NewOutStream(out), err: err}
  139. }
  140. // LoadDefaultConfigFile attempts to load the default config file and returns
  141. // an initialized ConfigFile struct if none is found.
  142. func LoadDefaultConfigFile(err io.Writer) *configfile.ConfigFile {
  143. configFile, e := cliconfig.Load(cliconfig.ConfigDir())
  144. if e != nil {
  145. fmt.Fprintf(err, "WARNING: Error loading config file:%v\n", e)
  146. }
  147. if !configFile.ContainsAuth() {
  148. credentials.DetectDefaultStore(configFile)
  149. }
  150. return configFile
  151. }
  152. // NewAPIClientFromFlags creates a new APIClient from command line flags
  153. func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.ConfigFile) (client.APIClient, error) {
  154. host, err := getServerHost(opts.Hosts, opts.TLSOptions)
  155. if err != nil {
  156. return &client.Client{}, err
  157. }
  158. customHeaders := configFile.HTTPHeaders
  159. if customHeaders == nil {
  160. customHeaders = map[string]string{}
  161. }
  162. customHeaders["User-Agent"] = clientUserAgent()
  163. verStr := api.DefaultVersion
  164. if tmpStr := os.Getenv("DOCKER_API_VERSION"); tmpStr != "" {
  165. verStr = tmpStr
  166. }
  167. httpClient, err := newHTTPClient(host, opts.TLSOptions)
  168. if err != nil {
  169. return &client.Client{}, err
  170. }
  171. return client.NewClient(host, verStr, httpClient, customHeaders)
  172. }
  173. func getServerHost(hosts []string, tlsOptions *tlsconfig.Options) (host string, err error) {
  174. switch len(hosts) {
  175. case 0:
  176. host = os.Getenv("DOCKER_HOST")
  177. case 1:
  178. host = hosts[0]
  179. default:
  180. return "", errors.New("Please specify only one -H")
  181. }
  182. host, err = dopts.ParseHost(tlsOptions != nil, host)
  183. return
  184. }
  185. func newHTTPClient(host string, tlsOptions *tlsconfig.Options) (*http.Client, error) {
  186. if tlsOptions == nil {
  187. // let the api client configure the default transport.
  188. return nil, nil
  189. }
  190. config, err := tlsconfig.Client(*tlsOptions)
  191. if err != nil {
  192. return nil, err
  193. }
  194. tr := &http.Transport{
  195. TLSClientConfig: config,
  196. }
  197. proto, addr, _, err := client.ParseHost(host)
  198. if err != nil {
  199. return nil, err
  200. }
  201. sockets.ConfigureTransport(tr, proto, addr)
  202. return &http.Client{
  203. Transport: tr,
  204. }, nil
  205. }
  206. func clientUserAgent() string {
  207. return "Docker-Client/" + dockerversion.Version + " (" + runtime.GOOS + ")"
  208. }