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

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"os"
     7  	"path"
     8  
     9  	"github.com/containers/common/pkg/config"
    10  	"github.com/containers/libpod/cmd/podman/cliconfig"
    11  	"github.com/containers/libpod/libpod"
    12  	"github.com/containers/libpod/libpod/define"
    13  	_ "github.com/containers/libpod/pkg/hooks/0.1.0"
    14  	"github.com/containers/libpod/pkg/rootless"
    15  	"github.com/containers/libpod/version"
    16  	"github.com/containers/storage/pkg/reexec"
    17  	"github.com/opentracing/opentracing-go"
    18  	"github.com/sirupsen/logrus"
    19  	"github.com/spf13/cobra"
    20  )
    21  
    22  // This is populated by the Makefile from the VERSION file
    23  // in the repository
    24  var (
    25  	exitCode = define.ExecErrorCodeGeneric
    26  	Ctx      context.Context
    27  	span     opentracing.Span // nolint:varcheck,deadcode,unused
    28  	closer   io.Closer        // nolint:varcheck,deadcode,unused
    29  )
    30  
    31  // Commands that the remote and local client have
    32  // implemented.
    33  var mainCommands = []*cobra.Command{
    34  	_attachCommand,
    35  	_buildCommand,
    36  	_commitCommand,
    37  	_diffCommand,
    38  	_createCommand,
    39  	_eventsCommand,
    40  	_execCommand,
    41  	_exportCommand,
    42  	_generateCommand,
    43  	_historyCommand,
    44  	&_imagesCommand,
    45  	_importCommand,
    46  	_infoCommand,
    47  	_initCommand,
    48  	&_inspectCommand,
    49  	_killCommand,
    50  	_loadCommand,
    51  	_logsCommand,
    52  	_pauseCommand,
    53  	podCommand.Command,
    54  	_portCommand,
    55  	&_psCommand,
    56  	_pullCommand,
    57  	_pushCommand,
    58  	_restartCommand,
    59  	_rmCommand,
    60  	&_rmiCommand,
    61  	_runCommand,
    62  	_saveCommand,
    63  	_stopCommand,
    64  	_tagCommand,
    65  	_topCommand,
    66  	_unpauseCommand,
    67  	_versionCommand,
    68  	_waitCommand,
    69  	imageCommand.Command,
    70  	_startCommand,
    71  	systemCommand.Command,
    72  	_untagCommand,
    73  }
    74  
    75  var rootCmd = &cobra.Command{
    76  	Use:                path.Base(os.Args[0]),
    77  	Long:               "manage pods and images",
    78  	RunE:               commandRunE(),
    79  	PersistentPreRunE:  before,
    80  	PersistentPostRunE: after,
    81  	SilenceUsage:       true,
    82  	SilenceErrors:      true,
    83  }
    84  
    85  var (
    86  	MainGlobalOpts         cliconfig.MainFlags
    87  	defaultContainerConfig = getDefaultContainerConfig()
    88  )
    89  
    90  func initCobra() {
    91  	cobra.OnInitialize(initConfig)
    92  	rootCmd.TraverseChildren = true
    93  	rootCmd.Version = version.Version
    94  	// Override default --help information of `--version` global flag
    95  	var dummyVersion bool
    96  	rootCmd.Flags().BoolVarP(&dummyVersion, "version", "v", false, "Version of podman")
    97  	rootCmd.AddCommand(mainCommands...)
    98  	rootCmd.AddCommand(getMainCommands()...)
    99  }
   100  
   101  func init() {
   102  	if err := libpod.SetXdgDirs(); err != nil {
   103  		logrus.Errorf(err.Error())
   104  		os.Exit(1)
   105  	}
   106  	initBuild()
   107  	initCobra()
   108  }
   109  
   110  func initConfig() {
   111  	//	we can do more stuff in here.
   112  }
   113  
   114  func before(cmd *cobra.Command, args []string) error {
   115  	//	Set log level; if not log-level is provided, default to error
   116  	logLevel := MainGlobalOpts.LogLevel
   117  	if logLevel == "" {
   118  		logLevel = "error"
   119  	}
   120  	level, err := logrus.ParseLevel(logLevel)
   121  	if err != nil {
   122  		return err
   123  	}
   124  	logrus.SetLevel(level)
   125  	if err := setSyslog(); err != nil {
   126  		return err
   127  	}
   128  
   129  	defaultContainerConfig.Engine.CgroupManager = MainGlobalOpts.CGroupManager
   130  	defaultContainerConfig.CheckCgroupsAndAdjustConfig()
   131  
   132  	if err := setupRootless(cmd, args); err != nil {
   133  		return err
   134  	}
   135  
   136  	// check that global opts input is valid
   137  	if err := checkInput(); err != nil {
   138  		return err
   139  	}
   140  
   141  	if err := setRLimits(); err != nil {
   142  		return err
   143  	}
   144  	if rootless.IsRootless() {
   145  		logrus.Info("running as rootless")
   146  	}
   147  	setUMask()
   148  
   149  	return profileOn(cmd)
   150  }
   151  
   152  func after(cmd *cobra.Command, args []string) error {
   153  	return profileOff(cmd)
   154  }
   155  
   156  func main() {
   157  	//debug := false
   158  	//cpuProfile := false
   159  
   160  	if reexec.Init() {
   161  		// We were invoked with a different argv[0] indicating that we
   162  		// had a specific job to do as a subprocess, and it's done.
   163  		return
   164  	}
   165  	// Hard code TMPDIR functions to use /var/tmp, if user did not override
   166  	if _, ok := os.LookupEnv("TMPDIR"); !ok {
   167  		os.Setenv("TMPDIR", "/var/tmp")
   168  	}
   169  	if err := rootCmd.Execute(); err != nil {
   170  		outputError(err)
   171  	} else if exitCode == define.ExecErrorCodeGeneric {
   172  		// The exitCode modified from define.ExecErrorCodeGeneric,
   173  		// indicates an application
   174  		// running inside of a container failed, as opposed to the
   175  		// podman command failed.  Must exit with that exit code
   176  		// otherwise command exited correctly.
   177  		exitCode = 0
   178  	}
   179  
   180  	// Check if /etc/containers/registries.conf exists when running in
   181  	// in a local environment.
   182  	CheckForRegistries()
   183  	os.Exit(exitCode)
   184  }
   185  
   186  func getDefaultContainerConfig() *config.Config {
   187  	defaultContainerConfig, err := config.Default()
   188  	if err != nil {
   189  		logrus.Error(err)
   190  		os.Exit(1)
   191  	}
   192  	return defaultContainerConfig
   193  }