Explorar o código

Decouple daemon and container to pause and unpause containers.

Signed-off-by: David Calavera <david.calavera@gmail.com>
David Calavera %!s(int64=9) %!d(string=hai) anos
pai
achega
9f79cfdb2f
Modificáronse 5 ficheiros con 55 adicións e 51 borrados
  1. 2 2
      daemon/commit.go
  2. 0 44
      daemon/container.go
  3. 3 3
      daemon/daemon.go
  4. 25 1
      daemon/pause.go
  5. 25 1
      daemon/unpause.go

+ 2 - 2
daemon/commit.go

@@ -22,8 +22,8 @@ type ContainerCommitConfig struct {
 // The image can optionally be tagged into a repository.
 // The image can optionally be tagged into a repository.
 func (daemon *Daemon) Commit(container *Container, c *ContainerCommitConfig) (*image.Image, error) {
 func (daemon *Daemon) Commit(container *Container, c *ContainerCommitConfig) (*image.Image, error) {
 	if c.Pause && !container.isPaused() {
 	if c.Pause && !container.isPaused() {
-		container.pause()
-		defer container.unpause()
+		daemon.containerPause(container)
+		defer daemon.containerUnpause(container)
 	}
 	}
 
 
 	rwTar, err := daemon.exportContainerRw(container)
 	rwTar, err := daemon.exportContainerRw(container)

+ 0 - 44
daemon/container.go

@@ -343,50 +343,6 @@ func (container *Container) ExitOnNext() {
 	container.monitor.ExitOnNext()
 	container.monitor.ExitOnNext()
 }
 }
 
 
-func (container *Container) pause() error {
-	container.Lock()
-	defer container.Unlock()
-
-	// We cannot Pause the container which is not running
-	if !container.Running {
-		return derr.ErrorCodeNotRunning.WithArgs(container.ID)
-	}
-
-	// We cannot Pause the container which is already paused
-	if container.Paused {
-		return derr.ErrorCodeAlreadyPaused.WithArgs(container.ID)
-	}
-
-	if err := container.daemon.execDriver.Pause(container.command); err != nil {
-		return err
-	}
-	container.Paused = true
-	container.logEvent("pause")
-	return nil
-}
-
-func (container *Container) unpause() error {
-	container.Lock()
-	defer container.Unlock()
-
-	// We cannot unpause the container which is not running
-	if !container.Running {
-		return derr.ErrorCodeNotRunning.WithArgs(container.ID)
-	}
-
-	// We cannot unpause the container which is not paused
-	if !container.Paused {
-		return derr.ErrorCodeNotPaused.WithArgs(container.ID)
-	}
-
-	if err := container.daemon.execDriver.Unpause(container.command); err != nil {
-		return err
-	}
-	container.Paused = false
-	container.logEvent("unpause")
-	return nil
-}
-
 // Resize changes the TTY of the process running inside the container
 // Resize changes the TTY of the process running inside the container
 // to the given height and width. The container must be running.
 // to the given height and width. The container must be running.
 func (container *Container) Resize(h, w int) error {
 func (container *Container) Resize(h, w int) error {

+ 3 - 3
daemon/daemon.go

@@ -849,10 +849,10 @@ func (daemon *Daemon) shutdownContainer(c *Container) error {
 		if !ok {
 		if !ok {
 			return fmt.Errorf("System doesn not support SIGTERM")
 			return fmt.Errorf("System doesn not support SIGTERM")
 		}
 		}
-		if err := c.daemon.kill(c, int(sig)); err != nil {
+		if err := daemon.kill(c, int(sig)); err != nil {
 			return fmt.Errorf("sending SIGTERM to container %s with error: %v", c.ID, err)
 			return fmt.Errorf("sending SIGTERM to container %s with error: %v", c.ID, err)
 		}
 		}
-		if err := c.unpause(); err != nil {
+		if err := daemon.containerUnpause(c); err != nil {
 			return fmt.Errorf("Failed to unpause container %s with error: %v", c.ID, err)
 			return fmt.Errorf("Failed to unpause container %s with error: %v", c.ID, err)
 		}
 		}
 		if _, err := c.WaitStop(10 * time.Second); err != nil {
 		if _, err := c.WaitStop(10 * time.Second); err != nil {
@@ -861,7 +861,7 @@ func (daemon *Daemon) shutdownContainer(c *Container) error {
 			if !ok {
 			if !ok {
 				return fmt.Errorf("System does not support SIGKILL")
 				return fmt.Errorf("System does not support SIGKILL")
 			}
 			}
-			if err := c.daemon.kill(c, int(sig)); err != nil {
+			if err := daemon.kill(c, int(sig)); err != nil {
 				logrus.Errorf("Failed to SIGKILL container %s", c.ID)
 				logrus.Errorf("Failed to SIGKILL container %s", c.ID)
 			}
 			}
 			c.WaitStop(-1 * time.Second)
 			c.WaitStop(-1 * time.Second)

+ 25 - 1
daemon/pause.go

@@ -11,9 +11,33 @@ func (daemon *Daemon) ContainerPause(name string) error {
 		return err
 		return err
 	}
 	}
 
 
-	if err := container.pause(); err != nil {
+	if err := daemon.containerPause(container); err != nil {
 		return derr.ErrorCodePauseError.WithArgs(name, err)
 		return derr.ErrorCodePauseError.WithArgs(name, err)
 	}
 	}
 
 
 	return nil
 	return nil
 }
 }
+
+// containerPause pauses the container execution without stopping the process.
+// The execution can be resumed by calling containerUnpause.
+func (daemon *Daemon) containerPause(container *Container) error {
+	container.Lock()
+	defer container.Unlock()
+
+	// We cannot Pause the container which is not running
+	if !container.Running {
+		return derr.ErrorCodeNotRunning.WithArgs(container.ID)
+	}
+
+	// We cannot Pause the container which is already paused
+	if container.Paused {
+		return derr.ErrorCodeAlreadyPaused.WithArgs(container.ID)
+	}
+
+	if err := daemon.execDriver.Pause(container.command); err != nil {
+		return err
+	}
+	container.Paused = true
+	daemon.logContainerEvent(container, "pause")
+	return nil
+}

+ 25 - 1
daemon/unpause.go

@@ -11,9 +11,33 @@ func (daemon *Daemon) ContainerUnpause(name string) error {
 		return err
 		return err
 	}
 	}
 
 
-	if err := container.unpause(); err != nil {
+	if err := daemon.containerUnpause(container); err != nil {
 		return derr.ErrorCodeCantUnpause.WithArgs(name, err)
 		return derr.ErrorCodeCantUnpause.WithArgs(name, err)
 	}
 	}
 
 
 	return nil
 	return nil
 }
 }
+
+// containerUnpause resumes the container execution after the container is paused.
+func (daemon *Daemon) containerUnpause(container *Container) error {
+	container.Lock()
+	defer container.Unlock()
+
+	// We cannot unpause the container which is not running
+	if !container.Running {
+		return derr.ErrorCodeNotRunning.WithArgs(container.ID)
+	}
+
+	// We cannot unpause the container which is not paused
+	if !container.Paused {
+		return derr.ErrorCodeNotPaused.WithArgs(container.ID)
+	}
+
+	if err := daemon.execDriver.Unpause(container.command); err != nil {
+		return err
+	}
+
+	container.Paused = false
+	daemon.logContainerEvent(container, "unpause")
+	return nil
+}