exec.go 3.0 KB

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