Merge pull request #267 from mrunalp/ctr_oplock

Add operation lock for containers
This commit is contained in:
Antonio Murdaca 2016-12-14 23:17:49 +01:00 committed by GitHub
commit c9dcea486f

View file

@ -143,6 +143,8 @@ func (r *Runtime) CreateContainer(c *Container) error {
// StartContainer starts a container.
func (r *Runtime) StartContainer(c *Container) error {
c.opLock.Lock()
defer c.opLock.Unlock()
if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "start", c.name); err != nil {
return err
}
@ -265,6 +267,8 @@ func (r *Runtime) ExecSync(c *Container, command []string, timeout int64) (resp
// StopContainer stops a container.
func (r *Runtime) StopContainer(c *Container) error {
c.opLock.Lock()
defer c.opLock.Unlock()
if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "kill", c.name); err != nil {
return err
}
@ -291,13 +295,15 @@ func (r *Runtime) StopContainer(c *Container) error {
// DeleteContainer deletes a container.
func (r *Runtime) DeleteContainer(c *Container) error {
c.opLock.Lock()
defer c.opLock.Unlock()
return utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "delete", c.name)
}
// UpdateStatus refreshes the status of the container.
func (r *Runtime) UpdateStatus(c *Container) error {
c.stateLock.Lock()
defer c.stateLock.Unlock()
c.opLock.Lock()
defer c.opLock.Unlock()
out, err := exec.Command(r.path, "state", c.name).CombinedOutput()
if err != nil {
return fmt.Errorf("error getting container state for %s: %s: %q", c.name, err, out)
@ -331,8 +337,8 @@ func (r *Runtime) UpdateStatus(c *Container) error {
// ContainerStatus returns the state of a container.
func (r *Runtime) ContainerStatus(c *Container) *ContainerState {
c.stateLock.Lock()
defer c.stateLock.Unlock()
c.opLock.Lock()
defer c.opLock.Unlock()
return c.state
}
@ -350,7 +356,7 @@ type Container struct {
terminal bool
state *ContainerState
metadata *pb.ContainerMetadata
stateLock sync.Mutex
opLock sync.Mutex
}
// ContainerState represents the status of a container.