cli.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. cliflags "github.com/docker/docker/cli/flags"
  11. "github.com/docker/docker/cliconfig"
  12. "github.com/docker/docker/cliconfig/configfile"
  13. "github.com/docker/docker/cliconfig/credentials"
  14. "github.com/docker/docker/dockerversion"
  15. "github.com/docker/docker/opts"
  16. "github.com/docker/docker/pkg/term"
  17. "github.com/docker/engine-api/client"
  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. // initializing closure
  25. init func() error
  26. // configFile has the client configuration file
  27. configFile *configfile.ConfigFile
  28. // in holds the input stream and closer (io.ReadCloser) for the client.
  29. in io.ReadCloser
  30. // out holds the output stream (io.Writer) for the client.
  31. out io.Writer
  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. inFd uintptr
  38. // outFd holds file descriptor of the client's STDOUT (if valid).
  39. outFd uintptr
  40. // isTerminalIn indicates whether the client's STDIN is a TTY
  41. isTerminalIn bool
  42. // isTerminalOut indicates whether the client's STDOUT is a TTY
  43. isTerminalOut bool
  44. // client is the http client that performs all API operations
  45. client client.APIClient
  46. // state holds the terminal input state
  47. inState *term.State
  48. // outState holds the terminal output state
  49. outState *term.State
  50. }
  51. // Initialize calls the init function that will setup the configuration for the client
  52. // such as the TLS, tcp and other parameters used to run the client.
  53. func (cli *DockerCli) Initialize() error {
  54. if cli.init == nil {
  55. return nil
  56. }
  57. return cli.init()
  58. }
  59. // Client returns the APIClient
  60. func (cli *DockerCli) Client() client.APIClient {
  61. return cli.client
  62. }
  63. // Out returns the writer used for stdout
  64. func (cli *DockerCli) Out() io.Writer {
  65. return cli.out
  66. }
  67. // Err returns the writer used for stderr
  68. func (cli *DockerCli) Err() io.Writer {
  69. return cli.err
  70. }
  71. // In returns the reader used for stdin
  72. func (cli *DockerCli) In() io.ReadCloser {
  73. return cli.in
  74. }
  75. // ConfigFile returns the ConfigFile
  76. func (cli *DockerCli) ConfigFile() *configfile.ConfigFile {
  77. return cli.configFile
  78. }
  79. // IsTerminalIn returns true if the clients stdin is a TTY
  80. func (cli *DockerCli) IsTerminalIn() bool {
  81. return cli.isTerminalIn
  82. }
  83. // IsTerminalOut returns true if the clients stdout is a TTY
  84. func (cli *DockerCli) IsTerminalOut() bool {
  85. return cli.isTerminalOut
  86. }
  87. // OutFd returns the fd for the stdout stream
  88. func (cli *DockerCli) OutFd() uintptr {
  89. return cli.outFd
  90. }
  91. // CheckTtyInput checks if we are trying to attach to a container tty
  92. // from a non-tty client input stream, and if so, returns an error.
  93. func (cli *DockerCli) CheckTtyInput(attachStdin, ttyMode bool) error {
  94. // In order to attach to a container tty, input stream for the client must
  95. // be a tty itself: redirecting or piping the client standard input is
  96. // incompatible with `docker run -t`, `docker exec -t` or `docker attach`.
  97. if ttyMode && attachStdin && !cli.isTerminalIn {
  98. eText := "the input device is not a TTY"
  99. if runtime.GOOS == "windows" {
  100. return errors.New(eText + ". If you are using mintty, try prefixing the command with 'winpty'")
  101. }
  102. return errors.New(eText)
  103. }
  104. return nil
  105. }
  106. // PsFormat returns the format string specified in the configuration.
  107. // String contains columns and format specification, for example {{ID}}\t{{Name}}.
  108. func (cli *DockerCli) PsFormat() string {
  109. return cli.configFile.PsFormat
  110. }
  111. // ImagesFormat returns the format string specified in the configuration.
  112. // String contains columns and format specification, for example {{ID}}\t{{Name}}.
  113. func (cli *DockerCli) ImagesFormat() string {
  114. return cli.configFile.ImagesFormat
  115. }
  116. // NetworksFormat returns the format string specified in the configuration.
  117. // String contains columns and format specification, for example {{ID}}\t{{Name}}
  118. func (cli *DockerCli) NetworksFormat() string {
  119. return cli.configFile.NetworksFormat
  120. }
  121. // VolumesFormat returns the format string specified in the configuration.
  122. // String contains columns and format specification, for example {{ID}}\t{{Name}}
  123. func (cli *DockerCli) VolumesFormat() string {
  124. return cli.configFile.VolumesFormat
  125. }
  126. func (cli *DockerCli) setRawTerminal() error {
  127. if os.Getenv("NORAW") == "" {
  128. if cli.isTerminalIn {
  129. state, err := term.SetRawTerminal(cli.inFd)
  130. if err != nil {
  131. return err
  132. }
  133. cli.inState = state
  134. }
  135. if cli.isTerminalOut {
  136. state, err := term.SetRawTerminalOutput(cli.outFd)
  137. if err != nil {
  138. return err
  139. }
  140. cli.outState = state
  141. }
  142. }
  143. return nil
  144. }
  145. func (cli *DockerCli) restoreTerminal(in io.Closer) error {
  146. if cli.inState != nil {
  147. term.RestoreTerminal(cli.inFd, cli.inState)
  148. }
  149. if cli.outState != nil {
  150. term.RestoreTerminal(cli.outFd, cli.outState)
  151. }
  152. // WARNING: DO NOT REMOVE THE OS CHECK !!!
  153. // For some reason this Close call blocks on darwin..
  154. // As the client exists right after, simply discard the close
  155. // until we find a better solution.
  156. if in != nil && runtime.GOOS != "darwin" {
  157. return in.Close()
  158. }
  159. return nil
  160. }
  161. // NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.
  162. // The key file, protocol (i.e. unix) and address are passed in as strings, along with the tls.Config. If the tls.Config
  163. // is set the client scheme will be set to https.
  164. // The client will be given a 32-second timeout (see https://github.com/docker/docker/pull/8035).
  165. func NewDockerCli(in io.ReadCloser, out, err io.Writer, clientFlags *cliflags.ClientFlags) *DockerCli {
  166. cli := &DockerCli{
  167. in: in,
  168. out: out,
  169. err: err,
  170. keyFile: clientFlags.Common.TrustKey,
  171. }
  172. cli.init = func() error {
  173. clientFlags.PostParse()
  174. cli.configFile = LoadDefaultConfigFile(err)
  175. client, err := NewAPIClientFromFlags(clientFlags, cli.configFile)
  176. if err != nil {
  177. return err
  178. }
  179. cli.client = client
  180. if cli.in != nil {
  181. cli.inFd, cli.isTerminalIn = term.GetFdInfo(cli.in)
  182. }
  183. if cli.out != nil {
  184. cli.outFd, cli.isTerminalOut = term.GetFdInfo(cli.out)
  185. }
  186. return nil
  187. }
  188. return cli
  189. }
  190. // LoadDefaultConfigFile attempts to load the default config file and returns
  191. // an initialized ConfigFile struct if none is found.
  192. func LoadDefaultConfigFile(err io.Writer) *configfile.ConfigFile {
  193. configFile, e := cliconfig.Load(cliconfig.ConfigDir())
  194. if e != nil {
  195. fmt.Fprintf(err, "WARNING: Error loading config file:%v\n", e)
  196. }
  197. if !configFile.ContainsAuth() {
  198. credentials.DetectDefaultStore(configFile)
  199. }
  200. return configFile
  201. }
  202. // NewAPIClientFromFlags creates a new APIClient from command line flags
  203. func NewAPIClientFromFlags(clientFlags *cliflags.ClientFlags, configFile *configfile.ConfigFile) (client.APIClient, error) {
  204. host, err := getServerHost(clientFlags.Common.Hosts, clientFlags.Common.TLSOptions)
  205. if err != nil {
  206. return &client.Client{}, err
  207. }
  208. customHeaders := configFile.HTTPHeaders
  209. if customHeaders == nil {
  210. customHeaders = map[string]string{}
  211. }
  212. customHeaders["User-Agent"] = clientUserAgent()
  213. verStr := api.DefaultVersion
  214. if tmpStr := os.Getenv("DOCKER_API_VERSION"); tmpStr != "" {
  215. verStr = tmpStr
  216. }
  217. httpClient, err := newHTTPClient(host, clientFlags.Common.TLSOptions)
  218. if err != nil {
  219. return &client.Client{}, err
  220. }
  221. return client.NewClient(host, verStr, httpClient, customHeaders)
  222. }
  223. func getServerHost(hosts []string, tlsOptions *tlsconfig.Options) (host string, err error) {
  224. switch len(hosts) {
  225. case 0:
  226. host = os.Getenv("DOCKER_HOST")
  227. case 1:
  228. host = hosts[0]
  229. default:
  230. return "", errors.New("Please specify only one -H")
  231. }
  232. host, err = opts.ParseHost(tlsOptions != nil, host)
  233. return
  234. }
  235. func newHTTPClient(host string, tlsOptions *tlsconfig.Options) (*http.Client, error) {
  236. if tlsOptions == nil {
  237. // let the api client configure the default transport.
  238. return nil, nil
  239. }
  240. config, err := tlsconfig.Client(*tlsOptions)
  241. if err != nil {
  242. return nil, err
  243. }
  244. tr := &http.Transport{
  245. TLSClientConfig: config,
  246. }
  247. proto, addr, _, err := client.ParseHost(host)
  248. if err != nil {
  249. return nil, err
  250. }
  251. sockets.ConfigureTransport(tr, proto, addr)
  252. return &http.Client{
  253. Transport: tr,
  254. }, nil
  255. }
  256. func clientUserAgent() string {
  257. return "Docker-Client/" + dockerversion.Version + " (" + runtime.GOOS + ")"
  258. }