utils.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package client
  2. import (
  3. "fmt"
  4. "os"
  5. gosignal "os/signal"
  6. "runtime"
  7. "time"
  8. "github.com/Sirupsen/logrus"
  9. "github.com/docker/docker/api/client/lib"
  10. "github.com/docker/docker/api/types"
  11. "github.com/docker/docker/pkg/signal"
  12. "github.com/docker/docker/pkg/term"
  13. "github.com/docker/docker/registry"
  14. )
  15. func (cli *DockerCli) encodeRegistryAuth(index *registry.IndexInfo) (string, error) {
  16. authConfig := registry.ResolveAuthConfig(cli.configFile, index)
  17. return authConfig.EncodeToBase64()
  18. }
  19. func (cli *DockerCli) registryAuthenticationPrivilegedFunc(index *registry.IndexInfo, cmdName string) lib.RequestPrivilegeFunc {
  20. return func() (string, error) {
  21. fmt.Fprintf(cli.out, "\nPlease login prior to %s:\n", cmdName)
  22. if err := cli.CmdLogin(index.GetAuthConfigKey()); err != nil {
  23. return "", err
  24. }
  25. return cli.encodeRegistryAuth(index)
  26. }
  27. }
  28. func (cli *DockerCli) resizeTty(id string, isExec bool) {
  29. height, width := cli.getTtySize()
  30. if height == 0 && width == 0 {
  31. return
  32. }
  33. options := types.ResizeOptions{
  34. ID: id,
  35. Height: height,
  36. Width: width,
  37. }
  38. var err error
  39. if !isExec {
  40. err = cli.client.ContainerExecResize(options)
  41. } else {
  42. err = cli.client.ContainerResize(options)
  43. }
  44. if err != nil {
  45. logrus.Debugf("Error resize: %s", err)
  46. }
  47. }
  48. // getExitCode perform an inspect on the container. It returns
  49. // the running state and the exit code.
  50. func getExitCode(cli *DockerCli, containerID string) (bool, int, error) {
  51. c, err := cli.client.ContainerInspect(containerID)
  52. if err != nil {
  53. // If we can't connect, then the daemon probably died.
  54. if err != lib.ErrConnectionFailed {
  55. return false, -1, err
  56. }
  57. return false, -1, nil
  58. }
  59. return c.State.Running, c.State.ExitCode, nil
  60. }
  61. // getExecExitCode perform an inspect on the exec command. It returns
  62. // the running state and the exit code.
  63. func getExecExitCode(cli *DockerCli, execID string) (bool, int, error) {
  64. resp, err := cli.client.ContainerExecInspect(execID)
  65. if err != nil {
  66. // If we can't connect, then the daemon probably died.
  67. if err != lib.ErrConnectionFailed {
  68. return false, -1, err
  69. }
  70. return false, -1, nil
  71. }
  72. return resp.Running, resp.ExitCode, nil
  73. }
  74. func (cli *DockerCli) monitorTtySize(id string, isExec bool) error {
  75. cli.resizeTty(id, isExec)
  76. if runtime.GOOS == "windows" {
  77. go func() {
  78. prevH, prevW := cli.getTtySize()
  79. for {
  80. time.Sleep(time.Millisecond * 250)
  81. h, w := cli.getTtySize()
  82. if prevW != w || prevH != h {
  83. cli.resizeTty(id, isExec)
  84. }
  85. prevH = h
  86. prevW = w
  87. }
  88. }()
  89. } else {
  90. sigchan := make(chan os.Signal, 1)
  91. gosignal.Notify(sigchan, signal.SIGWINCH)
  92. go func() {
  93. for range sigchan {
  94. cli.resizeTty(id, isExec)
  95. }
  96. }()
  97. }
  98. return nil
  99. }
  100. func (cli *DockerCli) getTtySize() (int, int) {
  101. if !cli.isTerminalOut {
  102. return 0, 0
  103. }
  104. ws, err := term.GetWinsize(cli.outFd)
  105. if err != nil {
  106. logrus.Debugf("Error getting size: %s", err)
  107. if ws == nil {
  108. return 0, 0
  109. }
  110. }
  111. return int(ws.Height), int(ws.Width)
  112. }