exec.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. errCh = promise.Go(func() error {
  78. return cli.holdHijackedConnection(execConfig.Tty, in, out, stderr, resp)
  79. })
  80. if execConfig.Tty && cli.isTerminalIn {
  81. if err := cli.monitorTtySize(execID, true); err != nil {
  82. fmt.Fprintf(cli.err, "Error monitoring TTY size: %s\n", err)
  83. }
  84. }
  85. if err := <-errCh; err != nil {
  86. logrus.Debugf("Error hijack: %s", err)
  87. return err
  88. }
  89. var status int
  90. if _, status, err = getExecExitCode(cli, execID); err != nil {
  91. return err
  92. }
  93. if status != 0 {
  94. return Cli.StatusError{StatusCode: status}
  95. }
  96. return nil
  97. }
  98. // ParseExec parses the specified args for the specified command and generates
  99. // an ExecConfig from it.
  100. // If the minimal number of specified args is not right or if specified args are
  101. // not valid, it will return an error.
  102. func ParseExec(cmd *flag.FlagSet, args []string) (*types.ExecConfig, error) {
  103. var (
  104. flStdin = cmd.Bool([]string{"i", "-interactive"}, false, "Keep STDIN open even if not attached")
  105. flTty = cmd.Bool([]string{"t", "-tty"}, false, "Allocate a pseudo-TTY")
  106. flDetach = cmd.Bool([]string{"d", "-detach"}, false, "Detached mode: run command in the background")
  107. flUser = cmd.String([]string{"u", "-user"}, "", "Username or UID (format: <name|uid>[:<group|gid>])")
  108. flPrivileged = cmd.Bool([]string{"-privileged"}, false, "Give extended privileges to the command")
  109. execCmd []string
  110. container string
  111. )
  112. cmd.Require(flag.Min, 2)
  113. if err := cmd.ParseFlags(args, true); err != nil {
  114. return nil, err
  115. }
  116. container = cmd.Arg(0)
  117. parsedArgs := cmd.Args()
  118. execCmd = parsedArgs[1:]
  119. execConfig := &types.ExecConfig{
  120. User: *flUser,
  121. Privileged: *flPrivileged,
  122. Tty: *flTty,
  123. Cmd: execCmd,
  124. Container: container,
  125. Detach: *flDetach,
  126. }
  127. // If -d is not set, attach to everything by default
  128. if !*flDetach {
  129. execConfig.AttachStdout = true
  130. execConfig.AttachStderr = true
  131. if *flStdin {
  132. execConfig.AttachStdin = true
  133. }
  134. }
  135. return execConfig, nil
  136. }