-
Notifications
You must be signed in to change notification settings - Fork 132
/
apptainer.go
847 lines (740 loc) · 26.3 KB
/
apptainer.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
// 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-2021, 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 (
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
"os/signal"
"os/user"
"path/filepath"
"strings"
"text/template"
"github.com/apptainer/apptainer/docs"
"github.com/apptainer/apptainer/internal/pkg/buildcfg"
"github.com/apptainer/apptainer/internal/pkg/plugin"
"github.com/apptainer/apptainer/internal/pkg/remote"
"github.com/apptainer/apptainer/internal/pkg/remote/endpoint"
"github.com/apptainer/apptainer/internal/pkg/util/env"
"github.com/apptainer/apptainer/internal/pkg/util/fs"
"github.com/apptainer/apptainer/internal/pkg/util/starter"
"github.com/apptainer/apptainer/pkg/cmdline"
clicallback "github.com/apptainer/apptainer/pkg/plugin/callback/cli"
"github.com/apptainer/apptainer/pkg/syfs"
"github.com/apptainer/apptainer/pkg/sylog"
"github.com/apptainer/apptainer/pkg/sypgp"
"github.com/apptainer/apptainer/pkg/util/apptainerconf"
keyClient "github.com/apptainer/container-key-client/client"
libClient "github.com/apptainer/container-library-client/client"
ocitypes "github.com/containers/image/v5/types"
"github.com/spf13/cobra"
"golang.org/x/term"
)
// cmdInits holds all the init function to be called
// for commands/flags registration.
var cmdInits = make([]func(*cmdline.CommandManager), 0)
// CurrentUser holds the current user account information
var CurrentUser = getCurrentUser()
// currentRemoteEndpoint holds the current remote endpoint
var currentRemoteEndpoint *endpoint.Config
var (
dockerAuthConfig ocitypes.DockerAuthConfig
dockerLogin bool
encryptionPEMPath string
promptForPassphrase bool
forceOverwrite bool
noHTTPS bool
useBuildConfig bool
tmpDir string
)
// apptainer command flags
var (
debug bool
nocolor bool
silent bool
verbose bool
quiet bool
configurationFile string
)
// -d|--debug
var singDebugFlag = cmdline.Flag{
ID: "singDebugFlag",
Value: &debug,
DefaultValue: false,
Name: "debug",
ShortHand: "d",
Usage: "print debugging information (highest verbosity)",
EnvKeys: []string{"DEBUG"},
}
// --nocolor
var singNoColorFlag = cmdline.Flag{
ID: "singNoColorFlag",
Value: &nocolor,
DefaultValue: false,
Name: "nocolor",
Usage: "print without color output (default False)",
}
// -s|--silent
var singSilentFlag = cmdline.Flag{
ID: "singSilentFlag",
Value: &silent,
DefaultValue: false,
Name: "silent",
ShortHand: "s",
Usage: "only print errors",
}
// -q|--quiet
var singQuietFlag = cmdline.Flag{
ID: "singQuietFlag",
Value: &quiet,
DefaultValue: false,
Name: "quiet",
ShortHand: "q",
Usage: "suppress normal output",
}
// -v|--verbose
var singVerboseFlag = cmdline.Flag{
ID: "singVerboseFlag",
Value: &verbose,
DefaultValue: false,
Name: "verbose",
ShortHand: "v",
Usage: "print additional information",
}
// --docker-username
var dockerUsernameFlag = cmdline.Flag{
ID: "dockerUsernameFlag",
Value: &dockerAuthConfig.Username,
DefaultValue: "",
Name: "docker-username",
Usage: "specify a username for docker authentication",
Hidden: true,
EnvKeys: []string{"DOCKER_USERNAME"},
}
// --docker-password
var dockerPasswordFlag = cmdline.Flag{
ID: "dockerPasswordFlag",
Value: &dockerAuthConfig.Password,
DefaultValue: "",
Name: "docker-password",
Usage: "specify a password for docker authentication",
Hidden: true,
EnvKeys: []string{"DOCKER_PASSWORD"},
}
// --docker-login
var dockerLoginFlag = cmdline.Flag{
ID: "dockerLoginFlag",
Value: &dockerLogin,
DefaultValue: false,
Name: "docker-login",
Usage: "login to a Docker Repository interactively",
EnvKeys: []string{"DOCKER_LOGIN"},
}
// --passphrase
var commonPromptForPassphraseFlag = cmdline.Flag{
ID: "commonPromptForPassphraseFlag",
Value: &promptForPassphrase,
DefaultValue: false,
Name: "passphrase",
Usage: "prompt for an encryption passphrase",
}
// --pem-path
var commonPEMFlag = cmdline.Flag{
ID: "actionEncryptionPEMPath",
Value: &encryptionPEMPath,
DefaultValue: "",
Name: "pem-path",
Usage: "enter an path to a PEM formatted RSA key for an encrypted container",
}
// -F|--force
var commonForceFlag = cmdline.Flag{
ID: "commonForceFlag",
Value: &forceOverwrite,
DefaultValue: false,
Name: "force",
ShortHand: "F",
Usage: "overwrite an image file if it exists",
EnvKeys: []string{"FORCE"},
}
// --no-https
var commonNoHTTPSFlag = cmdline.Flag{
ID: "commonNoHTTPSFlag",
Value: &noHTTPS,
DefaultValue: false,
Name: "no-https",
Usage: "use http instead of https for docker:// oras:// and library://<hostname>/... URIs",
EnvKeys: []string{"NOHTTPS", "NO_HTTPS"},
}
// --nohttps (deprecated)
var commonOldNoHTTPSFlag = cmdline.Flag{
ID: "commonOldNoHTTPSFlag",
Value: &noHTTPS,
DefaultValue: false,
Name: "nohttps",
Deprecated: "use --no-https",
Usage: "use http instead of https for docker:// oras:// and library://<hostname>/... URIs",
}
// --tmpdir
var commonTmpDirFlag = cmdline.Flag{
ID: "commonTmpDirFlag",
Value: &tmpDir,
DefaultValue: os.TempDir(),
Hidden: true,
Name: "tmpdir",
Usage: "specify a temporary directory to use for build",
EnvKeys: []string{"TMPDIR"},
}
// -c|--config
var singConfigFileFlag = cmdline.Flag{
ID: "singConfigFileFlag",
Value: &configurationFile,
DefaultValue: buildcfg.APPTAINER_CONF_FILE,
Name: "config",
ShortHand: "c",
Usage: "specify a configuration file (for root or unprivileged installation only)",
EnvKeys: []string{"CONFIG_FILE"},
}
// --build-config
var singBuildConfigFlag = cmdline.Flag{
ID: "singBuildConfigFlag",
Value: &useBuildConfig,
DefaultValue: false,
Name: "build-config",
Usage: "use configuration needed for building containers",
}
func getCurrentUser() *user.User {
usr, err := user.Current()
if err != nil {
sylog.Fatalf("Couldn't determine user account information: %v", err)
}
return usr
}
func addCmdInit(cmdInit func(*cmdline.CommandManager)) {
cmdInits = append(cmdInits, cmdInit)
}
func setSylogMessageLevel() {
var level int
if debug {
level = 5
// Propagate debug flag to nested `apptainer` calls.
os.Setenv("APPTAINER_DEBUG", "1")
} else if verbose {
level = 4
} else if quiet {
level = -1
} else if silent {
level = -3
} else {
level = 1
}
color := true
if nocolor || !term.IsTerminal(2) {
color = false
}
sylog.SetLevel(level, color)
}
// handleRemoteConf will make sure your 'remote.yaml' config file
// has the correct permission.
func handleRemoteConf(remoteConfFile string) error {
// Only check the permission if it exists.
if fs.IsFile(remoteConfFile) {
sylog.Debugf("Ensuring file permission of 0600 on %s", remoteConfFile)
if err := fs.EnsureFileWithPermission(remoteConfFile, 0o600); err != nil {
return fmt.Errorf("unable to correct the permission on %s: %w", remoteConfFile, err)
}
}
return nil
}
// handleConfDir tries to create the user's configuration directory and handles
// messages and/or errors.
func handleConfDir(confDir, legacyConfigDir string) {
ok, err := fs.PathExists(confDir)
if err != nil {
sylog.Warningf("Unable to retrieve information for %s: %s", confDir, err)
return
}
// apptainer user config directory exists, run perm check and return
if ok {
sylog.Debugf("%s already exists. Not creating.", confDir)
fi, err := os.Stat(confDir)
if err != nil {
sylog.Warningf("Unable to retrieve information for %s: %s", confDir, err)
return
}
if fi.Mode().Perm() != 0o700 {
sylog.Debugf("Enforce permission 0700 on %s", confDir)
// enforce permission on user configuration directory
if err := os.Chmod(confDir, 0o700); err != nil {
// best effort as chmod could fail for various reasons (eg: readonly FS)
sylog.Warningf("Couldn't enforce permission 0700 on %s: %s", confDir, err)
}
}
return
}
// apptainer user config directory doesnt exist, create it.
err = fs.Mkdir(confDir, 0o700)
if err != nil {
sylog.Debugf("Could not create %s: %s", confDir, err)
return
}
sylog.Debugf("Created %s", confDir)
// check if singularity user config directory exists, use it to populate configs of
// apptainer user config directory if it does.
ok, err = fs.PathExists(legacyConfigDir)
if err != nil {
sylog.Warningf("Failed to retrieve information for %s: %s", legacyConfigDir, err)
return
}
// singularity user config directory doesnt exist, return
if !ok {
return
}
sylog.Infof("Detected Singularity user configuration directory")
migrateRemoteConf(confDir, legacyConfigDir)
migrateDockerConf(confDir, legacyConfigDir)
migrateKeys(confDir, legacyConfigDir)
}
func migrateRemoteConf(confDir, legacyConfigDir string) {
ok, err := fs.PathExists(syfs.LegacyRemoteConf())
if err != nil {
sylog.Warningf("Failed to retrieve information for %s: %s", syfs.LegacyRemoteConf(), err)
sylog.Warningf("Migration failed, you can migrate manually with \"cp -a %s %s\"", syfs.LegacyRemoteConf(), syfs.RemoteConf())
return
} else if !ok {
return
}
sylog.Infof("Detected Singularity remote configuration, migrating...")
// Try to load legacy remote config to check version compatibility
_, err = loadRemoteConf(syfs.LegacyRemoteConf())
if err != nil {
sylog.Warningf("Migration failed, unable to read legacy remote configuration: %s", err)
sylog.Warningf("It may be of an incompatible format and needs to be reconstructed manually with the \"apptainer remote\" command group.")
return
}
err = fs.CopyFile(syfs.LegacyRemoteConf(), syfs.RemoteConf(), 0o600)
if err != nil {
sylog.Warningf("Failed to migrate %s to %s: %s", syfs.LegacyRemoteConf(), syfs.RemoteConf(), err)
sylog.Warningf("Migration failed, you can migrate manually with \"cp -a %s %s\"", syfs.LegacyRemoteConf(), syfs.RemoteConf())
}
}
func migrateDockerConf(confDir, legacyConfigDir string) {
ok, err := fs.PathExists(syfs.LegacyDockerConf())
if err != nil {
sylog.Warningf("Failed to retrieve information for %s: %s", syfs.LegacyDockerConf(), err)
sylog.Warningf("Migration failed, you can migrate manually with \"cp -a %s %s\"", syfs.LegacyDockerConf(), syfs.DockerConf())
return
} else if !ok {
return
}
sylog.Infof("Detected Singularity docker configuration, migrating...")
err = fs.CopyFile(syfs.LegacyDockerConf(), syfs.DockerConf(), 0o600)
if err != nil {
sylog.Warningf("Failed to migrate %s to %s: %s", syfs.LegacyDockerConf(), syfs.DockerConf(), err)
sylog.Warningf("Migration failed, you can migrate manually with \"cp -a %s %s\"", syfs.LegacyDockerConf(), syfs.DockerConf())
}
}
func migrateKeys(confDir, legacyConfigDir string) {
legacySypgpDir := filepath.Join(syfs.LegacyConfigDir(), sypgp.LegacyDirectory)
keysDir := filepath.Join(syfs.ConfigDir(), sypgp.Directory)
ok, err := fs.PathExists(legacySypgpDir)
if err != nil {
sylog.Warningf("Failed to retrieve information for %s: %s", legacySypgpDir, err)
sylog.Warningf("Migration failed, you can migrate manually with \"cp -a %s %s\"", legacySypgpDir, keysDir)
return
} else if !ok {
return
}
err = fs.Mkdir(keysDir, 0o700)
if err != nil {
sylog.Debugf("Could not create %s: %s", keysDir, err)
sylog.Warningf("Migration failed, you can migrate manually with \"cp -a %s %s\"", legacySypgpDir, keysDir)
return
}
migrateGPGPublic(keysDir, legacySypgpDir)
migrateGPGPrivate(keysDir, legacySypgpDir)
}
func migrateGPGPublic(sypgpDir, legacySypgpDir string) {
legacyPublicPath := filepath.Join(legacySypgpDir, sypgp.PublicFile)
publicPath := filepath.Join(sypgpDir, sypgp.PublicFile)
ok, err := fs.PathExists(legacyPublicPath)
if err != nil {
sylog.Warningf("Failed to retrieve information for %s: %s", legacyPublicPath, err)
sylog.Warningf("Migration failed, you can migrate manually with \"cp -a %s %s\"", legacyPublicPath, publicPath)
return
} else if !ok {
return
}
sylog.Infof("Detected public Singularity pgp keyring, migrating...")
err = fs.CopyFile(legacyPublicPath, publicPath, 0o600)
if err != nil {
sylog.Warningf("Failed to migrate %s to %s: %s", legacyPublicPath, publicPath, err)
sylog.Warningf("Migration failed, you can migrate manually with \"cp -a %s %s\"", legacyPublicPath, publicPath)
}
}
func migrateGPGPrivate(sypgpDir, legacySypgpDir string) {
legacyPrivatePath := filepath.Join(legacySypgpDir, sypgp.SecretFile)
privatePath := filepath.Join(sypgpDir, sypgp.SecretFile)
ok, err := fs.PathExists(legacyPrivatePath)
if err != nil {
sylog.Warningf("Failed to retrieve information for %s: %s", legacyPrivatePath, err)
sylog.Warningf("Migration failed, you can migrate manually with \"cp -a %s %s\"", legacyPrivatePath, privatePath)
return
} else if !ok {
return
}
sylog.Infof("Detected private Singularity pgp keyring, migrating...")
err = fs.CopyFile(legacyPrivatePath, privatePath, 0o600)
if err != nil {
sylog.Warningf("Failed to migrate %s to %s: %s", legacyPrivatePath, privatePath, err)
sylog.Warningf("Migration failed, you can migrate manually with \"cp -a %s %s\"", legacyPrivatePath, privatePath)
}
}
func persistentPreRun(*cobra.Command, []string) error {
setSylogMessageLevel()
sylog.Debugf("Apptainer version: %s", buildcfg.PACKAGE_VERSION)
var config *apptainerconf.File
var err error
if useBuildConfig {
sylog.Debugf("Using container build configuration")
// Base this on a default configuration.
config, err = apptainerconf.Parse("")
if err != nil {
return fmt.Errorf("failure getting default config: %v", err)
}
apptainerconf.ApplyBuildConfig(config)
} else {
if os.Geteuid() != 0 && starter.IsSuidInstall() {
if configurationFile != singConfigFileFlag.DefaultValue {
return fmt.Errorf("--config requires to be root or an unprivileged installation")
}
}
oldconfdir := filepath.Dir(filepath.Dir(configurationFile)) + "/singularity/"
if _, err := os.Stat(oldconfdir); err == nil {
sylog.Warningf("%s exists, migration to apptainer by system administrator is not complete", oldconfdir)
}
sylog.Debugf("Parsing configuration file %s", configurationFile)
config, err = apptainerconf.Parse(configurationFile)
if err != nil {
return fmt.Errorf("couldn't parse configuration file %s: %s", configurationFile, err)
}
}
apptainerconf.SetCurrentConfig(config)
// Include the user's PATH for now.
// It will be overridden later if using setuid flow.
apptainerconf.SetBinaryPath(buildcfg.LIBEXECDIR, true)
// Handle the config dir (~/.apptainer),
// then check the remove conf file permission.
handleConfDir(syfs.ConfigDir(), syfs.LegacyConfigDir())
if err := handleRemoteConf(syfs.RemoteConf()); err != nil {
return fmt.Errorf("while handling remote config: %w", err)
}
return nil
}
// Init initializes and registers all apptainer commands.
func Init(loadPlugins bool) {
cmdManager := cmdline.NewCommandManager(apptainerCmd)
apptainerCmd.Flags().SetInterspersed(false)
apptainerCmd.PersistentFlags().SetInterspersed(false)
templateFuncs := template.FuncMap{
"TraverseParentsUses": TraverseParentsUses,
}
cobra.AddTemplateFuncs(templateFuncs)
apptainerCmd.SetHelpTemplate(docs.HelpTemplate)
apptainerCmd.SetUsageTemplate(docs.UseTemplate)
vt := fmt.Sprintf("%s version {{printf \"%%s\" .Version}}\n", buildcfg.PACKAGE_NAME)
apptainerCmd.SetVersionTemplate(vt)
// set persistent pre run function here to avoid initialization loop error
apptainerCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
var err error
foundKeys := make(map[string]string)
for precedence := range env.ApptainerPrefixes {
err = cmdManager.UpdateCmdFlagFromEnv(apptainerCmd, precedence, foundKeys)
if nil != err {
sylog.Fatalf("While parsing global environment variables: %s", err)
}
}
for precedence := range env.ApptainerPrefixes {
err = cmdManager.UpdateCmdFlagFromEnv(cmd, precedence, foundKeys)
if nil != err {
sylog.Fatalf("While parsing environment variables: %s", err)
}
}
if err := persistentPreRun(cmd, args); err != nil {
sylog.Fatalf("While initializing: %s", err)
}
return nil
}
cmdManager.RegisterFlagForCmd(&singDebugFlag, apptainerCmd)
cmdManager.RegisterFlagForCmd(&singNoColorFlag, apptainerCmd)
cmdManager.RegisterFlagForCmd(&singSilentFlag, apptainerCmd)
cmdManager.RegisterFlagForCmd(&singQuietFlag, apptainerCmd)
cmdManager.RegisterFlagForCmd(&singVerboseFlag, apptainerCmd)
cmdManager.RegisterFlagForCmd(&singConfigFileFlag, apptainerCmd)
cmdManager.RegisterFlagForCmd(&singBuildConfigFlag, apptainerCmd)
cmdManager.RegisterCmd(VersionCmd)
// register all others commands/flags
for _, cmdInit := range cmdInits {
cmdInit(cmdManager)
}
// load plugins and register commands/flags if any
if loadPlugins {
callbackType := (clicallback.Command)(nil)
callbacks, err := plugin.LoadCallbacks(callbackType)
if err != nil {
sylog.Fatalf("Failed to load plugins callbacks '%T': %s", callbackType, err)
}
for _, c := range callbacks {
c.(clicallback.Command)(cmdManager)
}
}
// any error reported by command manager is considered as fatal
cliErrors := len(cmdManager.GetError())
if cliErrors > 0 {
for _, e := range cmdManager.GetError() {
sylog.Errorf("%s", e)
}
sylog.Fatalf("CLI command manager reported %d error(s)", cliErrors)
}
}
// apptainerCmd is the base command when called without any subcommands
var apptainerCmd = &cobra.Command{
TraverseChildren: true,
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) error {
return cmdline.CommandError("invalid command")
},
Use: docs.ApptainerUse,
Version: buildcfg.PACKAGE_VERSION,
Short: docs.ApptainerShort,
Long: docs.ApptainerLong,
Example: docs.ApptainerExample,
SilenceErrors: true,
SilenceUsage: true,
}
// RootCmd returns the root apptainer cobra command.
func RootCmd() *cobra.Command {
return apptainerCmd
}
// ExecuteApptainer adds all child commands to the root command and sets
// flags appropriately. This is called by main.main(). It only needs to happen
// once to the root command (apptainer).
func ExecuteApptainer() {
loadPlugins := true
// we avoid to load installed plugins to not double load
// them during execution of plugin compile and plugin install
args := os.Args
if len(args) > 1 {
loadPlugins = !strings.HasPrefix(args[1], "plugin")
}
Init(loadPlugins)
// Setup a cancellable context that will trap Ctrl-C / SIGINT
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
defer func() {
signal.Stop(c)
cancel()
}()
go func() {
select {
case <-c:
sylog.Debugf("User requested cancellation with interrupt")
cancel()
case <-ctx.Done():
}
}()
if err := apptainerCmd.ExecuteContext(ctx); err != nil {
// Find the subcommand to display more useful help, and the correct
// subcommand name in messages - i.e. 'run' not 'apptainer'
// This is required because we previously used ExecuteC that returns the
// subcommand... but there is no ExecuteC that variant accepts a context.
subCmd, _, subCmdErr := apptainerCmd.Find(args[1:])
if subCmdErr != nil {
apptainerCmd.Printf("Error: %v\n\n", subCmdErr)
}
name := subCmd.Name()
switch err.(type) {
case cmdline.FlagError:
usage := subCmd.Flags().FlagUsagesWrapped(getColumns())
apptainerCmd.Printf("Error for command %q: %s\n\n", name, err)
apptainerCmd.Printf("Options for %s command:\n\n%s\n", name, usage)
case cmdline.CommandError:
apptainerCmd.Println(subCmd.UsageString())
default:
apptainerCmd.Printf("Error for command %q: %s\n\n", name, err)
apptainerCmd.Println(subCmd.UsageString())
}
apptainerCmd.Printf("Run '%s --help' for more detailed usage information.\n",
apptainerCmd.CommandPath())
os.Exit(1)
}
}
// GenBashCompletion writes the bash completion file to w.
func GenBashCompletion(w io.Writer, name string) error {
Init(false)
apptainerCmd.Use = name
return apptainerCmd.GenBashCompletion(w)
}
// TraverseParentsUses walks the parent commands and outputs a properly formatted use string
func TraverseParentsUses(cmd *cobra.Command) string {
if cmd.HasParent() {
return TraverseParentsUses(cmd.Parent()) + cmd.Use + " "
}
return cmd.Use + " "
}
// VersionCmd displays installed apptainer version
var VersionCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(buildcfg.PACKAGE_VERSION)
},
Use: "version",
Short: "Show the version for Apptainer",
}
func loadRemoteConf(filepath string) (*remote.Config, error) {
f, err := os.OpenFile(filepath, os.O_RDONLY, 0o600)
if err != nil {
return nil, fmt.Errorf("while opening remote config file: %s", err)
}
defer f.Close()
c, err := remote.ReadFrom(f)
if err != nil {
return nil, fmt.Errorf("while parsing remote config data: %s", err)
}
return c, nil
}
// getRemote returns the remote in use or an error
func getRemote() (*endpoint.Config, error) {
var c *remote.Config
// try to load both remotes, check for errors, sync if both exist,
// if neither exist return errNoDefault to return to old auth behavior
cSys, sysErr := loadRemoteConf(remote.SystemConfigPath)
cUsr, usrErr := loadRemoteConf(syfs.RemoteConf())
if sysErr != nil && usrErr != nil {
return endpoint.DefaultEndpointConfig, nil
} else if sysErr != nil {
c = cUsr
} else if usrErr != nil {
c = cSys
} else {
// sync cUsr with system config cSys
if err := cUsr.SyncFrom(cSys); err != nil {
return nil, err
}
c = cUsr
}
ep, err := c.GetDefault()
if err == remote.ErrNoDefault {
// all remotes have been deleted, fix that by returning
// the default remote endpoint to avoid side effects when
// pulling from library
if len(c.Remotes) == 0 {
return endpoint.DefaultEndpointConfig, nil
}
// otherwise notify users about available endpoints and
// invite them to select one of them
help := "use 'apptainer remote use <endpoint>', available endpoints are: "
endpoints := make([]string, 0, len(c.Remotes))
for name := range c.Remotes {
endpoints = append(endpoints, name)
}
help += strings.Join(endpoints, ", ")
return nil, fmt.Errorf("no default endpoint set: %s", help)
}
return ep, err
}
func apptainerExec(image string, args []string) (string, error) {
// Record from stdout and store as a string to return as the contents of the file.
var stdout bytes.Buffer
var stderr bytes.Buffer
abspath, err := filepath.Abs(image)
if err != nil {
return "", fmt.Errorf("while determining absolute path for %s: %v", image, err)
}
// re-use apptainer exec to grab image file content,
// we reduce binds to the bare minimum with options below
cmdArgs := []string{"exec", "--contain", "--no-home", "--no-nv", "--no-rocm", abspath}
cmdArgs = append(cmdArgs, args...)
apptainerCmd := filepath.Join(buildcfg.BINDIR, "apptainer")
cmd := exec.Command(apptainerCmd, cmdArgs...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
// move to the root to not bind the current working directory
// while inspecting the image
cmd.Dir = "/"
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("unable to process command: %s: error output:\n%s", err, stderr.String())
}
return stdout.String(), nil
}
// CheckRoot ensures that a command is executed with root privileges.
func CheckRoot(cmd *cobra.Command, args []string) {
if os.Geteuid() != 0 {
sylog.Fatalf("%q command requires root privileges", cmd.CommandPath())
}
}
// CheckRootOrUnpriv ensures that a command is executed with root
// privileges or that Apptainer is installed unprivileged.
func CheckRootOrUnpriv(cmd *cobra.Command, args []string) {
if os.Geteuid() != 0 && starter.IsSuidInstall() {
sylog.Fatalf("%q command requires root privileges or an unprivileged installation", cmd.CommandPath())
}
}
// getKeyServerClientOpts returns client options for keyserver access.
// A "" value for uri will return client options for the current endpoint.
// A specified uri will return client options for that keyserver.
func getKeyserverClientOpts(uri string, op endpoint.KeyserverOp) ([]keyClient.Option, error) {
if currentRemoteEndpoint == nil {
var err error
// if we can load config and if default endpoint is set, use that
// otherwise fall back on regular authtoken and URI behavior
currentRemoteEndpoint, err = getRemote()
if err != nil {
return nil, fmt.Errorf("unable to load remote configuration: %v", err)
}
}
if currentRemoteEndpoint == endpoint.DefaultEndpointConfig {
sylog.Warningf("No default remote in use, falling back to default keyserver: %s", endpoint.DefaultKeyserverURI)
}
return currentRemoteEndpoint.KeyserverClientOpts(uri, op)
}
// getLibraryClientConfig returns client config for library server access.
// A "" value for uri will return client config for the current endpoint.
// A specified uri will return client options for that library server.
func getLibraryClientConfig(uri string) (*libClient.Config, error) {
if currentRemoteEndpoint == nil {
var err error
// if we can load config and if default endpoint is set, use that
// otherwise fall back on regular authtoken and URI behavior
currentRemoteEndpoint, err = getRemote()
if err != nil {
return nil, fmt.Errorf("unable to load remote configuration: %v", err)
}
}
if currentRemoteEndpoint == endpoint.DefaultEndpointConfig {
if endpoint.DefaultLibraryURI != "" {
sylog.Warningf("no default remote in use, falling back to default library: %s", endpoint.DefaultLibraryURI)
} else {
return nil, fmt.Errorf("no default remote with library client in use")
}
}
libClientConfig, err := currentRemoteEndpoint.LibraryClientConfig(uri)
if err != nil {
return nil, err
}
if libClientConfig.BaseURL == "" {
return nil, fmt.Errorf("remote has no library client")
}
return libClientConfig, nil
}