From 238b61a6823d6b73345b706c78b1d7ffd1ccc88b Mon Sep 17 00:00:00 2001 From: Gregory Kurtzer Date: Thu, 30 Sep 2021 23:40:16 -0700 Subject: [PATCH] Daemon logging to syslog (#152) --- etc/warewulf.conf | 1 + internal/app/wwctl/server/start/main.go | 7 ++ internal/pkg/warewulfconf/constructors.go | 76 ++++++++++++---------- internal/pkg/warewulfconf/datastructure.go | 1 + internal/pkg/warewulfd/util.go | 31 ++++++++- 5 files changed, 81 insertions(+), 35 deletions(-) diff --git a/etc/warewulf.conf b/etc/warewulf.conf index 2a75dc3f..938c0bc9 100644 --- a/etc/warewulf.conf +++ b/etc/warewulf.conf @@ -5,6 +5,7 @@ warewulf: secure: true autobuild overlays: true update interval: 60 + syslog: false dhcp: enabled: true range start: 192.168.200.50 diff --git a/internal/app/wwctl/server/start/main.go b/internal/app/wwctl/server/start/main.go index 0c184b9a..b1954a46 100644 --- a/internal/app/wwctl/server/start/main.go +++ b/internal/app/wwctl/server/start/main.go @@ -1,13 +1,20 @@ package start import ( + "github.com/hpcng/warewulf/internal/pkg/warewulfconf" "github.com/hpcng/warewulf/internal/pkg/warewulfd" "github.com/pkg/errors" "github.com/spf13/cobra" ) func CobraRunE(cmd *cobra.Command, args []string) error { + if SetForeground { + conf, err := warewulfconf.New() + if err != nil { + return errors.Wrap(err, "Could not read Warewulf configuration file") + } + conf.Warewulf.Syslog = false return errors.Wrap(warewulfd.RunServer(), "failed to start Warewulf server") } else { return errors.Wrap(warewulfd.DaemonStart(), "failed to start Warewulf server") diff --git a/internal/pkg/warewulfconf/constructors.go b/internal/pkg/warewulfconf/constructors.go index fc0886f3..76cefbe6 100644 --- a/internal/pkg/warewulfconf/constructors.go +++ b/internal/pkg/warewulfconf/constructors.go @@ -10,43 +10,53 @@ import ( "gopkg.in/yaml.v2" ) +var singleton ControllerConf + func New() (ControllerConf, error) { var ret ControllerConf - wwlog.Printf(wwlog.DEBUG, "Opening Warewulf configuration file: %s\n", ConfigFile) - data, err := ioutil.ReadFile(ConfigFile) - if err != nil { - fmt.Printf("error reading Warewulf configuration file\n") - return ret, err + if (ControllerConf{}) == singleton { + wwlog.Printf(wwlog.DEBUG, "Opening Warewulf configuration file: %s\n", ConfigFile) + data, err := ioutil.ReadFile(ConfigFile) + if err != nil { + fmt.Printf("error reading Warewulf configuration file\n") + return ret, err + } + + wwlog.Printf(wwlog.DEBUG, "Unmarshaling the Warewulf configuration\n") + err = yaml.Unmarshal(data, &ret) + if err != nil { + return ret, err + } + + if ret.Ipaddr == "" { + wwlog.Printf(wwlog.WARN, "IP address is not configured in warewulfd.conf\n") + } + if ret.Netmask == "" { + wwlog.Printf(wwlog.WARN, "Netmask is not configured in warewulfd.conf\n") + } + + if ret.Network == "" { + mask := net.IPMask(net.ParseIP(ret.Netmask).To4()) + size, _ := mask.Size() + + sub := ipsubnet.SubnetCalculator(ret.Ipaddr, size) + + ret.Network = sub.GetNetworkPortion() + } + + if ret.Warewulf.Port == 0 { + ret.Warewulf.Port = 9873 + } + + wwlog.Printf(wwlog.DEBUG, "Returning warewulf config object\n") + singleton = ret + + } else { + wwlog.Printf(wwlog.DEBUG, "Returning cached warewulf config object\n") + + ret = singleton } - wwlog.Printf(wwlog.DEBUG, "Unmarshaling the Warewulf configuration\n") - err = yaml.Unmarshal(data, &ret) - if err != nil { - return ret, err - } - - if ret.Ipaddr == "" { - wwlog.Printf(wwlog.WARN, "IP address is not configured in warewulfd.conf\n") - } - if ret.Netmask == "" { - wwlog.Printf(wwlog.WARN, "Netmask is not configured in warewulfd.conf\n") - } - - if ret.Network == "" { - mask := net.IPMask(net.ParseIP(ret.Netmask).To4()) - size, _ := mask.Size() - - sub := ipsubnet.SubnetCalculator(ret.Ipaddr, size) - - ret.Network = sub.GetNetworkPortion() - } - - if ret.Warewulf.Port == 0 { - ret.Warewulf.Port = 9873 - } - - wwlog.Printf(wwlog.DEBUG, "Returning node object\n") - return ret, nil } diff --git a/internal/pkg/warewulfconf/datastructure.go b/internal/pkg/warewulfconf/datastructure.go index 79c5a01c..8315ddd6 100644 --- a/internal/pkg/warewulfconf/datastructure.go +++ b/internal/pkg/warewulfconf/datastructure.go @@ -24,6 +24,7 @@ type WarewulfConf struct { Secure bool `yaml:"secure"` UpdateInterval int `yaml:"update interval"` AutobuildOverlays bool `yaml:"autobuild overlays"` + Syslog bool `yaml:"syslog"` } type DhcpConf struct { diff --git a/internal/pkg/warewulfd/util.go b/internal/pkg/warewulfd/util.go index f88514bb..c7c4991b 100644 --- a/internal/pkg/warewulfd/util.go +++ b/internal/pkg/warewulfd/util.go @@ -3,6 +3,8 @@ package warewulfd import ( "fmt" "io" + "log" + "log/syslog" "net/http" "os" "strconv" @@ -10,12 +12,37 @@ import ( "time" "github.com/hpcng/warewulf/internal/pkg/node" + "github.com/hpcng/warewulf/internal/pkg/warewulfconf" "github.com/pkg/errors" ) +var logwriter syslog.Writer +var loginit bool + func daemonLogf(message string, a ...interface{}) { - prefix := fmt.Sprintf("[%s] ", time.Now().Format(time.UnixDate)) - fmt.Printf(prefix+message, a...) + conf, err := warewulfconf.New() + if err != nil { + fmt.Printf("ERROR: Could not read Warewulf configuration file: %s\n", err) + return + } + + if conf.Warewulf.Syslog == true { + if loginit == false { + logwriter, err := syslog.New(syslog.LOG_NOTICE, "warewulfd") + if err != nil { + return + } + log.SetOutput(logwriter) + loginit = true + } + log.SetFlags(0) + log.SetPrefix("") + + log.Printf(message, a...) + } else { + prefix := fmt.Sprintf("[%s] ", time.Now().Format(time.UnixDate)) + fmt.Printf(prefix+message, a...) + } } func getSanity(req *http.Request) (node.NodeInfo, error) {