utils.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package client
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "os"
  9. gosignal "os/signal"
  10. "path/filepath"
  11. "runtime"
  12. "strings"
  13. "time"
  14. "github.com/Sirupsen/logrus"
  15. "github.com/docker/docker/pkg/signal"
  16. "github.com/docker/docker/pkg/term"
  17. "github.com/docker/docker/registry"
  18. "github.com/docker/engine-api/client"
  19. "github.com/docker/engine-api/types"
  20. registrytypes "github.com/docker/engine-api/types/registry"
  21. )
  22. func (cli *DockerCli) electAuthServer() string {
  23. // The daemon `/info` endpoint informs us of the default registry being
  24. // used. This is essential in cross-platforms environment, where for
  25. // example a Linux client might be interacting with a Windows daemon, hence
  26. // the default registry URL might be Windows specific.
  27. serverAddress := registry.IndexServer
  28. if info, err := cli.client.Info(); err != nil {
  29. fmt.Fprintf(cli.out, "Warning: failed to get default registry endpoint from daemon (%v). Using system default: %s\n", err, serverAddress)
  30. } else {
  31. serverAddress = info.IndexServerAddress
  32. }
  33. return serverAddress
  34. }
  35. // encodeAuthToBase64 serializes the auth configuration as JSON base64 payload
  36. func encodeAuthToBase64(authConfig types.AuthConfig) (string, error) {
  37. buf, err := json.Marshal(authConfig)
  38. if err != nil {
  39. return "", err
  40. }
  41. return base64.URLEncoding.EncodeToString(buf), nil
  42. }
  43. func (cli *DockerCli) registryAuthenticationPrivilegedFunc(index *registrytypes.IndexInfo, cmdName string) client.RequestPrivilegeFunc {
  44. return func() (string, error) {
  45. fmt.Fprintf(cli.out, "\nPlease login prior to %s:\n", cmdName)
  46. indexServer := registry.GetAuthConfigKey(index)
  47. authConfig, err := cli.configureAuth("", "", "", indexServer)
  48. if err != nil {
  49. return "", err
  50. }
  51. return encodeAuthToBase64(authConfig)
  52. }
  53. }
  54. func (cli *DockerCli) resizeTty(id string, isExec bool) {
  55. height, width := cli.getTtySize()
  56. if height == 0 && width == 0 {
  57. return
  58. }
  59. options := types.ResizeOptions{
  60. ID: id,
  61. Height: height,
  62. Width: width,
  63. }
  64. var err error
  65. if isExec {
  66. err = cli.client.ContainerExecResize(options)
  67. } else {
  68. err = cli.client.ContainerResize(options)
  69. }
  70. if err != nil {
  71. logrus.Debugf("Error resize: %s", err)
  72. }
  73. }
  74. // getExitCode perform an inspect on the container. It returns
  75. // the running state and the exit code.
  76. func getExitCode(cli *DockerCli, containerID string) (bool, int, error) {
  77. c, err := cli.client.ContainerInspect(containerID)
  78. if err != nil {
  79. // If we can't connect, then the daemon probably died.
  80. if err != client.ErrConnectionFailed {
  81. return false, -1, err
  82. }
  83. return false, -1, nil
  84. }
  85. return c.State.Running, c.State.ExitCode, nil
  86. }
  87. // getExecExitCode perform an inspect on the exec command. It returns
  88. // the running state and the exit code.
  89. func getExecExitCode(cli *DockerCli, execID string) (bool, int, error) {
  90. resp, err := cli.client.ContainerExecInspect(execID)
  91. if err != nil {
  92. // If we can't connect, then the daemon probably died.
  93. if err != client.ErrConnectionFailed {
  94. return false, -1, err
  95. }
  96. return false, -1, nil
  97. }
  98. return resp.Running, resp.ExitCode, nil
  99. }
  100. func (cli *DockerCli) monitorTtySize(id string, isExec bool) error {
  101. cli.resizeTty(id, isExec)
  102. if runtime.GOOS == "windows" {
  103. go func() {
  104. prevH, prevW := cli.getTtySize()
  105. for {
  106. time.Sleep(time.Millisecond * 250)
  107. h, w := cli.getTtySize()
  108. if prevW != w || prevH != h {
  109. cli.resizeTty(id, isExec)
  110. }
  111. prevH = h
  112. prevW = w
  113. }
  114. }()
  115. } else {
  116. sigchan := make(chan os.Signal, 1)
  117. gosignal.Notify(sigchan, signal.SIGWINCH)
  118. go func() {
  119. for range sigchan {
  120. cli.resizeTty(id, isExec)
  121. }
  122. }()
  123. }
  124. return nil
  125. }
  126. func (cli *DockerCli) getTtySize() (int, int) {
  127. if !cli.isTerminalOut {
  128. return 0, 0
  129. }
  130. ws, err := term.GetWinsize(cli.outFd)
  131. if err != nil {
  132. logrus.Debugf("Error getting size: %s", err)
  133. if ws == nil {
  134. return 0, 0
  135. }
  136. }
  137. return int(ws.Height), int(ws.Width)
  138. }
  139. func copyToFile(outfile string, r io.Reader) error {
  140. tmpFile, err := ioutil.TempFile(filepath.Dir(outfile), ".docker_temp_")
  141. if err != nil {
  142. return err
  143. }
  144. tmpPath := tmpFile.Name()
  145. _, err = io.Copy(tmpFile, r)
  146. tmpFile.Close()
  147. if err != nil {
  148. os.Remove(tmpPath)
  149. return err
  150. }
  151. if err = os.Rename(tmpPath, outfile); err != nil {
  152. os.Remove(tmpPath)
  153. return err
  154. }
  155. return nil
  156. }
  157. // resolveAuthConfig is like registry.ResolveAuthConfig, but if using the
  158. // default index, it uses the default index name for the daemon's platform,
  159. // not the client's platform.
  160. func (cli *DockerCli) resolveAuthConfig(authConfigs map[string]types.AuthConfig, index *registrytypes.IndexInfo) types.AuthConfig {
  161. configKey := index.Name
  162. if index.Official {
  163. configKey = cli.electAuthServer()
  164. }
  165. // First try the happy case
  166. if c, found := authConfigs[configKey]; found || index.Official {
  167. return c
  168. }
  169. convertToHostname := func(url string) string {
  170. stripped := url
  171. if strings.HasPrefix(url, "http://") {
  172. stripped = strings.Replace(url, "http://", "", 1)
  173. } else if strings.HasPrefix(url, "https://") {
  174. stripped = strings.Replace(url, "https://", "", 1)
  175. }
  176. nameParts := strings.SplitN(stripped, "/", 2)
  177. return nameParts[0]
  178. }
  179. // Maybe they have a legacy config file, we will iterate the keys converting
  180. // them to the new format and testing
  181. for registry, ac := range authConfigs {
  182. if configKey == convertToHostname(registry) {
  183. return ac
  184. }
  185. }
  186. // When all else fails, return an empty auth config
  187. return types.AuthConfig{}
  188. }