github.com/apptainer/singularity@v3.1.1+incompatible/cmd/internal/cli/oci_linux.go (about)

     1  // Copyright (c) 2018-2019, Sylabs Inc. All rights reserved.
     2  // This software is licensed under a 3-clause BSD license. Please consult the
     3  // LICENSE.md file distributed with the sources of this project regarding your
     4  // rights to use or distribute this software.
     5  
     6  package cli
     7  
     8  import (
     9  	"os"
    10  
    11  	"github.com/spf13/cobra"
    12  	"github.com/sylabs/singularity/docs"
    13  	"github.com/sylabs/singularity/internal/app/singularity"
    14  	"github.com/sylabs/singularity/internal/pkg/sylog"
    15  )
    16  
    17  var ociArgs singularity.OciArgs
    18  
    19  func init() {
    20  	SingularityCmd.AddCommand(OciCmd)
    21  
    22  	OciCreateCmd.Flags().SetInterspersed(false)
    23  	OciCreateCmd.Flags().StringVarP(&ociArgs.BundlePath, "bundle", "b", "", "specify the OCI bundle path")
    24  	OciCreateCmd.Flags().SetAnnotation("bundle", "argtag", []string{"<path>"})
    25  	OciCreateCmd.Flags().StringVarP(&ociArgs.SyncSocketPath, "sync-socket", "s", "", "specify the path to unix socket for state synchronization (internal)")
    26  	OciCreateCmd.Flags().SetAnnotation("sync-socket", "argtag", []string{"<path>"})
    27  	OciCreateCmd.Flags().BoolVar(&ociArgs.EmptyProcess, "empty-process", false, "run container without executing container process (eg: for POD container)")
    28  	OciCreateCmd.Flags().StringVarP(&ociArgs.LogPath, "log-path", "l", "", "specify the log file path")
    29  	OciCreateCmd.Flags().SetAnnotation("log-path", "argtag", []string{"<path>"})
    30  	OciCreateCmd.Flags().StringVar(&ociArgs.LogFormat, "log-format", "kubernetes", "specify the log file format. Available formats are basic, kubernetes and json")
    31  	OciCreateCmd.Flags().SetAnnotation("log-format", "argtag", []string{"<format>"})
    32  	OciCreateCmd.Flags().StringVar(&ociArgs.PidFile, "pid-file", "", "specify the pid file")
    33  	OciCreateCmd.Flags().SetAnnotation("pid-file", "argtag", []string{"<path>"})
    34  
    35  	OciStartCmd.Flags().SetInterspersed(false)
    36  	OciDeleteCmd.Flags().SetInterspersed(false)
    37  	OciAttachCmd.Flags().SetInterspersed(false)
    38  	OciExecCmd.Flags().SetInterspersed(false)
    39  	OciPauseCmd.Flags().SetInterspersed(false)
    40  	OciResumeCmd.Flags().SetInterspersed(false)
    41  
    42  	OciStateCmd.Flags().SetInterspersed(false)
    43  	OciStateCmd.Flags().StringVarP(&ociArgs.SyncSocketPath, "sync-socket", "s", "", "specify the path to unix socket for state synchronization (internal)")
    44  	OciStateCmd.Flags().SetAnnotation("sync-socket", "argtag", []string{"<path>"})
    45  
    46  	OciKillCmd.Flags().SetInterspersed(false)
    47  	OciKillCmd.Flags().StringVarP(&ociArgs.KillSignal, "signal", "s", "SIGTERM", "signal sent to the container (default SIGTERM)")
    48  
    49  	OciRunCmd.Flags().SetInterspersed(false)
    50  	OciRunCmd.Flags().StringVarP(&ociArgs.BundlePath, "bundle", "b", "", "specify the OCI bundle path")
    51  	OciRunCmd.Flags().SetAnnotation("bundle", "argtag", []string{"<path>"})
    52  	OciRunCmd.Flags().StringVarP(&ociArgs.LogPath, "log-path", "l", "", "specify the log file path")
    53  	OciRunCmd.Flags().SetAnnotation("log-path", "argtag", []string{"<path>"})
    54  	OciRunCmd.Flags().StringVar(&ociArgs.LogFormat, "log-format", "kubernetes", "specify the log file format. Available formats are basic, kubernetes and json")
    55  	OciRunCmd.Flags().SetAnnotation("log-format", "argtag", []string{"<format>"})
    56  	OciRunCmd.Flags().StringVar(&ociArgs.PidFile, "pid-file", "", "specify the pid file")
    57  	OciRunCmd.Flags().SetAnnotation("pid-file", "argtag", []string{"<path>"})
    58  
    59  	OciUpdateCmd.Flags().SetInterspersed(false)
    60  	OciUpdateCmd.Flags().StringVarP(&ociArgs.FromFile, "from-file", "f", "", "specify path to OCI JSON cgroups resource file ('-' to read from STDIN)")
    61  
    62  	OciCmd.AddCommand(OciStartCmd)
    63  	OciCmd.AddCommand(OciCreateCmd)
    64  	OciCmd.AddCommand(OciRunCmd)
    65  	OciCmd.AddCommand(OciDeleteCmd)
    66  	OciCmd.AddCommand(OciKillCmd)
    67  	OciCmd.AddCommand(OciStateCmd)
    68  	OciCmd.AddCommand(OciAttachCmd)
    69  	OciCmd.AddCommand(OciExecCmd)
    70  	OciCmd.AddCommand(OciUpdateCmd)
    71  	OciCmd.AddCommand(OciPauseCmd)
    72  	OciCmd.AddCommand(OciResumeCmd)
    73  	OciCmd.AddCommand(OciMountCmd)
    74  	OciCmd.AddCommand(OciUmountCmd)
    75  }
    76  
    77  func ensureRootPriv(cmd *cobra.Command, args []string) {
    78  	if os.Geteuid() != 0 {
    79  		sylog.Fatalf("this command is required to be run as root")
    80  	}
    81  }
    82  
    83  // OciCreateCmd represents oci create command.
    84  var OciCreateCmd = &cobra.Command{
    85  	Args:                  cobra.ExactArgs(1),
    86  	DisableFlagsInUseLine: true,
    87  	PreRun:                ensureRootPriv,
    88  	Run: func(cmd *cobra.Command, args []string) {
    89  		if err := singularity.OciCreate(args[0], &ociArgs); err != nil {
    90  			sylog.Fatalf("%s", err)
    91  		}
    92  	},
    93  	Use:     docs.OciCreateUse,
    94  	Short:   docs.OciCreateShort,
    95  	Long:    docs.OciCreateLong,
    96  	Example: docs.OciCreateExample,
    97  }
    98  
    99  // OciRunCmd allow to create/start in row.
   100  var OciRunCmd = &cobra.Command{
   101  	Args:                  cobra.ExactArgs(1),
   102  	DisableFlagsInUseLine: true,
   103  	PreRun:                ensureRootPriv,
   104  	Run: func(cmd *cobra.Command, args []string) {
   105  		if err := singularity.OciRun(args[0], &ociArgs); err != nil {
   106  			sylog.Fatalf("%s", err)
   107  		}
   108  	},
   109  	Use:     docs.OciRunUse,
   110  	Short:   docs.OciRunShort,
   111  	Long:    docs.OciRunLong,
   112  	Example: docs.OciRunExample,
   113  }
   114  
   115  // OciStartCmd represents oci start command.
   116  var OciStartCmd = &cobra.Command{
   117  	Args:                  cobra.ExactArgs(1),
   118  	DisableFlagsInUseLine: true,
   119  	PreRun:                ensureRootPriv,
   120  	Run: func(cmd *cobra.Command, args []string) {
   121  		if err := singularity.OciStart(args[0]); err != nil {
   122  			sylog.Fatalf("%s", err)
   123  		}
   124  	},
   125  	Use:     docs.OciStartUse,
   126  	Short:   docs.OciStartShort,
   127  	Long:    docs.OciStartLong,
   128  	Example: docs.OciStartExample,
   129  }
   130  
   131  // OciDeleteCmd represents oci delete command.
   132  var OciDeleteCmd = &cobra.Command{
   133  	Args:                  cobra.ExactArgs(1),
   134  	DisableFlagsInUseLine: true,
   135  	PreRun:                ensureRootPriv,
   136  	Run: func(cmd *cobra.Command, args []string) {
   137  		if err := singularity.OciDelete(args[0]); err != nil {
   138  			sylog.Fatalf("%s", err)
   139  		}
   140  	},
   141  	Use:     docs.OciDeleteUse,
   142  	Short:   docs.OciDeleteShort,
   143  	Long:    docs.OciDeleteLong,
   144  	Example: docs.OciDeleteExample,
   145  }
   146  
   147  // OciKillCmd represents oci kill command.
   148  var OciKillCmd = &cobra.Command{
   149  	Args:                  cobra.MinimumNArgs(1),
   150  	DisableFlagsInUseLine: true,
   151  	PreRun:                ensureRootPriv,
   152  	Run: func(cmd *cobra.Command, args []string) {
   153  		killSignal := ""
   154  		if len(args) > 1 && args[1] != "" {
   155  			killSignal = args[1]
   156  		} else {
   157  			killSignal = ociArgs.KillSignal
   158  		}
   159  		if err := singularity.OciKill(args[0], killSignal, 0); err != nil {
   160  			sylog.Fatalf("%s", err)
   161  		}
   162  	},
   163  	Use:     docs.OciKillUse,
   164  	Short:   docs.OciKillShort,
   165  	Long:    docs.OciKillLong,
   166  	Example: docs.OciKillExample,
   167  }
   168  
   169  // OciStateCmd represents oci state command.
   170  var OciStateCmd = &cobra.Command{
   171  	Args:                  cobra.ExactArgs(1),
   172  	DisableFlagsInUseLine: true,
   173  	PreRun:                ensureRootPriv,
   174  	Run: func(cmd *cobra.Command, args []string) {
   175  		if err := singularity.OciState(args[0], &ociArgs); err != nil {
   176  			sylog.Fatalf("%s", err)
   177  		}
   178  	},
   179  	Use:     docs.OciStateUse,
   180  	Short:   docs.OciStateShort,
   181  	Long:    docs.OciStateLong,
   182  	Example: docs.OciStateExample,
   183  }
   184  
   185  // OciAttachCmd represents oci attach command.
   186  var OciAttachCmd = &cobra.Command{
   187  	Args:                  cobra.ExactArgs(1),
   188  	DisableFlagsInUseLine: true,
   189  	PreRun:                ensureRootPriv,
   190  	Run: func(cmd *cobra.Command, args []string) {
   191  		if err := singularity.OciAttach(args[0]); err != nil {
   192  			sylog.Fatalf("%s", err)
   193  		}
   194  	},
   195  	Use:     docs.OciAttachUse,
   196  	Short:   docs.OciAttachShort,
   197  	Long:    docs.OciAttachLong,
   198  	Example: docs.OciAttachExample,
   199  }
   200  
   201  // OciExecCmd represents oci exec command.
   202  var OciExecCmd = &cobra.Command{
   203  	Args:                  cobra.MinimumNArgs(1),
   204  	DisableFlagsInUseLine: true,
   205  	PreRun:                ensureRootPriv,
   206  	Run: func(cmd *cobra.Command, args []string) {
   207  		if err := singularity.OciExec(args[0], args[1:]); err != nil {
   208  			sylog.Fatalf("%s", err)
   209  		}
   210  	},
   211  	Use:     docs.OciExecUse,
   212  	Short:   docs.OciExecShort,
   213  	Long:    docs.OciExecLong,
   214  	Example: docs.OciExecExample,
   215  }
   216  
   217  // OciUpdateCmd represents oci update command.
   218  var OciUpdateCmd = &cobra.Command{
   219  	Args:                  cobra.MinimumNArgs(1),
   220  	DisableFlagsInUseLine: true,
   221  	PreRun:                ensureRootPriv,
   222  	Run: func(cmd *cobra.Command, args []string) {
   223  		if err := singularity.OciUpdate(args[0], &ociArgs); err != nil {
   224  			sylog.Fatalf("%s", err)
   225  		}
   226  	},
   227  	Use:     docs.OciUpdateUse,
   228  	Short:   docs.OciUpdateShort,
   229  	Long:    docs.OciUpdateLong,
   230  	Example: docs.OciUpdateExample,
   231  }
   232  
   233  // OciPauseCmd represents oci pause command.
   234  var OciPauseCmd = &cobra.Command{
   235  	Args:                  cobra.ExactArgs(1),
   236  	DisableFlagsInUseLine: true,
   237  	PreRun:                ensureRootPriv,
   238  	Run: func(cmd *cobra.Command, args []string) {
   239  		if err := singularity.OciPauseResume(args[0], true); err != nil {
   240  			sylog.Fatalf("%s", err)
   241  		}
   242  	},
   243  	Use:     docs.OciPauseUse,
   244  	Short:   docs.OciPauseShort,
   245  	Long:    docs.OciPauseLong,
   246  	Example: docs.OciPauseExample,
   247  }
   248  
   249  // OciResumeCmd represents oci resume command.
   250  var OciResumeCmd = &cobra.Command{
   251  	Args:                  cobra.ExactArgs(1),
   252  	DisableFlagsInUseLine: true,
   253  	PreRun:                ensureRootPriv,
   254  	Run: func(cmd *cobra.Command, args []string) {
   255  		if err := singularity.OciPauseResume(args[0], false); err != nil {
   256  			sylog.Fatalf("%s", err)
   257  		}
   258  	},
   259  	Use:     docs.OciResumeUse,
   260  	Short:   docs.OciResumeShort,
   261  	Long:    docs.OciResumeLong,
   262  	Example: docs.OciResumeExample,
   263  }
   264  
   265  // OciMountCmd represents oci mount command.
   266  var OciMountCmd = &cobra.Command{
   267  	Args:                  cobra.ExactArgs(2),
   268  	DisableFlagsInUseLine: true,
   269  	PreRun:                ensureRootPriv,
   270  	Run: func(cmd *cobra.Command, args []string) {
   271  		if err := singularity.OciMount(args[0], args[1]); err != nil {
   272  			sylog.Fatalf("%s", err)
   273  		}
   274  	},
   275  	Use:     docs.OciMountUse,
   276  	Short:   docs.OciMountShort,
   277  	Long:    docs.OciMountLong,
   278  	Example: docs.OciMountExample,
   279  }
   280  
   281  // OciUmountCmd represents oci mount command.
   282  var OciUmountCmd = &cobra.Command{
   283  	Args:                  cobra.ExactArgs(1),
   284  	DisableFlagsInUseLine: true,
   285  	PreRun:                ensureRootPriv,
   286  	Run: func(cmd *cobra.Command, args []string) {
   287  		if err := singularity.OciUmount(args[0]); err != nil {
   288  			sylog.Fatalf("%s", err)
   289  		}
   290  	},
   291  	Use:     docs.OciUmountUse,
   292  	Short:   docs.OciUmountShort,
   293  	Long:    docs.OciUmountLong,
   294  	Example: docs.OciUmountExample,
   295  }
   296  
   297  // OciCmd singularity oci runtime.
   298  var OciCmd = &cobra.Command{
   299  	Run:                   nil,
   300  	DisableFlagsInUseLine: true,
   301  
   302  	Use:     docs.OciUse,
   303  	Short:   docs.OciShort,
   304  	Long:    docs.OciLong,
   305  	Example: docs.OciExample,
   306  }