diff options
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/kpod/stop.go | 43 |
1 files changed, 34 insertions, 9 deletions
diff --git a/cmd/kpod/stop.go b/cmd/kpod/stop.go index 78ed1216c..f18fbc232 100644 --- a/cmd/kpod/stop.go +++ b/cmd/kpod/stop.go @@ -5,6 +5,7 @@ import ( "os" "github.com/pkg/errors" + "github.com/projectatomic/libpod/libpod" "github.com/sirupsen/logrus" "github.com/urfave/cli" ) @@ -17,6 +18,10 @@ var ( Usage: "Seconds to wait for stop before killing the container", Value: defaultTimeout, }, + cli.BoolFlag{ + Name: "all, a", + Usage: "stop all running containers", + }, } stopDescription = ` kpod stop @@ -39,7 +44,10 @@ var ( func stopCmd(c *cli.Context) error { args := c.Args() stopTimeout := c.Int64("timeout") - if len(args) < 1 { + if c.Bool("all") && len(args) > 0 { + return errors.Errorf("no arguments are needed with -a") + } + if len(args) < 1 && !c.Bool("all") { return errors.Errorf("you must provide at least one container name or id") } if err := validateFlags(c, stopFlags); err != nil { @@ -54,26 +62,43 @@ func stopCmd(c *cli.Context) error { logrus.Debugf("Stopping containers with timeout %d", stopTimeout) + var filterFuncs []libpod.ContainerFilter + var containers []*libpod.Container var lastError error - for _, container := range c.Args() { - ctr, err := runtime.LookupContainer(container) + + if c.Bool("all") { + // only get running containers + filterFuncs = append(filterFuncs, func(c *libpod.Container) bool { + state, _ := c.State() + return state == libpod.ContainerStateRunning + }) + containers, err = runtime.GetContainers(filterFuncs...) if err != nil { - if lastError != nil { - fmt.Fprintln(os.Stderr, lastError) + return errors.Wrapf(err, "unable to get running containers") + } + } else { + for _, i := range args { + container, err := runtime.LookupContainer(i) + if err != nil { + if lastError != nil { + fmt.Fprintln(os.Stderr, lastError) + } + lastError = errors.Wrapf(err, "unable to find container %s", i) + continue } - lastError = errors.Wrapf(err, "failed to stop container %v", container) - continue + containers = append(containers, container) } + } + for _, ctr := range containers { if err := ctr.Stop(stopTimeout); err != nil { if lastError != nil { fmt.Fprintln(os.Stderr, lastError) } - lastError = errors.Wrapf(err, "failed to stop container %v", container) + lastError = errors.Wrapf(err, "failed to stop container %v", ctr.ID()) } else { fmt.Println(ctr.ID()) } } - return lastError } |