From ed2105246243707d8350c9afae4de9355d7a606b Mon Sep 17 00:00:00 2001 From: Christian Goll Date: Mon, 27 Jun 2022 11:50:27 +0200 Subject: [PATCH] readded configure hostfile command --- internal/app/wwctl/configure/hostfile/main.go | 10 ++++ internal/app/wwctl/configure/hostfile/root.go | 21 +++++++++ internal/app/wwctl/configure/main.go | 6 +++ internal/app/wwctl/configure/root.go | 3 ++ internal/pkg/configure/hostfile.go | 46 +++++++++++++++++++ internal/pkg/overlay/overlay.go | 6 +-- 6 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 internal/app/wwctl/configure/hostfile/main.go create mode 100644 internal/app/wwctl/configure/hostfile/root.go create mode 100644 internal/pkg/configure/hostfile.go diff --git a/internal/app/wwctl/configure/hostfile/main.go b/internal/app/wwctl/configure/hostfile/main.go new file mode 100644 index 00000000..eabf3da1 --- /dev/null +++ b/internal/app/wwctl/configure/hostfile/main.go @@ -0,0 +1,10 @@ +package hostfile + +import ( + "github.com/hpcng/warewulf/internal/pkg/configure" + "github.com/spf13/cobra" +) + +func CobraRunE(cmd *cobra.Command, args []string) error { + return configure.Hostfile() +} diff --git a/internal/app/wwctl/configure/hostfile/root.go b/internal/app/wwctl/configure/hostfile/root.go new file mode 100644 index 00000000..34ea9b1f --- /dev/null +++ b/internal/app/wwctl/configure/hostfile/root.go @@ -0,0 +1,21 @@ +package hostfile + +import "github.com/spf13/cobra" + +var ( + baseCmd = &cobra.Command{ + DisableFlagsInUseLine: true, + Use: "hostfile [OPTIONS]", + Short: "update hostfile on master", + Long: "Manage the hostfile on the master node\n", + RunE: CobraRunE, + } +) + +func init() { +} + +// GetRootCommand returns the root cobra.Command for the application. +func GetCommand() *cobra.Command { + return baseCmd +} diff --git a/internal/app/wwctl/configure/main.go b/internal/app/wwctl/configure/main.go index ccb7bbef..77742cd0 100644 --- a/internal/app/wwctl/configure/main.go +++ b/internal/app/wwctl/configure/main.go @@ -34,6 +34,12 @@ func CobraRunE(cmd *cobra.Command, args []string) error { wwlog.Printf(wwlog.ERROR, "%s\n", err) os.Exit(1) } + err = configure.Hostfile() + if err != nil { + wwlog.Printf(wwlog.ERROR, "%s\n", err) + os.Exit(1) + } + } else { _ = cmd.Help() os.Exit(0) diff --git a/internal/app/wwctl/configure/root.go b/internal/app/wwctl/configure/root.go index 52ad691d..c29511a0 100644 --- a/internal/app/wwctl/configure/root.go +++ b/internal/app/wwctl/configure/root.go @@ -2,6 +2,7 @@ package configure import ( "github.com/hpcng/warewulf/internal/app/wwctl/configure/dhcp" + "github.com/hpcng/warewulf/internal/app/wwctl/configure/hostfile" "github.com/hpcng/warewulf/internal/app/wwctl/configure/nfs" "github.com/hpcng/warewulf/internal/app/wwctl/configure/ssh" "github.com/hpcng/warewulf/internal/app/wwctl/configure/tftp" @@ -25,6 +26,8 @@ func init() { baseCmd.AddCommand(tftp.GetCommand()) baseCmd.AddCommand(ssh.GetCommand()) baseCmd.AddCommand(nfs.GetCommand()) + baseCmd.AddCommand(hostfile.GetCommand()) + baseCmd.PersistentFlags().BoolVarP(&allFunctions, "all", "a", false, "Configure all services") } diff --git a/internal/pkg/configure/hostfile.go b/internal/pkg/configure/hostfile.go new file mode 100644 index 00000000..fbe763b1 --- /dev/null +++ b/internal/pkg/configure/hostfile.go @@ -0,0 +1,46 @@ +package configure + +import ( + "os" + "path" + + "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/pkg/errors" +) + +/* +Creates '/etc/hosts' from the host template. +*/ +func Hostfile() error { + if !(util.IsFile(path.Join(overlay.OverlaySourceDir("host"), "/host/etc/hosts.ww"))) { + wwlog.Error("'the overlay template '/etc/hosts.ww' does not exists in 'host' overlay\n") + os.Exit(1) + } + var nodeInfo node.NodeInfo + tstruct := overlay.InitStruct(nodeInfo) + hostname, _ := os.Hostname() + nodeInfo.Id.Set(hostname) + buffer, backupFile, writeFile, err := overlay.RenderTemplateFile( + path.Join(overlay.OverlaySourceDir("host"), "/host/etc/hosts.ww"), + tstruct) + if err != nil { + wwlog.Printf(wwlog.ERROR, "%s\n", err) + os.Exit(1) + } + info, err := os.Stat(path.Join(overlay.OverlaySourceDir("host"), "/host/etc/hosts.ww")) + if err != nil { + wwlog.Printf(wwlog.ERROR, "%s\n", err) + os.Exit(1) + } + + if writeFile { + err = overlay.CarefulWriteBuffer("/etc/hosts", buffer, backupFile, info.Mode()) + if err != nil { + return errors.Wrap(err, "could not write file from template") + } + } + return nil +} diff --git a/internal/pkg/overlay/overlay.go b/internal/pkg/overlay/overlay.go index 7953c2fb..8906f447 100644 --- a/internal/pkg/overlay/overlay.go +++ b/internal/pkg/overlay/overlay.go @@ -260,7 +260,7 @@ func BuildOverlayIndir(nodeInfo node.NodeInfo, overlayNames []string, outputDir if len(filenameFromTemplate) != 0 { wwlog.Debug("Found multifile comment, new filename %s", filenameFromTemplate[0][1]) if foundFileComment { - err = carefulWriteBuffer(path.Join(outputDir, destFileName), + err = CarefulWriteBuffer(path.Join(outputDir, destFileName), fileBuffer, backupFile, info.Mode()) if err != nil { return errors.Wrap(err, "could not write file from template") @@ -277,7 +277,7 @@ func BuildOverlayIndir(nodeInfo node.NodeInfo, overlayNames []string, outputDir _, _ = fileBuffer.WriteString(line) } } - err = carefulWriteBuffer(path.Join(outputDir, destFileName), fileBuffer, backupFile, info.Mode()) + err = CarefulWriteBuffer(path.Join(outputDir, destFileName), fileBuffer, backupFile, info.Mode()) if err != nil { return errors.Wrap(err, "could not write file from template") } @@ -335,7 +335,7 @@ func BuildOverlayIndir(nodeInfo node.NodeInfo, overlayNames []string, outputDir /* Writes buffer to the destination file. If wwbackup is set a wwbackup will be created. */ -func carefulWriteBuffer(destFile string, buffer bytes.Buffer, backupFile bool, perm fs.FileMode) error { +func CarefulWriteBuffer(destFile string, buffer bytes.Buffer, backupFile bool, perm fs.FileMode) error { wwlog.Debug("Trying to careful write file (%d bytes): %s", buffer.Len(), destFile) if backupFile { if !util.IsFile(destFile+".wwbackup") && util.IsFile(destFile) {