File: oci_linux.go

package info (click to toggle)
singularity-container 4.0.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 21,672 kB
  • sloc: asm: 3,857; sh: 2,125; ansic: 1,677; awk: 414; makefile: 110; python: 99
file content (364 lines) | stat: -rw-r--r-- 10,628 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright (c) 2018-2022, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.

package cli

import (
	"errors"
	"os"
	"os/exec"

	"github.com/spf13/cobra"
	"github.com/sylabs/singularity/v4/docs"
	"github.com/sylabs/singularity/v4/internal/app/singularity"
	"github.com/sylabs/singularity/v4/internal/pkg/runtime/launcher/oci"
	"github.com/sylabs/singularity/v4/pkg/cmdline"
	"github.com/sylabs/singularity/v4/pkg/sylog"
)

var ociArgs singularity.OciArgs

// -b|--bundle
var ociBundleFlag = cmdline.Flag{
	ID:           "ociBundleFlag",
	Value:        &ociArgs.BundlePath,
	DefaultValue: "",
	Name:         "bundle",
	ShortHand:    "b",
	Required:     true,
	Usage:        "specify the OCI bundle path (required)",
	Tag:          "<path>",
	EnvKeys:      []string{"BUNDLE"},
}

// -l|--log-path
var ociLogPathFlag = cmdline.Flag{
	ID:           "ociLogPathFlag",
	Value:        &ociArgs.LogPath,
	DefaultValue: "",
	Name:         "log-path",
	ShortHand:    "l",
	Usage:        "specify the log file path",
	Tag:          "<path>",
	EnvKeys:      []string{"LOG_PATH"},
}

// --log-format
var ociLogFormatFlag = cmdline.Flag{
	ID:           "ociLogFormatFlag",
	Value:        &ociArgs.LogFormat,
	DefaultValue: "kubernetes",
	Name:         "log-format",
	Usage:        "specify the log file format. Available formats are basic, kubernetes and json",
	Tag:          "<format>",
	EnvKeys:      []string{"LOG_FORMAT"},
}

// --pid-file
var ociPidFileFlag = cmdline.Flag{
	ID:           "ociPidFileFlag",
	Value:        &ociArgs.PidFile,
	DefaultValue: "",
	Name:         "pid-file",
	Usage:        "specify the pid file",
	Tag:          "<path>",
	EnvKeys:      []string{"PID_FILE"},
}

// -s|--signal
var ociKillSignalFlag = cmdline.Flag{
	ID:           "ociKillSignalFlag",
	Value:        &ociArgs.KillSignal,
	DefaultValue: "SIGTERM",
	Name:         "signal",
	ShortHand:    "s",
	Usage:        "signal sent to the container",
	Tag:          "<signal>",
	EnvKeys:      []string{"SIGNAL"},
}

// -f|--force
var ociKillForceFlag = cmdline.Flag{
	ID:           "ociKillForceFlag",
	Value:        &ociArgs.ForceKill,
	DefaultValue: false,
	Name:         "force",
	ShortHand:    "f",
	Usage:        "kill container process with SIGKILL",
	EnvKeys:      []string{"FORCE"},
}

// -f|--from-file
var ociUpdateFromFileFlag = cmdline.Flag{
	ID:           "ociUpdateFromFileFlag",
	Value:        &ociArgs.FromFile,
	DefaultValue: "",
	Name:         "from-file",
	ShortHand:    "f",
	Usage:        "specify path to OCI JSON cgroups resource file ('-' to read from STDIN)",
	EnvKeys:      []string{"FROM_FILE"},
}

func init() {
	addCmdInit(func(cmdManager *cmdline.CommandManager) {
		cmdManager.RegisterCmd(OciCmd)
		cmdManager.RegisterSubCmd(OciCmd, OciStartCmd)
		cmdManager.RegisterSubCmd(OciCmd, OciCreateCmd)
		cmdManager.RegisterSubCmd(OciCmd, OciRunCmd)
		cmdManager.RegisterSubCmd(OciCmd, OciDeleteCmd)
		cmdManager.RegisterSubCmd(OciCmd, OciKillCmd)
		cmdManager.RegisterSubCmd(OciCmd, OciStateCmd)
		cmdManager.RegisterSubCmd(OciCmd, OciAttachCmd)
		cmdManager.RegisterSubCmd(OciCmd, OciExecCmd)
		cmdManager.RegisterSubCmd(OciCmd, OciUpdateCmd)
		cmdManager.RegisterSubCmd(OciCmd, OciPauseCmd)
		cmdManager.RegisterSubCmd(OciCmd, OciResumeCmd)
		cmdManager.RegisterSubCmd(OciCmd, OciMountCmd)
		cmdManager.RegisterSubCmd(OciCmd, OciUmountCmd)

		cmdManager.SetCmdGroup("create_run", OciCreateCmd, OciRunCmd)
		createRunCmd := cmdManager.GetCmdGroup("create_run")

		cmdManager.RegisterFlagForCmd(&ociBundleFlag, createRunCmd...)
		cmdManager.RegisterFlagForCmd(&ociLogPathFlag, createRunCmd...)
		cmdManager.RegisterFlagForCmd(&ociLogFormatFlag, createRunCmd...)
		cmdManager.RegisterFlagForCmd(&ociPidFileFlag, createRunCmd...)
		cmdManager.RegisterFlagForCmd(&ociKillForceFlag, OciKillCmd)
		cmdManager.RegisterFlagForCmd(&ociKillSignalFlag, OciKillCmd)
		cmdManager.RegisterFlagForCmd(&ociUpdateFromFileFlag, OciUpdateCmd)
	})
}

