Added wwctl overlay chown ... command group

This commit is contained in:
Gregory Kurtzer
2021-09-06 17:38:04 -07:00
parent 16843d8b4e
commit 349981f5bb
3 changed files with 126 additions and 0 deletions

View File

@@ -0,0 +1,98 @@
package chown
import (
"os"
"path"
"strconv"
"github.com/hpcng/warewulf/internal/pkg/config"
"github.com/hpcng/warewulf/internal/pkg/node"
"github.com/hpcng/warewulf/internal/pkg/overlay"
"github.com/hpcng/warewulf/internal/pkg/util"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
"github.com/spf13/cobra"
)
func CobraRunE(cmd *cobra.Command, args []string) error {
var overlaySourceDir string
overlayName := args[0]
fileName := args[1]
var uid int
var gid int
var err error
uid, err = strconv.Atoi(args[2])
if err != nil {
wwlog.Printf(wwlog.ERROR, "UID is not an integer: %s\n", args[2])
os.Exit(1)
}
if len(args) > 3 {
gid, err = strconv.Atoi(args[3])
if err != nil {
wwlog.Printf(wwlog.ERROR, "GID is not an integer: %s\n", args[3])
os.Exit(1)
}
} else {
gid = 0
}
if SystemOverlay {
overlaySourceDir = config.SystemOverlaySource(overlayName)
} else {
overlaySourceDir = config.RuntimeOverlaySource(overlayName)
}
if !util.IsDir(overlaySourceDir) {
wwlog.Printf(wwlog.ERROR, "Overlay does not exist: %s\n", overlayName)
os.Exit(1)
}
overlayFile := path.Join(overlaySourceDir, fileName)
if !util.IsFile(overlayFile) && !util.IsDir(overlayFile) {
wwlog.Printf(wwlog.ERROR, "File does not exist within overlay: %s:%s\n", overlayName, fileName)
os.Exit(1)
}
err = os.Chown(overlayFile, uid, gid)
if err != nil {
wwlog.Printf(wwlog.ERROR, "Could not set ownership: %s\n", err)
os.Exit(1)
}
if !NoOverlayUpdate {
n, err := node.New()
if err != nil {
wwlog.Printf(wwlog.ERROR, "Could not open node configuration: %s\n", err)
os.Exit(1)
}
nodes, err := n.FindAllNodes()
if err != nil {
wwlog.Printf(wwlog.ERROR, "Could not get node list: %s\n", err)
os.Exit(1)
}
var updateNodes []node.NodeInfo
for _, node := range nodes {
if SystemOverlay == true && node.SystemOverlay.Get() == overlayName {
updateNodes = append(updateNodes, node)
} else if node.RuntimeOverlay.Get() == overlayName {
updateNodes = append(updateNodes, node)
}
}
if SystemOverlay {
wwlog.Printf(wwlog.INFO, "Updating System Overlays...\n")
return overlay.BuildSystemOverlay(updateNodes)
} else {
wwlog.Printf(wwlog.INFO, "Updating Runtime Overlays...\n")
return overlay.BuildRuntimeOverlay(updateNodes)
}
}
return nil
}

View File

@@ -0,0 +1,26 @@
package chown
import "github.com/spf13/cobra"
var (
baseCmd = &cobra.Command{
Use: "chown [flags] <overlay> <path> <UID> [<GID>]",
Short: "Change file ownership within an overlay",
Long: "This command will allow you to change the ownership of a file within an\n" +
"overlay.",
RunE: CobraRunE,
Args: cobra.RangeArgs(3, 4),
}
SystemOverlay bool
NoOverlayUpdate bool
)
func init() {
baseCmd.PersistentFlags().BoolVarP(&SystemOverlay, "system", "s", false, "Show System Overlays as well")
baseCmd.PersistentFlags().BoolVarP(&NoOverlayUpdate, "noupdate", "n", false, "Don't update overlays")
}
// GetRootCommand returns the root cobra.Command for the application.
func GetCommand() *cobra.Command {
return baseCmd
}

View File

@@ -3,6 +3,7 @@ package overlay
import (
"github.com/hpcng/warewulf/internal/app/wwctl/overlay/build"
"github.com/hpcng/warewulf/internal/app/wwctl/overlay/chmod"
"github.com/hpcng/warewulf/internal/app/wwctl/overlay/chown"
"github.com/hpcng/warewulf/internal/app/wwctl/overlay/create"
"github.com/hpcng/warewulf/internal/app/wwctl/overlay/delete"
"github.com/hpcng/warewulf/internal/app/wwctl/overlay/edit"
@@ -32,6 +33,7 @@ func init() {
baseCmd.AddCommand(build.GetCommand())
baseCmd.AddCommand(imprt.GetCommand())
baseCmd.AddCommand(chmod.GetCommand())
baseCmd.AddCommand(chown.GetCommand())
}
// GetRootCommand returns the root cobra.Command for the application.