From 2927495de1f24c7b08d5f913cacde88764b1f1c1 Mon Sep 17 00:00:00 2001 From: Gregory Kurtzer Date: Sun, 27 Feb 2022 18:54:51 -0800 Subject: [PATCH] Fixes to #304 and #311 --- internal/app/wwctl/configure/dhcp/main.go | 3 +- internal/app/wwctl/configure/dhcp/root.go | 2 - internal/app/wwctl/configure/main.go | 43 ++++++++ internal/app/wwctl/configure/nfs/main.go | 2 +- internal/app/wwctl/configure/nfs/root.go | 2 - internal/app/wwctl/configure/root.go | 20 ---- internal/app/wwctl/configure/ssh/main.go | 2 +- internal/app/wwctl/configure/ssh/root.go | 2 - internal/app/wwctl/configure/tftp/main.go | 2 +- internal/pkg/configure/common.go | 44 --------- internal/pkg/configure/dhcp.go | 28 +++--- internal/pkg/configure/hosts.go | 110 --------------------- internal/pkg/configure/nfs.go | 8 +- internal/pkg/configure/ssh.go | 2 +- internal/pkg/configure/tftp.go | 2 +- overlays/host/etc/{ => dhcp}/dhcpd.conf.ww | 0 16 files changed, 66 insertions(+), 206 deletions(-) create mode 100644 internal/app/wwctl/configure/main.go delete mode 100644 internal/pkg/configure/common.go delete mode 100644 internal/pkg/configure/hosts.go rename overlays/host/etc/{ => dhcp}/dhcpd.conf.ww (100%) diff --git a/internal/app/wwctl/configure/dhcp/main.go b/internal/app/wwctl/configure/dhcp/main.go index 8ea2d8ff..38ff6f97 100644 --- a/internal/app/wwctl/configure/dhcp/main.go +++ b/internal/app/wwctl/configure/dhcp/main.go @@ -6,6 +6,5 @@ import ( ) func CobraRunE(cmd *cobra.Command, args []string) error { - return configure.Configure("DHCP", setShow) + return configure.Dhcp() } - diff --git a/internal/app/wwctl/configure/dhcp/root.go b/internal/app/wwctl/configure/dhcp/root.go index a4a32716..558d94a6 100644 --- a/internal/app/wwctl/configure/dhcp/root.go +++ b/internal/app/wwctl/configure/dhcp/root.go @@ -13,11 +13,9 @@ var ( "in the warewulf.conf file.", RunE: CobraRunE, } - setShow bool ) func init() { - baseCmd.PersistentFlags().BoolVarP(&setShow, "show", "s", false, "Show configuration (don't update)") } // GetRootCommand returns the root cobra.Command for the application. diff --git a/internal/app/wwctl/configure/main.go b/internal/app/wwctl/configure/main.go new file mode 100644 index 00000000..ccb7bbef --- /dev/null +++ b/internal/app/wwctl/configure/main.go @@ -0,0 +1,43 @@ +package configure + +import ( + "os" + + "github.com/hpcng/warewulf/internal/pkg/configure" + "github.com/hpcng/warewulf/internal/pkg/wwlog" + "github.com/spf13/cobra" +) + +func CobraRunE(cmd *cobra.Command, args []string) error { + var err error + if allFunctions { + err = configure.Dhcp() + if err != nil { + wwlog.Printf(wwlog.ERROR, "%s\n", err) + os.Exit(1) + } + + err = configure.NFS() + if err != nil { + wwlog.Printf(wwlog.ERROR, "%s\n", err) + os.Exit(1) + } + + err = configure.SSH() + if err != nil { + wwlog.Printf(wwlog.ERROR, "%s\n", err) + os.Exit(1) + } + + err = configure.TFTP() + if err != nil { + wwlog.Printf(wwlog.ERROR, "%s\n", err) + os.Exit(1) + } + } else { + _ = cmd.Help() + os.Exit(0) + } + + return nil +} diff --git a/internal/app/wwctl/configure/nfs/main.go b/internal/app/wwctl/configure/nfs/main.go index fa5c0df5..409152f3 100644 --- a/internal/app/wwctl/configure/nfs/main.go +++ b/internal/app/wwctl/configure/nfs/main.go @@ -6,5 +6,5 @@ import ( ) func CobraRunE(cmd *cobra.Command, args []string) error { - return configure.Configure("NFS", setShow) + return configure.NFS() } diff --git a/internal/app/wwctl/configure/nfs/root.go b/internal/app/wwctl/configure/nfs/root.go index ba403a49..92ad1a43 100644 --- a/internal/app/wwctl/configure/nfs/root.go +++ b/internal/app/wwctl/configure/nfs/root.go @@ -11,11 +11,9 @@ var ( "configure NFS as per the configuration in the warewulf.conf file.", RunE: CobraRunE, } - setShow bool ) func init() { - baseCmd.PersistentFlags().BoolVarP(&setShow, "show", "s", false, "Show configuration (don't update)") } // GetRootCommand returns the root cobra.Command for the application. diff --git a/internal/app/wwctl/configure/root.go b/internal/app/wwctl/configure/root.go index ecc7ce8a..52ad691d 100644 --- a/internal/app/wwctl/configure/root.go +++ b/internal/app/wwctl/configure/root.go @@ -1,13 +1,10 @@ package configure import ( - "os" - "github.com/hpcng/warewulf/internal/app/wwctl/configure/dhcp" "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" - "github.com/hpcng/warewulf/internal/pkg/configure" "github.com/spf13/cobra" ) @@ -35,20 +32,3 @@ func init() { func GetCommand() *cobra.Command { return baseCmd } - -func CobraRunE(cmd *cobra.Command, args []string) error { - var err error - if allFunctions { - for _, s := range [5]string{"DHPC", "NFS", "SSH", "TFTP"} { - err = configure.Configure(s, false) - if err != nil { - os.Exit(1) - } - } - } else { - _ = cmd.Help() - os.Exit(0) - } - - return nil -} diff --git a/internal/app/wwctl/configure/ssh/main.go b/internal/app/wwctl/configure/ssh/main.go index 1d7afb1b..a29c5833 100644 --- a/internal/app/wwctl/configure/ssh/main.go +++ b/internal/app/wwctl/configure/ssh/main.go @@ -6,5 +6,5 @@ import ( ) func CobraRunE(cmd *cobra.Command, args []string) error { - return configure.Configure("SSH", setShow) + return configure.SSH() } diff --git a/internal/app/wwctl/configure/ssh/root.go b/internal/app/wwctl/configure/ssh/root.go index a3909e64..6c41d68a 100644 --- a/internal/app/wwctl/configure/ssh/root.go +++ b/internal/app/wwctl/configure/ssh/root.go @@ -12,11 +12,9 @@ var ( "keys.", RunE: CobraRunE, } - setShow bool ) func init() { - baseCmd.PersistentFlags().BoolVarP(&setShow, "show", "s", false, "Show configuration (don't update)") } // GetRootCommand returns the root cobra.Command for the application. diff --git a/internal/app/wwctl/configure/tftp/main.go b/internal/app/wwctl/configure/tftp/main.go index f122fb28..666d77bc 100644 --- a/internal/app/wwctl/configure/tftp/main.go +++ b/internal/app/wwctl/configure/tftp/main.go @@ -6,5 +6,5 @@ import ( ) func CobraRunE(cmd *cobra.Command, args []string) error { - return configure.Configure("TFTP", setShow) + return configure.TFTP() } diff --git a/internal/pkg/configure/common.go b/internal/pkg/configure/common.go deleted file mode 100644 index fe6404b4..00000000 --- a/internal/pkg/configure/common.go +++ /dev/null @@ -1,44 +0,0 @@ -package configure - -import ( - "fmt" - - "github.com/pkg/errors" -) - -func Configure(serv string, show bool) error { - if !show { - fmt.Printf("################################################################################\n") - fmt.Printf("Configuring: %s\n", serv) - } - - var err error - switch serv { - case "DHCP": - err = configureDHCP(show) - case "hosts": - err = configureHosts(show) - case "NFS": - if !show { - err = configureNFS() - } else { - showNFS() - } - case "SSH": - if !show { - err = configureSSH() - } else { - fmt.Printf("'ssh -s' is not yet implemented.\n") - } - case "TFTP": - if !show { - err = configureTFTP() - } else { - fmt.Printf("'tftp -s' is not yet implemented.\n") - } - } - if err != nil { - return errors.Wrap(err, "Failed to execute configure on "+serv) - } - return nil -} diff --git a/internal/pkg/configure/dhcp.go b/internal/pkg/configure/dhcp.go index 1471df30..19037f1e 100644 --- a/internal/pkg/configure/dhcp.go +++ b/internal/pkg/configure/dhcp.go @@ -4,18 +4,18 @@ import ( "fmt" "os" - "github.com/hpcng/warewulf/internal/pkg/overlay" + "github.com/hpcng/warewulf/internal/pkg/overlay" "github.com/hpcng/warewulf/internal/pkg/util" "github.com/hpcng/warewulf/internal/pkg/warewulfconf" "github.com/hpcng/warewulf/internal/pkg/wwlog" "github.com/pkg/errors" ) + /* -Configures the dhcpd server, when show is set to false, else the +Configures the dhcpd server, when show is set to false, else the dhcp configuration is checked. */ -func configureDHCP(show bool) error { - +func Dhcp() error { controller, err := warewulfconf.New() if err != nil { @@ -37,18 +37,16 @@ func configureDHCP(show bool) error { wwlog.Printf(wwlog.ERROR, "Configuration is not defined: `dhcpd range end`\n") os.Exit(1) } - if !show { - err = overlay.BuildHostOverlay() - if err != nil { - wwlog.Printf(wwlog.WARN, "host overlay could not be built: %s\n", err) - } - fmt.Printf("Enabling and restarting the DHCP services\n") - err := util.SystemdStart(controller.Dhcp.SystemdName) - if err != nil { - return errors.Wrap(err, "failed to start") - } + err = overlay.BuildHostOverlay() + if err != nil { + wwlog.Printf(wwlog.WARN, "host overlay could not be built: %s\n", err) + } + fmt.Printf("Enabling and restarting the DHCP services\n") + err = util.SystemdStart(controller.Dhcp.SystemdName) + if err != nil { + return errors.Wrap(err, "failed to start") } - return nil + return nil } diff --git a/internal/pkg/configure/hosts.go b/internal/pkg/configure/hosts.go deleted file mode 100644 index d0c918fd..00000000 --- a/internal/pkg/configure/hosts.go +++ /dev/null @@ -1,110 +0,0 @@ -package configure - -import ( - "bytes" - "os" - "path" - "text/template" - - "github.com/hpcng/warewulf/internal/pkg/buildconfig" - "github.com/hpcng/warewulf/internal/pkg/node" - "github.com/hpcng/warewulf/internal/pkg/util" - "github.com/hpcng/warewulf/internal/pkg/warewulfconf" - "github.com/hpcng/warewulf/internal/pkg/wwlog" -) - -type hostsTemplate struct { - PrevHostFile string - Ipaddr string - Fqdn string - AllNodes []node.NodeInfo -} - -func configureHosts(show bool) error { - var replace hostsTemplate - - if !util.IsFile(path.Join(buildconfig.SYSCONFDIR(), "warewulf/hosts.tmpl")) { - wwlog.Printf(wwlog.WARN, "Template not found, not updating host file\n") - return nil - } - - controller, err := warewulfconf.New() - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } - - n, err := node.New() - if err != nil { - wwlog.Printf(wwlog.ERROR, "Could not open node configuration: %s\n", err) - os.Exit(1) - } - - tmpl, err := template.ParseFiles(path.Join(buildconfig.SYSCONFDIR(), "warewulf/hosts.tmpl")) - if err != nil { - wwlog.Printf(wwlog.ERROR, "Could not parse hosts template: %s\n", err) - os.Exit(1) - } - - replace.PrevHostFile = "" - w, err := os.Open("/etc/hosts") - if err != nil { - wwlog.Printf(wwlog.WARN, "%s\n", err) - } else { - // if /etc/hosts.ww does not exist, backup /etc/hosts to /etc/hosts.wwbackup - if !util.IsFile("/etc/hosts.wwbackup") { - err = util.CopyFile("/etc/hosts", "/etc/hosts.wwbackup") - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - } - } - - // read all lines before the # warewulf comment and put into PrevHostFile template variable - lines, _ := util.ReadFile("/etc/hosts") - if lines != nil { - var buffer bytes.Buffer - for _, line := range lines { - //wwlog.Printf(wwlog.INFO, "Reading line: %s\n", line) - if util.ValidString(line, "^#.*maintained by warewulf") { - break - } - buffer.WriteString(line) - buffer.WriteString("\n") - } - replace.PrevHostFile = buffer.String() - } - } - - w.Close() - - nodes, _ := n.FindAllNodes() - - replace.AllNodes = nodes - replace.Ipaddr = controller.Ipaddr - replace.Fqdn = controller.Fqdn - - if !show { - // only open "/etc/hosts" when intended to write, as 'os.O_TRUNC' will empty the file otherwise. - w, err = os.OpenFile("/etc/hosts", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } - defer w.Close() - - err = tmpl.Execute(w, replace) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } - } else { - err = tmpl.Execute(os.Stdout, replace) - if err != nil { - wwlog.Printf(wwlog.ERROR, "%s\n", err) - os.Exit(1) - } - - } - - return nil -} diff --git a/internal/pkg/configure/nfs.go b/internal/pkg/configure/nfs.go index dd29c86b..53159249 100644 --- a/internal/pkg/configure/nfs.go +++ b/internal/pkg/configure/nfs.go @@ -12,10 +12,10 @@ import ( ) /* -Creates '/etc/exports' from the host template, enables and start the +Creates '/etc/exports' from the host template, enables and start the nfs server. */ -func configureNFS() error { +func NFS() error { controller, err := warewulfconf.New() if err != nil { @@ -28,7 +28,7 @@ func configureNFS() error { fmt.Println(err) } err = overlay.BuildHostOverlay() - if err != nil { + if err != nil { wwlog.Printf(wwlog.WARN, "host overlay could not be built: %s\n", err) } fmt.Printf("Enabling and restarting the NFS services\n") @@ -49,7 +49,7 @@ func configureNFS() error { } /* -Prints the configured nfs exports in formated style, needs not to be +Prints the configured nfs exports in formated style, needs not to be the content of '/etc/exports' */ func showNFS() { diff --git a/internal/pkg/configure/ssh.go b/internal/pkg/configure/ssh.go index db6b810d..6e9db39e 100644 --- a/internal/pkg/configure/ssh.go +++ b/internal/pkg/configure/ssh.go @@ -11,7 +11,7 @@ import ( "github.com/pkg/errors" ) -func configureSSH() error { +func SSH() error { if os.Getuid() == 0 { fmt.Printf("Updating system keys\n") diff --git a/internal/pkg/configure/tftp.go b/internal/pkg/configure/tftp.go index f6f21fed..710cd61f 100644 --- a/internal/pkg/configure/tftp.go +++ b/internal/pkg/configure/tftp.go @@ -13,7 +13,7 @@ import ( var tftpdir string = path.Join(buildconfig.TFTPDIR(), "warewulf") -func configureTFTP() error { +func TFTP() error { controller, err := warewulfconf.New() if err != nil { wwlog.Printf(wwlog.ERROR, "%s\n", err) diff --git a/overlays/host/etc/dhcpd.conf.ww b/overlays/host/etc/dhcp/dhcpd.conf.ww similarity index 100% rename from overlays/host/etc/dhcpd.conf.ww rename to overlays/host/etc/dhcp/dhcpd.conf.ww