diff options
Diffstat (limited to 'runsc/container')
-rw-r--r-- | runsc/container/container.go | 30 | ||||
-rw-r--r-- | runsc/container/container_test.go | 10 |
2 files changed, 31 insertions, 9 deletions
diff --git a/runsc/container/container.go b/runsc/container/container.go index 3589272f2..513085836 100644 --- a/runsc/container/container.go +++ b/runsc/container/container.go @@ -445,6 +445,14 @@ func (c *Container) Restore(spec *specs.Spec, conf *boot.Config, restoreFile str return err } + // "If any prestart hook fails, the runtime MUST generate an error, + // stop and destroy the container" -OCI spec. + if c.Spec.Hooks != nil { + if err := executeHooks(c.Spec.Hooks.Prestart, c.State()); err != nil { + return err + } + } + if err := c.Sandbox.Restore(c.ID, spec, conf, restoreFile); err != nil { return err } @@ -453,7 +461,7 @@ func (c *Container) Restore(spec *specs.Spec, conf *boot.Config, restoreFile str } // Run is a helper that calls Create + Start + Wait. -func Run(id string, spec *specs.Spec, conf *boot.Config, bundleDir, consoleSocket, pidFile, userLog string) (syscall.WaitStatus, error) { +func Run(id string, spec *specs.Spec, conf *boot.Config, bundleDir, consoleSocket, pidFile, userLog string, detach bool) (syscall.WaitStatus, error) { log.Debugf("Run container %q in root dir: %s", id, conf.RootDir) c, err := Create(id, spec, conf, bundleDir, consoleSocket, pidFile, userLog) if err != nil { @@ -461,10 +469,24 @@ func Run(id string, spec *specs.Spec, conf *boot.Config, bundleDir, consoleSocke } // Clean up partially created container if an error ocurrs. // Any errors returned by Destroy() itself are ignored. - defer c.Destroy() + cu := specutils.MakeCleanup(func() { + c.Destroy() + }) + defer cu.Clean() - if err := c.Start(conf); err != nil { - return 0, fmt.Errorf("starting container: %v", err) + if conf.RestoreFile != "" { + log.Debugf("Restore: %v", conf.RestoreFile) + if err := c.Restore(spec, conf, conf.RestoreFile); err != nil { + return 0, fmt.Errorf("starting container: %v", err) + } + } else { + if err := c.Start(conf); err != nil { + return 0, fmt.Errorf("starting container: %v", err) + } + } + if detach { + cu.Release() + return 0, nil } return c.Wait() } diff --git a/runsc/container/container_test.go b/runsc/container/container_test.go index 269d28448..dcd9910a0 100644 --- a/runsc/container/container_test.go +++ b/runsc/container/container_test.go @@ -210,7 +210,7 @@ func run(spec *specs.Spec, conf *boot.Config) error { defer os.RemoveAll(bundleDir) // Create, start and wait for the container. - ws, err := Run(testutil.UniqueContainerID(), spec, conf, bundleDir, "", "", "") + ws, err := Run(testutil.UniqueContainerID(), spec, conf, bundleDir, "", "", "", false) if err != nil { return fmt.Errorf("running container: %v", err) } @@ -416,7 +416,7 @@ func TestExePath(t *testing.T) { t.Fatalf("exec: %s, error setting up container: %v", test.path, err) } - ws, err := Run(testutil.UniqueContainerID(), spec, conf, bundleDir, "", "", "") + ws, err := Run(testutil.UniqueContainerID(), spec, conf, bundleDir, "", "", "", false) os.RemoveAll(rootDir) os.RemoveAll(bundleDir) @@ -449,7 +449,7 @@ func TestAppExitStatus(t *testing.T) { defer os.RemoveAll(rootDir) defer os.RemoveAll(bundleDir) - ws, err := Run(testutil.UniqueContainerID(), succSpec, conf, bundleDir, "", "", "") + ws, err := Run(testutil.UniqueContainerID(), succSpec, conf, bundleDir, "", "", "", false) if err != nil { t.Fatalf("error running container: %v", err) } @@ -468,7 +468,7 @@ func TestAppExitStatus(t *testing.T) { defer os.RemoveAll(rootDir2) defer os.RemoveAll(bundleDir2) - ws, err = Run(testutil.UniqueContainerID(), errSpec, conf, bundleDir2, "", "", "") + ws, err = Run(testutil.UniqueContainerID(), errSpec, conf, bundleDir2, "", "", "", false) if err != nil { t.Fatalf("error running container: %v", err) } @@ -1519,7 +1519,7 @@ func TestUserLog(t *testing.T) { userLog := filepath.Join(dir, "user.log") // Create, start and wait for the container. - ws, err := Run(testutil.UniqueContainerID(), spec, conf, bundleDir, "", "", userLog) + ws, err := Run(testutil.UniqueContainerID(), spec, conf, bundleDir, "", "", userLog, false) if err != nil { t.Fatalf("error running container: %v", err) } |