cli.go 5.8 KB

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