github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/stop.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/containers/libpod/cmd/podman/cliconfig"
     7  	"github.com/containers/libpod/pkg/adapter"
     8  	"github.com/opentracing/opentracing-go"
     9  	"github.com/pkg/errors"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  var (
    14  	stopCommand     cliconfig.StopValues
    15  	stopDescription = fmt.Sprintf(`Stops one or more running containers.  The container name or ID can be used.
    16  
    17    A timeout to forcibly stop the container can also be set but defaults to %d seconds otherwise.`, defaultContainerConfig.Engine.StopTimeout)
    18  	_stopCommand = &cobra.Command{
    19  		Use:   "stop [flags] CONTAINER [CONTAINER...]",
    20  		Short: "Stop one or more containers",
    21  		Long:  stopDescription,
    22  		RunE: func(cmd *cobra.Command, args []string) error {
    23  			stopCommand.InputArgs = args
    24  			stopCommand.GlobalFlags = MainGlobalOpts
    25  			stopCommand.Remote = remoteclient
    26  			return stopCmd(&stopCommand)
    27  		},
    28  		Args: func(cmd *cobra.Command, args []string) error {
    29  			return checkAllLatestAndCIDFile(cmd, args, false, true)
    30  		},
    31  		Example: `podman stop ctrID
    32    podman stop --latest
    33    podman stop --time 2 mywebserver 6e534f14da9d`,
    34  	}
    35  )
    36  
    37  func init() {
    38  	stopCommand.Command = _stopCommand
    39  	stopCommand.SetHelpTemplate(HelpTemplate())
    40  	stopCommand.SetUsageTemplate(UsageTemplate())
    41  	flags := stopCommand.Flags()
    42  	flags.BoolVarP(&stopCommand.All, "all", "a", false, "Stop all running containers")
    43  	flags.BoolVarP(&stopCommand.Ignore, "ignore", "i", false, "Ignore errors when a specified container is missing")
    44  	flags.StringArrayVarP(&stopCommand.CIDFiles, "cidfile", "", nil, "Read the container ID from the file")
    45  	flags.BoolVarP(&stopCommand.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
    46  	flags.UintVarP(&stopCommand.Timeout, "time", "t", defaultContainerConfig.Engine.StopTimeout, "Seconds to wait for stop before killing the container")
    47  	markFlagHiddenForRemoteClient("latest", flags)
    48  	markFlagHiddenForRemoteClient("cidfile", flags)
    49  	markFlagHiddenForRemoteClient("ignore", flags)
    50  	flags.SetNormalizeFunc(aliasFlags)
    51  }
    52  
    53  // stopCmd stops a container or containers
    54  func stopCmd(c *cliconfig.StopValues) error {
    55  	if c.Bool("trace") {
    56  		span, _ := opentracing.StartSpanFromContext(Ctx, "stopCmd")
    57  		defer span.Finish()
    58  	}
    59  
    60  	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
    61  	if err != nil {
    62  		return errors.Wrapf(err, "could not get runtime")
    63  	}
    64  	defer runtime.DeferredShutdown(false)
    65  
    66  	ok, failures, err := runtime.StopContainers(getContext(), c)
    67  	if err != nil {
    68  		return err
    69  	}
    70  	return printCmdResults(ok, failures)
    71  }