summaryrefslogtreecommitdiff
path: root/cmd/podman/stop.go
diff options
context:
space:
mode:
authorJhon Honce <jhonce@redhat.com>2019-02-18 16:01:31 -0700
committerJhon Honce <jhonce@redhat.com>2019-03-02 08:57:20 -0700
commit4d13a80fa46ce57e3c889934536320525338b3a4 (patch)
tree8d3d5bd4f0209aa8ce4e3371478e6edc305209e6 /cmd/podman/stop.go
parent9adcda73892fa0a33cbdf971ad97cf079e8e425f (diff)
downloadpodman-4d13a80fa46ce57e3c889934536320525338b3a4.tar.gz
podman-4d13a80fa46ce57e3c889934536320525338b3a4.tar.bz2
podman-4d13a80fa46ce57e3c889934536320525338b3a4.zip
Support podman-remote stop container(s)
* Clean up adapter code * Add GetContainersByContext to Varlink API * Add missing comments * Restore save command * Restore error type mapping when using varlink Signed-off-by: Jhon Honce <jhonce@redhat.com>
Diffstat (limited to 'cmd/podman/stop.go')
-rw-r--r--cmd/podman/stop.go67
1 files changed, 23 insertions, 44 deletions
diff --git a/cmd/podman/stop.go b/cmd/podman/stop.go
index ab9a2cf38..7bd160494 100644
--- a/cmd/podman/stop.go
+++ b/cmd/podman/stop.go
@@ -2,15 +2,14 @@ package main
import (
"fmt"
+ "reflect"
"github.com/containers/libpod/cmd/podman/cliconfig"
- "github.com/containers/libpod/cmd/podman/libpodruntime"
- "github.com/containers/libpod/cmd/podman/shared"
"github.com/containers/libpod/libpod"
+ "github.com/containers/libpod/pkg/adapter"
"github.com/containers/libpod/pkg/rootless"
- opentracing "github.com/opentracing/opentracing-go"
+ "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
- "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
@@ -52,63 +51,43 @@ func init() {
markFlagHiddenForRemoteClient("latest", flags)
}
+// stopCmd stops a container or containers
func stopCmd(c *cliconfig.StopValues) error {
+ if c.Flag("timeout").Changed && c.Flag("time").Changed {
+ return errors.New("the --timeout and --time flags are mutually exclusive")
+ }
+
if c.Bool("trace") {
span, _ := opentracing.StartSpanFromContext(Ctx, "stopCmd")
defer span.Finish()
}
rootless.SetSkipStorageSetup(true)
- runtime, err := libpodruntime.GetRuntime(&c.PodmanCommand)
+ runtime, err := adapter.GetRuntime(&c.PodmanCommand)
if err != nil {
return errors.Wrapf(err, "could not get runtime")
}
defer runtime.Shutdown(false)
- containers, err := getAllOrLatestContainers(&c.PodmanCommand, runtime, libpod.ContainerStateRunning, "running")
+ ok, failures, err := runtime.StopContainers(getContext(), c)
if err != nil {
- if len(containers) == 0 {
- return err
- }
- fmt.Println(err.Error())
+ return err
}
- if c.Flag("timeout").Changed && c.Flag("time").Changed {
- return errors.New("the --timeout and --time flags are mutually exclusive")
+ for _, id := range ok {
+ fmt.Println(id)
}
- var stopFuncs []shared.ParallelWorkerInput
- for _, ctr := range containers {
- con := ctr
- var stopTimeout uint
- if c.Flag("timeout").Changed || c.Flag("time").Changed {
- stopTimeout = c.Timeout
- } else {
- stopTimeout = ctr.StopTimeout()
- logrus.Debugf("Set timeout to container %s default (%d)", ctr.ID(), stopTimeout)
- }
- f := func() error {
- if err := con.StopWithTimeout(stopTimeout); err != nil {
- if errors.Cause(err) == libpod.ErrCtrStopped {
- logrus.Debugf("Container %s already stopped", con.ID())
- return nil
- }
- return err
- }
- return nil
- }
- stopFuncs = append(stopFuncs, shared.ParallelWorkerInput{
- ContainerID: con.ID(),
- ParallelFunc: f,
- })
- }
+ if len(failures) > 0 {
+ keys := reflect.ValueOf(failures).MapKeys()
+ lastKey := keys[len(keys)-1].String()
+ lastErr := failures[lastKey]
+ delete(failures, lastKey)
- maxWorkers := shared.Parallelize("stop")
- if c.GlobalIsSet("max-workers") {
- maxWorkers = c.GlobalFlags.MaxWorks
+ for _, err := range failures {
+ outputError(err)
+ }
+ return lastErr
}
- logrus.Debugf("Setting maximum workers to %d", maxWorkers)
-
- stopErrors, errCount := shared.ParallelExecuteWorkerPool(maxWorkers, stopFuncs)
- return printParallelOutput(stopErrors, errCount)
+ return nil
}