summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorbaude <bbaude@redhat.com>2017-11-29 20:01:39 -0600
committerAtomic Bot <atomic-devel@projectatomic.io>2017-12-01 13:18:32 +0000
commit1f01faf4375b2dc667b2794e4decdf360d6e32b8 (patch)
tree6736a22e1387b8443204327a4275efe4c8817e53 /cmd
parent82018a4a9f5bcbda82c88b1f387be2953bc7a68a (diff)
downloadpodman-1f01faf4375b2dc667b2794e4decdf360d6e32b8.tar.gz
podman-1f01faf4375b2dc667b2794e4decdf360d6e32b8.tar.bz2
podman-1f01faf4375b2dc667b2794e4decdf360d6e32b8.zip
kpod stop -a
Stop all running containers with single switch. Useful for maintainence of a system or integration tests. Signed-off-by: baude <bbaude@redhat.com> Closes: #90 Approved by: rhatdan
Diffstat (limited to 'cmd')
-rw-r--r--cmd/kpod/stop.go43
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
}