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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"syscall"
     6  
     7  	"github.com/containers/libpod/cmd/podman/cliconfig"
     8  	"github.com/containers/libpod/pkg/adapter"
     9  	"github.com/containers/libpod/pkg/util"
    10  	"github.com/pkg/errors"
    11  	"github.com/sirupsen/logrus"
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  var (
    16  	podKillCommand     cliconfig.PodKillValues
    17  	podKillDescription = `Signals are sent to the main process of each container inside the specified pod.
    18  
    19    The default signal is SIGKILL, or any signal specified with option --signal.`
    20  	_podKillCommand = &cobra.Command{
    21  		Use:   "kill [flags] POD [POD...]",
    22  		Short: "Send the specified signal or SIGKILL to containers in pod",
    23  		Long:  podKillDescription,
    24  		RunE: func(cmd *cobra.Command, args []string) error {
    25  			podKillCommand.InputArgs = args
    26  			podKillCommand.GlobalFlags = MainGlobalOpts
    27  			podKillCommand.Remote = remoteclient
    28  			return podKillCmd(&podKillCommand)
    29  		},
    30  		Args: func(cmd *cobra.Command, args []string) error {
    31  			return checkAllLatestAndCIDFile(cmd, args, false, false)
    32  		},
    33  		Example: `podman pod kill podID
    34    podman pod kill --signal TERM mywebserver
    35    podman pod kill --latest`,
    36  	}
    37  )
    38  
    39  func init() {
    40  	podKillCommand.Command = _podKillCommand
    41  	podKillCommand.SetHelpTemplate(HelpTemplate())
    42  	podKillCommand.SetUsageTemplate(UsageTemplate())
    43  	flags := podKillCommand.Flags()
    44  	flags.BoolVarP(&podKillCommand.All, "all", "a", false, "Kill all containers in all pods")
    45  	flags.BoolVarP(&podKillCommand.Latest, "latest", "l", false, "Act on the latest pod podman is aware of")
    46  	flags.StringVarP(&podKillCommand.Signal, "signal", "s", "KILL", "Signal to send to the containers in the pod")
    47  	markFlagHiddenForRemoteClient("latest", flags)
    48  }
    49  
    50  // podKillCmd kills one or more pods with a signal
    51  func podKillCmd(c *cliconfig.PodKillValues) error {
    52  	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
    53  	if err != nil {
    54  		return errors.Wrapf(err, "could not get runtime")
    55  	}
    56  	defer runtime.DeferredShutdown(false)
    57  
    58  	killSignal := uint(syscall.SIGTERM)
    59  
    60  	if c.Signal != "" {
    61  		// Check if the signalString provided by the user is valid
    62  		// Invalid signals will return err
    63  		sysSignal, err := util.ParseSignal(c.Signal)
    64  		if err != nil {
    65  			return err
    66  		}
    67  		killSignal = uint(sysSignal)
    68  	}
    69  
    70  	podKillIds, podKillErrors := runtime.KillPods(getContext(), c, killSignal)
    71  	for _, p := range podKillIds {
    72  		fmt.Println(p)
    73  	}
    74  	if len(podKillErrors) == 0 {
    75  		return nil
    76  	}
    77  	// Grab the last error
    78  	lastError := podKillErrors[len(podKillErrors)-1]
    79  	// Remove the last error from the error slice
    80  	podKillErrors = podKillErrors[:len(podKillErrors)-1]
    81  
    82  	for _, err := range podKillErrors {
    83  		logrus.Errorf("%q", err)
    84  	}
    85  	return lastError
    86  }