github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podmanV2/pods/kill.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  	podKillDescription = `Signals are sent to the main process of each container inside the specified pod.
    16  
    17    The default signal is SIGKILL, or any signal specified with option --signal.`
    18  	killCommand = &cobra.Command{
    19  		Use:   "kill [flags] POD [POD...]",
    20  		Short: "Send the specified signal or SIGKILL to containers in pod",
    21  		Long:  podKillDescription,
    22  		RunE:  kill,
    23  		Args: func(cmd *cobra.Command, args []string) error {
    24  			return parse.CheckAllLatestAndCIDFile(cmd, args, false, false)
    25  		},
    26  		Example: `podman pod kill podID
    27    podman pod kill --signal TERM mywebserver
    28    podman pod kill --latest`,
    29  	}
    30  )
    31  
    32  var (
    33  	killOpts entities.PodKillOptions
    34  )
    35  
    36  func init() {
    37  	registry.Commands = append(registry.Commands, registry.CliCommand{
    38  		Mode:    []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
    39  		Command: killCommand,
    40  		Parent:  podCmd,
    41  	})
    42  	flags := killCommand.Flags()
    43  	flags.BoolVarP(&killOpts.All, "all", "a", false, "Kill all containers in all pods")
    44  	flags.BoolVarP(&killOpts.Latest, "latest", "l", false, "Act on the latest pod podman is aware of")
    45  	flags.StringVarP(&killOpts.Signal, "signal", "s", "KILL", "Signal to send to the containers in the pod")
    46  	if registry.IsRemote() {
    47  		_ = flags.MarkHidden("latest")
    48  	}
    49  
    50  }
    51  func kill(cmd *cobra.Command, args []string) error {
    52  	var (
    53  		errs utils.OutputErrors
    54  	)
    55  	responses, err := registry.ContainerEngine().PodKill(context.Background(), args, killOpts)
    56  	if err != nil {
    57  		return err
    58  	}
    59  	// in the cli, first we print out all the successful attempts
    60  	for _, r := range responses {
    61  		if len(r.Errs) == 0 {
    62  			fmt.Println(r.Id)
    63  		} else {
    64  			errs = append(errs, r.Errs...)
    65  		}
    66  	}
    67  	return errs.PrintErrors()
    68  }