summaryrefslogtreecommitdiff
path: root/cmd/podman/restart.go
diff options
context:
space:
mode:
authorMatthew Heon <matthew.heon@gmail.com>2018-03-15 15:00:18 -0400
committerAtomic Bot <atomic-devel@projectatomic.io>2018-03-16 13:55:49 +0000
commit2724434369037938f5dceaf7bf8268d4d5ce1a68 (patch)
tree92254cdf40bfb937fe405eec2b51a9c18705cbb6 /cmd/podman/restart.go
parent8840b92da603f9db5ba2590f4b0836ad580fbe56 (diff)
downloadpodman-2724434369037938f5dceaf7bf8268d4d5ce1a68.tar.gz
podman-2724434369037938f5dceaf7bf8268d4d5ce1a68.tar.bz2
podman-2724434369037938f5dceaf7bf8268d4d5ce1a68.zip
Add 'podman restart' command
Signed-off-by: Matthew Heon <matthew.heon@gmail.com> Closes: #503 Approved by: rhatdan
Diffstat (limited to 'cmd/podman/restart.go')
-rw-r--r--cmd/podman/restart.go110
1 files changed, 110 insertions, 0 deletions
diff --git a/cmd/podman/restart.go b/cmd/podman/restart.go
new file mode 100644
index 000000000..f96959a27
--- /dev/null
+++ b/cmd/podman/restart.go
@@ -0,0 +1,110 @@
+package main
+
+import (
+ "fmt"
+ "os"
+
+ "github.com/pkg/errors"
+ "github.com/projectatomic/libpod/libpod"
+ "github.com/urfave/cli"
+)
+
+var (
+ restartFlags = []cli.Flag{
+ cli.UintFlag{
+ Name: "timeout, time, t",
+ Usage: "Seconds to wait for stop before killing the container",
+ Value: libpod.CtrRemoveTimeout,
+ },
+ LatestFlag,
+ }
+ restartDescription = `Restarts one or more running containers. The container ID or name can be used. A timeout before forcibly stopping can be set, but defaults to 10 seconds`
+
+ restartCommand = cli.Command{
+ Name: "restart",
+ Usage: "Restart one or more containers",
+ Description: restartDescription,
+ Flags: restartFlags,
+ Action: restartCmd,
+ ArgsUsage: "CONTAINER [CONTAINER ...]",
+ UseShortOptionHandling: true,
+ }
+)
+
+func restartCmd(c *cli.Context) error {
+ args := c.Args()
+ if len(args) < 1 && !c.Bool("latest") {
+ return errors.Wrapf(libpod.ErrInvalidArg, "you must provide at least one container name or ID")
+ }
+
+ if err := validateFlags(c, restartFlags); err != nil {
+ return err
+ }
+
+ runtime, err := getRuntime(c)
+ if err != nil {
+ return errors.Wrapf(err, "error creating libpod runtime")
+ }
+ defer runtime.Shutdown(false)
+
+ var lastError error
+
+ timeout := c.Uint("timeout")
+ useTimeout := c.IsSet("timeout")
+
+ // Handle --latest
+ if c.Bool("latest") {
+ lastCtr, err := runtime.GetLatestContainer()
+ if err != nil {
+ lastError = errors.Wrapf(err, "unable to get latest container")
+ } else {
+ lastError = restartCtr(timeout, useTimeout, lastCtr)
+ }
+ }
+
+ for _, id := range args {
+ ctr, err := runtime.LookupContainer(id)
+ if err != nil {
+ if lastError != nil {
+ fmt.Fprintln(os.Stderr, lastError)
+ }
+ lastError = errors.Wrapf(err, "unable to find container %s", id)
+ continue
+ }
+
+ if err := restartCtr(timeout, useTimeout, ctr); err != nil {
+ if lastError != nil {
+ fmt.Fprintln(os.Stderr, lastError)
+ }
+ lastError = errors.Wrapf(err, "error restarting container %s", ctr.ID())
+ }
+ }
+
+ return lastError
+}
+
+// Restart a single container
+func restartCtr(cliTimeout uint, useTimeout bool, ctr *libpod.Container) error {
+ timeout := ctr.StopTimeout()
+ if useTimeout {
+ timeout = cliTimeout
+ }
+
+ state, err := ctr.State()
+ if err != nil {
+ return err
+ }
+ if state == libpod.ContainerStateRunning {
+ if err := ctr.StopWithTimeout(timeout); err != nil {
+ return err
+ }
+ }
+
+ if err := ctr.Start(); err != nil {
+ return err
+ }
+
+ fmt.Printf("%s\n", ctr.ID())
+
+ return nil
+}