From 889b4b10ae3ec1d6e7879c30860aafd7674cb576 Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Wed, 15 Jan 2014 11:46:25 -0800 Subject: [PATCH] Cleanup + add Info to driver in order to have specific IsRunning() Docker-DCO-1.1-Signed-off-by: Guillaume J. Charmes (github: creack) Docker-DCO-1.1-Signed-off-by: Guillaume J. Charmes (github: crosbymichael) --- container.go | 15 ++++++++------- execdriver/chroot/driver.go | 21 ++++++++++++++------- execdriver/driver.go | 6 ++++++ execdriver/lxc/driver.go | 34 ++++++++++++++++++++++++++++++---- runtime.go | 28 +++++++++++++++++----------- 5 files changed, 75 insertions(+), 29 deletions(-) diff --git a/container.go b/container.go index 702d94e391..257925fdda 100644 --- a/container.go +++ b/container.go @@ -697,17 +697,18 @@ func (container *Container) Start() (err error) { } container.waitLock = make(chan struct{}) - // Setup pipes + // Setuping pipes and/or Pty + var setup func() error if container.Config.Tty { - err = container.setupPty() + setup = container.setupPty } else { - err = container.setupStd() + setup = container.setupStd } - if err != nil { + if err := setup(); err != nil { return err } - waitLock := make(chan struct{}) + callbackLock := make(chan struct{}) callback := func(process *execdriver.Process) { container.State.SetRunning(process.Pid()) if process.Tty { @@ -721,7 +722,7 @@ func (container *Container) Start() (err error) { if err := container.ToDisk(); err != nil { utils.Debugf("%s", err) } - close(waitLock) + close(callbackLock) } // We use a callback here instead of a goroutine and an chan for @@ -730,7 +731,7 @@ func (container *Container) Start() (err error) { // Start should not return until the process is actually running select { - case <-waitLock: + case <-callbackLock: case err := <-cErr: return err } diff --git a/execdriver/chroot/driver.go b/execdriver/chroot/driver.go index 1f98bba0ac..be8eb58a1a 100644 --- a/execdriver/chroot/driver.go +++ b/execdriver/chroot/driver.go @@ -7,7 +7,10 @@ import ( "os/exec" ) -const DriverName = "chroot" +const ( + DriverName = "chroot" + Version = "0.1" +) func init() { execdriver.RegisterDockerInitFct(DriverName, func(args *execdriver.DockerInitArgs) error { @@ -32,10 +35,6 @@ func NewDriver() (*driver, error) { return &driver{}, nil } -func (d *driver) Name() string { - return DriverName -} - func (d *driver) Run(c *execdriver.Process, startCallback execdriver.StartCallback) (int, error) { params := []string{ "chroot", @@ -78,6 +77,14 @@ func (d *driver) Wait(id string) error { panic("Not Implemented") } -func (d *driver) Version() string { - return "0.1" +func (d *driver) Info(id string) execdriver.Info { + panic("Not implemented") +} + +func (d *driver) Name() string { + return DriverName +} + +func (d *driver) Version() string { + return Version } diff --git a/execdriver/driver.go b/execdriver/driver.go index ca0a2991f5..3de3369dab 100644 --- a/execdriver/driver.go +++ b/execdriver/driver.go @@ -51,6 +51,10 @@ type DockerInitArgs struct { Driver string } +type Info interface { + IsRunning() bool +} + type Driver interface { Run(c *Process, startCallback StartCallback) (int, error) // Run executes the process and blocks until the process exits and returns the exit code Kill(c *Process, sig int) error @@ -58,6 +62,8 @@ type Driver interface { Wait(id string) error // Wait on an out of process...process - lxc ghosts Version() string Name() string + + Info(id string) Info // "temporary" hack (until we move state from core to plugins) } // Network settings of the container diff --git a/execdriver/lxc/driver.go b/execdriver/lxc/driver.go index 61fc99fae0..1ae136b512 100644 --- a/execdriver/lxc/driver.go +++ b/execdriver/lxc/driver.go @@ -221,9 +221,9 @@ func (d *driver) waitForStart(c *execdriver.Process, waitLock chan struct{}) err default: } - output, err = d.getInfo(c) + output, err = d.getInfo(c.ID) if err != nil { - output, err = d.getInfo(c) + output, err = d.getInfo(c.ID) if err != nil { return err } @@ -236,8 +236,34 @@ func (d *driver) waitForStart(c *execdriver.Process, waitLock chan struct{}) err return execdriver.ErrNotRunning } -func (d *driver) getInfo(c *execdriver.Process) ([]byte, error) { - return exec.Command("lxc-info", "-s", "-n", c.ID).CombinedOutput() +func (d *driver) getInfo(id string) ([]byte, error) { + return exec.Command("lxc-info", "-s", "-n", id).CombinedOutput() +} + +type info struct { + ID string + driver *driver +} + +func (i *info) IsRunning() bool { + var running bool + + output, err := i.driver.getInfo(i.ID) + if err != nil { + panic(err) + } + if strings.Contains(string(output), "RUNNING") { + running = true + } + return running +} + +func (d *driver) Info(id string) execdriver.Info { + + return &info{ + ID: id, + driver: d, + } } func linkLxcStart(root string) error { diff --git a/runtime.go b/runtime.go index 8b7d5d3494..5a11faec9f 100644 --- a/runtime.go +++ b/runtime.go @@ -18,7 +18,6 @@ import ( "io/ioutil" "log" "os" - "os/exec" "path" "regexp" "sort" @@ -164,11 +163,9 @@ func (runtime *Runtime) Register(container *Container) error { // if so, then we need to restart monitor and init a new lock // If the container is supposed to be running, make sure of it if container.State.IsRunning() { - output, err := exec.Command("lxc-info", "-n", container.ID).CombinedOutput() - if err != nil { - return err - } - if !strings.Contains(string(output), "RUNNING") { + info := runtime.execDriver.Info(container.ID) + + if !info.IsRunning() { utils.Debugf("Container %s was supposed to be running but is not.", container.ID) if runtime.config.AutoRestart { utils.Debugf("Restarting") @@ -736,12 +733,21 @@ func NewRuntimeFromDirectory(config *DaemonConfig) (*Runtime, error) { } capabilities := NewRuntimeCapabilities(false) - var ed execdriver.Driver - if driver := os.Getenv("EXEC_DRIVER"); driver == "lxc" { - ed, err = lxc.NewDriver(config.Root, capabilities.AppArmor) - } else { - ed, err = chroot.NewDriver() + + /* + temporarilly disabled. + */ + if false { + var ed execdriver.Driver + if driver := os.Getenv("EXEC_DRIVER"); driver == "lxc" { + ed, err = lxc.NewDriver(config.Root, capabilities.AppArmor) + } else { + ed, err = chroot.NewDriver() + } + if ed != nil { + } } + ed, err := lxc.NewDriver(config.Root, capabilities.AppArmor) if err != nil { return nil, err }