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