2014-09-09 00:19:32 -04:00
|
|
|
package runconfig
|
|
|
|
|
|
|
|
import (
|
2014-12-01 12:16:49 -05:00
|
|
|
"fmt"
|
|
|
|
|
2014-09-09 00:19:32 -04:00
|
|
|
"github.com/docker/docker/engine"
|
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ExecConfig struct {
|
|
|
|
User string
|
|
|
|
Privileged bool
|
|
|
|
Tty bool
|
|
|
|
Container string
|
|
|
|
AttachStdin bool
|
|
|
|
AttachStderr bool
|
|
|
|
AttachStdout bool
|
|
|
|
Detach bool
|
|
|
|
Cmd []string
|
|
|
|
}
|
|
|
|
|
2014-12-01 12:16:49 -05:00
|
|
|
func ExecConfigFromJob(job *engine.Job) (*ExecConfig, error) {
|
2014-09-09 00:19:32 -04:00
|
|
|
execConfig := &ExecConfig{
|
2014-11-20 13:01:59 -05:00
|
|
|
// TODO(vishh): Expose 'User' once it is supported.
|
|
|
|
//User: job.Getenv("User"),
|
|
|
|
// TODO(vishh): Expose 'Privileged' once it is supported.
|
|
|
|
//Privileged: job.GetenvBool("Privileged"),
|
2014-09-09 00:19:32 -04:00
|
|
|
Tty: job.GetenvBool("Tty"),
|
|
|
|
AttachStdin: job.GetenvBool("AttachStdin"),
|
|
|
|
AttachStderr: job.GetenvBool("AttachStderr"),
|
|
|
|
AttachStdout: job.GetenvBool("AttachStdout"),
|
|
|
|
}
|
2014-12-01 12:16:49 -05:00
|
|
|
cmd := job.GetenvList("Cmd")
|
|
|
|
if len(cmd) == 0 {
|
|
|
|
return nil, fmt.Errorf("No exec command specified")
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
|
2014-12-01 12:16:49 -05:00
|
|
|
execConfig.Cmd = cmd
|
|
|
|
|
|
|
|
return execConfig, nil
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func ParseExec(cmd *flag.FlagSet, args []string) (*ExecConfig, error) {
|
|
|
|
var (
|
2014-09-09 13:36:13 -04:00
|
|
|
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")
|
|
|
|
execCmd []string
|
|
|
|
container string
|
2014-09-09 00:19:32 -04:00
|
|
|
)
|
|
|
|
if err := cmd.Parse(args); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
parsedArgs := cmd.Args()
|
2014-12-01 12:16:49 -05:00
|
|
|
if len(parsedArgs) < 2 {
|
|
|
|
return nil, fmt.Errorf("not enough arguments to create exec command")
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
2014-12-01 12:16:49 -05:00
|
|
|
container = cmd.Arg(0)
|
|
|
|
execCmd = parsedArgs[1:]
|
2014-09-09 00:19:32 -04:00
|
|
|
|
|
|
|
execConfig := &ExecConfig{
|
2014-09-09 13:36:13 -04:00
|
|
|
// TODO(vishh): Expose '-u' flag once it is supported.
|
|
|
|
User: "",
|
|
|
|
// TODO(vishh): Expose '-p' flag once it is supported.
|
|
|
|
Privileged: false,
|
2014-09-09 00:19:32 -04:00
|
|
|
Tty: *flTty,
|
|
|
|
Cmd: execCmd,
|
|
|
|
Container: container,
|
|
|
|
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
|
|
|
|
}
|