2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-03-24 23:57:23 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-05-05 00:18:28 -04:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-03-24 23:57:23 -04:00
|
|
|
"github.com/docker/docker/pkg/promise"
|
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
)
|
|
|
|
|
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)
|
2015-03-24 23:57:23 -04:00
|
|
|
|
|
|
|
execConfig, err := runconfig.ParseExec(cmd, args)
|
|
|
|
// just in case the ParseExec does not exit
|
|
|
|
if execConfig.Container == "" || err != nil {
|
2015-05-05 00:18:28 -04:00
|
|
|
return Cli.StatusError{StatusCode: 1}
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
2015-12-06 00:33:38 -05:00
|
|
|
response, err := cli.client.ContainerExecCreate(*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,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cli.client.ContainerExecStart(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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-06 00:33:38 -05:00
|
|
|
resp, err := cli.client.ContainerExecAttach(execID, *execConfig)
|
|
|
|
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 {
|
|
|
|
return cli.holdHijackedConnection(execConfig.Tty, in, out, stderr, resp)
|
|
|
|
})
|
2015-03-24 23:57:23 -04:00
|
|
|
|
|
|
|
if execConfig.Tty && cli.isTerminalIn {
|
|
|
|
if err := cli.monitorTtySize(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
|
|
|
|
if _, status, err = getExecExitCode(cli, execID); err != nil {
|
|
|
|
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
|
|
|
|
}
|