Fix writing of nodes.conf

The caching functionality of nodes.conf, and the optional persistence
functionality that came with it, appears to have been preventing
actually writing the configuration to disk in all cases. This change
fixes the ability to write to nodes.conf.

Somewhat opinionatedly, this change removes the caching functionality of
nodes.conf. The tests have also been updated to use real files for i/o
rather than tying into the caching system, making them more accurately
test the ability of Warewulf to actually read and write its
configuration.

Fixes #779

Signed-off-by: Jonathon Anderson <janderson@ciq.co>
This commit is contained in:
Jonathon Anderson
2023-04-10 19:26:06 -06:00
parent 0f2fcb326f
commit 2319906d97
8 changed files with 96 additions and 95 deletions

View File

@@ -17,8 +17,6 @@ import (
var ConfigFile string
var DefaultConfig string
var cachedDB NodeYaml
// used as fallback if DefaultConfig can't be read
var FallBackConf = `---
defaultnode:
@@ -47,32 +45,33 @@ func init() {
if DefaultConfig == "" {
DefaultConfig = path.Join(conf.Paths.Datadir, "warewulf/defaults.conf")
}
cachedDB.current = false
cachedDB.persist = true
}
/*
Creates a new nodeDb object from the actual configuration
*/
func New() (NodeYaml, error) {
if cachedDB.current {
wwlog.Debug("Returning cached object")
return cachedDB, nil
}
var ret NodeYaml
wwlog.Verbose("Opening node configuration file: %s", ConfigFile)
data, err := os.ReadFile(ConfigFile)
if err != nil {
return ret, err
return NodeYaml{}, err
}
return Parse(data)
}
func Parse(data []byte) (NodeYaml, error) {
var ret NodeYaml
var err error
wwlog.Debug("Unmarshaling the node configuration")
err = yaml.Unmarshal(data, &ret)
if err != nil {
return ret, err
}
wwlog.Debug("Checking nodes for types")
if ret.Nodes == nil {
ret.Nodes = map[string]*NodeConf{}
}
for nodeName, node := range ret.Nodes {
err = node.Check()
if err != nil {
@@ -80,6 +79,9 @@ func New() (NodeYaml, error) {
return ret, err
}
}
if ret.NodeProfiles == nil {
ret.NodeProfiles = map[string]*NodeConf{}
}
for profileName, profile := range ret.NodeProfiles {
err = profile.Check()
if err != nil {
@@ -89,39 +91,9 @@ func New() (NodeYaml, error) {
}
wwlog.Debug("Returning node object")
cachedDB = ret
cachedDB.current = true
return ret, nil
}
/*
Creates a database object from a given buffer, always create
a new object, never return the cached one.
*/
func TestNew(buffer []byte) (db NodeYaml, err error) {
db.NodeProfiles = make(map[string]*NodeConf)
db.Nodes = make(map[string]*NodeConf)
err = yaml.Unmarshal(buffer, &db)
db.persist = false
cachedDB = db
cachedDB.current = true
wwlog.Debug("Created cached object")
return
}
func (config *NodeYaml) DBDump() (buffer []byte) {
for _, n := range config.Nodes {
n.Flatten()
}
for _, p := range config.NodeProfiles {
p.Flatten()
}
buffer, err := yaml.Marshal(config)
if err != nil {
wwlog.Warn("porblems on dumping nodedb: %s", err)
}
return
}
/*
Get all the nodes of a configuration. This function also merges