2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2016-03-16 15:19:22 -04:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-05-05 00:18:28 -04:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-12-21 17:43:59 -05:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
2015-03-24 23:57:23 -04:00
|
|
|
"github.com/docker/docker/pkg/promise"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types"
|
2015-03-24 23:57:23 -04:00
|
|
|
)
|
|
|
|
|
2015-03-25 13:34:41 -04:00
|
|
|
// CmdExec runs a command in a running container.
|
|
|
|
//
|
|
|
|
// Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
|
2015-03-24 23:57:23 -04:00
|
|
|
func (cli *DockerCli) CmdExec(args ...string) error {
|
2015-10-08 08:46:21 -04:00
|
|
|
cmd := Cli.Subcmd("exec", []string{"CONTAINER COMMAND [ARG...]"}, Cli.DockerCommands["exec"].Description, true)
|
2016-01-03 17:03:39 -05:00
|
|
|
detachKeys := cmd.String([]string{"-detach-keys"}, "", "Override the key sequence for detaching a container")
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-12-21 17:43:59 -05:00
|
|
|
execConfig, err := ParseExec(cmd, args)
|
2016-04-13 04:33:46 -04:00
|
|
|
container := cmd.Arg(0)
|
2015-03-24 23:57:23 -04:00
|
|
|
// just in case the ParseExec does not exit
|
2016-04-13 04:33:46 -04:00
|
|
|
if container == "" || err != nil {
|
2015-05-05 00:18:28 -04:00
|
|
|
return Cli.StatusError{StatusCode: 1}
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
2016-01-03 17:03:39 -05:00
|
|
|
if *detachKeys != "" {
|
|
|
|
cli.configFile.DetachKeys = *detachKeys
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send client escape keys
|
|
|
|
execConfig.DetachKeys = cli.configFile.DetachKeys
|
|
|
|
|
2016-05-21 09:57:57 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
response, err := cli.client.ContainerExecCreate(ctx, container, *execConfig)
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
execID := response.ID
|
|
|
|
if execID == "" {
|
|
|
|
fmt.Fprintf(cli.out, "exec ID empty")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-17 01:36:23 -04:00
|
|
|
//Temp struct for execStart so that we don't need to transfer all the execConfig
|
2015-03-24 23:57:23 -04:00
|
|
|
if !execConfig.Detach {
|
|
|
|
if err := cli.CheckTtyInput(execConfig.AttachStdin, execConfig.Tty); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2015-12-06 00:33:38 -05:00
|
|
|
execStartCheck := types.ExecStartCheck{
|
|
|
|
Detach: execConfig.Detach,
|
|
|
|
Tty: execConfig.Tty,
|
|
|
|
}
|
|
|
|
|
2016-05-21 09:57:57 -04:00
|
|
|
if err := cli.client.ContainerExecStart(ctx, execID, execStartCheck); err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// For now don't print this - wait for when we support exec wait()
|
|
|
|
// fmt.Fprintf(cli.out, "%s\n", execID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interactive exec requested.
|
|
|
|
var (
|
|
|
|
out, stderr io.Writer
|
|
|
|
in io.ReadCloser
|
|
|
|
errCh chan error
|
|
|
|
)
|
|
|
|
|
|
|
|
if execConfig.AttachStdin {
|
|
|
|
in = cli.in
|
|
|
|
}
|
|
|
|
if execConfig.AttachStdout {
|
|
|
|
out = cli.out
|
|
|
|
}
|
|
|
|
if execConfig.AttachStderr {
|
|
|
|
if execConfig.Tty {
|
|
|
|
stderr = cli.out
|
|
|
|
} else {
|
|
|
|
stderr = cli.err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-21 09:57:57 -04:00
|
|
|
resp, err := cli.client.ContainerExecAttach(ctx, execID, *execConfig)
|
2015-12-06 00:33:38 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2015-12-06 00:33:38 -05:00
|
|
|
defer resp.Close()
|
|
|
|
errCh = promise.Go(func() error {
|
2016-05-31 19:49:32 -04:00
|
|
|
return cli.HoldHijackedConnection(ctx, execConfig.Tty, in, out, stderr, resp)
|
2015-12-06 00:33:38 -05:00
|
|
|
})
|
2015-03-24 23:57:23 -04:00
|
|
|
|
|
|
|
if execConfig.Tty && cli.isTerminalIn {
|
2016-05-31 19:49:32 -04:00
|
|
|
if err := cli.MonitorTtySize(ctx, execID, true); err != nil {
|
2015-05-09 14:33:06 -04:00
|
|
|
fmt.Fprintf(cli.err, "Error monitoring TTY size: %s\n", err)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := <-errCh; err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Error hijack: %s", err)
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var status int
|
2016-05-21 09:57:57 -04:00
|
|
|
if _, status, err = cli.getExecExitCode(ctx, execID); err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if status != 0 {
|
2015-05-05 00:18:28 -04:00
|
|
|
return Cli.StatusError{StatusCode: status}
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-12-21 17:43:59 -05:00
|
|
|
|
|
|
|
// ParseExec parses the specified args for the specified command and generates
|
|
|
|
// an ExecConfig from it.
|
|
|
|
// If the minimal number of specified args is not right or if specified args are
|
|
|
|
// not valid, it will return an error.
|
|
|
|
func ParseExec(cmd *flag.FlagSet, args []string) (*types.ExecConfig, error) {
|
|
|
|
var (
|
|
|
|
flStdin = cmd.Bool([]string{"i", "-interactive"}, false, "Keep STDIN open even if not attached")
|
|
|
|
flTty = cmd.Bool([]string{"t", "-tty"}, false, "Allocate a pseudo-TTY")
|
|
|
|
flDetach = cmd.Bool([]string{"d", "-detach"}, false, "Detached mode: run command in the background")
|
|
|
|
flUser = cmd.String([]string{"u", "-user"}, "", "Username or UID (format: <name|uid>[:<group|gid>])")
|
|
|
|
flPrivileged = cmd.Bool([]string{"-privileged"}, false, "Give extended privileges to the command")
|
|
|
|
execCmd []string
|
|
|
|
)
|
|
|
|
cmd.Require(flag.Min, 2)
|
|
|
|
if err := cmd.ParseFlags(args, true); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
parsedArgs := cmd.Args()
|
|
|
|
execCmd = parsedArgs[1:]
|
|
|
|
|
|
|
|
execConfig := &types.ExecConfig{
|
|
|
|
User: *flUser,
|
|
|
|
Privileged: *flPrivileged,
|
|
|
|
Tty: *flTty,
|
|
|
|
Cmd: execCmd,
|
|
|
|
Detach: *flDetach,
|
|
|
|
}
|
|
|
|
|
|
|
|
// If -d is not set, attach to everything by default
|
|
|
|
if !*flDetach {
|
|
|
|
execConfig.AttachStdout = true
|
|
|
|
execConfig.AttachStderr = true
|
|
|
|
if *flStdin {
|
|
|
|
execConfig.AttachStdin = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return execConfig, nil
|
|
|
|
}
|