From 4b285db6143ddab2b8986904cee253c1ba23449b Mon Sep 17 00:00:00 2001 From: Gregory Kurtzer Date: Thu, 3 Dec 2020 17:27:29 -0800 Subject: [PATCH] Added a package for Warewulf config --- internal/pkg/warewulfconf/constructors.go | 33 ++++++++++++ internal/pkg/warewulfconf/datastructure.go | 59 ++++++++++++++++++++++ internal/pkg/warewulfconf/modifiers.go | 30 +++++++++++ 3 files changed, 122 insertions(+) create mode 100644 internal/pkg/warewulfconf/constructors.go create mode 100644 internal/pkg/warewulfconf/datastructure.go create mode 100644 internal/pkg/warewulfconf/modifiers.go diff --git a/internal/pkg/warewulfconf/constructors.go b/internal/pkg/warewulfconf/constructors.go new file mode 100644 index 00000000..93bce4d8 --- /dev/null +++ b/internal/pkg/warewulfconf/constructors.go @@ -0,0 +1,33 @@ +package warewulfconf + +import ( + "fmt" + "github.com/hpcng/warewulf/internal/pkg/wwlog" + "gopkg.in/yaml.v2" + "io/ioutil" +) + +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 node 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.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 new file mode 100644 index 00000000..ca5c2cda --- /dev/null +++ b/internal/pkg/warewulfconf/datastructure.go @@ -0,0 +1,59 @@ +package warewulfconf + +import ( + "github.com/hpcng/warewulf/internal/pkg/util" + "github.com/hpcng/warewulf/internal/pkg/wwlog" + "os" +) + +const ConfigFile = "/etc/warewulf/warewulf.conf" + +type ControllerConf struct { + Comment string `yaml:"comment"` + Ipaddr string `yaml:"ipaddr"` + Netmask string `yaml:"netmask,omitempty"` + Fqdn string `yaml:"fqdn,omitempty"` + Warewulf *WarewulfConf `yaml:"warewulf"` + Dhcp *DhcpConf `yaml:"dhcp"` + Tftp *TftpConf `yaml:"tftp"` + Nfs *NfsConf `yaml:"nfs"` +} + +type WarewulfConf struct { + Port int `yaml:"port,omitempty"` + Secure bool `yaml:"secure,omitempty"` + Enable string `yaml:"enable command,omitempty"` + Restart string `yaml:"restart command,omitempty"` +} + +type DhcpConf struct { + Enabled bool `yaml:"enabled"` + Template string `yaml:"template,omitempty"` + RangeStart string `yaml:"range start,omitempty"` + RangeEnd string `yaml:"range end,omitempty"` + ConfigFile string `yaml:"config file,omitempty"` + Enable string `yaml:"enable command,omitempty"` + Restart string `yaml:"restart command,omitempty"` +} + +type TftpConf struct { + Enabled bool `yaml:"enabled"` + Root string `yaml:"root,omitempty"` + Enable string `yaml:"enable command,omitempty"` + Restart string `yaml:"restart command,omitempty"` +} + +type NfsConf struct { + Enabled bool `yaml:"enabled"` + Exports []string `yaml:"exports,omitempty"` + Enable string `yaml:"enable command,omitempty"` + Restart string `yaml:"restart command,omitempty"` +} + +func init() { + //TODO: Check to make sure nodes.conf is found + if util.IsFile(ConfigFile) == false { + wwlog.Printf(wwlog.ERROR, "Configuration file not found: %s\n", ConfigFile) + os.Exit(1) + } +} diff --git a/internal/pkg/warewulfconf/modifiers.go b/internal/pkg/warewulfconf/modifiers.go new file mode 100644 index 00000000..f50e329e --- /dev/null +++ b/internal/pkg/warewulfconf/modifiers.go @@ -0,0 +1,30 @@ +package warewulfconf + +import ( + "github.com/hpcng/warewulf/internal/pkg/wwlog" + "gopkg.in/yaml.v2" + "os" +) + +func (self *ControllerConf) Persist() error { + + out, err := yaml.Marshal(self) + if err != nil { + return err + } + + file, err := os.OpenFile(ConfigFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + wwlog.Printf(wwlog.ERROR, "%s\n", err) + os.Exit(1) + } + + defer file.Close() + + _, err = file.WriteString(string(out)) + if err != nil { + return err + } + + return nil +}