浏览代码

Embed exec.Cmd on Process

Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
Michael Crosby 11 年之前
父节点
当前提交
5573c744e4
共有 3 个文件被更改,包括 25 次插入69 次删除
  1. 2 2
      container.go
  2. 6 40
      execdriver/driver.go
  3. 17 27
      execdriver/lxc/driver.go

+ 2 - 2
container.go

@@ -733,10 +733,10 @@ func (container *Container) Start() (err error) {
 	container.process = &execdriver.Process{
 		Name:       container.ID,
 		Privileged: container.hostConfig.Privileged,
-		Dir:        root,
+		Rootfs:     root,
 		InitPath:   "/.dockerinit",
 		Entrypoint: container.Path,
-		Args:       container.Args,
+		Arguments:  container.Args,
 		WorkingDir: workingDir,
 		ConfigPath: container.lxcConfigPath(),
 		Network:    en,

+ 6 - 40
execdriver/driver.go

@@ -2,7 +2,6 @@ package execdriver
 
 import (
 	"errors"
-	"io"
 	"os/exec"
 	"syscall"
 	"time"
@@ -28,59 +27,26 @@ type Network struct {
 }
 
 type Process struct {
+	exec.Cmd
+
 	Name       string // unique name for the conatienr
 	Privileged bool
 	User       string
-	Dir        string // root fs of the container
+	Rootfs     string // root fs of the container
 	InitPath   string // dockerinit
 	Entrypoint string
-	Args       []string
-	//	Environment map[string]string // we don't use this right now because we use an env file
+	Arguments  []string
 	WorkingDir string
 	ConfigPath string
 	Tty        bool
 	Network    *Network // if network is nil then networking is disabled
-	Stdin      io.Reader
-	Stdout     io.Writer
-	Stderr     io.Writer
-
-	cmd *exec.Cmd
-}
-
-func (c *Process) SetCmd(cmd *exec.Cmd) error {
-	c.cmd = cmd
-	cmd.Stdout = c.Stdout
-	cmd.Stderr = c.Stderr
-	cmd.Stdin = c.Stdin
-	return nil
 }
 
 func (c *Process) Pid() int {
-	return c.cmd.Process.Pid
-}
-
-func (c *Process) StdinPipe() (io.WriteCloser, error) {
-	return c.cmd.StdinPipe()
-}
-
-func (c *Process) StderrPipe() (io.ReadCloser, error) {
-	return c.cmd.StderrPipe()
-}
-
-func (c *Process) StdoutPipe() (io.ReadCloser, error) {
-	return c.cmd.StdoutPipe()
+	return c.Process.Pid
 }
 
 func (c *Process) GetExitCode() int {
-	if c.cmd != nil {
-		return c.cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
-	}
+	return c.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
 	return -1
 }
-
-func (c *Process) Wait() error {
-	if c.cmd != nil {
-		return c.cmd.Wait()
-	}
-	return ErrCommandIsNil
-}

+ 17 - 27
execdriver/lxc/driver.go

@@ -67,22 +67,28 @@ func (d *driver) Start(c *execdriver.Process) error {
 	}
 
 	params = append(params, "--", c.Entrypoint)
-	params = append(params, c.Args...)
+	params = append(params, c.Arguments...)
 
-	cmd := exec.Command(params[0], params[1:]...)
-	cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
-	cmd.SysProcAttr.Setctty = true
-
-	if err := c.SetCmd(cmd); err != nil {
-		return err
+	var (
+		name = params[0]
+		arg  = params[1:]
+	)
+	aname, err := exec.LookPath(name)
+	if err != nil {
+		aname = name
 	}
+	c.Path = aname
+	c.Args = append([]string{name}, arg...)
+
+	c.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
+	c.SysProcAttr.Setctty = true
 
-	if err := cmd.Start(); err != nil {
+	if err := c.Start(); err != nil {
 		return err
 	}
 
 	// Poll for running
-	if err := d.waitForStart(cmd, c); err != nil {
+	if err := d.waitForStart(c); err != nil {
 		return err
 	}
 	return nil
@@ -113,11 +119,11 @@ func (d *driver) Wait(c *execdriver.Process, duration time.Duration) error {
 
 // If seconds < 0 then wait forever
 func (d *driver) wait(c *execdriver.Process, duration time.Duration) error {
-begin:
 	var (
 		killer bool
 		done   = d.waitCmd(c)
 	)
+begin:
 	if duration > 0 {
 		select {
 		case err := <-done:
@@ -146,23 +152,7 @@ func (d *driver) kill(c *execdriver.Process, sig int) error {
 	return exec.Command("lxc-kill", "-n", c.Name, strconv.Itoa(sig)).Run()
 }
 
-/* Generate the lxc configuration and return the path to the file
-func (d *driver) generateConfig(c *execdriver.Process) (string, error) {
-	p := path.Join(d.root, c.Name)
-	f, err := os.Create(p)
-	if err != nil {
-		return "", nil
-	}
-	defer f.Close()
-
-	if err := LxcTemplateCompiled.Execute(f, c.Context); err != nil {
-		return "", err
-	}
-	return p, nil
-}
-*/
-
-func (d *driver) waitForStart(cmd *exec.Cmd, c *execdriver.Process) error {
+func (d *driver) waitForStart(c *execdriver.Process) error {
 	// We wait for the container to be fully running.
 	// Timeout after 5 seconds. In case of broken pipe, just retry.
 	// Note: The container can run and finish correctly before