2014-09-09 00:19:32 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2014-09-23 17:47:33 -04:00
|
|
|
"strings"
|
2014-09-15 18:56:47 -04:00
|
|
|
"sync"
|
2014-09-09 00:19:32 -04:00
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-09-09 00:19:32 -04:00
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
2014-09-23 17:47:33 -04:00
|
|
|
"github.com/docker/docker/daemon/execdriver/lxc"
|
2014-09-09 00:19:32 -04:00
|
|
|
"github.com/docker/docker/engine"
|
|
|
|
"github.com/docker/docker/pkg/broadcastwriter"
|
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2014-09-30 02:16:27 -04:00
|
|
|
"github.com/docker/docker/pkg/promise"
|
2015-03-24 07:25:26 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2014-09-09 00:19:32 -04:00
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
)
|
|
|
|
|
2014-09-15 18:56:47 -04:00
|
|
|
type execConfig struct {
|
2014-09-15 19:14:04 -04:00
|
|
|
sync.Mutex
|
2014-09-15 18:56:47 -04:00
|
|
|
ID string
|
2014-09-16 02:43:43 -04:00
|
|
|
Running bool
|
2014-11-17 18:50:09 -05:00
|
|
|
ExitCode int
|
2014-09-09 00:19:32 -04:00
|
|
|
ProcessConfig execdriver.ProcessConfig
|
2014-09-09 13:36:13 -04:00
|
|
|
StreamConfig
|
2014-09-15 18:56:47 -04:00
|
|
|
OpenStdin bool
|
|
|
|
OpenStderr bool
|
|
|
|
OpenStdout bool
|
|
|
|
Container *Container
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
|
2014-09-15 18:56:47 -04:00
|
|
|
type execStore struct {
|
|
|
|
s map[string]*execConfig
|
2014-12-23 17:03:20 -05:00
|
|
|
sync.RWMutex
|
2014-09-15 18:56:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func newExecStore() *execStore {
|
|
|
|
return &execStore{s: make(map[string]*execConfig, 0)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *execStore) Add(id string, execConfig *execConfig) {
|
|
|
|
e.Lock()
|
|
|
|
e.s[id] = execConfig
|
|
|
|
e.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *execStore) Get(id string) *execConfig {
|
2014-12-23 17:03:20 -05:00
|
|
|
e.RLock()
|
2014-09-15 18:56:47 -04:00
|
|
|
res := e.s[id]
|
2014-12-23 17:03:20 -05:00
|
|
|
e.RUnlock()
|
2014-09-15 18:56:47 -04:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *execStore) Delete(id string) {
|
|
|
|
e.Lock()
|
|
|
|
delete(e.s, id)
|
|
|
|
e.Unlock()
|
|
|
|
}
|
|
|
|
|
2014-12-23 17:03:20 -05:00
|
|
|
func (e *execStore) List() []string {
|
|
|
|
var IDs []string
|
|
|
|
e.RLock()
|
2014-12-23 19:03:24 -05:00
|
|
|
for id := range e.s {
|
2014-12-23 17:03:20 -05:00
|
|
|
IDs = append(IDs, id)
|
|
|
|
}
|
|
|
|
e.RUnlock()
|
|
|
|
return IDs
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:56:47 -04:00
|
|
|
func (execConfig *execConfig) Resize(h, w int) error {
|
|
|
|
return execConfig.ProcessConfig.Terminal.Resize(h, w)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Daemon) registerExecCommand(execConfig *execConfig) {
|
2015-02-26 06:03:44 -05:00
|
|
|
// Storing execs in container in order to kill them gracefully whenever the container is stopped or removed.
|
2014-09-15 18:56:47 -04:00
|
|
|
execConfig.Container.execCommands.Add(execConfig.ID, execConfig)
|
|
|
|
// Storing execs in daemon for easy access via remote API.
|
|
|
|
d.execCommands.Add(execConfig.ID, execConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Daemon) getExecConfig(name string) (*execConfig, error) {
|
|
|
|
if execConfig := d.execCommands.Get(name); execConfig != nil {
|
|
|
|
if !execConfig.Container.IsRunning() {
|
2014-09-25 17:23:27 -04:00
|
|
|
return nil, fmt.Errorf("Container %s is not running", execConfig.Container.ID)
|
2014-09-15 18:56:47 -04:00
|
|
|
}
|
|
|
|
return execConfig, nil
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
|
2014-09-17 14:36:51 -04:00
|
|
|
return nil, fmt.Errorf("No such exec instance '%s' found in daemon", name)
|
2014-09-15 18:56:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Daemon) unregisterExecCommand(execConfig *execConfig) {
|
|
|
|
execConfig.Container.execCommands.Delete(execConfig.ID)
|
|
|
|
d.execCommands.Delete(execConfig.ID)
|
|
|
|
}
|
2014-09-09 00:19:32 -04:00
|
|
|
|
2014-09-15 18:56:47 -04:00
|
|
|
func (d *Daemon) getActiveContainer(name string) (*Container, error) {
|
2014-12-16 18:06:35 -05:00
|
|
|
container, err := d.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
|
2014-09-10 20:06:59 -04:00
|
|
|
if !container.IsRunning() {
|
2014-09-25 17:23:27 -04:00
|
|
|
return nil, fmt.Errorf("Container %s is not running", name)
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
2014-11-27 11:08:39 -05:00
|
|
|
if container.IsPaused() {
|
|
|
|
return nil, fmt.Errorf("Container %s is paused, unpause the container before exec", name)
|
|
|
|
}
|
2014-09-15 18:56:47 -04:00
|
|
|
return container, nil
|
|
|
|
}
|
2014-09-09 00:19:32 -04:00
|
|
|
|
2015-03-25 03:44:12 -04:00
|
|
|
func (d *Daemon) ContainerExecCreate(job *engine.Job) error {
|
2014-09-15 18:56:47 -04:00
|
|
|
if len(job.Args) != 1 {
|
2015-03-25 03:44:12 -04:00
|
|
|
return fmt.Errorf("Usage: %s [options] container command [args]", job.Name)
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
2014-09-15 18:56:47 -04:00
|
|
|
|
2014-09-23 17:47:33 -04:00
|
|
|
if strings.HasPrefix(d.execDriver.Name(), lxc.DriverName) {
|
2015-03-25 03:44:12 -04:00
|
|
|
return lxc.ErrExec
|
2014-09-23 17:47:33 -04:00
|
|
|
}
|
|
|
|
|
2014-09-15 18:56:47 -04:00
|
|
|
var name = job.Args[0]
|
|
|
|
|
|
|
|
container, err := d.getActiveContainer(name)
|
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
|
2014-12-01 12:16:49 -05:00
|
|
|
config, err := runconfig.ExecConfigFromJob(job)
|
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-12-01 12:16:49 -05:00
|
|
|
}
|
2014-09-15 18:56:47 -04:00
|
|
|
|
2014-09-09 00:19:32 -04:00
|
|
|
entrypoint, args := d.getEntrypointAndArgs(nil, config.Cmd)
|
|
|
|
|
|
|
|
processConfig := execdriver.ProcessConfig{
|
|
|
|
Tty: config.Tty,
|
|
|
|
Entrypoint: entrypoint,
|
|
|
|
Arguments: args,
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:56:47 -04:00
|
|
|
execConfig := &execConfig{
|
2015-03-24 07:25:26 -04:00
|
|
|
ID: stringid.GenerateRandomID(),
|
2014-09-09 00:19:32 -04:00
|
|
|
OpenStdin: config.AttachStdin,
|
2014-09-15 18:56:47 -04:00
|
|
|
OpenStdout: config.AttachStdout,
|
|
|
|
OpenStderr: config.AttachStderr,
|
2014-09-09 00:19:32 -04:00
|
|
|
StreamConfig: StreamConfig{},
|
|
|
|
ProcessConfig: processConfig,
|
2014-09-15 18:56:47 -04:00
|
|
|
Container: container,
|
2014-09-16 02:43:43 -04:00
|
|
|
Running: false,
|
2014-09-15 18:56:47 -04:00
|
|
|
}
|
|
|
|
|
2014-11-14 06:34:59 -05:00
|
|
|
container.LogEvent("exec_create: " + execConfig.ProcessConfig.Entrypoint + " " + strings.Join(execConfig.ProcessConfig.Arguments, " "))
|
|
|
|
|
2014-09-15 18:56:47 -04:00
|
|
|
d.registerExecCommand(execConfig)
|
|
|
|
|
|
|
|
job.Printf("%s\n", execConfig.ID)
|
|
|
|
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-09-15 18:56:47 -04:00
|
|
|
}
|
|
|
|
|
2015-03-25 03:44:12 -04:00
|
|
|
func (d *Daemon) ContainerExecStart(job *engine.Job) error {
|
2014-09-16 02:43:43 -04:00
|
|
|
if len(job.Args) != 1 {
|
2015-03-25 03:44:12 -04:00
|
|
|
return fmt.Errorf("Usage: %s [options] exec", job.Name)
|
2014-09-15 18:56:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
cStdin io.ReadCloser
|
|
|
|
cStdout, cStderr io.Writer
|
|
|
|
execName = job.Args[0]
|
|
|
|
)
|
|
|
|
|
2014-09-15 19:14:04 -04:00
|
|
|
execConfig, err := d.getExecConfig(execName)
|
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-09-15 18:56:47 -04:00
|
|
|
}
|
|
|
|
|
2014-09-15 19:14:04 -04:00
|
|
|
func() {
|
|
|
|
execConfig.Lock()
|
|
|
|
defer execConfig.Unlock()
|
|
|
|
if execConfig.Running {
|
|
|
|
err = fmt.Errorf("Error: Exec command %s is already running", execName)
|
|
|
|
}
|
|
|
|
execConfig.Running = true
|
|
|
|
}()
|
2014-09-15 18:56:47 -04:00
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-09-15 18:56:47 -04:00
|
|
|
}
|
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("starting exec command %s in container %s", execConfig.ID, execConfig.Container.ID)
|
2014-09-15 18:56:47 -04:00
|
|
|
container := execConfig.Container
|
|
|
|
|
2014-11-14 06:34:59 -05:00
|
|
|
container.LogEvent("exec_start: " + execConfig.ProcessConfig.Entrypoint + " " + strings.Join(execConfig.ProcessConfig.Arguments, " "))
|
|
|
|
|
2014-09-15 18:56:47 -04:00
|
|
|
if execConfig.OpenStdin {
|
|
|
|
r, w := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
defer w.Close()
|
2015-03-26 18:22:04 -04:00
|
|
|
defer logrus.Debugf("Closing buffered stdin pipe")
|
2014-09-15 18:56:47 -04:00
|
|
|
io.Copy(w, job.Stdin)
|
|
|
|
}()
|
|
|
|
cStdin = r
|
|
|
|
}
|
|
|
|
if execConfig.OpenStdout {
|
|
|
|
cStdout = job.Stdout
|
|
|
|
}
|
|
|
|
if execConfig.OpenStderr {
|
|
|
|
cStderr = job.Stderr
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
execConfig.StreamConfig.stderr = broadcastwriter.New()
|
|
|
|
execConfig.StreamConfig.stdout = broadcastwriter.New()
|
|
|
|
// Attach to stdin
|
|
|
|
if execConfig.OpenStdin {
|
|
|
|
execConfig.StreamConfig.stdin, execConfig.StreamConfig.stdinPipe = io.Pipe()
|
|
|
|
} else {
|
|
|
|
execConfig.StreamConfig.stdinPipe = ioutils.NopWriteCloser(ioutil.Discard) // Silently drop stdin
|
|
|
|
}
|
|
|
|
|
2015-04-06 15:19:38 -04:00
|
|
|
attachErr := attach(&execConfig.StreamConfig, execConfig.OpenStdin, true, execConfig.ProcessConfig.Tty, cStdin, cStdout, cStderr)
|
2014-09-09 00:19:32 -04:00
|
|
|
|
2014-09-10 03:20:05 -04:00
|
|
|
execErr := make(chan error)
|
2014-09-15 18:56:47 -04:00
|
|
|
|
2014-11-17 18:50:09 -05:00
|
|
|
// Note, the execConfig data will be removed when the container
|
|
|
|
// itself is deleted. This allows us to query it (for things like
|
|
|
|
// the exitStatus) even after the cmd is done running.
|
2014-09-15 18:56:47 -04:00
|
|
|
|
2014-09-09 00:19:32 -04:00
|
|
|
go func() {
|
|
|
|
err := container.Exec(execConfig)
|
|
|
|
if err != nil {
|
2014-09-15 18:56:47 -04:00
|
|
|
execErr <- fmt.Errorf("Cannot run exec command %s in container %s: %s", execName, container.ID, err)
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-attachErr:
|
2014-09-10 03:20:05 -04:00
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return fmt.Errorf("attach failed with error: %s", err)
|
2014-09-10 03:20:05 -04:00
|
|
|
}
|
|
|
|
break
|
2014-09-09 00:19:32 -04:00
|
|
|
case err := <-execErr:
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
|
2014-09-15 18:56:47 -04:00
|
|
|
func (d *Daemon) Exec(c *Container, execConfig *execConfig, pipes *execdriver.Pipes, startCallback execdriver.StartCallback) (int, error) {
|
2014-11-17 18:50:09 -05:00
|
|
|
exitStatus, err := d.execDriver.Exec(c.command, &execConfig.ProcessConfig, pipes, startCallback)
|
|
|
|
|
|
|
|
// On err, make sure we don't leave ExitCode at zero
|
|
|
|
if err != nil && exitStatus == 0 {
|
|
|
|
exitStatus = 128
|
|
|
|
}
|
|
|
|
|
|
|
|
execConfig.ExitCode = exitStatus
|
|
|
|
execConfig.Running = false
|
|
|
|
|
|
|
|
return exitStatus, err
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
|
2014-12-23 17:03:20 -05:00
|
|
|
func (container *Container) GetExecIDs() []string {
|
|
|
|
return container.execCommands.List()
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:56:47 -04:00
|
|
|
func (container *Container) Exec(execConfig *execConfig) error {
|
2014-09-09 00:19:32 -04:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
|
|
|
|
waitStart := make(chan struct{})
|
|
|
|
|
|
|
|
callback := func(processConfig *execdriver.ProcessConfig, pid int) {
|
|
|
|
if processConfig.Tty {
|
|
|
|
// The callback is called after the process Start()
|
2014-09-28 11:01:35 -04:00
|
|
|
// so we are in the parent process. In TTY mode, stdin/out/err is the PtySlave
|
2014-09-09 00:19:32 -04:00
|
|
|
// which we close here.
|
|
|
|
if c, ok := processConfig.Stdout.(io.Closer); ok {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(waitStart)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We use a callback here instead of a goroutine and an chan for
|
|
|
|
// syncronization purposes
|
2014-09-30 02:16:27 -04:00
|
|
|
cErr := promise.Go(func() error { return container.monitorExec(execConfig, callback) })
|
2014-09-09 00:19:32 -04:00
|
|
|
|
|
|
|
// Exec should not return until the process is actually running
|
|
|
|
select {
|
|
|
|
case <-waitStart:
|
|
|
|
case err := <-cErr:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:56:47 -04:00
|
|
|
func (container *Container) monitorExec(execConfig *execConfig, callback execdriver.StartCallback) error {
|
2014-09-09 00:19:32 -04:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
exitCode int
|
|
|
|
)
|
|
|
|
|
|
|
|
pipes := execdriver.NewPipes(execConfig.StreamConfig.stdin, execConfig.StreamConfig.stdout, execConfig.StreamConfig.stderr, execConfig.OpenStdin)
|
|
|
|
exitCode, err = container.daemon.Exec(container, execConfig, pipes, callback)
|
|
|
|
if err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("Error running command in existing container %s: %s", container.ID, err)
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Exec task in container %s exited with code %d", container.ID, exitCode)
|
2014-09-09 00:19:32 -04:00
|
|
|
if execConfig.OpenStdin {
|
|
|
|
if err := execConfig.StreamConfig.stdin.Close(); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("Error closing stdin while running in %s: %s", container.ID, err)
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := execConfig.StreamConfig.stdout.Clean(); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("Error closing stdout while running in %s: %s", container.ID, err)
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
if err := execConfig.StreamConfig.stderr.Clean(); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("Error closing stderr while running in %s: %s", container.ID, err)
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
if execConfig.ProcessConfig.Terminal != nil {
|
|
|
|
if err := execConfig.ProcessConfig.Terminal.Close(); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("Error closing terminal while running in container %s: %s", container.ID, err)
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|