exec.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. execID := response.ID
  30. if execID == "" {
  31. fmt.Fprintf(cli.out, "exec ID empty")
  32. return nil
  33. }
  34. //Temp struct for execStart so that we don't need to transfer all the execConfig
  35. execStartCheck := &types.ExecStartCheck{
  36. Detach: execConfig.Detach,
  37. Tty: execConfig.Tty,
  38. }
  39. if !execConfig.Detach {
  40. if err := cli.CheckTtyInput(execConfig.AttachStdin, execConfig.Tty); err != nil {
  41. return err
  42. }
  43. } else {
  44. if _, _, err := readBody(cli.call("POST", "/exec/"+execID+"/start", execStartCheck, nil)); err != nil {
  45. return err
  46. }
  47. // For now don't print this - wait for when we support exec wait()
  48. // fmt.Fprintf(cli.out, "%s\n", execID)
  49. return nil
  50. }
  51. // Interactive exec requested.
  52. var (
  53. out, stderr io.Writer
  54. in io.ReadCloser
  55. hijacked = make(chan io.Closer)
  56. errCh chan error
  57. )
  58. // Block the return until the chan gets closed
  59. defer func() {
  60. logrus.Debugf("End of CmdExec(), Waiting for hijack to finish.")
  61. if _, ok := <-hijacked; ok {
  62. fmt.Fprintln(cli.err, "Hijack did not finish (chan still open)")
  63. }
  64. }()
  65. if execConfig.AttachStdin {
  66. in = cli.in
  67. }
  68. if execConfig.AttachStdout {
  69. out = cli.out
  70. }
  71. if execConfig.AttachStderr {
  72. if execConfig.Tty {
  73. stderr = cli.out
  74. } else {
  75. stderr = cli.err
  76. }
  77. }
  78. errCh = promise.Go(func() error {
  79. return cli.hijack("POST", "/exec/"+execID+"/start", execConfig.Tty, in, out, stderr, hijacked, execConfig)
  80. })
  81. // Acknowledge the hijack before starting
  82. select {
  83. case closer := <-hijacked:
  84. // Make sure that hijack gets closed when returning. (result
  85. // in closing hijack chan and freeing server's goroutines.
  86. if closer != nil {
  87. defer closer.Close()
  88. }
  89. case err := <-errCh:
  90. if err != nil {
  91. logrus.Debugf("Error hijack: %s", err)
  92. return err
  93. }
  94. }
  95. if execConfig.Tty && cli.isTerminalIn {
  96. if err := cli.monitorTtySize(execID, true); err != nil {
  97. fmt.Fprintf(cli.err, "Error monitoring TTY size: %s\n", err)
  98. }
  99. }
  100. if err := <-errCh; err != nil {
  101. logrus.Debugf("Error hijack: %s", err)
  102. return err
  103. }
  104. var status int
  105. if _, status, err = getExecExitCode(cli, execID); err != nil {
  106. return err
  107. }
  108. if status != 0 {
  109. return StatusError{StatusCode: status}
  110. }
  111. return nil
  112. }