utils.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. "time"
  13. "github.com/Sirupsen/logrus"
  14. "github.com/docker/docker/pkg/signal"
  15. "github.com/docker/docker/pkg/term"
  16. "github.com/docker/docker/registry"
  17. "github.com/docker/engine-api/client"
  18. "github.com/docker/engine-api/types"
  19. registrytypes "github.com/docker/engine-api/types/registry"
  20. )
  21. func (cli *DockerCli) electAuthServer() string {
  22. // The daemon `/info` endpoint informs us of the default registry being
  23. // used. This is essential in cross-platforms environment, where for
  24. // example a Linux client might be interacting with a Windows daemon, hence
  25. // the default registry URL might be Windows specific.
  26. serverAddress := registry.IndexServer
  27. if info, err := cli.client.Info(); err != nil {
  28. fmt.Fprintf(cli.out, "Warning: failed to get default registry endpoint from daemon (%v). Using system default: %s\n", err, serverAddress)
  29. } else {
  30. serverAddress = info.IndexServerAddress
  31. }
  32. return serverAddress
  33. }
  34. // encodeAuthToBase64 serializes the auth configuration as JSON base64 payload
  35. func encodeAuthToBase64(authConfig types.AuthConfig) (string, error) {
  36. buf, err := json.Marshal(authConfig)
  37. if err != nil {
  38. return "", err
  39. }
  40. return base64.URLEncoding.EncodeToString(buf), nil
  41. }
  42. func (cli *DockerCli) registryAuthenticationPrivilegedFunc(index *registrytypes.IndexInfo, cmdName string) client.RequestPrivilegeFunc {
  43. return func() (string, error) {
  44. fmt.Fprintf(cli.out, "\nPlease login prior to %s:\n", cmdName)
  45. indexServer := registry.GetAuthConfigKey(index)
  46. authConfig, err := cli.configureAuth("", "", "", indexServer)
  47. if err != nil {
  48. return "", err
  49. }
  50. return encodeAuthToBase64(authConfig)
  51. }
  52. }
  53. func (cli *DockerCli) resizeTty(id string, isExec bool) {
  54. height, width := cli.getTtySize()
  55. if height == 0 && width == 0 {
  56. return
  57. }
  58. options := types.ResizeOptions{
  59. ID: id,
  60. Height: height,
  61. Width: width,
  62. }
  63. var err error
  64. if isExec {
  65. err = cli.client.ContainerExecResize(options)
  66. } else {
  67. err = cli.client.ContainerResize(options)
  68. }
  69. if err != nil {
  70. logrus.Debugf("Error resize: %s", err)
  71. }
  72. }
  73. // getExitCode perform an inspect on the container. It returns
  74. // the running state and the exit code.
  75. func getExitCode(cli *DockerCli, containerID string) (bool, int, error) {
  76. c, err := cli.client.ContainerInspect(containerID)
  77. if err != nil {
  78. // If we can't connect, then the daemon probably died.
  79. if err != client.ErrConnectionFailed {
  80. return false, -1, err
  81. }
  82. return false, -1, nil
  83. }
  84. return c.State.Running, c.State.ExitCode, nil
  85. }
  86. // getExecExitCode perform an inspect on the exec command. It returns
  87. // the running state and the exit code.
  88. func getExecExitCode(cli *DockerCli, execID string) (bool, int, error) {
  89. resp, err := cli.client.ContainerExecInspect(execID)
  90. if err != nil {
  91. // If we can't connect, then the daemon probably died.
  92. if err != client.ErrConnectionFailed {
  93. return false, -1, err
  94. }
  95. return false, -1, nil
  96. }
  97. return resp.Running, resp.ExitCode, nil
  98. }
  99. func (cli *DockerCli) monitorTtySize(id string, isExec bool) error {
  100. cli.resizeTty(id, isExec)
  101. if runtime.GOOS == "windows" {
  102. go func() {
  103. prevH, prevW := cli.getTtySize()
  104. for {
  105. time.Sleep(time.Millisecond * 250)
  106. h, w := cli.getTtySize()
  107. if prevW != w || prevH != h {
  108. cli.resizeTty(id, isExec)
  109. }
  110. prevH = h
  111. prevW = w
  112. }
  113. }()
  114. } else {
  115. sigchan := make(chan os.Signal, 1)
  116. gosignal.Notify(sigchan, signal.SIGWINCH)
  117. go func() {
  118. for range sigchan {
  119. cli.resizeTty(id, isExec)
  120. }
  121. }()
  122. }
  123. return nil
  124. }
  125. func (cli *DockerCli) getTtySize() (int, int) {
  126. if !cli.isTerminalOut {
  127. return 0, 0
  128. }
  129. ws, err := term.GetWinsize(cli.outFd)
  130. if err != nil {
  131. logrus.Debugf("Error getting size: %s", err)
  132. if ws == nil {
  133. return 0, 0
  134. }
  135. }
  136. return int(ws.Height), int(ws.Width)
  137. }
  138. func copyToFile(outfile string, r io.Reader) error {
  139. tmpFile, err := ioutil.TempFile(filepath.Dir(outfile), ".docker_temp_")
  140. if err != nil {
  141. return err
  142. }
  143. tmpPath := tmpFile.Name()
  144. _, err = io.Copy(tmpFile, r)
  145. tmpFile.Close()
  146. if err != nil {
  147. os.Remove(tmpPath)
  148. return err
  149. }
  150. if err = os.Rename(tmpPath, outfile); err != nil {
  151. os.Remove(tmpPath)
  152. return err
  153. }
  154. return nil
  155. }