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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/containers/libpod/cmd/podman/cliconfig"
     8  	"github.com/containers/libpod/cmd/podman/libpodruntime"
     9  	"github.com/pkg/errors"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  var (
    14  	refreshCommand     cliconfig.RefreshValues
    15  	refreshDescription = `Resets the state of all containers to handle database changes after a Podman upgrade.
    16  
    17    All running containers will be restarted.
    18  `
    19  	_refreshCommand = &cobra.Command{
    20  		Use:   "refresh",
    21  		Args:  noSubArgs,
    22  		Short: "Refresh container state",
    23  		Long:  refreshDescription,
    24  		RunE: func(cmd *cobra.Command, args []string) error {
    25  			refreshCommand.InputArgs = args
    26  			refreshCommand.GlobalFlags = MainGlobalOpts
    27  			refreshCommand.Remote = remoteclient
    28  			return refreshCmd(&refreshCommand)
    29  		},
    30  	}
    31  )
    32  
    33  func init() {
    34  	_refreshCommand.Hidden = true
    35  	refreshCommand.Command = _refreshCommand
    36  	refreshCommand.SetHelpTemplate(HelpTemplate())
    37  	refreshCommand.SetUsageTemplate(UsageTemplate())
    38  }
    39  
    40  func refreshCmd(c *cliconfig.RefreshValues) error {
    41  	runtime, err := libpodruntime.GetRuntime(getContext(), &c.PodmanCommand)
    42  	if err != nil {
    43  		return errors.Wrapf(err, "error creating libpod runtime")
    44  	}
    45  	defer runtime.DeferredShutdown(false)
    46  
    47  	allCtrs, err := runtime.GetAllContainers()
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	ctx := getContext()
    53  
    54  	var lastError error
    55  	for _, ctr := range allCtrs {
    56  		if err := ctr.Refresh(ctx); err != nil {
    57  			if lastError != nil {
    58  				fmt.Fprintln(os.Stderr, lastError)
    59  			}
    60  			lastError = errors.Wrapf(err, "error refreshing container %s state", ctr.ID())
    61  		}
    62  	}
    63  
    64  	return lastError
    65  }