mirror of https://github.com/containers/podman.git
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
This commit is contained in:
parent
82018a4a9f
commit
1f01faf437
|
@ -5,6 +5,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"github.com/projectatomic/libpod/libpod"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
@ -17,6 +18,10 @@ var (
|
||||||
Usage: "Seconds to wait for stop before killing the container",
|
Usage: "Seconds to wait for stop before killing the container",
|
||||||
Value: defaultTimeout,
|
Value: defaultTimeout,
|
||||||
},
|
},
|
||||||
|
cli.BoolFlag{
|
||||||
|
Name: "all, a",
|
||||||
|
Usage: "stop all running containers",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
stopDescription = `
|
stopDescription = `
|
||||||
kpod stop
|
kpod stop
|
||||||
|
@ -39,7 +44,10 @@ var (
|
||||||
func stopCmd(c *cli.Context) error {
|
func stopCmd(c *cli.Context) error {
|
||||||
args := c.Args()
|
args := c.Args()
|
||||||
stopTimeout := c.Int64("timeout")
|
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")
|
return errors.Errorf("you must provide at least one container name or id")
|
||||||
}
|
}
|
||||||
if err := validateFlags(c, stopFlags); err != nil {
|
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)
|
logrus.Debugf("Stopping containers with timeout %d", stopTimeout)
|
||||||
|
|
||||||
|
var filterFuncs []libpod.ContainerFilter
|
||||||
|
var containers []*libpod.Container
|
||||||
var lastError error
|
var lastError error
|
||||||
for _, container := range c.Args() {
|
|
||||||
ctr, err := runtime.LookupContainer(container)
|
|
||||||
if err != nil {
|
|
||||||
if lastError != nil {
|
|
||||||
fmt.Fprintln(os.Stderr, lastError)
|
|
||||||
}
|
|
||||||
lastError = errors.Wrapf(err, "failed to stop container %v", container)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
|
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 {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
containers = append(containers, container)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, ctr := range containers {
|
||||||
if err := ctr.Stop(stopTimeout); err != nil {
|
if err := ctr.Stop(stopTimeout); err != nil {
|
||||||
if lastError != nil {
|
if lastError != nil {
|
||||||
fmt.Fprintln(os.Stderr, lastError)
|
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 {
|
} else {
|
||||||
fmt.Println(ctr.ID())
|
fmt.Println(ctr.ID())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return lastError
|
return lastError
|
||||||
}
|
}
|
||||||
|
|
|
@ -1352,7 +1352,9 @@ _kpod_stop() {
|
||||||
local options_with_args="
|
local options_with_args="
|
||||||
--timeout -t
|
--timeout -t
|
||||||
"
|
"
|
||||||
local boolean_options=""
|
local boolean_options="
|
||||||
|
--all
|
||||||
|
-a"
|
||||||
_complete_ "$options_with_args" "$boolean_options"
|
_complete_ "$options_with_args" "$boolean_options"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,10 @@ is issued to the container. The default is 10 seconds.
|
||||||
|
|
||||||
Timeout to wait before forcibly stopping the container
|
Timeout to wait before forcibly stopping the container
|
||||||
|
|
||||||
|
**--all, -a**
|
||||||
|
|
||||||
|
Stop all running containers. This does not include paused containers.
|
||||||
|
|
||||||
|
|
||||||
## EXAMPLE
|
## EXAMPLE
|
||||||
|
|
||||||
|
@ -26,8 +30,12 @@ kpod stop mywebserver
|
||||||
|
|
||||||
kpod stop 860a4b23
|
kpod stop 860a4b23
|
||||||
|
|
||||||
|
kpod stop mywebserver 860a4b23
|
||||||
|
|
||||||
kpod stop --timeout 2 860a4b23
|
kpod stop --timeout 2 860a4b23
|
||||||
|
|
||||||
|
kpod stop -a
|
||||||
|
|
||||||
## SEE ALSO
|
## SEE ALSO
|
||||||
kpod(1), kpod-rm(1)
|
kpod(1), kpod-rm(1)
|
||||||
|
|
||||||
|
|
|
@ -38,3 +38,12 @@ function setup() {
|
||||||
run bash -c "${KPOD_BINARY} ${KPOD_OPTIONS} ps"
|
run bash -c "${KPOD_BINARY} ${KPOD_OPTIONS} ps"
|
||||||
[ "$status" -eq 0 ]
|
[ "$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 ]
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue