2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"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"
|
|
|
|
"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 {
|
|
|
|
cmd := cli.Subcmd("exec", "CONTAINER COMMAND [ARG...]", "Run a command in a running container", true)
|
|
|
|
|
|
|
|
execConfig, err := runconfig.ParseExec(cmd, args)
|
|
|
|
// just in case the ParseExec does not exit
|
|
|
|
if execConfig.Container == "" || err != nil {
|
2015-04-15 04:57:52 -04:00
|
|
|
return StatusError{StatusCode: 1}
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
2015-01-12 14:56:01 -05:00
|
|
|
stream, _, err := cli.call("POST", "/containers/"+execConfig.Container+"/exec", execConfig, nil)
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var response types.ContainerExecCreateResponse
|
|
|
|
if err := json.NewDecoder(stream).Decode(&response); 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
|
|
|
|
execStartCheck := &types.ExecStartCheck{
|
|
|
|
Detach: execConfig.Detach,
|
|
|
|
Tty: execConfig.Tty,
|
|
|
|
}
|
|
|
|
|
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-04-17 01:36:23 -04:00
|
|
|
if _, _, err := readBody(cli.call("POST", "/exec/"+execID+"/start", execStartCheck, nil)); 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
|
|
|
|
hijacked = make(chan io.Closer)
|
|
|
|
errCh chan error
|
|
|
|
)
|
|
|
|
|
|
|
|
// Block the return until the chan gets closed
|
|
|
|
defer func() {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("End of CmdExec(), Waiting for hijack to finish.")
|
2015-03-24 23:57:23 -04:00
|
|
|
if _, ok := <-hijacked; ok {
|
2015-05-09 14:33:06 -04:00
|
|
|
fmt.Fprintln(cli.err, "Hijack did not finish (chan still open)")
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
errCh = promise.Go(func() error {
|
|
|
|
return cli.hijack("POST", "/exec/"+execID+"/start", execConfig.Tty, in, out, stderr, hijacked, execConfig)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Acknowledge the hijack before starting
|
|
|
|
select {
|
|
|
|
case closer := <-hijacked:
|
|
|
|
// Make sure that hijack gets closed when returning. (result
|
|
|
|
// in closing hijack chan and freeing server's goroutines.
|
|
|
|
if closer != nil {
|
|
|
|
defer closer.Close()
|
|
|
|
}
|
|
|
|
case err := <-errCh:
|
|
|
|
if 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-15 04:57:52 -04:00
|
|
|
return StatusError{StatusCode: status}
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|