github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/pod_pause.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/pkg/errors"
     9  	"github.com/sirupsen/logrus"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  var (
    14  	podPauseCommand     cliconfig.PodPauseValues
    15  	podPauseDescription = `The pod name or ID can be used.
    16  
    17    All running containers within each specified pod will then be paused.`
    18  	_podPauseCommand = &cobra.Command{
    19  		Use:   "pause [flags] POD [POD...]",
    20  		Short: "Pause one or more pods",
    21  		Long:  podPauseDescription,
    22  		RunE: func(cmd *cobra.Command, args []string) error {
    23  			podPauseCommand.InputArgs = args
    24  			podPauseCommand.GlobalFlags = MainGlobalOpts
    25  			podPauseCommand.Remote = remoteclient
    26  			return podPauseCmd(&podPauseCommand)
    27  		},
    28  		Args: func(cmd *cobra.Command, args []string) error {
    29  			return checkAllLatestAndCIDFile(cmd, args, false, false)
    30  		},
    31  		Example: `podman pod pause podID1 podID2
    32    podman pod pause --latest
    33    podman pod pause --all`,
    34  	}
    35  )
    36  
    37  func init() {
    38  	podPauseCommand.Command = _podPauseCommand
    39  	podPauseCommand.SetHelpTemplate(HelpTemplate())
    40  	podPauseCommand.SetUsageTemplate(UsageTemplate())
    41  	flags := podPauseCommand.Flags()
    42  	flags.BoolVarP(&podPauseCommand.All, "all", "a", false, "Pause all running pods")
    43  	flags.BoolVarP(&podPauseCommand.Latest, "latest", "l", false, "Act on the latest pod podman is aware of")
    44  	markFlagHiddenForRemoteClient("latest", flags)
    45  }
    46  
    47  func podPauseCmd(c *cliconfig.PodPauseValues) error {
    48  	var lastError error
    49  	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
    50  	if err != nil {
    51  		return errors.Wrapf(err, "error creating libpod runtime")
    52  	}
    53  	defer runtime.DeferredShutdown(false)
    54  
    55  	pauseIDs, conErrors, pauseErrors := runtime.PausePods(c)
    56  
    57  	for _, p := range pauseIDs {
    58  		fmt.Println(p)
    59  	}
    60  	if len(conErrors) > 0 {
    61  		for ctr, err := range conErrors {
    62  			if lastError != nil {
    63  				logrus.Errorf("%q", lastError)
    64  			}
    65  			lastError = errors.Wrapf(err, "unable to pause container %s", ctr)
    66  		}
    67  	}
    68  	if len(pauseErrors) > 0 {
    69  		lastError = pauseErrors[len(pauseErrors)-1]
    70  		// Remove the last error from the error slice
    71  		pauseErrors = pauseErrors[:len(pauseErrors)-1]
    72  	}
    73  	for _, err := range pauseErrors {
    74  		logrus.Errorf("%q", err)
    75  	}
    76  	return lastError
    77  }