cli.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package command
  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. "golang.org/x/net/context"
  21. )
  22. // Streams is an interface which exposes the standard input and output streams
  23. type Streams interface {
  24. In() *InStream
  25. Out() *OutStream
  26. Err() io.Writer
  27. }
  28. // DockerCli represents the docker command line client.
  29. // Instances of the client can be returned from NewDockerCli.
  30. type DockerCli struct {
  31. configFile *configfile.ConfigFile
  32. in *InStream
  33. out *OutStream
  34. err io.Writer
  35. keyFile string
  36. client client.APIClient
  37. hasExperimental *bool
  38. }
  39. // HasExperimental returns true if experimental features are accessible
  40. func (cli *DockerCli) HasExperimental() bool {
  41. if cli.hasExperimental == nil {
  42. if cli.client == nil {
  43. return false
  44. }
  45. enabled := false
  46. cli.hasExperimental = &enabled
  47. enabled, _ = cli.client.Ping(context.Background())
  48. }
  49. return *cli.hasExperimental
  50. }
  51. // Client returns the APIClient
  52. func (cli *DockerCli) Client() client.APIClient {
  53. return cli.client
  54. }
  55. // Out returns the writer used for stdout
  56. func (cli *DockerCli) Out() *OutStream {
  57. return cli.out
  58. }
  59. // Err returns the writer used for stderr
  60. func (cli *DockerCli) Err() io.Writer {
  61. return cli.err
  62. }
  63. // In returns the reader used for stdin
  64. func (cli *DockerCli) In() *InStream {
  65. return cli.in
  66. }
  67. // ConfigFile returns the ConfigFile
  68. func (cli *DockerCli) ConfigFile() *configfile.ConfigFile {
  69. return cli.configFile
  70. }
  71. // CredentialsStore returns a new credentials store based
  72. // on the settings provided in the configuration file.
  73. func (cli *DockerCli) CredentialsStore() credentials.Store {
  74. if cli.configFile.CredentialsStore != "" {
  75. return credentials.NewNativeStore(cli.configFile)
  76. }
  77. return credentials.NewFileStore(cli.configFile)
  78. }
  79. // Initialize the dockerCli runs initialization that must happen after command
  80. // line flags are parsed.
  81. func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions) error {
  82. cli.configFile = LoadDefaultConfigFile(cli.err)
  83. var err error
  84. cli.client, err = NewAPIClientFromFlags(opts.Common, cli.configFile)
  85. if err != nil {
  86. return err
  87. }
  88. if opts.Common.TrustKey == "" {
  89. cli.keyFile = filepath.Join(cliconfig.ConfigDir(), cliflags.DefaultTrustKeyFile)
  90. } else {
  91. cli.keyFile = opts.Common.TrustKey
  92. }
  93. return nil
  94. }
  95. // NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.
  96. func NewDockerCli(in io.ReadCloser, out, err io.Writer) *DockerCli {
  97. return &DockerCli{in: NewInStream(in), out: NewOutStream(out), err: err}
  98. }
  99. // LoadDefaultConfigFile attempts to load the default config file and returns
  100. // an initialized ConfigFile struct if none is found.
  101. func LoadDefaultConfigFile(err io.Writer) *configfile.ConfigFile {
  102. configFile, e := cliconfig.Load(cliconfig.ConfigDir())
  103. if e != nil {
  104. fmt.Fprintf(err, "WARNING: Error loading config file:%v\n", e)
  105. }
  106. if !configFile.ContainsAuth() {
  107. credentials.DetectDefaultStore(configFile)
  108. }
  109. return configFile
  110. }
  111. // NewAPIClientFromFlags creates a new APIClient from command line flags
  112. func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.ConfigFile) (client.APIClient, error) {
  113. host, err := getServerHost(opts.Hosts, opts.TLSOptions)
  114. if err != nil {
  115. return &client.Client{}, err
  116. }
  117. customHeaders := configFile.HTTPHeaders
  118. if customHeaders == nil {
  119. customHeaders = map[string]string{}
  120. }
  121. customHeaders["User-Agent"] = UserAgent()
  122. verStr := api.DefaultVersion
  123. if tmpStr := os.Getenv("DOCKER_API_VERSION"); tmpStr != "" {
  124. verStr = tmpStr
  125. }
  126. httpClient, err := newHTTPClient(host, opts.TLSOptions)
  127. if err != nil {
  128. return &client.Client{}, err
  129. }
  130. return client.NewClient(host, verStr, httpClient, customHeaders)
  131. }
  132. func getServerHost(hosts []string, tlsOptions *tlsconfig.Options) (host string, err error) {
  133. switch len(hosts) {
  134. case 0:
  135. host = os.Getenv("DOCKER_HOST")
  136. case 1:
  137. host = hosts[0]
  138. default:
  139. return "", errors.New("Please specify only one -H")
  140. }
  141. host, err = dopts.ParseHost(tlsOptions != nil, host)
  142. return
  143. }
  144. func newHTTPClient(host string, tlsOptions *tlsconfig.Options) (*http.Client, error) {
  145. if tlsOptions == nil {
  146. // let the api client configure the default transport.
  147. return nil, nil
  148. }
  149. config, err := tlsconfig.Client(*tlsOptions)
  150. if err != nil {
  151. return nil, err
  152. }
  153. tr := &http.Transport{
  154. TLSClientConfig: config,
  155. }
  156. proto, addr, _, err := client.ParseHost(host)
  157. if err != nil {
  158. return nil, err
  159. }
  160. sockets.ConfigureTransport(tr, proto, addr)
  161. return &http.Client{
  162. Transport: tr,
  163. }, nil
  164. }
  165. // UserAgent returns the user agent string used for making API requests
  166. func UserAgent() string {
  167. return "Docker-Client/" + dockerversion.Version + " (" + runtime.GOOS + ")"
  168. }