Cleanup + add Info to driver in order to have specific IsRunning()

Docker-DCO-1.1-Signed-off-by: Guillaume J. Charmes <guillaume.charmes@docker.com> (github: creack)

Docker-DCO-1.1-Signed-off-by: Guillaume J. Charmes <guillaume.charmes@docker.com> (github: crosbymichael)
This commit is contained in:
Guillaume J. Charmes 2014-01-15 11:46:25 -08:00 коммит произвёл Michael Crosby
Родитель f7684ea7f6
Коммит 889b4b10ae
5 изменённых файлов: 75 добавлений и 29 удалений

Просмотреть файл

@ -697,17 +697,18 @@ func (container *Container) Start() (err error) {
} }
container.waitLock = make(chan struct{}) container.waitLock = make(chan struct{})
// Setup pipes // Setuping pipes and/or Pty
var setup func() error
if container.Config.Tty { if container.Config.Tty {
err = container.setupPty() setup = container.setupPty
} else { } else {
err = container.setupStd() setup = container.setupStd
} }
if err != nil { if err := setup(); err != nil {
return err return err
} }
waitLock := make(chan struct{}) callbackLock := make(chan struct{})
callback := func(process *execdriver.Process) { callback := func(process *execdriver.Process) {
container.State.SetRunning(process.Pid()) container.State.SetRunning(process.Pid())
if process.Tty { if process.Tty {
@ -721,7 +722,7 @@ func (container *Container) Start() (err error) {
if err := container.ToDisk(); err != nil { if err := container.ToDisk(); err != nil {
utils.Debugf("%s", err) utils.Debugf("%s", err)
} }
close(waitLock) close(callbackLock)
} }
// We use a callback here instead of a goroutine and an chan for // 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 // Start should not return until the process is actually running
select { select {
case <-waitLock: case <-callbackLock:
case err := <-cErr: case err := <-cErr:
return err return err
} }

Просмотреть файл

@ -7,7 +7,10 @@ import (
"os/exec" "os/exec"
) )
const DriverName = "chroot" const (
DriverName = "chroot"
Version = "0.1"
)
func init() { func init() {
execdriver.RegisterDockerInitFct(DriverName, func(args *execdriver.DockerInitArgs) error { execdriver.RegisterDockerInitFct(DriverName, func(args *execdriver.DockerInitArgs) error {
@ -32,10 +35,6 @@ func NewDriver() (*driver, error) {
return &driver{}, nil return &driver{}, nil
} }
func (d *driver) Name() string {
return DriverName
}
func (d *driver) Run(c *execdriver.Process, startCallback execdriver.StartCallback) (int, error) { func (d *driver) Run(c *execdriver.Process, startCallback execdriver.StartCallback) (int, error) {
params := []string{ params := []string{
"chroot", "chroot",
@ -78,6 +77,14 @@ func (d *driver) Wait(id string) error {
panic("Not Implemented") panic("Not Implemented")
} }
func (d *driver) Version() string { func (d *driver) Info(id string) execdriver.Info {
return "0.1" panic("Not implemented")
}
func (d *driver) Name() string {
return DriverName
}
func (d *driver) Version() string {
return Version
} }

Просмотреть файл

@ -51,6 +51,10 @@ type DockerInitArgs struct {
Driver string Driver string
} }
type Info interface {
IsRunning() bool
}
type Driver interface { 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 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 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 Wait(id string) error // Wait on an out of process...process - lxc ghosts
Version() string Version() string
Name() string Name() string
Info(id string) Info // "temporary" hack (until we move state from core to plugins)
} }
// Network settings of the container // Network settings of the container

Просмотреть файл

@ -221,9 +221,9 @@ func (d *driver) waitForStart(c *execdriver.Process, waitLock chan struct{}) err
default: default:
} }
output, err = d.getInfo(c) output, err = d.getInfo(c.ID)
if err != nil { if err != nil {
output, err = d.getInfo(c) output, err = d.getInfo(c.ID)
if err != nil { if err != nil {
return err return err
} }
@ -236,8 +236,34 @@ func (d *driver) waitForStart(c *execdriver.Process, waitLock chan struct{}) err
return execdriver.ErrNotRunning return execdriver.ErrNotRunning
} }
func (d *driver) getInfo(c *execdriver.Process) ([]byte, error) { func (d *driver) getInfo(id string) ([]byte, error) {
return exec.Command("lxc-info", "-s", "-n", c.ID).CombinedOutput() 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 { func linkLxcStart(root string) error {

Просмотреть файл

@ -18,7 +18,6 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"os/exec"
"path" "path"
"regexp" "regexp"
"sort" "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 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 the container is supposed to be running, make sure of it
if container.State.IsRunning() { if container.State.IsRunning() {
output, err := exec.Command("lxc-info", "-n", container.ID).CombinedOutput() info := runtime.execDriver.Info(container.ID)
if err != nil {
return err if !info.IsRunning() {
}
if !strings.Contains(string(output), "RUNNING") {
utils.Debugf("Container %s was supposed to be running but is not.", container.ID) utils.Debugf("Container %s was supposed to be running but is not.", container.ID)
if runtime.config.AutoRestart { if runtime.config.AutoRestart {
utils.Debugf("Restarting") utils.Debugf("Restarting")
@ -736,12 +733,21 @@ func NewRuntimeFromDirectory(config *DaemonConfig) (*Runtime, error) {
} }
capabilities := NewRuntimeCapabilities(false) capabilities := NewRuntimeCapabilities(false)
var ed execdriver.Driver
if driver := os.Getenv("EXEC_DRIVER"); driver == "lxc" { /*
ed, err = lxc.NewDriver(config.Root, capabilities.AppArmor) temporarilly disabled.
} else { */
ed, err = chroot.NewDriver() 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 { if err != nil {
return nil, err return nil, err
} }