From 6212f40e4c3b3f030c504b4243a44e00e7877e34 Mon Sep 17 00:00:00 2001 From: xu yang Date: Thu, 8 Aug 2024 06:43:00 +0000 Subject: [PATCH] Refine wwctl configure error handling - Closes #1273 Signed-off-by: xu yang --- CHANGELOG.md | 1 + internal/pkg/configure/dhcp.go | 11 ++++------- internal/pkg/configure/hostfile.go | 15 ++++++--------- internal/pkg/configure/ssh.go | 6 ++---- internal/pkg/configure/tftp.go | 14 ++++++-------- 5 files changed, 19 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea82d044..798634fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Changed - Added a link to an example SELinux-enabled node image in documentation. #1305 +- Refine error handling for `wwctl configure`. #1273 ## v4.5.6, 2024-08-05 diff --git a/internal/pkg/configure/dhcp.go b/internal/pkg/configure/dhcp.go index 15f8f362..dfeb0f1e 100644 --- a/internal/pkg/configure/dhcp.go +++ b/internal/pkg/configure/dhcp.go @@ -2,7 +2,6 @@ package configure import ( "fmt" - "os" "github.com/pkg/errors" warewulfconf "github.com/warewulf/warewulf/internal/pkg/config" @@ -20,18 +19,16 @@ func DHCP() (err error) { controller := warewulfconf.Get() if !controller.DHCP.Enabled { - wwlog.Info("This system is not configured as a Warewulf DHCP controller") - os.Exit(1) + wwlog.Warn("This system is not configured as a Warewulf DHCP controller, need to manually run `wwctl configure dhcp` later") + return } if controller.DHCP.RangeStart == "" { - wwlog.Error("Configuration is not defined: `dhcpd range start`") - os.Exit(1) + return fmt.Errorf("configuration is not defined: `dhcpd range start`") } if controller.DHCP.RangeEnd == "" { - wwlog.Error("Configuration is not defined: `dhcpd range end`") - os.Exit(1) + return fmt.Errorf("configuration is not defined: `dhcpd range end`") } if controller.Warewulf.EnableHostOverlay { err = overlay.BuildHostOverlay() diff --git a/internal/pkg/configure/hostfile.go b/internal/pkg/configure/hostfile.go index 8c2e5dda..c696f6fc 100644 --- a/internal/pkg/configure/hostfile.go +++ b/internal/pkg/configure/hostfile.go @@ -1,6 +1,7 @@ package configure import ( + "fmt" "os" "path" @@ -8,17 +9,15 @@ import ( "github.com/warewulf/warewulf/internal/pkg/node" "github.com/warewulf/warewulf/internal/pkg/overlay" "github.com/warewulf/warewulf/internal/pkg/util" - "github.com/warewulf/warewulf/internal/pkg/wwlog" ) /* Creates '/etc/hosts' from the host template. */ -func Hostfile() error { +func Hostfile() (err error) { hostTemplate := path.Join(overlay.OverlaySourceDir("host"), "/etc/hosts.ww") if !(util.IsFile(hostTemplate)) { - wwlog.Error("'the overlay template '/etc/hosts.ww' does not exists in 'host' overlay") - os.Exit(1) + return fmt.Errorf("'the overlay template '/etc/hosts.ww' does not exists in 'host' overlay") } nodeInfo := node.NewInfo() @@ -29,13 +28,11 @@ func Hostfile() error { hostTemplate, tstruct) if err != nil { - wwlog.Error("%s", err) - os.Exit(1) + return } info, err := os.Stat(hostTemplate) if err != nil { - wwlog.Error("%s", err) - os.Exit(1) + return } if writeFile { @@ -44,5 +41,5 @@ func Hostfile() error { return errors.Wrap(err, "could not write file from template") } } - return nil + return } diff --git a/internal/pkg/configure/ssh.go b/internal/pkg/configure/ssh.go index 4d12c1db..724277da 100644 --- a/internal/pkg/configure/ssh.go +++ b/internal/pkg/configure/ssh.go @@ -23,8 +23,7 @@ func SSH(keyTypes ...string) error { err := os.MkdirAll(path.Join(conf.Paths.Sysconfdir, "warewulf/keys"), 0755) if err != nil { - wwlog.Error("Could not create base directory: %s", err) - os.Exit(1) + return fmt.Errorf("could not create base directory: %s", err) } for _, k := range keyTypes { @@ -47,8 +46,7 @@ func SSH(keyTypes ...string) error { homeDir, err := os.UserHomeDir() if err != nil { - wwlog.Error("Could not obtain the user's home directory: %s", err) - os.Exit(1) + return fmt.Errorf("could not obtain the user's home directory: %s", err) } authorizedKeys := path.Join(homeDir, "/.ssh/authorized_keys") diff --git a/internal/pkg/configure/tftp.go b/internal/pkg/configure/tftp.go index 06d31752..08bea5f7 100644 --- a/internal/pkg/configure/tftp.go +++ b/internal/pkg/configure/tftp.go @@ -10,14 +10,13 @@ import ( "github.com/warewulf/warewulf/internal/pkg/wwlog" ) -func TFTP() error { +func TFTP() (err error) { controller := warewulfconf.Get() var tftpdir string = path.Join(controller.TFTP.TftpRoot, "warewulf") - err := os.MkdirAll(tftpdir, 0755) + err = os.MkdirAll(tftpdir, 0755) if err != nil { - wwlog.Error("%s", err) - return err + return } if controller.Warewulf.GrubBoot { @@ -43,15 +42,14 @@ func TFTP() error { } } if !controller.TFTP.Enabled { - wwlog.Info("Warewulf does not auto start TFTP services due to disable by warewulf.conf") - os.Exit(0) + wwlog.Warn("Warewulf does not auto start TFTP services due to disable by warewulf.conf, need to manually run `wwctl configure tftp` later") + return nil } wwlog.Info("Enabling and restarting the TFTP services") err = util.SystemdStart(controller.TFTP.SystemdName) if err != nil { - wwlog.Error("%s", err) - return err + return } return nil