utils.go 3.8 KB

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