diff --git a/internal/app/wwctl/overlay/parse/main.go b/internal/app/wwctl/overlay/parse/main.go deleted file mode 100644 index ee6add2e..00000000 --- a/internal/app/wwctl/overlay/parse/main.go +++ /dev/null @@ -1,89 +0,0 @@ -package parse - -import ( - "bufio" - "bytes" - "os" - "path/filepath" - "regexp" - "strings" - - "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 host node.NodeInfo - if NodeName == "" { - host.Kernel = new(node.KernelEntry) - host.Ipmi = new(node.IpmiEntry) - var idEntry node.Entry - hostname, _ := os.Hostname() - idEntry.Set(hostname) - host.Id = idEntry - } else { - nodeDB, err := node.New() - if err != nil { - wwlog.Printf(wwlog.ERROR, "Could not open node configuration: %s\n", err) - os.Exit(1) - } - nodes, err := nodeDB.FindAllNodes() - if err != nil { - wwlog.Printf(wwlog.ERROR, "Could not get node list: %s\n", err) - os.Exit(1) - } - node := node.FilterByName(nodes, []string{NodeName}) - if len(node) != 1 { - wwlog.Printf(wwlog.ERROR, "%v does not identify a single node\n", NodeName) - os.Exit(1) - } - host = node[0] - } - if !util.IsFile(args[0]) { - wwlog.Printf(wwlog.ERROR, "%s is not a file\n", args[0]) - } - tstruct := overlay.InitStruct(host) - tstruct.BuildSource = args[0] - buffer, backupFile, writeFile, err := overlay.RenderTemplateFile(args[0], tstruct) - if err != nil { - return err - } - if filepath.Ext(args[0]) != ".ww" { - wwlog.Printf(wwlog.WARN, "%s has not the '.ww' so wont be rendered if in overlay\n", args[0]) - } - var outBuffer bytes.Buffer - // search for magic file name comment - bufferScanner := bufio.NewScanner(bytes.NewReader(buffer.Bytes())) - bufferScanner.Split(overlay.ScanLines) - reg := regexp.MustCompile(`.*{{\s*/\*\s*file\s*["'](.*)["']\s*\*/\s*}}.*`) - foundFileComment := false - destFileName := strings.TrimSuffix(args[0], ".ww") - for bufferScanner.Scan() { - line := bufferScanner.Text() - filenameFromTemplate := reg.FindAllStringSubmatch(line, -1) - if len(filenameFromTemplate) != 0 { - wwlog.Printf(wwlog.DEBUG, "Found multifile comment, new filename %s\n", filenameFromTemplate[0][1]) - if foundFileComment { - if !Quiet { - wwlog.Printf(wwlog.INFO, "backupFile: %v\nwriteFile: %v\n", backupFile, writeFile) - wwlog.Printf(wwlog.INFO, "Filename: %s\n\n", destFileName) - } - wwlog.Printf(wwlog.INFO, "%s", outBuffer.String()) - outBuffer.Reset() - } - destFileName = filenameFromTemplate[0][1] - foundFileComment = true - } else { - _, _ = outBuffer.WriteString(line) - } - } - if !Quiet { - wwlog.Printf(wwlog.INFO, "backupFile: %v\nwriteFile: %v\n", backupFile, writeFile) - wwlog.Printf(wwlog.INFO, "Filename: %s\n\n", destFileName) - } - wwlog.Printf(wwlog.INFO, "%s", outBuffer.String()) - return nil -} diff --git a/internal/app/wwctl/overlay/parse/root.go b/internal/app/wwctl/overlay/parse/root.go deleted file mode 100644 index 730a6210..00000000 --- a/internal/app/wwctl/overlay/parse/root.go +++ /dev/null @@ -1,43 +0,0 @@ -package parse - -import ( - "log" - - "github.com/hpcng/warewulf/internal/pkg/node" - "github.com/spf13/cobra" -) - -var ( - baseCmd = &cobra.Command{ - DisableFlagsInUseLine: true, - Use: "parse [OPTIONS] FILE_NAME", - Short: "parses interprets the given file as warewulf template", - Long: "This command tries to parse a given file as warewulf template, it needs not to be in overlay folder.", - RunE: CobraRunE, - Aliases: []string{"parse"}, - Args: cobra.ExactArgs(1), - } - NodeName string - Quiet bool -) - -func init() { - baseCmd.PersistentFlags().StringVarP(&NodeName, "node", "n", "", "node used for the variables in the template") - if err := baseCmd.RegisterFlagCompletionFunc("node", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - nodeDB, _ := node.New() - nodes, _ := nodeDB.FindAllNodes() - var node_names []string - for _, node := range nodes { - node_names = append(node_names, node.Id.Get()) - } - return node_names, cobra.ShellCompDirectiveNoFileComp - }); err != nil { - log.Println(err) - } - baseCmd.PersistentFlags().BoolVarP(&Quiet, "quiet", "q", false, "do not print information if multiple, backup files are written") -} - -// GetRootCommand returns the root cobra.Command for the application. -func GetCommand() *cobra.Command { - return baseCmd -} diff --git a/internal/app/wwctl/overlay/root.go b/internal/app/wwctl/overlay/root.go index 00d931c0..b3713a7c 100644 --- a/internal/app/wwctl/overlay/root.go +++ b/internal/app/wwctl/overlay/root.go @@ -10,7 +10,6 @@ import ( "github.com/hpcng/warewulf/internal/app/wwctl/overlay/imprt" "github.com/hpcng/warewulf/internal/app/wwctl/overlay/list" "github.com/hpcng/warewulf/internal/app/wwctl/overlay/mkdir" - "github.com/hpcng/warewulf/internal/app/wwctl/overlay/parse" "github.com/hpcng/warewulf/internal/app/wwctl/overlay/show" "github.com/spf13/cobra" ) @@ -35,7 +34,6 @@ func init() { baseCmd.AddCommand(imprt.GetCommand()) baseCmd.AddCommand(chmod.GetCommand()) baseCmd.AddCommand(chown.GetCommand()) - baseCmd.AddCommand(parse.GetCommand()) } diff --git a/internal/app/wwctl/overlay/show/main.go b/internal/app/wwctl/overlay/show/main.go index dac7c150..8e5e0824 100644 --- a/internal/app/wwctl/overlay/show/main.go +++ b/internal/app/wwctl/overlay/show/main.go @@ -1,11 +1,17 @@ package show import ( + "bufio" + "bytes" "fmt" "io/ioutil" "os" "path" + "path/filepath" + "regexp" + "strings" + "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" @@ -17,28 +23,91 @@ func CobraRunE(cmd *cobra.Command, args []string) error { overlayName := args[0] fileName := args[1] + if NodeName == "" { + overlaySourceDir = overlay.OverlaySourceDir(overlayName) - overlaySourceDir = overlay.OverlaySourceDir(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) { + wwlog.Printf(wwlog.ERROR, "File does not exist within overlay: %s:%s\n", overlayName, fileName) + os.Exit(1) + } + + f, err := ioutil.ReadFile(overlayFile) + if err != nil { + wwlog.Printf(wwlog.ERROR, "Could not read file: %s\n", err) + os.Exit(1) + } + + fmt.Print(string(f)) + } else { + var host node.NodeInfo + nodeDB, err := node.New() + if err != nil { + wwlog.Printf(wwlog.ERROR, "Could not open node configuration: %s\n", err) + os.Exit(1) + } + nodes, err := nodeDB.FindAllNodes() + if err != nil { + wwlog.Printf(wwlog.ERROR, "Could not get node list: %s\n", err) + os.Exit(1) + } + node := node.FilterByName(nodes, []string{NodeName}) + if len(node) != 1 { + wwlog.Printf(wwlog.ERROR, "%v does not identify a single node\n", NodeName) + os.Exit(1) + } + host = node[0] + + if !util.IsFile(args[0]) { + wwlog.Printf(wwlog.ERROR, "%s is not a file\n", args[0]) + } + tstruct := overlay.InitStruct(host) + tstruct.BuildSource = args[0] + buffer, backupFile, writeFile, err := overlay.RenderTemplateFile(args[0], tstruct) + if err != nil { + return err + } + if filepath.Ext(args[0]) != ".ww" { + wwlog.Printf(wwlog.WARN, "%s has not the '.ww' so wont be rendered if in overlay\n", args[0]) + } + var outBuffer bytes.Buffer + // search for magic file name comment + bufferScanner := bufio.NewScanner(bytes.NewReader(buffer.Bytes())) + bufferScanner.Split(overlay.ScanLines) + reg := regexp.MustCompile(`.*{{\s*/\*\s*file\s*["'](.*)["']\s*\*/\s*}}.*`) + foundFileComment := false + destFileName := strings.TrimSuffix(args[0], ".ww") + for bufferScanner.Scan() { + line := bufferScanner.Text() + filenameFromTemplate := reg.FindAllStringSubmatch(line, -1) + if len(filenameFromTemplate) != 0 { + wwlog.Printf(wwlog.DEBUG, "Found multifile comment, new filename %s\n", filenameFromTemplate[0][1]) + if foundFileComment { + if !Quiet { + wwlog.Printf(wwlog.INFO, "backupFile: %v\nwriteFile: %v\n", backupFile, writeFile) + wwlog.Printf(wwlog.INFO, "Filename: %s\n\n", destFileName) + } + wwlog.Printf(wwlog.INFO, "%s", outBuffer.String()) + outBuffer.Reset() + } + destFileName = filenameFromTemplate[0][1] + foundFileComment = true + } else { + _, _ = outBuffer.WriteString(line) + } + } + if !Quiet { + wwlog.Printf(wwlog.INFO, "backupFile: %v\nwriteFile: %v\n", backupFile, writeFile) + wwlog.Printf(wwlog.INFO, "Filename: %s\n\n", destFileName) + } + wwlog.Printf(wwlog.INFO, "%s", outBuffer.String()) - 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) { - wwlog.Printf(wwlog.ERROR, "File does not exist within overlay: %s:%s\n", overlayName, fileName) - os.Exit(1) - } - - f, err := ioutil.ReadFile(overlayFile) - if err != nil { - wwlog.Printf(wwlog.ERROR, "Could not read file: %s\n", err) - os.Exit(1) - } - - fmt.Print(string(f)) - return nil } diff --git a/internal/app/wwctl/overlay/show/root.go b/internal/app/wwctl/overlay/show/root.go index 168c4059..aaa3961b 100644 --- a/internal/app/wwctl/overlay/show/root.go +++ b/internal/app/wwctl/overlay/show/root.go @@ -1,6 +1,9 @@ package show import ( + "log" + + "github.com/hpcng/warewulf/internal/pkg/node" "github.com/spf13/cobra" ) @@ -14,9 +17,24 @@ var ( Aliases: []string{"cat"}, Args: cobra.ExactArgs(2), } + NodeName string + Quiet bool ) func init() { + baseCmd.PersistentFlags().StringVarP(&NodeName, "name", "n", "", "node used for the variables in the template") + if err := baseCmd.RegisterFlagCompletionFunc("name", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + nodeDB, _ := node.New() + nodes, _ := nodeDB.FindAllNodes() + var node_names []string + for _, node := range nodes { + node_names = append(node_names, node.Id.Get()) + } + return node_names, cobra.ShellCompDirectiveNoFileComp + }); err != nil { + log.Println(err) + } + baseCmd.PersistentFlags().BoolVarP(&Quiet, "quiet", "q", false, "do not print information if multiple, backup files are written") } // GetRootCommand returns the root cobra.Command for the application.