cli.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/go-connections/sockets"
  19. "github.com/docker/go-connections/tlsconfig"
  20. )
  21. // DockerCli represents the docker command line client.
  22. // Instances of the client can be returned from NewDockerCli.
  23. type DockerCli struct {
  24. configFile *configfile.ConfigFile
  25. in *InStream
  26. out *OutStream
  27. err io.Writer
  28. keyFile string
  29. client client.APIClient
  30. }
  31. // Client returns the APIClient
  32. func (cli *DockerCli) Client() client.APIClient {
  33. return cli.client
  34. }
  35. // Out returns the writer used for stdout
  36. func (cli *DockerCli) Out() *OutStream {
  37. return cli.out
  38. }
  39. // Err returns the writer used for stderr
  40. func (cli *DockerCli) Err() io.Writer {
  41. return cli.err
  42. }
  43. // In returns the reader used for stdin
  44. func (cli *DockerCli) In() *InStream {
  45. return cli.in
  46. }
  47. // ConfigFile returns the ConfigFile
  48. func (cli *DockerCli) ConfigFile() *configfile.ConfigFile {
  49. return cli.configFile
  50. }
  51. func (cli *DockerCli) setRawTerminal() error {
  52. if err := cli.in.setRawTerminal(); err != nil {
  53. return err
  54. }
  55. return cli.out.setRawTerminal()
  56. }
  57. func (cli *DockerCli) restoreTerminal(in io.Closer) error {
  58. cli.in.restoreTerminal()
  59. cli.out.restoreTerminal()
  60. // WARNING: DO NOT REMOVE THE OS CHECK !!!
  61. // For some reason this Close call blocks on darwin..
  62. // As the client exists right after, simply discard the close
  63. // until we find a better solution.
  64. if in != nil && runtime.GOOS != "darwin" {
  65. return in.Close()
  66. }
  67. return nil
  68. }
  69. // Initialize the dockerCli runs initialization that must happen after command
  70. // line flags are parsed.
  71. func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions) (err error) {
  72. cli.configFile = LoadDefaultConfigFile(cli.err)
  73. cli.client, err = NewAPIClientFromFlags(opts.Common, cli.configFile)
  74. if err != nil {
  75. return err
  76. }
  77. if opts.Common.TrustKey == "" {
  78. cli.keyFile = filepath.Join(cliconfig.ConfigDir(), cliflags.DefaultTrustKeyFile)
  79. } else {
  80. cli.keyFile = opts.Common.TrustKey
  81. }
  82. return nil
  83. }
  84. // NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.
  85. func NewDockerCli(in io.ReadCloser, out, err io.Writer) *DockerCli {
  86. return &DockerCli{in: NewInStream(in), out: NewOutStream(out), err: err}
  87. }
  88. // LoadDefaultConfigFile attempts to load the default config file and returns
  89. // an initialized ConfigFile struct if none is found.
  90. func LoadDefaultConfigFile(err io.Writer) *configfile.ConfigFile {
  91. configFile, e := cliconfig.Load(cliconfig.ConfigDir())
  92. if e != nil {
  93. fmt.Fprintf(err, "WARNING: Error loading config file:%v\n", e)
  94. }
  95. if !configFile.ContainsAuth() {
  96. credentials.DetectDefaultStore(configFile)
  97. }
  98. return configFile
  99. }
  100. // NewAPIClientFromFlags creates a new APIClient from command line flags
  101. func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.ConfigFile) (client.APIClient, error) {
  102. host, err := getServerHost(opts.Hosts, opts.TLSOptions)
  103. if err != nil {
  104. return &client.Client{}, err
  105. }
  106. customHeaders := configFile.HTTPHeaders
  107. if customHeaders == nil {
  108. customHeaders = map[string]string{}
  109. }
  110. customHeaders["User-Agent"] = clientUserAgent()
  111. verStr := api.DefaultVersion
  112. if tmpStr := os.Getenv("DOCKER_API_VERSION"); tmpStr != "" {
  113. verStr = tmpStr
  114. }
  115. httpClient, err := newHTTPClient(host, opts.TLSOptions)
  116. if err != nil {
  117. return &client.Client{}, err
  118. }
  119. return client.NewClient(host, verStr, httpClient, customHeaders)
  120. }
  121. func getServerHost(hosts []string, tlsOptions *tlsconfig.Options) (host string, err error) {
  122. switch len(hosts) {
  123. case 0:
  124. host = os.Getenv("DOCKER_HOST")
  125. case 1:
  126. host = hosts[0]
  127. default:
  128. return "", errors.New("Please specify only one -H")
  129. }
  130. host, err = dopts.ParseHost(tlsOptions != nil, host)
  131. return
  132. }
  133. func newHTTPClient(host string, tlsOptions *tlsconfig.Options) (*http.Client, error) {
  134. if tlsOptions == nil {
  135. // let the api client configure the default transport.
  136. return nil, nil
  137. }
  138. config, err := tlsconfig.Client(*tlsOptions)
  139. if err != nil {
  140. return nil, err
  141. }
  142. tr := &http.Transport{
  143. TLSClientConfig: config,
  144. }
  145. proto, addr, _, err := client.ParseHost(host)
  146. if err != nil {
  147. return nil, err
  148. }
  149. sockets.ConfigureTransport(tr, proto, addr)
  150. return &http.Client{
  151. Transport: tr,
  152. }, nil
  153. }
  154. func clientUserAgent() string {
  155. return "Docker-Client/" + dockerversion.Version + " (" + runtime.GOOS + ")"
  156. }