exec.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package client
  2. import (
  3. "fmt"
  4. "io"
  5. "golang.org/x/net/context"
  6. "github.com/Sirupsen/logrus"
  7. Cli "github.com/docker/docker/cli"
  8. flag "github.com/docker/docker/pkg/mflag"
  9. "github.com/docker/docker/pkg/promise"
  10. "github.com/docker/engine-api/types"
  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{"[OPTIONS] CONTAINER COMMAND [ARG...]"}, Cli.DockerCommands["exec"].Description, true)
  17. detachKeys := cmd.String([]string{"-detach-keys"}, "", "Override the key sequence for detaching a container")
  18. execConfig, err := ParseExec(cmd, args)
  19. container := cmd.Arg(0)
  20. // just in case the ParseExec does not exit
  21. if container == "" || err != nil {
  22. return Cli.StatusError{StatusCode: 1}
  23. }
  24. if *detachKeys != "" {
  25. cli.configFile.DetachKeys = *detachKeys
  26. }
  27. // Send client escape keys
  28. execConfig.DetachKeys = cli.configFile.DetachKeys
  29. ctx := context.Background()
  30. response, err := cli.client.ContainerExecCreate(ctx, container, *execConfig)
  31. if err != nil {
  32. return err
  33. }
  34. execID := response.ID
  35. if execID == "" {
  36. fmt.Fprintf(cli.out, "exec ID empty")
  37. return nil
  38. }
  39. //Temp struct for execStart so that we don't need to transfer all the execConfig
  40. if !execConfig.Detach {
  41. if err := cli.CheckTtyInput(execConfig.AttachStdin, execConfig.Tty); err != nil {
  42. return err
  43. }
  44. } else {
  45. execStartCheck := types.ExecStartCheck{
  46. Detach: execConfig.Detach,
  47. Tty: execConfig.Tty,
  48. }
  49. if err := cli.client.ContainerExecStart(ctx, execID, execStartCheck); err != nil {
  50. return err
  51. }
  52. // For now don't print this - wait for when we support exec wait()
  53. // fmt.Fprintf(cli.out, "%s\n", execID)
  54. return nil
  55. }
  56. // Interactive exec requested.
  57. var (
  58. out, stderr io.Writer
  59. in io.ReadCloser
  60. errCh chan error
  61. )
  62. if execConfig.AttachStdin {
  63. in = cli.in
  64. }
  65. if execConfig.AttachStdout {
  66. out = cli.out
  67. }
  68. if execConfig.AttachStderr {
  69. if execConfig.Tty {
  70. stderr = cli.out
  71. } else {
  72. stderr = cli.err
  73. }
  74. }
  75. resp, err := cli.client.ContainerExecAttach(ctx, execID, *execConfig)
  76. if err != nil {
  77. return err
  78. }
  79. defer resp.Close()
  80. errCh = promise.Go(func() error {
  81. return cli.HoldHijackedConnection(ctx, execConfig.Tty, in, out, stderr, resp)
  82. })
  83. if execConfig.Tty && cli.isTerminalIn {
  84. if err := cli.MonitorTtySize(ctx, execID, true); err != nil {
  85. fmt.Fprintf(cli.err, "Error monitoring TTY size: %s\n", err)
  86. }
  87. }
  88. if err := <-errCh; err != nil {
  89. logrus.Debugf("Error hijack: %s", err)
  90. return err
  91. }
  92. var status int
  93. if _, status, err = cli.getExecExitCode(ctx, execID); err != nil {
  94. return err
  95. }
  96. if status != 0 {
  97. return Cli.StatusError{StatusCode: status}
  98. }
  99. return nil
  100. }
  101. // ParseExec parses the specified args for the specified command and generates
  102. // an ExecConfig from it.
  103. // If the minimal number of specified args is not right or if specified args are
  104. // not valid, it will return an error.
  105. func ParseExec(cmd *flag.FlagSet, args []string) (*types.ExecConfig, error) {
  106. var (
  107. flStdin = cmd.Bool([]string{"i", "-interactive"}, false, "Keep STDIN open even if not attached")
  108. flTty = cmd.Bool([]string{"t", "-tty"}, false, "Allocate a pseudo-TTY")
  109. flDetach = cmd.Bool([]string{"d", "-detach"}, false, "Detached mode: run command in the background")
  110. flUser = cmd.String([]string{"u", "-user"}, "", "Username or UID (format: <name|uid>[:<group|gid>])")
  111. flPrivileged = cmd.Bool([]string{"-privileged"}, false, "Give extended privileges to the command")
  112. execCmd []string
  113. )
  114. cmd.Require(flag.Min, 2)
  115. if err := cmd.ParseFlags(args, true); err != nil {
  116. return nil, err
  117. }
  118. parsedArgs := cmd.Args()
  119. execCmd = parsedArgs[1:]
  120. execConfig := &types.ExecConfig{
  121. User: *flUser,
  122. Privileged: *flPrivileged,
  123. Tty: *flTty,
  124. Cmd: execCmd,
  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. }