exec.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package client
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "github.com/Sirupsen/logrus"
  7. "github.com/docker/docker/api/types"
  8. "github.com/docker/docker/pkg/promise"
  9. "github.com/docker/docker/runconfig"
  10. "github.com/docker/docker/utils"
  11. )
  12. // CmdExec runs a command in a running container.
  13. //
  14. // Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
  15. func (cli *DockerCli) CmdExec(args ...string) error {
  16. cmd := cli.Subcmd("exec", "CONTAINER COMMAND [ARG...]", "Run a command in a running container", true)
  17. execConfig, err := runconfig.ParseExec(cmd, args)
  18. // just in case the ParseExec does not exit
  19. if execConfig.Container == "" || err != nil {
  20. return &utils.StatusError{StatusCode: 1}
  21. }
  22. stream, _, err := cli.call("POST", "/containers/"+execConfig.Container+"/exec", execConfig, nil)
  23. if err != nil {
  24. return err
  25. }
  26. var response types.ContainerExecCreateResponse
  27. if err := json.NewDecoder(stream).Decode(&response); err != nil {
  28. return err
  29. }
  30. for _, warning := range response.Warnings {
  31. fmt.Fprintf(cli.err, "WARNING: %s\n", warning)
  32. }
  33. execID := response.ID
  34. if execID == "" {
  35. fmt.Fprintf(cli.out, "exec ID empty")
  36. return nil
  37. }
  38. if !execConfig.Detach {
  39. if err := cli.CheckTtyInput(execConfig.AttachStdin, execConfig.Tty); err != nil {
  40. return err
  41. }
  42. } else {
  43. if _, _, err := readBody(cli.call("POST", "/exec/"+execID+"/start", execConfig, nil)); err != nil {
  44. return err
  45. }
  46. // For now don't print this - wait for when we support exec wait()
  47. // fmt.Fprintf(cli.out, "%s\n", execID)
  48. return nil
  49. }
  50. // Interactive exec requested.
  51. var (
  52. out, stderr io.Writer
  53. in io.ReadCloser
  54. hijacked = make(chan io.Closer)
  55. errCh chan error
  56. )
  57. // Block the return until the chan gets closed
  58. defer func() {
  59. logrus.Debugf("End of CmdExec(), Waiting for hijack to finish.")
  60. if _, ok := <-hijacked; ok {
  61. logrus.Errorf("Hijack did not finish (chan still open)")
  62. }
  63. }()
  64. if execConfig.AttachStdin {
  65. in = cli.in
  66. }
  67. if execConfig.AttachStdout {
  68. out = cli.out
  69. }
  70. if execConfig.AttachStderr {
  71. if execConfig.Tty {
  72. stderr = cli.out
  73. } else {
  74. stderr = cli.err
  75. }
  76. }
  77. errCh = promise.Go(func() error {
  78. return cli.hijack("POST", "/exec/"+execID+"/start", execConfig.Tty, in, out, stderr, hijacked, execConfig)
  79. })
  80. // Acknowledge the hijack before starting
  81. select {
  82. case closer := <-hijacked:
  83. // Make sure that hijack gets closed when returning. (result
  84. // in closing hijack chan and freeing server's goroutines.
  85. if closer != nil {
  86. defer closer.Close()
  87. }
  88. case err := <-errCh:
  89. if err != nil {
  90. logrus.Debugf("Error hijack: %s", err)
  91. return err
  92. }
  93. }
  94. if execConfig.Tty && cli.isTerminalIn {
  95. if err := cli.monitorTtySize(execID, true); err != nil {
  96. logrus.Errorf("Error monitoring TTY size: %s", err)
  97. }
  98. }
  99. if err := <-errCh; err != nil {
  100. logrus.Debugf("Error hijack: %s", err)
  101. return err
  102. }
  103. var status int
  104. if _, status, err = getExecExitCode(cli, execID); err != nil {
  105. return err
  106. }
  107. if status != 0 {
  108. return &utils.StatusError{StatusCode: status}
  109. }
  110. return nil
  111. }