mirror of https://github.com/docker/docs.git
Keep state in core on container
Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
parent
f2680e5a5b
commit
25a2697717
|
|
@ -761,6 +761,8 @@ func (container *Container) Start() (err error) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
container.State.SetRunning(container.process.Pid())
|
||||||
|
|
||||||
// Init the lock
|
// Init the lock
|
||||||
container.waitLock = make(chan struct{})
|
container.waitLock = make(chan struct{})
|
||||||
|
|
||||||
|
|
@ -1161,6 +1163,9 @@ func (container *Container) monitor() {
|
||||||
container.stdin, container.stdinPipe = io.Pipe()
|
container.stdin, container.stdinPipe = io.Pipe()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exitCode := container.process.GetExitCode()
|
||||||
|
container.State.SetStopped(exitCode)
|
||||||
|
|
||||||
// Release the lock
|
// Release the lock
|
||||||
close(container.waitLock)
|
close(container.waitLock)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"sync"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
@ -28,44 +27,7 @@ type Network struct {
|
||||||
Mtu int
|
Mtu int
|
||||||
}
|
}
|
||||||
|
|
||||||
type State struct {
|
|
||||||
sync.RWMutex
|
|
||||||
running bool
|
|
||||||
pid int
|
|
||||||
exitCode int
|
|
||||||
startedAt time.Time
|
|
||||||
finishedAt time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *State) IsRunning() bool {
|
|
||||||
s.RLock()
|
|
||||||
defer s.RUnlock()
|
|
||||||
return s.running
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *State) SetRunning() error {
|
|
||||||
s.Lock()
|
|
||||||
defer s.Unlock()
|
|
||||||
s.running = true
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *State) SetStopped(exitCode int) error {
|
|
||||||
s.Lock()
|
|
||||||
defer s.Unlock()
|
|
||||||
s.exitCode = exitCode
|
|
||||||
s.running = false
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Container / Process / Whatever, we can redefine the conatiner here
|
|
||||||
// to be what it should be and not have to carry the baggage of the
|
|
||||||
// container type in the core with backward compat. This is what a
|
|
||||||
// driver needs to execute a process inside of a conatiner. This is what
|
|
||||||
// a container is at it's core.
|
|
||||||
type Process struct {
|
type Process struct {
|
||||||
State State
|
|
||||||
|
|
||||||
Name string // unique name for the conatienr
|
Name string // unique name for the conatienr
|
||||||
Privileged bool
|
Privileged bool
|
||||||
User string
|
User string
|
||||||
|
|
@ -93,6 +55,10 @@ func (c *Process) SetCmd(cmd *exec.Cmd) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Process) Pid() int {
|
||||||
|
return c.cmd.Process.Pid
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Process) StdinPipe() (io.WriteCloser, error) {
|
func (c *Process) StdinPipe() (io.WriteCloser, error) {
|
||||||
return c.cmd.StdinPipe()
|
return c.cmd.StdinPipe()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,10 +38,6 @@ func NewDriver(root string) (execdriver.Driver, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *driver) Start(c *execdriver.Process) error {
|
func (d *driver) Start(c *execdriver.Process) error {
|
||||||
if c.State.IsRunning() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
params := []string{
|
params := []string{
|
||||||
startPath,
|
startPath,
|
||||||
"-n", c.Name,
|
"-n", c.Name,
|
||||||
|
|
@ -104,13 +100,10 @@ func (d *driver) Stop(c *execdriver.Process) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exitCode := c.GetExitCode()
|
return nil
|
||||||
return c.State.SetStopped(exitCode)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *driver) Kill(c *execdriver.Process, sig int) error {
|
func (d *driver) Kill(c *execdriver.Process, sig int) error {
|
||||||
c.State.Lock()
|
|
||||||
defer c.State.Unlock()
|
|
||||||
return d.kill(c, sig)
|
return d.kill(c, sig)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -146,8 +139,7 @@ begin:
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exitCode := c.GetExitCode()
|
return nil
|
||||||
return c.State.SetStopped(exitCode)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *driver) kill(c *execdriver.Process, sig int) error {
|
func (d *driver) kill(c *execdriver.Process, sig int) error {
|
||||||
|
|
@ -177,9 +169,11 @@ func (d *driver) waitForStart(cmd *exec.Cmd, c *execdriver.Process) error {
|
||||||
// the end of this loop
|
// the end of this loop
|
||||||
for now := time.Now(); time.Since(now) < 5*time.Second; {
|
for now := time.Now(); time.Since(now) < 5*time.Second; {
|
||||||
// If the container dies while waiting for it, just return
|
// If the container dies while waiting for it, just return
|
||||||
if !c.State.IsRunning() {
|
/*
|
||||||
return nil
|
if !c.State.IsRunning() {
|
||||||
}
|
return nil
|
||||||
|
}
|
||||||
|
*/
|
||||||
output, err := exec.Command("lxc-info", "-s", "-n", c.Name).CombinedOutput()
|
output, err := exec.Command("lxc-info", "-s", "-n", c.Name).CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
output, err = exec.Command("lxc-info", "-s", "-n", c.Name).CombinedOutput()
|
output, err = exec.Command("lxc-info", "-s", "-n", c.Name).CombinedOutput()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue