diff options
author | baude <bbaude@redhat.com> | 2017-11-29 20:01:39 -0600 |
---|---|---|
committer | Atomic Bot <atomic-devel@projectatomic.io> | 2017-12-01 13:18:32 +0000 |
commit | 1f01faf4375b2dc667b2794e4decdf360d6e32b8 (patch) | |
tree | 6736a22e1387b8443204327a4275efe4c8817e53 | |
parent | 82018a4a9f5bcbda82c88b1f387be2953bc7a68a (diff) | |
download | podman-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
-rw-r--r-- | cmd/kpod/stop.go | 43 | ||||
-rw-r--r-- | completions/bash/kpod | 4 | ||||
-rw-r--r-- | docs/kpod-stop.1.md | 8 | ||||
-rw-r--r-- | test/kpod_stop.bats | 9 |
4 files changed, 54 insertions, 10 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 } diff --git a/completions/bash/kpod b/completions/bash/kpod index 93009e6af..68001c36e 100644 --- a/completions/bash/kpod +++ b/completions/bash/kpod @@ -1352,7 +1352,9 @@ _kpod_stop() { local options_with_args=" --timeout -t " - local boolean_options="" + local boolean_options=" + --all + -a" _complete_ "$options_with_args" "$boolean_options" } diff --git a/docs/kpod-stop.1.md b/docs/kpod-stop.1.md index 52a358156..f45e6c078 100644 --- a/docs/kpod-stop.1.md +++ b/docs/kpod-stop.1.md @@ -19,6 +19,10 @@ is issued to the container. The default is 10 seconds. Timeout to wait before forcibly stopping the container +**--all, -a** + +Stop all running containers. This does not include paused containers. + ## EXAMPLE @@ -26,8 +30,12 @@ kpod stop mywebserver kpod stop 860a4b23 +kpod stop mywebserver 860a4b23 + kpod stop --timeout 2 860a4b23 +kpod stop -a + ## SEE ALSO kpod(1), kpod-rm(1) diff --git a/test/kpod_stop.bats b/test/kpod_stop.bats index 0c1bd329c..3804147fd 100644 --- a/test/kpod_stop.bats +++ b/test/kpod_stop.bats @@ -38,3 +38,12 @@ function setup() { run bash -c "${KPOD_BINARY} ${KPOD_OPTIONS} ps" [ "$status" -eq 0 ] } + +@test "stop all containers" { + run bash -c "${KPOD_BINARY} ${KPOD_OPTIONS} run --name test1 -d ${ALPINE} sleep 9999" + run bash -c "${KPOD_BINARY} ${KPOD_OPTIONS} run --name test2 -d ${ALPINE} sleep 9999" + run bash -c "${KPOD_BINARY} ${KPOD_OPTIONS} run --name test3 -d ${ALPINE} sleep 9999" + run bash -c "${KPOD_BINARY} ${KPOD_OPTIONS} stop -a -t 1" + echo "$output" + [ "$status" -eq 0 ] +} |