exec.go 3.2 KB

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