From 026a65207b490fce29493977360c16a10180cc5e Mon Sep 17 00:00:00 2001 From: Cedric Clerget Date: Fri, 30 Aug 2019 11:15:03 +0200 Subject: [PATCH 1/2] Add singularity config fakeroot command to manage fakeroot user mappings --- cmd/internal/cli/actions_linux.go | 2 +- cmd/internal/cli/config_fakeroot_linux.go | 101 ++++++++++++++++++ cmd/internal/cli/config_linux.go | 27 +++++ docs/content.go | 29 +++++ .../app/singularity/config_fakeroot_linux.go | 82 ++++++++++++++ internal/pkg/fakeroot/fakeroot.go | 9 +- 6 files changed, 246 insertions(+), 4 deletions(-) create mode 100644 cmd/internal/cli/config_fakeroot_linux.go create mode 100644 cmd/internal/cli/config_linux.go create mode 100644 internal/app/singularity/config_fakeroot_linux.go diff --git a/cmd/internal/cli/actions_linux.go b/cmd/internal/cli/actions_linux.go index 5e84b2d4a8..74cd8c04e9 100644 --- a/cmd/internal/cli/actions_linux.go +++ b/cmd/internal/cli/actions_linux.go @@ -48,7 +48,7 @@ func EnsureRootPriv(cmd *cobra.Command, args []string) { // The first argument is the context sylog.Fatalf("command '%s %s' requires root privileges", args[0], cmd.Name()) } else { - sylog.Fatalf("command %s requires root privileges", cmd.Name()) + sylog.Fatalf("command '%s' requires root privileges", cmd.CommandPath()) } } } diff --git a/cmd/internal/cli/config_fakeroot_linux.go b/cmd/internal/cli/config_fakeroot_linux.go new file mode 100644 index 0000000000..32ddf27e7a --- /dev/null +++ b/cmd/internal/cli/config_fakeroot_linux.go @@ -0,0 +1,101 @@ +// Copyright (c) 2019, 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 ( + "fmt" + + "github.com/spf13/cobra" + "github.com/sylabs/singularity/docs" + "github.com/sylabs/singularity/internal/app/singularity" + "github.com/sylabs/singularity/internal/pkg/sylog" + "github.com/sylabs/singularity/pkg/cmdline" +) + +// -a|--add +var fakerootConfigAdd bool +var fakerootConfigAddFlag = cmdline.Flag{ + ID: "fakerootConfigAddFlag", + Value: &fakerootConfigAdd, + DefaultValue: false, + Name: "add", + ShortHand: "a", + Usage: "add a fakeroot mapping entry for a user allowing him to use the fakeroot feature", +} + +// -r|--remove +var fakerootConfigRemove bool +var fakerootConfigRemoveFlag = cmdline.Flag{ + ID: "fakerootConfigRemoveFlag", + Value: &fakerootConfigRemove, + DefaultValue: false, + Name: "remove", + ShortHand: "r", + Usage: "remove the user fakeroot mapping entry preventing him to use the fakeroot feature", +} + +// -e|--enable +var fakerootConfigEnable bool +var fakerootConfigEnableFlag = cmdline.Flag{ + ID: "fakerootConfigEnableFlag", + Value: &fakerootConfigEnable, + DefaultValue: false, + Name: "enable", + ShortHand: "e", + Usage: "enable a user fakeroot mapping entry allowing him to use the fakeroot feature", +} + +// -d|--disable +var fakerootConfigDisable bool +var fakerootConfigDisableFlag = cmdline.Flag{ + ID: "fakerootConfigDisableFlag", + Value: &fakerootConfigDisable, + DefaultValue: false, + Name: "disable", + ShortHand: "d", + Usage: "disable a user fakeroot mapping entry preventing him to use the fakeroot feature", +} + +// configFakerootCmd singularity config fakeroot +var configFakerootCmd = &cobra.Command{ + Args: cobra.ExactArgs(1), + DisableFlagsInUseLine: true, + PreRun: func(cmd *cobra.Command, args []string) { EnsureRootPriv(cmd, nil) }, + RunE: func(cmd *cobra.Command, args []string) error { + username := args[0] + var op singularity.FakerootConfigOp + + if fakerootConfigAdd { + op = singularity.FakerootAddUser + } else if fakerootConfigRemove { + op = singularity.FakerootRemoveUser + } else if fakerootConfigEnable { + op = singularity.FakerootEnableUser + } else if fakerootConfigDisable { + op = singularity.FakerootDisableUser + } else { + return fmt.Errorf("you must specify an option (eg: --add/--remove)") + } + + if err := singularity.FakerootConfig(username, op); err != nil { + sylog.Fatalf("%s", err) + } + + return nil + }, + + Use: docs.ConfigFakerootUse, + Short: docs.ConfigFakerootShort, + Long: docs.ConfigFakerootLong, + Example: docs.ConfigFakerootExample, +} + +func init() { + cmdManager.RegisterFlagForCmd(&fakerootConfigAddFlag, configFakerootCmd) + cmdManager.RegisterFlagForCmd(&fakerootConfigRemoveFlag, configFakerootCmd) + cmdManager.RegisterFlagForCmd(&fakerootConfigEnableFlag, configFakerootCmd) + cmdManager.RegisterFlagForCmd(&fakerootConfigDisableFlag, configFakerootCmd) +} diff --git a/cmd/internal/cli/config_linux.go b/cmd/internal/cli/config_linux.go new file mode 100644 index 0000000000..889a1896a3 --- /dev/null +++ b/cmd/internal/cli/config_linux.go @@ -0,0 +1,27 @@ +// Copyright (c) 2019, 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 ( + "github.com/spf13/cobra" + "github.com/sylabs/singularity/docs" +) + +// configCmd is the config command +var configCmd = &cobra.Command{ + DisableFlagsInUseLine: true, + Use: docs.ConfigUse, + Short: docs.ConfigShort, + Long: docs.ConfigLong, + Example: docs.ConfigExample, + SilenceErrors: true, +} + +func init() { + cmdManager.RegisterCmd(configCmd) + + cmdManager.RegisterSubCmd(configCmd, configFakerootCmd) +} diff --git a/docs/content.go b/docs/content.go index 78cc92b76e..5444bbc0b9 100644 --- a/docs/content.go +++ b/docs/content.go @@ -966,4 +966,33 @@ Enterprise Performance Computing (EPC)` mount.` OciUmountExample string = ` $ singularity oci umount /var/lib/singularity/bundles/example` + + ConfigUse string = `config` + ConfigShort string = `Manage various singularity configuration (root user only)` + ConfigLong string = ` + The config command allow root user to manage various configuration like fakeroot + user mapping entries.` + ConfigExample string = ` + All config commands have their own help output: + + $ singularity help config fakeroot + $ singularity config fakeroot --help` + + ConfigFakerootUse string = `fakeroot