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

     1  package main
     2  
     3  import (
     4  	"github.com/containers/libpod/cmd/podman/cliconfig"
     5  	"github.com/containers/libpod/libpod/define"
     6  	"github.com/containers/libpod/pkg/adapter"
     7  	"github.com/containers/libpod/pkg/rootless"
     8  	"github.com/pkg/errors"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  var (
    13  	pauseCommand     cliconfig.PauseValues
    14  	pauseDescription = `Pauses one or more running containers.  The container name or ID can be used.`
    15  	_pauseCommand    = &cobra.Command{
    16  		Use:   "pause [flags] CONTAINER [CONTAINER...]",
    17  		Short: "Pause all the processes in one or more containers",
    18  		Long:  pauseDescription,
    19  		RunE: func(cmd *cobra.Command, args []string) error {
    20  			pauseCommand.InputArgs = args
    21  			pauseCommand.GlobalFlags = MainGlobalOpts
    22  			pauseCommand.Remote = remoteclient
    23  			return pauseCmd(&pauseCommand)
    24  		},
    25  		Example: `podman pause mywebserver
    26    podman pause 860a4b23
    27    podman pause -a`,
    28  	}
    29  )
    30  
    31  func init() {
    32  	pauseCommand.Command = _pauseCommand
    33  	pauseCommand.SetHelpTemplate(HelpTemplate())
    34  	pauseCommand.SetUsageTemplate(UsageTemplate())
    35  	flags := pauseCommand.Flags()
    36  	flags.BoolVarP(&pauseCommand.All, "all", "a", false, "Pause all running containers")
    37  
    38  }
    39  
    40  func pauseCmd(c *cliconfig.PauseValues) error {
    41  	if rootless.IsRootless() && !remoteclient {
    42  		return errors.New("pause is not supported for rootless containers")
    43  	}
    44  
    45  	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
    46  	if err != nil {
    47  		return errors.Wrapf(err, "could not get runtime")
    48  	}
    49  	defer runtime.DeferredShutdown(false)
    50  
    51  	args := c.InputArgs
    52  	if len(args) < 1 && !c.All {
    53  		return errors.Errorf("you must provide at least one container name or id")
    54  	}
    55  	ok, failures, err := runtime.PauseContainers(getContext(), c)
    56  	if err != nil {
    57  		if errors.Cause(err) == define.ErrNoSuchCtr {
    58  			if len(c.InputArgs) > 1 {
    59  				exitCode = define.ExecErrorCodeGeneric
    60  			} else {
    61  				exitCode = 1
    62  			}
    63  		}
    64  		return err
    65  	}
    66  	if len(failures) > 0 {
    67  		exitCode = define.ExecErrorCodeGeneric
    68  	}
    69  	return printCmdResults(ok, failures)
    70  }