utils.go 3.8 KB

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