// OciCreateCmd represents oci create command.
var OciCreateCmd = &cobra.Command{
	Args:                  cobra.ExactArgs(1),
	DisableFlagsInUseLine: true,
	PreRun:                CheckRoot,
	Run: func(cmd *cobra.Command, args []string) {
		if err := singularity.OciCreate(args[0], &ociArgs); err != nil {
			sylog.Fatalf("%s", err)
		}
	},
	Use:     docs.OciCreateUse,
	Short:   docs.OciCreateShort,
	Long:    docs.OciCreateLong,
	Example: docs.OciCreateExample,
}

// OciRunCmd allow to create/start in row.
var OciRunCmd = &cobra.Command{
	Args:                  cobra.ExactArgs(1),
	DisableFlagsInUseLine: true,
	PreRun:                CheckRoot,
	Run: func(cmd *cobra.Command, args []string) {
		if err := singularity.OciRun(cmd.Context(), args[0], &ociArgs); err != nil {
			var exitErr *exec.ExitError
			if errors.As(err, &exitErr) {
				os.Exit(exitErr.ExitCode())
			}
			sylog.Fatalf("%s", err)
		}
	},
	Use:     docs.OciRunUse,
	Short:   docs.OciRunShort,
	Long:    docs.OciRunLong,
	Example: docs.OciRunExample,
}

// OciStartCmd represents oci start command.
var OciStartCmd = &cobra.Command{
	Args:                  cobra.ExactArgs(1),
	DisableFlagsInUseLine: true,
	PreRun:                CheckRoot,
	Run: func(cmd *cobra.Command, args []string) {
		if err := singularity.OciStart(args[0]); err != nil {
			sylog.Fatalf("%s", err)
		}
	},
	Use:     docs.OciStartUse,
	Short:   docs.OciStartShort,
	Long:    docs.OciStartLong,
	Example: docs.OciStartExample,
}

// OciDeleteCmd represents oci delete command.
var OciDeleteCmd = &cobra.Command{
	Args:                  cobra.ExactArgs(1),
	DisableFlagsInUseLine: true,
	PreRun:                CheckRoot,
	Run: func(cmd *cobra.Command, args []string) {
		if err := singularity.OciDelete(cmd.Context(), args[0]); err != nil {
			sylog.Fatalf("%s", err)
		}
	},
	Use:     docs.OciDeleteUse,
	Short:   docs.OciDeleteShort,
	Long:    docs.OciDeleteLong,
	Example: docs.OciDeleteExample,
}

// OciKillCmd represents oci kill command.
var OciKillCmd = &cobra.Command{
	Args:                  cobra.MinimumNArgs(1),
	DisableFlagsInUseLine: true,
	PreRun:                CheckRoot,
	Run: func(cmd *cobra.Command, args []string) {
		killSignal := ""
		if len(args) > 1 && args[1] != "" {
			killSignal = args[1]
		} else {
			killSignal = ociArgs.KillSignal
		}
		if ociArgs.ForceKill {
			killSignal = "SIGKILL"
		}
		if err := singularity.OciKill(args[0], killSignal); err != nil {
			sylog.Fatalf("%s", err)
		}
	},
	Use:     docs.OciKillUse,
	Short:   docs.OciKillShort,
	Long:    docs.OciKillLong,
	Example: docs.OciKillExample,
}

// OciStateCmd represents oci state command.
var OciStateCmd = &cobra.Command{
	Args:                  cobra.ExactArgs(1),
	DisableFlagsInUseLine: true,
	PreRun:                CheckRoot,
	Run: func(cmd *cobra.Command, args []string) {
		if err := singularity.OciState(args[0], &ociArgs); err != nil {
			sylog.Fatalf("%s", err)
		}
	},
	Use:     docs.OciStateUse,
	Short:   docs.OciStateShort,
	Long:    docs.OciStateLong,
	Example: docs.OciStateExample,
}

