From 3137f477bfd9d5fc4055d8ca6e01f33a5e6315d4 Mon Sep 17 00:00:00 2001 From: Gregory Kurtzer Date: Mon, 13 Sep 2021 13:28:44 -0700 Subject: [PATCH] UI updates to include "overlay kind" --- internal/app/wwctl/overlay/build/main.go | 18 +++- internal/app/wwctl/overlay/build/root.go | 8 +- internal/app/wwctl/overlay/chmod/main.go | 25 +++-- internal/app/wwctl/overlay/chmod/root.go | 6 +- internal/app/wwctl/overlay/chown/main.go | 31 +++--- internal/app/wwctl/overlay/chown/root.go | 6 +- internal/app/wwctl/overlay/create/main.go | 29 +++--- internal/app/wwctl/overlay/create/root.go | 6 +- internal/app/wwctl/overlay/delete/main.go | 112 ++++++++++++---------- internal/app/wwctl/overlay/delete/root.go | 6 +- internal/app/wwctl/overlay/edit/main.go | 35 ++++--- internal/app/wwctl/overlay/edit/root.go | 6 +- internal/app/wwctl/overlay/imprt/main.go | 37 +++---- internal/app/wwctl/overlay/imprt/root.go | 6 +- internal/app/wwctl/overlay/list/main.go | 54 ++++------- internal/app/wwctl/overlay/list/root.go | 8 +- internal/app/wwctl/overlay/mkdir/main.go | 33 ++++--- internal/app/wwctl/overlay/mkdir/root.go | 6 +- internal/app/wwctl/overlay/show/main.go | 15 ++- internal/app/wwctl/overlay/show/root.go | 6 +- internal/pkg/overlay/overlay.go | 2 +- 21 files changed, 241 insertions(+), 214 deletions(-) diff --git a/internal/app/wwctl/overlay/build/main.go b/internal/app/wwctl/overlay/build/main.go index e5a49328..85b5cd9d 100644 --- a/internal/app/wwctl/overlay/build/main.go +++ b/internal/app/wwctl/overlay/build/main.go @@ -6,12 +6,20 @@ import ( "github.com/hpcng/warewulf/internal/pkg/node" "github.com/hpcng/warewulf/internal/pkg/overlay" "github.com/hpcng/warewulf/internal/pkg/wwlog" + "github.com/pkg/errors" "github.com/spf13/cobra" ) func CobraRunE(cmd *cobra.Command, args []string) error { var updateNodes []node.NodeInfo + overlayKind := args[0] + overlayName := args[1] + + if overlayKind != "system" && overlayKind != "runtime" { + return errors.New("overlay kind must be of type 'system' or 'runtime'") + } + n, err := node.New() if err != nil { wwlog.Printf(wwlog.ERROR, "Could not open node configuration: %s\n", err) @@ -26,9 +34,9 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } for _, node := range nodes { - if SystemOverlay && node.SystemOverlay.Get() == args[0] { + if overlayKind == "system" && node.SystemOverlay.Get() == overlayName { updateNodes = append(updateNodes, node) - } else if node.RuntimeOverlay.Get() == args[0] { + } else if overlayKind == "runtime" && node.RuntimeOverlay.Get() == overlayName { updateNodes = append(updateNodes, node) } } @@ -42,7 +50,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } wwlog.Printf(wwlog.DEBUG, "Checking on system overlay update\n") - if SystemOverlay || BuildAll { + if overlayKind == "system" || BuildAll { wwlog.Printf(wwlog.INFO, "Updating System Overlays...\n") err := overlay.BuildSystemOverlay(updateNodes) if err != nil { @@ -50,8 +58,8 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } } - wwlog.Printf(wwlog.DEBUG, "Checking on system overlay update\n") - if !SystemOverlay || BuildAll { + wwlog.Printf(wwlog.DEBUG, "Checking on runtime overlay update\n") + if overlayKind == "runtime" || BuildAll { wwlog.Printf(wwlog.INFO, "Updating Runtime Overlays...\n") err := overlay.BuildRuntimeOverlay(updateNodes) if err != nil { diff --git a/internal/app/wwctl/overlay/build/root.go b/internal/app/wwctl/overlay/build/root.go index eeaf7089..be753768 100644 --- a/internal/app/wwctl/overlay/build/root.go +++ b/internal/app/wwctl/overlay/build/root.go @@ -4,19 +4,17 @@ import "github.com/spf13/cobra" var ( baseCmd = &cobra.Command{ - Use: "build [flags] ", + Use: "build [flags] ", Short: "(Re)build an overlay", Long: "This command will build a system or runtime overlay.", RunE: CobraRunE, + Args: cobra.ExactArgs(2), } - SystemOverlay bool - BuildAll bool + BuildAll bool ) func init() { - baseCmd.PersistentFlags().BoolVarP(&SystemOverlay, "system", "s", false, "Show System Overlays as well") baseCmd.PersistentFlags().BoolVarP(&BuildAll, "all", "a", false, "Build all overlays (runtime and system)") - } // GetRootCommand returns the root cobra.Command for the application. diff --git a/internal/app/wwctl/overlay/chmod/main.go b/internal/app/wwctl/overlay/chmod/main.go index 06d6cb98..b6eb4cad 100644 --- a/internal/app/wwctl/overlay/chmod/main.go +++ b/internal/app/wwctl/overlay/chmod/main.go @@ -10,23 +10,30 @@ import ( "github.com/hpcng/warewulf/internal/pkg/overlay" "github.com/hpcng/warewulf/internal/pkg/util" "github.com/hpcng/warewulf/internal/pkg/wwlog" + "github.com/pkg/errors" "github.com/spf13/cobra" ) func CobraRunE(cmd *cobra.Command, args []string) error { var overlaySourceDir string - overlayName := args[0] - fileName := args[1] - permissionMode, err := strconv.ParseInt(args[2], 8, 32) + overlayKind := args[0] + overlayName := args[1] + fileName := args[2] + + if overlayKind != "system" && overlayKind != "runtime" { + return errors.New("overlay kind must be of type 'system' or 'runtime'") + } + + permissionMode, err := strconv.ParseInt(args[3], 8, 32) if err != nil { wwlog.Printf(wwlog.ERROR, "Could not convert requested mode: %s\n", err) os.Exit(1) } - if SystemOverlay { + if overlayKind == "system" { overlaySourceDir = config.SystemOverlaySource(overlayName) - } else { + } else if overlayKind == "runtime" { overlaySourceDir = config.RuntimeOverlaySource(overlayName) } @@ -64,17 +71,17 @@ func CobraRunE(cmd *cobra.Command, args []string) error { var updateNodes []node.NodeInfo for _, node := range nodes { - if SystemOverlay && node.SystemOverlay.Get() == overlayName { + if overlayKind == "system" && node.SystemOverlay.Get() == overlayName { updateNodes = append(updateNodes, node) - } else if node.RuntimeOverlay.Get() == overlayName { + } else if overlayKind == "runtime" && node.RuntimeOverlay.Get() == overlayName { updateNodes = append(updateNodes, node) } } - if SystemOverlay { + if overlayKind == "system" { wwlog.Printf(wwlog.INFO, "Updating System Overlays...\n") return overlay.BuildSystemOverlay(updateNodes) - } else { + } else if overlayKind == "runtime" { wwlog.Printf(wwlog.INFO, "Updating Runtime Overlays...\n") return overlay.BuildRuntimeOverlay(updateNodes) } diff --git a/internal/app/wwctl/overlay/chmod/root.go b/internal/app/wwctl/overlay/chmod/root.go index 44173ecb..4a7431c3 100644 --- a/internal/app/wwctl/overlay/chmod/root.go +++ b/internal/app/wwctl/overlay/chmod/root.go @@ -4,19 +4,17 @@ import "github.com/spf13/cobra" var ( baseCmd = &cobra.Command{ - Use: "chmod [flags] ", + Use: "chmod [flags] ", Short: "Change file permissions within an overlay", Long: "This command will allow you to change the permissions of a file within an\n" + "overlay.", RunE: CobraRunE, - Args: cobra.ExactArgs(3), + Args: cobra.ExactArgs(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") } diff --git a/internal/app/wwctl/overlay/chown/main.go b/internal/app/wwctl/overlay/chown/main.go index 7946991b..541b328f 100644 --- a/internal/app/wwctl/overlay/chown/main.go +++ b/internal/app/wwctl/overlay/chown/main.go @@ -11,36 +11,43 @@ import ( "github.com/hpcng/warewulf/internal/pkg/util" "github.com/hpcng/warewulf/internal/pkg/wwlog" + "github.com/pkg/errors" "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]) + overlayKind := args[0] + overlayName := args[1] + fileName := args[2] + + if overlayKind != "system" && overlayKind != "runtime" { + return errors.New("overlay kind must be of type 'system' or 'runtime'") + } + + uid, err = strconv.Atoi(args[3]) if err != nil { - wwlog.Printf(wwlog.ERROR, "UID is not an integer: %s\n", args[2]) + wwlog.Printf(wwlog.ERROR, "UID is not an integer: %s\n", args[3]) os.Exit(1) } if len(args) > 3 { - gid, err = strconv.Atoi(args[3]) + gid, err = strconv.Atoi(args[4]) if err != nil { - wwlog.Printf(wwlog.ERROR, "GID is not an integer: %s\n", args[3]) + wwlog.Printf(wwlog.ERROR, "GID is not an integer: %s\n", args[4]) os.Exit(1) } } else { gid = 0 } - if SystemOverlay { + if overlayKind == "system" { overlaySourceDir = config.SystemOverlaySource(overlayName) - } else { + } else if overlayKind == "runtime" { overlaySourceDir = config.RuntimeOverlaySource(overlayName) } @@ -78,17 +85,17 @@ func CobraRunE(cmd *cobra.Command, args []string) error { var updateNodes []node.NodeInfo for _, node := range nodes { - if SystemOverlay && node.SystemOverlay.Get() == overlayName { + if overlayKind == "system" && node.SystemOverlay.Get() == overlayName { updateNodes = append(updateNodes, node) - } else if node.RuntimeOverlay.Get() == overlayName { + } else if overlayKind == "runtime" && node.RuntimeOverlay.Get() == overlayName { updateNodes = append(updateNodes, node) } } - if SystemOverlay { + if overlayKind == "system" { wwlog.Printf(wwlog.INFO, "Updating System Overlays...\n") return overlay.BuildSystemOverlay(updateNodes) - } else { + } else if overlayKind == "runtime" { wwlog.Printf(wwlog.INFO, "Updating Runtime Overlays...\n") return overlay.BuildRuntimeOverlay(updateNodes) } diff --git a/internal/app/wwctl/overlay/chown/root.go b/internal/app/wwctl/overlay/chown/root.go index 35e9d794..93218a3a 100644 --- a/internal/app/wwctl/overlay/chown/root.go +++ b/internal/app/wwctl/overlay/chown/root.go @@ -4,19 +4,17 @@ import "github.com/spf13/cobra" var ( baseCmd = &cobra.Command{ - Use: "chown [flags] []", + Use: "chown [flags] []", 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), + Args: cobra.RangeArgs(4, 5), } - 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") } diff --git a/internal/app/wwctl/overlay/create/main.go b/internal/app/wwctl/overlay/create/main.go index b89942ea..a6d91356 100644 --- a/internal/app/wwctl/overlay/create/main.go +++ b/internal/app/wwctl/overlay/create/main.go @@ -6,30 +6,33 @@ import ( "github.com/hpcng/warewulf/internal/pkg/node" "github.com/hpcng/warewulf/internal/pkg/overlay" "github.com/hpcng/warewulf/internal/pkg/wwlog" + "github.com/pkg/errors" "github.com/spf13/cobra" ) func CobraRunE(cmd *cobra.Command, args []string) error { - if len(args) < 1 { - //nolint:errcheck - cmd.Help() - os.Exit(1) + + overlayKind := args[0] + overlayName := args[1] + + if overlayKind != "system" && overlayKind != "runtime" { + return errors.New("overlay kind must be of type 'system' or 'runtime'") } - if SystemOverlay { - err := overlay.SystemOverlayInit(args[0]) + if overlayKind == "system" { + err := overlay.SystemOverlayInit(overlayName) if err != nil { wwlog.Printf(wwlog.ERROR, "%s\n", err) os.Exit(1) } - wwlog.Printf(wwlog.INFO, "Created new system overlay: %s\n", args[0]) + wwlog.Printf(wwlog.INFO, "Created new system overlay: %s\n", overlayName) } else { - err := overlay.RuntimeOverlayInit(args[0]) + err := overlay.RuntimeOverlayInit(overlayName) if err != nil { wwlog.Printf(wwlog.ERROR, "%s\n", err) os.Exit(1) } - wwlog.Printf(wwlog.INFO, "Created new runtime overlay: %s\n", args[0]) + wwlog.Printf(wwlog.INFO, "Created new runtime overlay: %s\n", overlayName) } if !NoOverlayUpdate { @@ -48,17 +51,17 @@ func CobraRunE(cmd *cobra.Command, args []string) error { var updateNodes []node.NodeInfo for _, node := range nodes { - if SystemOverlay && node.SystemOverlay.Get() == args[0] { + if overlayKind == "system" && node.SystemOverlay.Get() == overlayName { updateNodes = append(updateNodes, node) - } else if node.RuntimeOverlay.Get() == args[0] { + } else if overlayKind == "runtime" && node.RuntimeOverlay.Get() == overlayName { updateNodes = append(updateNodes, node) } } - if SystemOverlay { + if overlayKind == "system" { wwlog.Printf(wwlog.INFO, "Updating System Overlays...\n") return overlay.BuildSystemOverlay(updateNodes) - } else { + } else if overlayKind == "runtime" { wwlog.Printf(wwlog.INFO, "Updating Runtime Overlays...\n") return overlay.BuildRuntimeOverlay(updateNodes) } diff --git a/internal/app/wwctl/overlay/create/root.go b/internal/app/wwctl/overlay/create/root.go index e44baff4..49ff28fa 100644 --- a/internal/app/wwctl/overlay/create/root.go +++ b/internal/app/wwctl/overlay/create/root.go @@ -6,18 +6,16 @@ import ( var ( baseCmd = &cobra.Command{ - Use: "create [flags] ", + Use: "create [flags] ", Short: "Initialize a new Overlay", Long: "This command will create a new empty overlay.", RunE: CobraRunE, - Args: cobra.ExactArgs(1), + Args: cobra.ExactArgs(2), } - 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") } diff --git a/internal/app/wwctl/overlay/delete/main.go b/internal/app/wwctl/overlay/delete/main.go index a4e957d8..62238d89 100644 --- a/internal/app/wwctl/overlay/delete/main.go +++ b/internal/app/wwctl/overlay/delete/main.go @@ -10,88 +10,94 @@ import ( "github.com/hpcng/warewulf/internal/pkg/overlay" "github.com/hpcng/warewulf/internal/pkg/util" "github.com/hpcng/warewulf/internal/pkg/wwlog" + "github.com/pkg/errors" "github.com/spf13/cobra" ) func CobraRunE(cmd *cobra.Command, args []string) error { var overlayPath string + var fileName string - if SystemOverlay { - overlayPath = config.SystemOverlaySource(args[0]) - } else { - overlayPath = config.RuntimeOverlaySource(args[0]) + overlayKind := args[0] + overlayName := args[1] + + if len(args) == 3 { + fileName = args[2] + } + + if overlayKind != "system" && overlayKind != "runtime" { + return errors.New("overlay kind must be of type 'system' or 'runtime'") + } + + if overlayKind == "system" { + overlayPath = config.SystemOverlaySource(overlayName) + } else if overlayKind == "runtime" { + overlayPath = config.RuntimeOverlaySource(overlayName) } if overlayPath == "" { - wwlog.Printf(wwlog.ERROR, "Overlay name did not render: '%s'\n", args[0]) + wwlog.Printf(wwlog.ERROR, "Overlay name did not resolve: '%s'\n", overlayName) os.Exit(1) } if !util.IsDir(overlayPath) { - wwlog.Printf(wwlog.ERROR, "Overlay name does not exist: '%s'\n", args[0]) + wwlog.Printf(wwlog.ERROR, "Overlay does not exist: '%s:%s'\n", overlayKind, overlayName) os.Exit(1) } - if len(args) == 1 { + if fileName == "" { if Force { err := os.RemoveAll(overlayPath) if err != nil { - wwlog.Printf(wwlog.ERROR, "Failed deleting overlay: %s\n", args[0]) - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) + return errors.Wrap(err, "failed deleting overlay") } } else { err := os.Remove(overlayPath) if err != nil { - wwlog.Printf(wwlog.ERROR, "Failed deleting overlay: %s\n", args[0]) - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) + return errors.Wrap(err, "failed deleting overlay") } } fmt.Printf("Deleted overlay: %s\n", args[0]) - } else if len(args) > 1 { - for i := 1; i < len(args); i++ { - removePath := path.Join(overlayPath, args[i]) + } else { + removePath := path.Join(overlayPath, overlayPath) - if !util.IsDir(removePath) && !util.IsFile(removePath) { - wwlog.Printf(wwlog.ERROR, "Path to remove doesn't exist in overlay: %s\n", removePath) + if !util.IsDir(removePath) && !util.IsFile(removePath) { + wwlog.Printf(wwlog.ERROR, "Path to remove doesn't exist in overlay: %s\n", removePath) + os.Exit(1) + } + + if Force { + err := os.RemoveAll(removePath) + if err != nil { + wwlog.Printf(wwlog.ERROR, "Failed deleting file from overlay: %s:%s:%s\n", overlayKind, overlayName, overlayPath) + wwlog.Printf(wwlog.ERROR, "%s\n", err) os.Exit(1) } - - if Force { - err := os.RemoveAll(removePath) - if err != nil { - wwlog.Printf(wwlog.ERROR, "Failed deleting file from overlay: %s:%s\n", args[0], args[i]) - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } - } else { - err := os.Remove(removePath) - if err != nil { - wwlog.Printf(wwlog.ERROR, "Failed deleting overlay: %s:%s\n", args[0], args[i]) - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } - } - - if Parents { - // Cleanup any empty directories left behind... - i := path.Dir(removePath) - for i != overlayPath { - wwlog.Printf(wwlog.DEBUG, "Evaluating directory to remove: %s\n", i) - err := os.Remove(i) - if err != nil { - break - } - - wwlog.Printf(wwlog.VERBOSE, "Removed empty directory: %s\n", i) - i = path.Dir(i) - } + } else { + err := os.Remove(removePath) + if err != nil { + wwlog.Printf(wwlog.ERROR, "Failed deleting overlay: %s:%s:%s\n", overlayKind, overlayName, overlayPath) + wwlog.Printf(wwlog.ERROR, "%s\n", err) + os.Exit(1) } } - fmt.Printf("Deleted from overlay: %s:%s\n", args[0], args[1]) + if Parents { + // Cleanup any empty directories left behind... + i := path.Dir(removePath) + for i != overlayPath { + wwlog.Printf(wwlog.DEBUG, "Evaluating directory to remove: %s\n", i) + err := os.Remove(i) + if err != nil { + break + } + + wwlog.Printf(wwlog.VERBOSE, "Removed empty directory: %s\n", i) + i = path.Dir(i) + } + } + fmt.Printf("Deleted from overlay: %s:%s:%s\n", overlayKind, overlayName, overlayPath) } if !NoOverlayUpdate { @@ -110,17 +116,17 @@ func CobraRunE(cmd *cobra.Command, args []string) error { var updateNodes []node.NodeInfo for _, node := range nodes { - if SystemOverlay && node.SystemOverlay.Get() == args[0] { + if overlayKind == "system" && node.SystemOverlay.Get() == overlayName { updateNodes = append(updateNodes, node) - } else if node.RuntimeOverlay.Get() == args[0] { + } else if overlayKind == "runtime" && node.RuntimeOverlay.Get() == overlayName { updateNodes = append(updateNodes, node) } } - if SystemOverlay { + if overlayKind == "system" { wwlog.Printf(wwlog.INFO, "Updating System Overlays...\n") return overlay.BuildSystemOverlay(updateNodes) - } else { + } else if overlayKind == "runtime" { wwlog.Printf(wwlog.INFO, "Updating Runtime Overlays...\n") return overlay.BuildRuntimeOverlay(updateNodes) } diff --git a/internal/app/wwctl/overlay/delete/root.go b/internal/app/wwctl/overlay/delete/root.go index 49327554..d38da801 100644 --- a/internal/app/wwctl/overlay/delete/root.go +++ b/internal/app/wwctl/overlay/delete/root.go @@ -6,22 +6,20 @@ import ( var ( baseCmd = &cobra.Command{ - Use: "delete [flags] [overlay file]", + Use: "delete [flags] [overlay file]", Short: "Delete Warewulf Overlay or files", Long: "This command will delete files within an overlay or an entire overlay if no\n" + "files are given to remove (use with caution).", RunE: CobraRunE, - Args: cobra.MinimumNArgs(1), + Args: cobra.RangeArgs(2, 3), Aliases: []string{"rm", "del"}, } - SystemOverlay bool Force bool Parents bool NoOverlayUpdate bool ) func init() { - baseCmd.PersistentFlags().BoolVarP(&SystemOverlay, "system", "s", false, "Show system overlays instead of runtime") baseCmd.PersistentFlags().BoolVarP(&Force, "force", "f", false, "Force deletion of a non-empty overlay") baseCmd.PersistentFlags().BoolVarP(&Parents, "parents", "p", false, "Remove empty parent directories") baseCmd.PersistentFlags().BoolVarP(&NoOverlayUpdate, "noupdate", "n", false, "Don't update overlays") diff --git a/internal/app/wwctl/overlay/edit/main.go b/internal/app/wwctl/overlay/edit/main.go index ada96eae..b6fa531b 100644 --- a/internal/app/wwctl/overlay/edit/main.go +++ b/internal/app/wwctl/overlay/edit/main.go @@ -11,6 +11,7 @@ import ( "github.com/hpcng/warewulf/internal/pkg/overlay" "github.com/hpcng/warewulf/internal/pkg/util" "github.com/hpcng/warewulf/internal/pkg/wwlog" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -18,22 +19,30 @@ func CobraRunE(cmd *cobra.Command, args []string) error { editor := os.Getenv("EDITOR") var overlaySourceDir string - if editor == "" { - editor = "vi" + overlayKind := args[0] + overlayName := args[1] + fileName := args[2] + + if overlayKind != "system" && overlayKind != "runtime" { + return errors.New("overlay kind must be of type 'system' or 'runtime'") } - if SystemOverlay { - overlaySourceDir = config.SystemOverlaySource(args[0]) - } else { - overlaySourceDir = config.RuntimeOverlaySource(args[0]) + if editor == "" { + editor = "/bin/vi" + } + + if overlayKind == "system" { + overlaySourceDir = config.SystemOverlaySource(overlayName) + } else if overlayKind == "runtime" { + overlaySourceDir = config.RuntimeOverlaySource(overlayName) } if !util.IsDir(overlaySourceDir) { - wwlog.Printf(wwlog.ERROR, "Overlay does not exist: %s\n", args[0]) + wwlog.Printf(wwlog.ERROR, "Overlay does not exist: %s:%s\n", overlayKind, overlayName) os.Exit(1) } - overlayFile := path.Join(overlaySourceDir, args[1]) + overlayFile := path.Join(overlaySourceDir, fileName) wwlog.Printf(wwlog.DEBUG, "Will edit overlay file: %s\n", overlayFile) @@ -83,7 +92,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { wwlog.Printf(wwlog.ERROR, "Editor process existed with non-zero\n") os.Exit(1) } - wwlog.Printf(wwlog.INFO, "Updated: %s %s\n", args[0], args[1]) + wwlog.Printf(wwlog.INFO, "Updated: %s:%s:%s\n", overlayKind, overlayName, fileName) shasum2, err := util.ShaSumFile(overlayFile) if err != nil { @@ -111,17 +120,17 @@ func CobraRunE(cmd *cobra.Command, args []string) error { var updateNodes []node.NodeInfo for _, node := range nodes { - if SystemOverlay && node.SystemOverlay.Get() == args[0] { + if overlayKind == "system" && node.SystemOverlay.Get() == overlayName { updateNodes = append(updateNodes, node) - } else if node.RuntimeOverlay.Get() == args[0] { + } else if overlayKind == "runtime" && node.RuntimeOverlay.Get() == overlayName { updateNodes = append(updateNodes, node) } } - if SystemOverlay { + if overlayKind == "system" { wwlog.Printf(wwlog.INFO, "Updating System Overlays...\n") return overlay.BuildSystemOverlay(updateNodes) - } else { + } else if overlayKind == "runtime" { wwlog.Printf(wwlog.INFO, "Updating Runtime Overlays...\n") return overlay.BuildRuntimeOverlay(updateNodes) } diff --git a/internal/app/wwctl/overlay/edit/root.go b/internal/app/wwctl/overlay/edit/root.go index 922f6f85..64cb7b31 100644 --- a/internal/app/wwctl/overlay/edit/root.go +++ b/internal/app/wwctl/overlay/edit/root.go @@ -6,15 +6,14 @@ import ( var ( baseCmd = &cobra.Command{ - Use: "edit [flags] ", + Use: "edit [flags] ", Short: "Edit/Create a file within a Warewulf Overlay", Long: "This command will allow you to edit or create a new file within a given\n" + "overlay. Note: when creating files ending in a '.ww' suffix this will always be\n" + "parsed as a Warewulf template file, and the suffix will be removed automatically.", RunE: CobraRunE, - Args: cobra.ExactArgs(2), + Args: cobra.ExactArgs(3), } - SystemOverlay bool ListFiles bool CreateDirs bool PermMode int32 @@ -22,7 +21,6 @@ var ( ) func init() { - baseCmd.PersistentFlags().BoolVarP(&SystemOverlay, "system", "s", false, "Show system overlays instead of runtime") baseCmd.PersistentFlags().BoolVarP(&ListFiles, "files", "f", false, "List files contained within a given overlay") baseCmd.PersistentFlags().BoolVarP(&CreateDirs, "parents", "p", false, "Create any necessary parent directories") baseCmd.PersistentFlags().Int32VarP(&PermMode, "mode", "m", 0755, "Permission mode for directory") diff --git a/internal/app/wwctl/overlay/imprt/main.go b/internal/app/wwctl/overlay/imprt/main.go index 56adcc84..d64099ca 100644 --- a/internal/app/wwctl/overlay/imprt/main.go +++ b/internal/app/wwctl/overlay/imprt/main.go @@ -9,39 +9,44 @@ import ( "github.com/hpcng/warewulf/internal/pkg/overlay" "github.com/hpcng/warewulf/internal/pkg/util" "github.com/hpcng/warewulf/internal/pkg/wwlog" + "github.com/pkg/errors" "github.com/spf13/cobra" ) func CobraRunE(cmd *cobra.Command, args []string) error { - overlayName := args[0] - source := args[1] var dest string var overlaySource string - if len(args) == 3 { - dest = args[2] + overlayKind := args[0] + overlayName := args[1] + source := args[2] + + if overlayKind != "system" && overlayKind != "runtime" { + return errors.New("overlay kind must be of type 'system' or 'runtime'") + } + + if len(args) == 4 { + dest = args[3] } else { dest = source } - if SystemOverlay { - wwlog.Printf(wwlog.VERBOSE, "Importing '%s' into system overlay '%s:%s'\n", source, overlayName, dest) + if overlayKind == "system" { + wwlog.Printf(wwlog.VERBOSE, "Copying '%s' into system overlay '%s:%s'\n", source, overlayName, dest) overlaySource = config.SystemOverlaySource(overlayName) - } else { - wwlog.Printf(wwlog.VERBOSE, "Importing '%s' into runtime overlay '%s:%s'\n", source, overlayName, dest) + } else if overlayKind == "runtime" { + wwlog.Printf(wwlog.VERBOSE, "Copying '%s' into runtime overlay '%s:%s'\n", source, overlayName, dest) overlaySource = config.RuntimeOverlaySource(overlayName) } if !util.IsDir(overlaySource) { - wwlog.Printf(wwlog.ERROR, "Overlay does not exist: %s\n", overlayName) + wwlog.Printf(wwlog.ERROR, "Overlay does not exist: %s:%s\n", overlayKind, overlayName) os.Exit(1) } err := util.CopyFile(source, path.Join(overlaySource, dest)) if err != nil { - wwlog.Printf(wwlog.ERROR, "Failed copying file into overlay sourcedir:\n") - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) + return errors.Wrap(err, "could not copy file into overlay") } if !NoOverlayUpdate { @@ -60,17 +65,17 @@ func CobraRunE(cmd *cobra.Command, args []string) error { var updateNodes []node.NodeInfo for _, node := range nodes { - if SystemOverlay && node.SystemOverlay.Get() == overlayName { + if overlayKind == "system" && node.SystemOverlay.Get() == overlayName { updateNodes = append(updateNodes, node) - } else if node.RuntimeOverlay.Get() == overlayName { + } else if overlayKind == "runtime" && node.RuntimeOverlay.Get() == overlayName { updateNodes = append(updateNodes, node) } } - if SystemOverlay { + if overlayKind == "system" { wwlog.Printf(wwlog.INFO, "Updating System Overlays...\n") return overlay.BuildSystemOverlay(updateNodes) - } else { + } else if overlayKind == "runtime" { wwlog.Printf(wwlog.INFO, "Updating Runtime Overlays...\n") return overlay.BuildRuntimeOverlay(updateNodes) } diff --git a/internal/app/wwctl/overlay/imprt/root.go b/internal/app/wwctl/overlay/imprt/root.go index 2cc223a0..983052b8 100644 --- a/internal/app/wwctl/overlay/imprt/root.go +++ b/internal/app/wwctl/overlay/imprt/root.go @@ -4,20 +4,18 @@ import "github.com/spf13/cobra" var ( baseCmd = &cobra.Command{ - Use: "import [flags] [dest location]", + Use: "import [flags] [overlay target]", Short: "Import a file into a Warewulf Overlay", Long: "This command will import a file into a given Warewulf overlay.", RunE: CobraRunE, - Args: cobra.RangeArgs(2, 3), + Args: cobra.RangeArgs(3, 4), Aliases: []string{"cp"}, } - SystemOverlay bool PermMode int32 NoOverlayUpdate bool ) func init() { - baseCmd.PersistentFlags().BoolVarP(&SystemOverlay, "system", "s", false, "Show system overlays instead of runtime") baseCmd.PersistentFlags().Int32VarP(&PermMode, "mode", "m", 0755, "Permission mode for directory") baseCmd.PersistentFlags().BoolVarP(&NoOverlayUpdate, "noupdate", "n", false, "Don't update overlays") } diff --git a/internal/app/wwctl/overlay/list/main.go b/internal/app/wwctl/overlay/list/main.go index d9a7fdc7..dca311d8 100644 --- a/internal/app/wwctl/overlay/list/main.go +++ b/internal/app/wwctl/overlay/list/main.go @@ -10,6 +10,7 @@ import ( "github.com/hpcng/warewulf/internal/pkg/overlay" "github.com/hpcng/warewulf/internal/pkg/util" "github.com/hpcng/warewulf/internal/pkg/wwlog" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -18,6 +19,17 @@ func CobraRunE(cmd *cobra.Command, args []string) error { var o []string var err error var nodeList []node.NodeInfo + var overlayName string + + overlayKind := args[0] + + if len(args) > 1 { + overlayName = args[1] + } + + if overlayKind != "system" && overlayKind != "runtime" { + return errors.New("overlay kind must be of type 'system' or 'runtime'") + } n, err := node.New() if err != nil { @@ -25,14 +37,14 @@ func CobraRunE(cmd *cobra.Command, args []string) error { os.Exit(1) } - if SystemOverlay { + if overlayKind == "system" { if !ListLong { fmt.Printf("%-30s %-12s %-12s\n", "SYSTEM OVERLAY NAME", "NODES", "FILES/DIRS") } else { fmt.Printf("%-10s %5s %-5s %-18s %s\n", "PERM MODE", "UID", "GID", "SYSTEM-OVERLAY", "FILE PATH") } o, err = overlay.FindSystemOverlays() - } else { + } else if overlayKind == "runtime" { if !ListLong { fmt.Printf("%-30s %-12s %-12s\n", "RUNTIME OVERLAY NAME", "NODES", "FILES/DIRS") } else { @@ -52,11 +64,11 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } for _, node := range nodeList { - if SystemOverlay { + if overlayKind == "system" { if node.SystemOverlay.Get() != "" { set[node.SystemOverlay.Get()]++ } - } else { + } else if overlayKind == "runtime" { if node.RuntimeOverlay.Get() != "" { set[node.RuntimeOverlay.Get()]++ } @@ -67,15 +79,13 @@ func CobraRunE(cmd *cobra.Command, args []string) error { var path string name := o[overlay] - if len(args) > 0 { - if args[0] != name { - continue - } + if overlayName != "" && overlayName != name { + continue } - if SystemOverlay { + if overlayKind == "system" { path = config.SystemOverlaySource(o[overlay]) - } else { + } else if overlayKind == "runtime" { path = config.RuntimeOverlaySource(o[overlay]) } @@ -116,29 +126,5 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } } - var unconfigured bool - for overlay := range set { - var overlayPath string - - if SystemOverlay { - overlayPath = config.SystemOverlaySource(overlay) - } else { - overlayPath = config.SystemOverlaySource(overlay) - } - - if !util.IsDir(overlayPath) { - fmt.Printf("%-30s %-12d 0\n", "("+overlay+")", set[overlay]) - unconfigured = true - } - } - - if unconfigured { - fmt.Printf("\n") - wwlog.Printf(wwlog.WARN, "There are unconfigured overlays present, run the following command to\n") - wwlog.Printf(wwlog.WARN, "create a new overlay:\n") - wwlog.Printf(wwlog.WARN, "\n") - wwlog.Printf(wwlog.WARN, " $ sudo wwctl overlay create ...\n") - } - return nil } diff --git a/internal/app/wwctl/overlay/list/root.go b/internal/app/wwctl/overlay/list/root.go index 41b087d1..96ccfa45 100644 --- a/internal/app/wwctl/overlay/list/root.go +++ b/internal/app/wwctl/overlay/list/root.go @@ -6,20 +6,18 @@ import ( var ( baseCmd = &cobra.Command{ - Use: "list [flags] [overlay name]", + Use: "list [flags] ", Short: "List Warewulf Overlays and files", Long: "This command will show you information about Warewulf overlays and the\n" + "files contained within.", RunE: CobraRunE, Aliases: []string{"ls"}, } - SystemOverlay bool - ListContents bool - ListLong bool + ListContents bool + ListLong bool ) func init() { - baseCmd.PersistentFlags().BoolVarP(&SystemOverlay, "system", "s", false, "Show system overlays instead of runtime") baseCmd.PersistentFlags().BoolVarP(&ListContents, "all", "a", false, "List the contents of overlays") baseCmd.PersistentFlags().BoolVarP(&ListLong, "long", "l", false, "List 'long' of all overlay contents") diff --git a/internal/app/wwctl/overlay/mkdir/main.go b/internal/app/wwctl/overlay/mkdir/main.go index 9da913a2..c072276f 100644 --- a/internal/app/wwctl/overlay/mkdir/main.go +++ b/internal/app/wwctl/overlay/mkdir/main.go @@ -10,26 +10,35 @@ import ( "github.com/hpcng/warewulf/internal/pkg/overlay" "github.com/hpcng/warewulf/internal/pkg/util" "github.com/hpcng/warewulf/internal/pkg/wwlog" + "github.com/pkg/errors" "github.com/spf13/cobra" ) func CobraRunE(cmd *cobra.Command, args []string) error { var overlaySourceDir string - if SystemOverlay { - overlaySourceDir = config.SystemOverlaySource(args[0]) - } else { - overlaySourceDir = config.RuntimeOverlaySource(args[0]) + overlayKind := args[0] + overlayName := args[1] + dirName := args[2] + + if overlayKind != "system" && overlayKind != "runtime" { + return errors.New("overlay kind must be of type 'system' or 'runtime'") + } + + if overlayKind == "system" { + overlaySourceDir = config.SystemOverlaySource(overlayName) + } else if overlayKind == "runtime" { + overlaySourceDir = config.RuntimeOverlaySource(overlayName) } if !util.IsDir(overlaySourceDir) { - wwlog.Printf(wwlog.ERROR, "Overlay does not exist: %s\n", args[0]) + wwlog.Printf(wwlog.ERROR, "Overlay does not exist: %s:%s\n", overlayKind, overlayName) os.Exit(1) } - overlayDir := path.Join(overlaySourceDir, args[1]) + overlayDir := path.Join(overlaySourceDir, dirName) - wwlog.Printf(wwlog.DEBUG, "Will create directory in overlay: %s:%s\n", args[0], overlayDir) + wwlog.Printf(wwlog.DEBUG, "Will create directory in overlay: %s:%s:%s\n", overlayKind, overlayName, dirName) err := os.MkdirAll(overlayDir, os.FileMode(PermMode)) if err != nil { @@ -37,7 +46,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { os.Exit(1) } - fmt.Printf("Created directory within overlay: %s:%s\n", args[0], args[1]) + fmt.Printf("Created directory within overlay: %s:%s:%s\n", overlayKind, overlayName, overlayDir) if !NoOverlayUpdate { n, err := node.New() @@ -55,17 +64,17 @@ func CobraRunE(cmd *cobra.Command, args []string) error { var updateNodes []node.NodeInfo for _, node := range nodes { - if SystemOverlay && node.SystemOverlay.Get() == args[0] { + if overlayKind == "system" && node.SystemOverlay.Get() == overlayName { updateNodes = append(updateNodes, node) - } else if node.RuntimeOverlay.Get() == args[0] { + } else if overlayKind == "runtime" && node.RuntimeOverlay.Get() == overlayName { updateNodes = append(updateNodes, node) } } - if SystemOverlay { + if overlayKind == "system" { wwlog.Printf(wwlog.INFO, "Updating System Overlays...\n") return overlay.BuildSystemOverlay(updateNodes) - } else { + } else if overlayKind == "runtime" { wwlog.Printf(wwlog.INFO, "Updating Runtime Overlays...\n") return overlay.BuildRuntimeOverlay(updateNodes) } diff --git a/internal/app/wwctl/overlay/mkdir/root.go b/internal/app/wwctl/overlay/mkdir/root.go index 4f293d4e..88f385b8 100644 --- a/internal/app/wwctl/overlay/mkdir/root.go +++ b/internal/app/wwctl/overlay/mkdir/root.go @@ -6,19 +6,17 @@ import ( var ( baseCmd = &cobra.Command{ - Use: "mkdir [flags] ", + Use: "mkdir [flags] ", Short: "Create a new directory within an Overlay", Long: "This command will allow you to create a new file within a given Warewulf overlay.", RunE: CobraRunE, - Args: cobra.MinimumNArgs(2), + Args: cobra.MinimumNArgs(3), } - SystemOverlay bool PermMode int32 NoOverlayUpdate bool ) func init() { - baseCmd.PersistentFlags().BoolVarP(&SystemOverlay, "system", "s", false, "Show System Overlays as well") baseCmd.PersistentFlags().Int32VarP(&PermMode, "mode", "m", 0755, "Permission mode for directory") baseCmd.PersistentFlags().BoolVarP(&NoOverlayUpdate, "noupdate", "n", false, "Don't update overlays") } diff --git a/internal/app/wwctl/overlay/show/main.go b/internal/app/wwctl/overlay/show/main.go index 1f0edc4b..b4d09201 100644 --- a/internal/app/wwctl/overlay/show/main.go +++ b/internal/app/wwctl/overlay/show/main.go @@ -9,17 +9,24 @@ import ( "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/util" "github.com/hpcng/warewulf/internal/pkg/wwlog" + "github.com/pkg/errors" "github.com/spf13/cobra" ) func CobraRunE(cmd *cobra.Command, args []string) error { var overlaySourceDir string - overlayName := args[0] - fileName := args[1] - if SystemOverlay { + overlayKind := args[0] + overlayName := args[1] + fileName := args[2] + + if overlayKind != "system" && overlayKind != "runtime" { + return errors.New("overlay kind must be of type 'system' or 'runtime'") + } + + if overlayKind == "system" { overlaySourceDir = config.SystemOverlaySource(overlayName) - } else { + } else if overlayKind == "runtime" { overlaySourceDir = config.RuntimeOverlaySource(overlayName) } diff --git a/internal/app/wwctl/overlay/show/root.go b/internal/app/wwctl/overlay/show/root.go index 6d848a25..a8c51efc 100644 --- a/internal/app/wwctl/overlay/show/root.go +++ b/internal/app/wwctl/overlay/show/root.go @@ -6,19 +6,17 @@ import ( var ( baseCmd = &cobra.Command{ - Use: "show [flags] ", + Use: "show [flags] ", Short: "Show (cat) a file within a Warewulf Overlay", Long: "This command will output the contents of a file within a given\n" + "Warewulf overlay.", RunE: CobraRunE, Aliases: []string{"cat"}, - Args: cobra.ExactArgs(2), + Args: cobra.ExactArgs(3), } - SystemOverlay bool ) func init() { - baseCmd.PersistentFlags().BoolVarP(&SystemOverlay, "system", "s", false, "Show System Overlays as well") } // GetRootCommand returns the root cobra.Command for the application. diff --git a/internal/pkg/overlay/overlay.go b/internal/pkg/overlay/overlay.go index 16da31dd..9eb4bee1 100644 --- a/internal/pkg/overlay/overlay.go +++ b/internal/pkg/overlay/overlay.go @@ -298,7 +298,7 @@ func buildOverlay(nodeList []node.NodeInfo, overlayType string) error { wwlog.Printf(wwlog.VERBOSE, "Could not locate PIGZ, using GZIP\n") compressor = "gzip" } else { - wwlog.Printf(wwlog.VERBOSE, "Using PIGZ to compress the container: %s\n", compressor) + wwlog.Printf(wwlog.VERBOSE, "Using PIGZ to compress the overlay: %s\n", compressor) } cmd := fmt.Sprintf("cd \"%s\"; find . | cpio --quiet -o -H newc | %s -c > \"%s\"", tmpDir, compressor, OverlayFile)