summaryrefslogtreecommitdiff
path: root/libpod/container_internal.go
diff options
context:
space:
mode:
authorMatthew Heon <matthew.heon@pm.me>2019-04-01 15:22:32 -0400
committerMatthew Heon <matthew.heon@pm.me>2019-05-03 10:36:16 -0400
commit0d73ee40b2e95ec7d50c7ee72fcb24cae0e190a7 (patch)
tree665382c42613e99258536536d61b00462a67ae53 /libpod/container_internal.go
parent3fb52f4fbb04781e32f72888c9e509dea5d7b434 (diff)
downloadpodman-0d73ee40b2e95ec7d50c7ee72fcb24cae0e190a7.tar.gz
podman-0d73ee40b2e95ec7d50c7ee72fcb24cae0e190a7.tar.bz2
podman-0d73ee40b2e95ec7d50c7ee72fcb24cae0e190a7.zip
Add container restart policy to Libpod & Podman
This initial version does not support restart count, but it works as advertised otherwise. Signed-off-by: Matthew Heon <matthew.heon@pm.me>
Diffstat (limited to 'libpod/container_internal.go')
-rw-r--r--libpod/container_internal.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/libpod/container_internal.go b/libpod/container_internal.go
index 0b9c5a96a..f72d86c84 100644
--- a/libpod/container_internal.go
+++ b/libpod/container_internal.go
@@ -210,6 +210,43 @@ func (c *Container) handleExitFile(exitFile string, fi os.FileInfo) error {
return nil
}
+// Handle container restart policy.
+// This is called when a container has exited, and was not explicitly stopped by
+// an API call to stop the container or pod it is in.
+func (c *Container) handleRestartPolicy(ctx context.Context) (err error) {
+ logrus.Debugf("Restarting container %s due to restart policy %s", c.ID(), c.config.RestartPolicy)
+
+ // Need to check if dependencies are alive.
+ if err = c.checkDependenciesAndHandleError(ctx); err != nil {
+ return err
+ }
+
+ defer func() {
+ if err != nil {
+ if err2 := c.cleanup(ctx); err2 != nil {
+ logrus.Errorf("error cleaning up container %s: %v", c.ID(), err2)
+ }
+ }
+ }()
+ if err := c.prepare(); err != nil {
+ return err
+ }
+
+ if c.state.State == ContainerStateStopped {
+ // Reinitialize the container if we need to
+ if err := c.reinit(ctx); err != nil {
+ return err
+ }
+ } else if c.state.State == ContainerStateConfigured ||
+ c.state.State == ContainerStateExited {
+ // Initialize the container
+ if err := c.init(ctx); err != nil {
+ return err
+ }
+ }
+ return c.start()
+}
+
// Sync this container with on-disk state and runtime status
// Should only be called with container lock held
// This function should suffice to ensure a container's state is accurate and
@@ -230,6 +267,14 @@ func (c *Container) syncContainer() error {
}
// Only save back to DB if state changed
if c.state.State != oldState {
+ // Check for a restart policy match
+ if c.config.RestartPolicy != "" && c.config.RestartPolicy != "no" &&
+ (oldState == ContainerStateRunning || oldState == ContainerStatePaused) &&
+ (c.state.State == ContainerStateStopped || c.state.State == ContainerStateExited) &&
+ !c.state.StoppedByUser {
+ c.state.RestartPolicyMatch = true
+ }
+
if err := c.save(); err != nil {
return err
}
@@ -377,6 +422,7 @@ func resetState(state *ContainerState) error {
state.NetworkStatus = nil
state.BindMounts = make(map[string]string)
state.StoppedByUser = false
+ state.RestartPolicyMatch = false
return nil
}
@@ -791,6 +837,7 @@ func (c *Container) init(ctx context.Context) error {
c.state.Exited = false
c.state.State = ContainerStateCreated
c.state.StoppedByUser = false
+ c.state.RestartPolicyMatch = false
if err := c.save(); err != nil {
return err