exec.go 3.1 KB

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