// OciAttachCmd represents oci attach command.
var OciAttachCmd = &cobra.Command{
	Args:                  cobra.ExactArgs(1),
	DisableFlagsInUseLine: true,
	PreRun:                CheckRoot,
	Run: func(cmd *cobra.Command, args []string) {
		if err := oci.Attach(cmd.Context(), args[0]); err != nil {
			sylog.Fatalf("%s", err)
		}
	},
	Use:     docs.OciAttachUse,
	Short:   docs.OciAttachShort,
	Long:    docs.OciAttachLong,
	Example: docs.OciAttachExample,
}

// OciExecCmd represents oci exec command.
var OciExecCmd = &cobra.Command{
	Args:                  cobra.MinimumNArgs(1),
	DisableFlagsInUseLine: true,
	PreRun:                CheckRoot,
	Run: func(cmd *cobra.Command, args []string) {
		if err := singularity.OciExec(args[0], args[1:]); err != nil {
			sylog.Fatalf("%s", err)
		}
	},
	Use:     docs.OciExecUse,
	Short:   docs.OciExecShort,
	Long:    docs.OciExecLong,
	Example: docs.OciExecExample,
}

// OciUpdateCmd represents oci update command.
var OciUpdateCmd = &cobra.Command{
	Args:                  cobra.MinimumNArgs(1),
	DisableFlagsInUseLine: true,
	PreRun:                CheckRoot,
	Run: func(cmd *cobra.Command, args []string) {
		if err := singularity.OciUpdate(args[0], &ociArgs); err != nil {
			sylog.Fatalf("%s", err)
		}
	},
	Use:     docs.OciUpdateUse,
	Short:   docs.OciUpdateShort,
	Long:    docs.OciUpdateLong,
	Example: docs.OciUpdateExample,
}

// OciPauseCmd represents oci pause command.
var OciPauseCmd = &cobra.Command{
	Args:                  cobra.ExactArgs(1),
	DisableFlagsInUseLine: true,
	PreRun:                CheckRoot,
	Run: func(cmd *cobra.Command, args []string) {
		if err := singularity.OciPause(args[0]); err != nil {
			sylog.Fatalf("%s", err)
		}
	},
	Use:     docs.OciPauseUse,
	Short:   docs.OciPauseShort,
	Long:    docs.OciPauseLong,
	Example: docs.OciPauseExample,
}

// OciResumeCmd represents oci resume command.
var OciResumeCmd = &cobra.Command{
	Args:                  cobra.ExactArgs(1),
	DisableFlagsInUseLine: true,
	PreRun:                CheckRoot,
	Run: func(cmd *cobra.Command, args []string) {
		if err := singularity.OciResume(args[0]); err != nil {
			sylog.Fatalf("%s", err)
		}
	},
	Use:     docs.OciResumeUse,
	Short:   docs.OciResumeShort,
	Long:    docs.OciResumeLong,
	Example: docs.OciResumeExample,
}

// OciMountCmd represents oci mount command.
var OciMountCmd = &cobra.Command{
	Args:                  cobra.ExactArgs(2),
	DisableFlagsInUseLine: true,
	PreRun:                CheckRoot,
	Run: func(cmd *cobra.Command, args []string) {
		if err := singularity.OciMount(cmd.Context(), args[0], args[1]); err != nil {
			sylog.Fatalf("%s", err)
		}
	},
	Use:     docs.OciMountUse,
	Short:   docs.OciMountShort,
	Long:    docs.OciMountLong,
	Example: docs.OciMountExample,
}

// OciUmountCmd represents oci mount command.
var OciUmountCmd = &cobra.Command{
	Args:                  cobra.ExactArgs(1),
	DisableFlagsInUseLine: true,
	PreRun:                CheckRoot,
	Run: func(cmd *cobra.Command, args []string) {
		if err := singularity.OciUmount(cmd.Context(), args[0]); err != nil {
			sylog.Fatalf("%s", err)
		}
	},
	Use:     docs.OciUmountUse,
	Short:   docs.OciUmountShort,
	Long:    docs.OciUmountLong,
	Example: docs.OciUmountExample,
}

// OciCmd singularity oci runtime.
var OciCmd = &cobra.Command{
	Run:                   nil,
	DisableFlagsInUseLine: true,

	Use:     docs.OciUse,
	Short:   docs.OciShort,
	Long:    docs.OciLong,
	Example: docs.OciExample,
}