use yaml/v3 and don't export Nodes

introduced wwbool and don't export
Nodes and NodeConfs. This requires new
explict Yaml (un)marshaling as the standard
marshaller won't touch these fields

Signed-off-by: Christian Goll <cgoll@suse.com>
This commit is contained in:
Christian Goll
2023-12-20 15:50:15 +01:00
committed by Jonathon Anderson
parent ac6cd69ca6
commit 5bd4fd4712
12 changed files with 470 additions and 196 deletions

View File

@@ -1,10 +1,12 @@
package node
import (
"bytes"
"encoding/gob"
"os"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
)
@@ -15,10 +17,10 @@ Add a node with the given ID and return a pointer to it
func (config *NodeYaml) AddNode(nodeID string) (*NodeConf, error) {
newNode := NewNode(nodeID)
wwlog.Verbose("Adding new node: %s", nodeID)
if _, ok := config.Nodes[nodeID]; ok {
if _, ok := config.nodes[nodeID]; ok {
return nil, errors.New("nodename already exists: " + nodeID)
} else {
config.Nodes[nodeID] = &newNode
config.nodes[nodeID] = &newNode
}
return &newNode, nil
}
@@ -27,26 +29,64 @@ func (config *NodeYaml) AddNode(nodeID string) (*NodeConf, error) {
delete node with the given id
*/
func (config *NodeYaml) DelNode(nodeID string) error {
if _, ok := config.Nodes[nodeID]; !ok {
if _, ok := config.nodes[nodeID]; !ok {
return errors.New("nodename does not exist: " + nodeID)
}
wwlog.Verbose("Deleting node: %s", nodeID)
delete(config.Nodes, nodeID)
delete(config.nodes, nodeID)
return nil
}
/*
set node for the node with id the values of vals
*/
func (config *NodeYaml) SetNode(nodeID string, vals NodeConf) error {
node, ok := config.nodes[nodeID]
if !ok {
return ErrNotFound
}
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
dec := gob.NewDecoder(&buf)
err := enc.Encode(vals)
if err != nil {
return err
}
err = dec.Decode(node)
return err
}
/*
set profile for the node with id the values of vals
*/
func (config *NodeYaml) SetProfile(profileId string, vals ProfileConf) error {
profile, ok := config.nodeProfiles[profileId]
if !ok {
return ErrNotFound
}
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
dec := gob.NewDecoder(&buf)
err := enc.Encode(vals)
if err != nil {
return err
}
err = dec.Decode(profile)
return err
}
/*
Add a node with the given ID and return a pointer to it
*/
func (config *NodeYaml) AddProfile(profileId string) (*ProfileConf, error) {
profile := NewProfile(profileId)
wwlog.Verbose("adding new profile: %s", profileId)
if _, ok := config.NodeProfiles[profileId]; ok {
if _, ok := config.nodeProfiles[profileId]; ok {
return nil, errors.New("profile already exists: " + profileId)
} else {
config.NodeProfiles[profileId] = &profile
config.nodeProfiles[profileId] = &profile
}
return &profile, nil
}
@@ -55,12 +95,12 @@ func (config *NodeYaml) AddProfile(profileId string) (*ProfileConf, error) {
delete node with the given id
*/
func (config *NodeYaml) DelProfile(nodeID string) error {
if _, ok := config.Nodes[nodeID]; !ok {
if _, ok := config.nodes[nodeID]; !ok {
return errors.New("profile does not exist: " + nodeID)
}
wwlog.Verbose("deleting profile: %s", nodeID)
delete(config.Nodes, nodeID)
delete(config.nodes, nodeID)
return nil
}
@@ -84,6 +124,7 @@ func (config *NodeYaml) Persist() error {
if err != nil {
return err
}
wwlog.Debug("persisted: %s", ConfigFile)
return nil
}
@@ -93,11 +134,16 @@ instance. Passes through any errors generated by yaml.Marshal.
*/
func (config *NodeYaml) Dump() ([]byte, error) {
// flatten out profiles and nodes
for _, val := range config.NodeProfiles {
for _, val := range config.nodeProfiles {
val.Flatten()
}
for _, val := range config.Nodes {
for _, val := range config.nodes {
val.Flatten()
}
return yaml.Marshal(config)
var buf bytes.Buffer
// Run through encoder
yamlEncoder := yaml.NewEncoder(&buf)
yamlEncoder.SetIndent(2)
err := yamlEncoder.Encode(config)
return buf.Bytes(), err //yaml.Marshal(config)
}