exec.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package client
  2. import (
  3. "fmt"
  4. "io"
  5. "github.com/Sirupsen/logrus"
  6. Cli "github.com/docker/docker/cli"
  7. flag "github.com/docker/docker/pkg/mflag"
  8. "github.com/docker/docker/pkg/promise"
  9. "github.com/docker/engine-api/types"
  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...]"}, Cli.DockerCommands["exec"].Description, true)
  16. detachKeys := cmd.String([]string{"-detach-keys"}, "", "Override the key sequence for detaching a container")
  17. execConfig, err := 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. if *detachKeys != "" {
  23. cli.configFile.DetachKeys = *detachKeys
  24. }
  25. // Send client escape keys
  26. execConfig.DetachKeys = cli.configFile.DetachKeys
  27. response, err := cli.client.ContainerExecCreate(*execConfig)
  28. if 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. if !execConfig.Detach {
  38. if err := cli.CheckTtyInput(execConfig.AttachStdin, execConfig.Tty); err != nil {
  39. return err
  40. }
  41. } else {
  42. execStartCheck := types.ExecStartCheck{
  43. Detach: execConfig.Detach,
  44. Tty: execConfig.Tty,
  45. }
  46. if err := cli.client.ContainerExecStart(execID, execStartCheck); 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. errCh chan error
  58. )
  59. if execConfig.AttachStdin {
  60. in = cli.in
  61. }
  62. if execConfig.AttachStdout {
  63. out = cli.out
  64. }
  65. if execConfig.AttachStderr {
  66. if execConfig.Tty {
  67. stderr = cli.out
  68. } else {
  69. stderr = cli.err
  70. }
  71. }
  72. resp, err := cli.client.ContainerExecAttach(execID, *execConfig)
  73. if err != nil {
  74. return err
  75. }
  76. defer resp.Close()
  77. if in != nil && execConfig.Tty {
  78. if err := cli.setRawTerminal(); err != nil {
  79. return err
  80. }
  81. defer cli.restoreTerminal(in)
  82. }
  83. errCh = promise.Go(func() error {
  84. return cli.holdHijackedConnection(execConfig.Tty, in, out, stderr, resp)
  85. })
  86. if execConfig.Tty && cli.isTerminalIn {
  87. if err := cli.monitorTtySize(execID, true); err != nil {
  88. fmt.Fprintf(cli.err, "Error monitoring TTY size: %s\n", err)
  89. }
  90. }
  91. if err := <-errCh; err != nil {
  92. logrus.Debugf("Error hijack: %s", err)
  93. return err
  94. }
  95. var status int
  96. if _, status, err = getExecExitCode(cli, execID); err != nil {
  97. return err
  98. }
  99. if status != 0 {
  100. return Cli.StatusError{StatusCode: status}
  101. }
  102. return nil
  103. }
  104. // ParseExec parses the specified args for the specified command and generates
  105. // an ExecConfig from it.
  106. // If the minimal number of specified args is not right or if specified args are
  107. // not valid, it will return an error.
  108. func ParseExec(cmd *flag.FlagSet, args []string) (*types.ExecConfig, error) {
  109. var (
  110. flStdin = cmd.Bool([]string{"i", "-interactive"}, false, "Keep STDIN open even if not attached")
  111. flTty = cmd.Bool([]string{"t", "-tty"}, false, "Allocate a pseudo-TTY")
  112. flDetach = cmd.Bool([]string{"d", "-detach"}, false, "Detached mode: run command in the background")
  113. flUser = cmd.String([]string{"u", "-user"}, "", "Username or UID (format: <name|uid>[:<group|gid>])")
  114. flPrivileged = cmd.Bool([]string{"-privileged"}, false, "Give extended privileges to the command")
  115. execCmd []string
  116. container string
  117. )
  118. cmd.Require(flag.Min, 2)
  119. if err := cmd.ParseFlags(args, true); err != nil {
  120. return nil, err
  121. }
  122. container = cmd.Arg(0)
  123. parsedArgs := cmd.Args()
  124. execCmd = parsedArgs[1:]
  125. execConfig := &types.ExecConfig{
  126. User: *flUser,
  127. Privileged: *flPrivileged,
  128. Tty: *flTty,
  129. Cmd: execCmd,
  130. Container: container,
  131. Detach: *flDetach,
  132. }
  133. // If -d is not set, attach to everything by default
  134. if !*flDetach {
  135. execConfig.AttachStdout = true
  136. execConfig.AttachStderr = true
  137. if *flStdin {
  138. execConfig.AttachStdin = true
  139. }
  140. }
  141. return execConfig, nil
  142. }