-
Notifications
You must be signed in to change notification settings - Fork 124
/
actions.go
371 lines (329 loc) · 10.5 KB
/
actions.go
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
365
366
367
368
369
370
371
// Copyright (c) Contributors to the Apptainer project, established as
// Apptainer a Series of LF Projects LLC.
// For website terms of use, trademark policy, privacy policy and other
// project policies see https://lfprojects.org/policies
// Copyright (c) 2020, Control Command Inc. All rights reserved.
// 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 (
"context"
"fmt"
"os"
"runtime"
"strings"
"github.com/apptainer/apptainer/docs"
"github.com/apptainer/apptainer/internal/pkg/cache"
"github.com/apptainer/apptainer/internal/pkg/client/library"
"github.com/apptainer/apptainer/internal/pkg/client/net"
"github.com/apptainer/apptainer/internal/pkg/client/oci"
"github.com/apptainer/apptainer/internal/pkg/client/oras"
"github.com/apptainer/apptainer/internal/pkg/client/shub"
"github.com/apptainer/apptainer/internal/pkg/runtime/launch"
"github.com/apptainer/apptainer/internal/pkg/util/env"
"github.com/apptainer/apptainer/internal/pkg/util/uri"
"github.com/apptainer/apptainer/pkg/sylog"
"github.com/spf13/cobra"
)
const (
defaultPath = "/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
)
func getCacheHandle(cfg cache.Config) *cache.Handle {
envKey := env.TrimApptainerKey(cache.DirEnv)
h, err := cache.New(cache.Config{
ParentDir: env.GetenvLegacy(envKey, envKey),
Disable: cfg.Disable,
})
if err != nil {
sylog.Fatalf("Failed to create an image cache handle: %s", err)
}
return h
}
// actionPreRun will run replaceURIWithImage and will also do the proper path unsetting
func actionPreRun(cmd *cobra.Command, args []string) {
// For compatibility - we still set USER_PATH so it will be visible in the
// container, and can be used there if needed. USER_PATH is not used by
// apptainer itself in 1.0.0+
userPath := strings.Join([]string{os.Getenv("PATH"), defaultPath}, ":")
os.Setenv("USER_PATH", userPath)
os.Setenv("IMAGE_ARG", args[0])
replaceURIWithImage(cmd.Context(), cmd, args)
// --compat infers other options that give increased OCI / Docker compatibility
// Excludes uts/user/net namespaces as these are restrictive for many Apptainer
// installs.
if isCompat {
isContainAll = true
isWritableTmpfs = true
noInit = true
noUmask = true
noEval = true
}
}
func handleOCI(ctx context.Context, imgCache *cache.Handle, cmd *cobra.Command, pullFrom string) (string, error) {
ociAuth, err := makeDockerCredentials(cmd)
if err != nil {
sylog.Fatalf("While creating Docker credentials: %v", err)
}
pullOpts := oci.PullOptions{
TmpDir: tmpDir,
OciAuth: ociAuth,
DockerHost: dockerHost,
NoHTTPS: noHTTPS,
}
return oci.Pull(ctx, imgCache, pullFrom, pullOpts)
}
func handleOras(ctx context.Context, imgCache *cache.Handle, cmd *cobra.Command, pullFrom string) (string, error) {
ociAuth, err := makeDockerCredentials(cmd)
if err != nil {
return "", fmt.Errorf("while creating docker credentials: %v", err)
}
return oras.Pull(ctx, imgCache, pullFrom, tmpDir, ociAuth, noHTTPS)
}
func handleLibrary(ctx context.Context, imgCache *cache.Handle, pullFrom string) (string, error) {
r, err := library.NormalizeLibraryRef(pullFrom)
if err != nil {
return "", err
}
// Default "" = use current remote endpoint
var libraryURI string
if r.Host != "" {
if noHTTPS {
libraryURI = "http://" + r.Host
} else {
libraryURI = "https://" + r.Host
}
}
c, err := getLibraryClientConfig(libraryURI)
if err != nil {
return "", err
}
return library.Pull(ctx, imgCache, r, runtime.GOARCH, tmpDir, c)
}
func handleShub(ctx context.Context, imgCache *cache.Handle, pullFrom string) (string, error) {
return shub.Pull(ctx, imgCache, pullFrom, tmpDir, noHTTPS)
}
func handleNet(ctx context.Context, imgCache *cache.Handle, pullFrom string) (string, error) {
return net.Pull(ctx, imgCache, pullFrom, tmpDir)
}
func replaceURIWithImage(ctx context.Context, cmd *cobra.Command, args []string) {
// If args[0] is not transport:ref (ex. instance://...) formatted return, not a URI
t, _ := uri.Split(args[0])
if t == "instance" || t == "" {
return
}
var image string
var err error
// Create a cache handle only when we know we are are using a URI
imgCache := getCacheHandle(cache.Config{Disable: disableCache})
if imgCache == nil {
sylog.Fatalf("failed to create a new image cache handle")
}
switch t {
case uri.Library:
image, err = handleLibrary(ctx, imgCache, args[0])
case uri.Oras:
image, err = handleOras(ctx, imgCache, cmd, args[0])
case uri.Shub:
image, err = handleShub(ctx, imgCache, args[0])
case oci.IsSupported(t):
image, err = handleOCI(ctx, imgCache, cmd, args[0])
case uri.HTTP:
image, err = handleNet(ctx, imgCache, args[0])
case uri.HTTPS:
image, err = handleNet(ctx, imgCache, args[0])
default:
sylog.Fatalf("Unsupported transport type: %s", t)
}
if err != nil {
sylog.Fatalf("Unable to handle %s uri: %v", args[0], err)
}
args[0] = image
}
// setVM will set the --vm option if needed by other options
func setVM(cmd *cobra.Command) {
// check if --vm-ram or --vm-cpu changed from default value
for _, flagName := range []string{"vm-ram", "vm-cpu"} {
if flag := cmd.Flag(flagName); flag != nil && flag.Changed {
// this option requires the VM setting to be enabled
cmd.Flags().Set("vm", "true")
return
}
}
// since --syos is a boolean, it cannot be added to the above list
if isSyOS && !vm {
// let the user know that passing --syos implicitly enables --vm
sylog.Warningf("The --syos option requires a virtual machine, automatically enabling --vm option.")
cmd.Flags().Set("vm", "true")
}
}
// ExecCmd represents the exec command
var ExecCmd = &cobra.Command{
DisableFlagsInUseLine: true,
TraverseChildren: true,
Args: cobra.MinimumNArgs(2),
PreRun: actionPreRun,
Run: func(cmd *cobra.Command, args []string) {
a := append([]string{"/.singularity.d/actions/exec"}, args[1:]...)
setVM(cmd)
if vm {
execVM(cmd, args[0], a)
return
}
if err := launchContainer(cmd, args[0], a, ""); err != nil {
sylog.Fatalf("%s", err)
}
},
Use: docs.ExecUse,
Short: docs.ExecShort,
Long: docs.ExecLong,
Example: docs.ExecExamples,
}
// ShellCmd represents the shell command
var ShellCmd = &cobra.Command{
DisableFlagsInUseLine: true,
TraverseChildren: true,
Args: cobra.MinimumNArgs(1),
PreRun: actionPreRun,
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 1 {
sylog.Warningf("Parameters to shell command are ignored")
}
a := []string{"/.singularity.d/actions/shell"}
setVM(cmd)
if vm {
execVM(cmd, args[0], a)
return
}
if err := launchContainer(cmd, args[0], a, ""); err != nil {
sylog.Fatalf("%s", err)
}
},
Use: docs.ShellUse,
Short: docs.ShellShort,
Long: docs.ShellLong,
Example: docs.ShellExamples,
}
// RunCmd represents the run command
var RunCmd = &cobra.Command{
DisableFlagsInUseLine: true,
TraverseChildren: true,
Args: cobra.MinimumNArgs(1),
PreRun: actionPreRun,
Run: func(cmd *cobra.Command, args []string) {
a := append([]string{"/.singularity.d/actions/run"}, args[1:]...)
setVM(cmd)
if vm {
execVM(cmd, args[0], a)
return
}
if err := launchContainer(cmd, args[0], a, ""); err != nil {
sylog.Fatalf("%s", err)
}
},
Use: docs.RunUse,
Short: docs.RunShort,
Long: docs.RunLong,
Example: docs.RunExamples,
}
// TestCmd represents the test command
var TestCmd = &cobra.Command{
DisableFlagsInUseLine: true,
TraverseChildren: true,
Args: cobra.MinimumNArgs(1),
PreRun: actionPreRun,
Run: func(cmd *cobra.Command, args []string) {
a := append([]string{"/.singularity.d/actions/test"}, args[1:]...)
setVM(cmd)
if vm {
execVM(cmd, args[0], a)
return
}
if err := launchContainer(cmd, args[0], a, ""); err != nil {
sylog.Fatalf("%s", err)
}
},
Use: docs.RunTestUse,
Short: docs.RunTestShort,
Long: docs.RunTestLong,
Example: docs.RunTestExample,
}
func launchContainer(cmd *cobra.Command, image string, args []string, instanceName string) error {
ns := launch.Namespaces{
User: userNamespace,
UTS: utsNamespace,
PID: pidNamespace,
IPC: ipcNamespace,
Net: netNamespace,
}
cgJSON, err := getCgroupsJSON()
if err != nil {
return err
}
if cgJSON != "" && strings.HasPrefix(image, "instance://") {
cgJSON = ""
sylog.Warningf("Resource limits & cgroups configuration are only applied to instances at instance start.")
}
ki, err := getEncryptionMaterial(cmd)
if err != nil {
return err
}
opts := []launch.Option{
launch.OptWritable(isWritable),
launch.OptWritableTmpfs(isWritableTmpfs),
launch.OptOverlayPaths(overlayPath),
launch.OptScratchDirs(scratchPath),
launch.OptWorkDir(workdirPath),
launch.OptHome(
homePath,
cmd.Flag(actionHomeFlag.Name).Changed,
noHome,
),
launch.OptMounts(bindPaths, mounts, fuseMount),
launch.OptNoMount(noMount),
launch.OptNvidia(nvidia, nvCCLI),
launch.OptNoNvidia(noNvidia),
launch.OptRocm(rocm),
launch.OptNoRocm(noRocm),
launch.OptContainLibs(containLibsPath),
launch.OptEnv(apptainerEnv, apptainerEnvFile, isCleanEnv),
launch.OptNoEval(noEval),
launch.OptNamespaces(ns),
launch.OptNetwork(network, networkArgs),
launch.OptHostname(hostname),
launch.OptDNS(dns),
launch.OptCaps(addCaps, dropCaps),
launch.OptAllowSUID(allowSUID),
launch.OptKeepPrivs(keepPrivs),
launch.OptNoPrivs(noPrivs),
launch.OptSecurity(security),
launch.OptNoUmask(noUmask),
launch.OptCgroupsJSON(cgJSON),
launch.OptConfigFile(configurationFile),
launch.OptShellPath(shellPath),
launch.OptPwdPath(pwdPath),
launch.OptFakeroot(isFakeroot),
launch.OptBoot(isBoot),
launch.OptNoInit(noInit),
launch.OptContain(isContained),
launch.OptContainAll(isContainAll),
launch.OptAppName(appName),
launch.OptKeyInfo(ki),
launch.OptCacheDisabled(disableCache),
launch.OptDMTCPLaunch(dmtcpLaunch),
launch.OptDMTCPRestart(dmtcpRestart),
launch.OptUnsquash(unsquash),
launch.OptIgnoreSubuid(ignoreSubuid),
launch.OptIgnoreFakerootCmd(ignoreFakerootCmd),
launch.OptIgnoreUserns(ignoreUserns),
launch.OptUseBuildConfig(useBuildConfig),
launch.OptTmpDir(tmpDir),
launch.OptUnderlay(underlay),
}
l, err := launch.NewLauncher(opts...)
if err != nil {
return fmt.Errorf("while configuring container: %s", err)
}
return l.Exec(cmd.Context(), image, args, instanceName)
}