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

     1  package pods
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/containers/libpod/cmd/podmanV2/parse"
     8  	"github.com/containers/libpod/cmd/podmanV2/registry"
     9  	"github.com/containers/libpod/cmd/podmanV2/utils"
    10  	"github.com/containers/libpod/pkg/domain/entities"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  var (
    15  	podStopDescription = `The pod name or ID can be used.
    16  
    17    This command will stop all running containers in each of the specified pods.`
    18  
    19  	stopCommand = &cobra.Command{
    20  		Use:   "stop [flags] POD [POD...]",
    21  		Short: "Stop one or more pods",
    22  		Long:  podStopDescription,
    23  		RunE:  stop,
    24  		Args: func(cmd *cobra.Command, args []string) error {
    25  			return parse.CheckAllLatestAndCIDFile(cmd, args, false, false)
    26  		},
    27  		Example: `podman pod stop mywebserverpod
    28    podman pod stop --latest
    29    podman pod stop --time 0 490eb 3557fb`,
    30  	}
    31  )
    32  
    33  var (
    34  	stopOptions = entities.PodStopOptions{
    35  		Timeout: -1,
    36  	}
    37  	timeout uint
    38  )
    39  
    40  func init() {
    41  	registry.Commands = append(registry.Commands, registry.CliCommand{
    42  		Mode:    []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
    43  		Command: stopCommand,
    44  		Parent:  podCmd,
    45  	})
    46  	flags := stopCommand.Flags()
    47  	flags.BoolVarP(&stopOptions.All, "all", "a", false, "Stop all running pods")
    48  	flags.BoolVarP(&stopOptions.Ignore, "ignore", "i", false, "Ignore errors when a specified pod is missing")
    49  	flags.BoolVarP(&stopOptions.Latest, "latest", "l", false, "Stop the latest pod podman is aware of")
    50  	flags.UintVarP(&timeout, "time", "t", 0, "Seconds to wait for pod stop before killing the container")
    51  	if registry.IsRemote() {
    52  		_ = flags.MarkHidden("latest")
    53  		_ = flags.MarkHidden("ignore")
    54  
    55  	}
    56  	flags.SetNormalizeFunc(utils.AliasFlags)
    57  }
    58  
    59  func stop(cmd *cobra.Command, args []string) error {
    60  	var (
    61  		errs utils.OutputErrors
    62  	)
    63  	if cmd.Flag("time").Changed {
    64  		stopOptions.Timeout = int(timeout)
    65  	}
    66  	responses, err := registry.ContainerEngine().PodStop(context.Background(), args, stopOptions)
    67  	if err != nil {
    68  		return err
    69  	}
    70  	// in the cli, first we print out all the successful attempts
    71  	for _, r := range responses {
    72  		if len(r.Errs) == 0 {
    73  			fmt.Println(r.Id)
    74  		} else {
    75  			errs = append(errs, r.Errs...)
    76  		}
    77  	}
    78  	return errs.PrintErrors()
    79  }