utils.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. // encodeAuthToBase64 serializes the auth configuration as JSON base64 payload
  22. func encodeAuthToBase64(authConfig types.AuthConfig) (string, error) {
  23. buf, err := json.Marshal(authConfig)
  24. if err != nil {
  25. return "", err
  26. }
  27. return base64.URLEncoding.EncodeToString(buf), nil
  28. }
  29. func (cli *DockerCli) registryAuthenticationPrivilegedFunc(index *registrytypes.IndexInfo, cmdName string) client.RequestPrivilegeFunc {
  30. return func() (string, error) {
  31. fmt.Fprintf(cli.out, "\nPlease login prior to %s:\n", cmdName)
  32. indexServer := registry.GetAuthConfigKey(index)
  33. authConfig, err := cli.configureAuth("", "", "", indexServer)
  34. if err != nil {
  35. return "", err
  36. }
  37. return encodeAuthToBase64(authConfig)
  38. }
  39. }
  40. func (cli *DockerCli) resizeTty(id string, isExec bool) {
  41. height, width := cli.getTtySize()
  42. if height == 0 && width == 0 {
  43. return
  44. }
  45. options := types.ResizeOptions{
  46. ID: id,
  47. Height: height,
  48. Width: width,
  49. }
  50. var err error
  51. if isExec {
  52. err = cli.client.ContainerExecResize(options)
  53. } else {
  54. err = cli.client.ContainerResize(options)
  55. }
  56. if err != nil {
  57. logrus.Debugf("Error resize: %s", err)
  58. }
  59. }
  60. // getExitCode perform an inspect on the container. It returns
  61. // the running state and the exit code.
  62. func getExitCode(cli *DockerCli, containerID string) (bool, int, error) {
  63. c, err := cli.client.ContainerInspect(containerID)
  64. if err != nil {
  65. // If we can't connect, then the daemon probably died.
  66. if err != client.ErrConnectionFailed {
  67. return false, -1, err
  68. }
  69. return false, -1, nil
  70. }
  71. return c.State.Running, c.State.ExitCode, nil
  72. }
  73. // getExecExitCode perform an inspect on the exec command. It returns
  74. // the running state and the exit code.
  75. func getExecExitCode(cli *DockerCli, execID string) (bool, int, error) {
  76. resp, err := cli.client.ContainerExecInspect(execID)
  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 resp.Running, resp.ExitCode, nil
  85. }
  86. func (cli *DockerCli) monitorTtySize(id string, isExec bool) error {
  87. cli.resizeTty(id, isExec)
  88. if runtime.GOOS == "windows" {
  89. go func() {
  90. prevH, prevW := cli.getTtySize()
  91. for {
  92. time.Sleep(time.Millisecond * 250)
  93. h, w := cli.getTtySize()
  94. if prevW != w || prevH != h {
  95. cli.resizeTty(id, isExec)
  96. }
  97. prevH = h
  98. prevW = w
  99. }
  100. }()
  101. } else {
  102. sigchan := make(chan os.Signal, 1)
  103. gosignal.Notify(sigchan, signal.SIGWINCH)
  104. go func() {
  105. for range sigchan {
  106. cli.resizeTty(id, isExec)
  107. }
  108. }()
  109. }
  110. return nil
  111. }
  112. func (cli *DockerCli) getTtySize() (int, int) {
  113. if !cli.isTerminalOut {
  114. return 0, 0
  115. }
  116. ws, err := term.GetWinsize(cli.outFd)
  117. if err != nil {
  118. logrus.Debugf("Error getting size: %s", err)
  119. if ws == nil {
  120. return 0, 0
  121. }
  122. }
  123. return int(ws.Height), int(ws.Width)
  124. }
  125. func copyToFile(outfile string, r io.Reader) error {
  126. tmpFile, err := ioutil.TempFile(filepath.Dir(outfile), ".docker_temp_")
  127. if err != nil {
  128. return err
  129. }
  130. tmpPath := tmpFile.Name()
  131. _, err = io.Copy(tmpFile, r)
  132. tmpFile.Close()
  133. if err != nil {
  134. os.Remove(tmpPath)
  135. return err
  136. }
  137. if err = os.Rename(tmpPath, outfile); err != nil {
  138. os.Remove(tmpPath)
  139. return err
  140. }
  141. return nil
  142. }