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
|
// Copyright (c) 2020, Control Command Inc. All rights reserved.
// Copyright (c) 2018-2023, 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"
ocitypes "github.com/containers/image/v5/types"
ggcrv1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/spf13/cobra"
scskeyclient "github.com/sylabs/scs-key-client/client"
scslibclient "github.com/sylabs/scs-library-client/client"
"github.com/sylabs/singularity/v4/docs"
"github.com/sylabs/singularity/v4/internal/pkg/buildcfg"
"github.com/sylabs/singularity/v4/internal/pkg/ociplatform"
"github.com/sylabs/singularity/v4/internal/pkg/plugin"
"github.com/sylabs/singularity/v4/internal/pkg/remote"
"github.com/sylabs/singularity/v4/internal/pkg/remote/endpoint"
ocilauncher "github.com/sylabs/singularity/v4/internal/pkg/runtime/launcher/oci"
"github.com/sylabs/singularity/v4/internal/pkg/util/fs"
"github.com/sylabs/singularity/v4/internal/pkg/util/rootless"
"github.com/sylabs/singularity/v4/pkg/cmdline"
clicallback "github.com/sylabs/singularity/v4/pkg/plugin/callback/cli"
"github.com/sylabs/singularity/v4/pkg/syfs"
"github.com/sylabs/singularity/v4/pkg/sylog"
"github.com/sylabs/singularity/v4/pkg/util/singularityconf"
"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
const (
envPrefix = "SINGULARITY_"
)
// Top level options on the `singularity` root command.
var (
debug bool
nocolor bool
silent bool
verbose bool
quiet bool
configurationFile string
)
// Common options used with multiple sub-commands.
var (
// OCI Registry Authentication
dockerAuthConfig ocitypes.DockerAuthConfig
dockerLogin bool
dockerHost string
noHTTPS bool
// Encryption Material
encryptionPEMPath string
promptForPassphrase bool
// Paths / file handling
tmpDir string
forceOverwrite bool
// Options controlling the unpacking of images to temporary sandboxes
canUseTmpSandbox bool
noTmpSandbox bool
// Use OCI runtime and OCI SIF?
isOCI bool
noOCI bool
// Platform for retrieving images
arch string
platform string
)
//
// Top level option flags
//
// -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",
}
// -c|--config
var singConfigFileFlag = cmdline.Flag{
ID: "singConfigFileFlag",
Value: &configurationFile,
DefaultValue: buildcfg.SINGULARITY_CONF_FILE,
Name: "config",
ShortHand: "c",
Usage: "specify a configuration file (for root or unprivileged installation only)",
EnvKeys: []string{"CONFIG_FILE"},
}
//
// Common option flags for multiple subcommands
//
// --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"},
WithoutPrefix: true,
}
// --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"},
WithoutPrefix: true,
}
// --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"},
}
// --docker-host
var dockerHostFlag = cmdline.Flag{
ID: "dockerHostFlag",
Value: &dockerHost,
DefaultValue: "",
Name: "docker-host",
Usage: "specify a custom Docker daemon host",
EnvKeys: []string{"DOCKER_HOST"},
WithoutPrefix: true,
}
// --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",
}
// --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"},
}
// --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"},
}
// --oci
var commonOCIFlag = cmdline.Flag{
ID: "actionOCI",
Value: &isOCI,
DefaultValue: false,
Name: "oci",
Usage: "Launch container with OCI runtime (experimental)",
EnvKeys: []string{"OCI"},
}
// --no-oci
var commonNoOCIFlag = cmdline.Flag{
ID: "commonNoOCI",
Value: &noOCI,
DefaultValue: false,
Name: "no-oci",
Usage: "Launch container with native runtime",
EnvKeys: []string{"NO_OCI"},
}
// --no-tmp-sandbox
var actionNoTmpSandbox = cmdline.Flag{
ID: "actionNoTmpSandbox",
Value: &noTmpSandbox,
DefaultValue: false,
Name: "no-tmp-sandbox",
Usage: "Prohibits unpacking of images into temporary sandbox dirs",
EnvKeys: []string{"NO_TMP_SANDBOX"},
}
// --arch
var commonArchFlag = cmdline.Flag{
ID: "commonArchFlag",
Value: &arch,
DefaultValue: "",
Name: "arch",
Usage: "architecture to use when pulling images",
EnvKeys: []string{"PULL_ARCH", "ARCH"},
}
// --platform
var commonPlatformFlag = cmdline.Flag{
ID: "commonPlatformFlag",
Value: &platform,
DefaultValue: "",
Name: "platform",
Usage: "platform (OS/Architecture/Variant) to use when pulling images",
EnvKeys: []string{"PLATFORM"},
}
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 `singularity` calls.
os.Setenv("SINGULARITY_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 string) error {
if err := fs.Mkdir(confDir, 0o700); err != nil {
if os.IsExist(err) {
sylog.Debugf("%s already exists. Not creating.", confDir)
fi, err := os.Stat(confDir)
if err != nil {
return fmt.Errorf("failed to retrieve information for %s: %s", confDir, err)
}
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)
}
}
} else {
sylog.Debugf("Could not create %s: %s", confDir, err)
}
} else {
sylog.Debugf("Created %s", confDir)
}
return nil
}
func persistentPreRun(*cobra.Command, []string) error {
setSylogMessageLevel()
sylog.Debugf("Singularity version: %s", buildcfg.PACKAGE_VERSION)
if os.Geteuid() != 0 && buildcfg.SINGULARITY_SUID_INSTALL == 1 {
if configurationFile != singConfigFileFlag.DefaultValue {
return fmt.Errorf("--config requires to be root or an unprivileged installation")
}
}
sylog.Debugf("Parsing configuration file %s", configurationFile)
config, err := singularityconf.Parse(configurationFile)
if err != nil {
return fmt.Errorf("couldn't parse configuration file %s: %s", configurationFile, err)
}
singularityconf.SetCurrentConfig(config)
// Honor 'oci mode' in singularity.conf, and allow negation with `--no-oci`.
if isOCI && noOCI {
return fmt.Errorf("--oci and --no-oci cannot be used together")
}
isOCI = isOCI || config.OCIMode
if noOCI {
isOCI = false
}
// Honor 'tmp sandbox' in singularity.conf, and allow negation with
// `--no-tmp-sandbox`.
canUseTmpSandbox = config.TmpSandboxAllowed
if noTmpSandbox {
canUseTmpSandbox = false
}
// If we need to enter a namespace (oci-mode) do the re-exec now, before any
// other handling happens.
if err := maybeReExec(); err != nil {
return err
}
// Handle the config dir (~/.singularity),
// then check the remove conf file permission.
if err := handleConfDir(syfs.ConfigDir()); err != nil {
return fmt.Errorf("while handling config dir: %w", err)
}
if err := handleRemoteConf(syfs.RemoteConf()); err != nil {
return fmt.Errorf("while handling remote config: %w", err)
}
return nil
}
// Init initializes and registers all singularity commands.
func Init(loadPlugins bool) {
cmdManager := cmdline.NewCommandManager(singularityCmd)
singularityCmd.Flags().SetInterspersed(false)
singularityCmd.PersistentFlags().SetInterspersed(false)
templateFuncs := template.FuncMap{
"TraverseParentsUses": TraverseParentsUses,
}
cobra.AddTemplateFuncs(templateFuncs)
singularityCmd.SetHelpTemplate(docs.HelpTemplate)
singularityCmd.SetUsageTemplate(docs.UseTemplate)
vt := fmt.Sprintf("%s version {{printf \"%%s\" .Version}}\n", buildcfg.PACKAGE_NAME)
singularityCmd.SetVersionTemplate(vt)
// set persistent pre run function here to avoid initialization loop error
singularityCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
if err := cmdManager.UpdateCmdFlagFromEnv(singularityCmd, envPrefix); err != nil {
sylog.Fatalf("While parsing global environment variables: %s", err)
}
if err := cmdManager.UpdateCmdFlagFromEnv(cmd, envPrefix); err != nil {
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, singularityCmd)
cmdManager.RegisterFlagForCmd(&singNoColorFlag, singularityCmd)
cmdManager.RegisterFlagForCmd(&singSilentFlag, singularityCmd)
cmdManager.RegisterFlagForCmd(&singQuietFlag, singularityCmd)
cmdManager.RegisterFlagForCmd(&singVerboseFlag, singularityCmd)
cmdManager.RegisterFlagForCmd(&singConfigFileFlag, singularityCmd)
cmdManager.RegisterCmd(VersionCmd)
// register all others commands/flags
for _, cmdInit := range cmdInits {
cmdInit(cmdManager)
}
// load plugins and register commands/flags if any
//nolint:forcetypeassert
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)
}
}
// singularityCmd is the base command when called without any subcommands
var singularityCmd = &cobra.Command{
TraverseChildren: true,
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) error {
return cmdline.CommandError("invalid command")
},
Use: docs.SingularityUse,
Version: buildcfg.PACKAGE_VERSION,
Short: docs.SingularityShort,
Long: docs.SingularityLong,
Example: docs.SingularityExample,
SilenceErrors: true,
SilenceUsage: true,
}
// RootCmd returns the root singularity cobra command.
func RootCmd() *cobra.Command {
return singularityCmd
}
// ExecuteSingularity 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 (singularity).
func ExecuteSingularity() {
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 := singularityCmd.ExecuteContext(ctx); err != nil {
// Find the subcommand to display more useful help, and the correct
// subcommand name in messages - i.e. 'run' not 'singularity'
// This is required because we previously used ExecuteC that returns the
// subcommand... but there is no ExecuteC that variant accepts a context.
subCmd, _, subCmdErr := singularityCmd.Find(args[1:])
if subCmdErr != nil {
singularityCmd.Printf("Error: %v\n\n", subCmdErr)
}
name := subCmd.Name()
switch err.(type) {
case cmdline.FlagError:
usage := subCmd.Flags().FlagUsagesWrapped(getColumns())
singularityCmd.Printf("Error for command %q: %s\n\n", name, err)
singularityCmd.Printf("Options for %s command:\n\n%s\n", name, usage)
case cmdline.CommandError:
singularityCmd.Println(subCmd.UsageString())
default:
singularityCmd.Printf("Error for command %q: %s\n\n", name, err)
singularityCmd.Println(subCmd.UsageString())
}
singularityCmd.Printf("Run '%s --help' for more detailed usage information.\n",
singularityCmd.CommandPath())
os.Exit(1)
}
}
// GenBashCompletion writes the bash completion file to w.
func GenBashCompletion(w io.Writer) error {
Init(false)
return singularityCmd.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 singularity 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 Singularity",
}
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
}
// sylabsRemote returns the remote in use or an error
func sylabsRemote() (*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 or with remote build
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 'singularity 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 singularityExec(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 singularity 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...)
singularityCmd := filepath.Join(buildcfg.BINDIR, "singularity")
cmd := exec.Command(singularityCmd, 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, _ []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 Singularity is installed unprivileged.
func CheckRootOrUnpriv(cmd *cobra.Command, _ []string) {
if os.Geteuid() != 0 && buildcfg.SINGULARITY_SUID_INSTALL == 1 {
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) ([]scskeyclient.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 = sylabsRemote()
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.SCSDefaultKeyserverURI)
}
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) (*scslibclient.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 = sylabsRemote()
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 library: %s", endpoint.SCSDefaultLibraryURI)
}
return currentRemoteEndpoint.LibraryClientConfig(uri)
}
// getBuilderClientConfig returns the base URI and auth token to use for build server access. A ""
// value for uri will use the current endpoint. A specified uri will return client options for that
// build server.
func getBuilderClientConfig(uri string) (baseURI, authToken string, err 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 = sylabsRemote()
if err != nil {
return "", "", fmt.Errorf("unable to load remote configuration: %v", err)
}
}
if currentRemoteEndpoint == endpoint.DefaultEndpointConfig {
sylog.Warningf("No default remote in use, falling back to default builder: %s", endpoint.SCSDefaultBuilderURI)
}
return currentRemoteEndpoint.BuilderClientConfig(uri)
}
func maybeReExec() error {
sylog.Debugf("Checking whether to re-exec")
// The OCI runtime must always be launched where the effective uid/gid is 0 (root or fake-root).
if isOCI && !rootless.InNS() {
// If we need to, enter a new cgroup now, to workaround an issue with crun container cgroup creation (#1538).
if err := ocilauncher.CrunNestCgroup(); err != nil {
return fmt.Errorf("while applying crun cgroup workaround: %w", err)
}
// If we are root already, run the launcher in a new mount namespace only.
if os.Geteuid() == 0 {
return rootless.RunInMountNS(os.Args[1:])
}
// If we are not root, re-exec in a root-mapped user namespace and mount namespace.
return rootless.ExecWithFakeroot(os.Args[1:])
}
return nil
}
// getOCIPlatform returns the appropriate OCI platform to use according to `--arch` and `--platform`
func getOCIPlatform() ggcrv1.Platform {
var (
p *ggcrv1.Platform
err error
)
if arch != "" && platform != "" {
err = fmt.Errorf("--arch and --platform cannot be used together")
}
if arch == "" && platform == "" {
p, err = ociplatform.DefaultPlatform()
}
if arch != "" {
p, err = ociplatform.PlatformFromArch(arch)
}
if platform != "" {
p, err = ociplatform.PlatformFromString(platform)
}
if err != nil {
sylog.Fatalf("%v", err)
}
return *p
}
|