utils.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. "golang.org/x/net/context"
  14. "github.com/Sirupsen/logrus"
  15. "github.com/docker/docker/pkg/signal"
  16. "github.com/docker/docker/pkg/term"
  17. "github.com/docker/docker/registry"
  18. "github.com/docker/engine-api/client"
  19. "github.com/docker/engine-api/types"
  20. registrytypes "github.com/docker/engine-api/types/registry"
  21. )
  22. func (cli *DockerCli) electAuthServer(ctx context.Context) string {
  23. // The daemon `/info` endpoint informs us of the default registry being
  24. // used. This is essential in cross-platforms environment, where for
  25. // example a Linux client might be interacting with a Windows daemon, hence
  26. // the default registry URL might be Windows specific.
  27. serverAddress := registry.IndexServer
  28. if info, err := cli.client.Info(ctx); err != nil {
  29. fmt.Fprintf(cli.out, "Warning: failed to get default registry endpoint from daemon (%v). Using system default: %s\n", err, serverAddress)
  30. } else {
  31. serverAddress = info.IndexServerAddress
  32. }
  33. return serverAddress
  34. }
  35. // EncodeAuthToBase64 serializes the auth configuration as JSON base64 payload
  36. func EncodeAuthToBase64(authConfig types.AuthConfig) (string, error) {
  37. buf, err := json.Marshal(authConfig)
  38. if err != nil {
  39. return "", err
  40. }
  41. return base64.URLEncoding.EncodeToString(buf), nil
  42. }
  43. // RegistryAuthenticationPrivilegedFunc return a RequestPrivilegeFunc from the specified registry index info
  44. // for the given command.
  45. func (cli *DockerCli) RegistryAuthenticationPrivilegedFunc(index *registrytypes.IndexInfo, cmdName string) types.RequestPrivilegeFunc {
  46. return func() (string, error) {
  47. fmt.Fprintf(cli.out, "\nPlease login prior to %s:\n", cmdName)
  48. indexServer := registry.GetAuthConfigKey(index)
  49. authConfig, err := cli.configureAuth("", "", indexServer, false)
  50. if err != nil {
  51. return "", err
  52. }
  53. return EncodeAuthToBase64(authConfig)
  54. }
  55. }
  56. func (cli *DockerCli) resizeTty(ctx context.Context, id string, isExec bool) {
  57. height, width := cli.GetTtySize()
  58. cli.ResizeTtyTo(ctx, id, height, width, isExec)
  59. }
  60. // ResizeTtyTo resizes tty to specific height and width
  61. // TODO: this can be unexported again once all container related commands move to package container
  62. func (cli *DockerCli) ResizeTtyTo(ctx context.Context, id string, height, width int, isExec bool) {
  63. if height == 0 && width == 0 {
  64. return
  65. }
  66. options := types.ResizeOptions{
  67. Height: height,
  68. Width: width,
  69. }
  70. var err error
  71. if isExec {
  72. err = cli.client.ContainerExecResize(ctx, id, options)
  73. } else {
  74. err = cli.client.ContainerResize(ctx, id, options)
  75. }
  76. if err != nil {
  77. logrus.Debugf("Error resize: %s", err)
  78. }
  79. }
  80. // getExecExitCode perform an inspect on the exec command. It returns
  81. // the running state and the exit code.
  82. func (cli *DockerCli) getExecExitCode(ctx context.Context, execID string) (bool, int, error) {
  83. resp, err := cli.client.ContainerExecInspect(ctx, execID)
  84. if err != nil {
  85. // If we can't connect, then the daemon probably died.
  86. if err != client.ErrConnectionFailed {
  87. return false, -1, err
  88. }
  89. return false, -1, nil
  90. }
  91. return resp.Running, resp.ExitCode, nil
  92. }
  93. // MonitorTtySize updates the container tty size when the terminal tty changes size
  94. func (cli *DockerCli) MonitorTtySize(ctx context.Context, id string, isExec bool) error {
  95. cli.resizeTty(ctx, id, isExec)
  96. if runtime.GOOS == "windows" {
  97. go func() {
  98. prevH, prevW := cli.GetTtySize()
  99. for {
  100. time.Sleep(time.Millisecond * 250)
  101. h, w := cli.GetTtySize()
  102. if prevW != w || prevH != h {
  103. cli.resizeTty(ctx, id, isExec)
  104. }
  105. prevH = h
  106. prevW = w
  107. }
  108. }()
  109. } else {
  110. sigchan := make(chan os.Signal, 1)
  111. gosignal.Notify(sigchan, signal.SIGWINCH)
  112. go func() {
  113. for range sigchan {
  114. cli.resizeTty(ctx, id, isExec)
  115. }
  116. }()
  117. }
  118. return nil
  119. }
  120. // GetTtySize returns the height and width in characters of the tty
  121. func (cli *DockerCli) GetTtySize() (int, int) {
  122. if !cli.isTerminalOut {
  123. return 0, 0
  124. }
  125. ws, err := term.GetWinsize(cli.outFd)
  126. if err != nil {
  127. logrus.Debugf("Error getting size: %s", err)
  128. if ws == nil {
  129. return 0, 0
  130. }
  131. }
  132. return int(ws.Height), int(ws.Width)
  133. }
  134. // CopyToFile writes the content of the reader to the specified file
  135. func CopyToFile(outfile string, r io.Reader) error {
  136. tmpFile, err := ioutil.TempFile(filepath.Dir(outfile), ".docker_temp_")
  137. if err != nil {
  138. return err
  139. }
  140. tmpPath := tmpFile.Name()
  141. _, err = io.Copy(tmpFile, r)
  142. tmpFile.Close()
  143. if err != nil {
  144. os.Remove(tmpPath)
  145. return err
  146. }
  147. if err = os.Rename(tmpPath, outfile); err != nil {
  148. os.Remove(tmpPath)
  149. return err
  150. }
  151. return nil
  152. }
  153. // ResolveAuthConfig is like registry.ResolveAuthConfig, but if using the
  154. // default index, it uses the default index name for the daemon's platform,
  155. // not the client's platform.
  156. func (cli *DockerCli) ResolveAuthConfig(ctx context.Context, index *registrytypes.IndexInfo) types.AuthConfig {
  157. configKey := index.Name
  158. if index.Official {
  159. configKey = cli.electAuthServer(ctx)
  160. }
  161. a, _ := getCredentials(cli.configFile, configKey)
  162. return a
  163. }
  164. // RetrieveAuthConfigs return all credentials.
  165. func (cli *DockerCli) RetrieveAuthConfigs() map[string]types.AuthConfig {
  166. acs, _ := getAllCredentials(cli.configFile)
  167. return acs
  168. }
  169. // ForwardAllSignals forwards signals to the container
  170. // TODO: this can be unexported again once all container commands are under
  171. // api/client/container
  172. func (cli *DockerCli) ForwardAllSignals(ctx context.Context, cid string) chan os.Signal {
  173. sigc := make(chan os.Signal, 128)
  174. signal.CatchAll(sigc)
  175. go func() {
  176. for s := range sigc {
  177. if s == signal.SIGCHLD || s == signal.SIGPIPE {
  178. continue
  179. }
  180. var sig string
  181. for sigStr, sigN := range signal.SignalMap {
  182. if sigN == s {
  183. sig = sigStr
  184. break
  185. }
  186. }
  187. if sig == "" {
  188. fmt.Fprintf(cli.err, "Unsupported signal: %v. Discarding.\n", s)
  189. continue
  190. }
  191. if err := cli.client.ContainerKill(ctx, cid, sig); err != nil {
  192. logrus.Debugf("Error sending signal: %s", err)
  193. }
  194. }
  195. }()
  196. return sigc
  197. }