Files
warewulf/internal/pkg/node/modifiers.go
Christian Goll 28a7b9fe84 removed NodeInfo and using NodeConf instead
This is a significant change in the undelying data model!

nodeDb, err := node.New()

will result in a structure which contains the on disk
values. Only

nodeDb.FindAllNodes() or nodeDb.GetNode(id) will give
the nodes with its merged in profiles.

Signed-off-by: Christian Goll <cgoll@suse.com>
2024-10-17 13:35:42 -04:00

104 lines
2.2 KiB
Go

package node
import (
"os"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
)
/*
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 {
return nil, errors.New("nodename already exists: " + nodeID)
} else {
config.Nodes[nodeID] = &newNode
}
return &newNode, nil
}
/*
delete node with the given id
*/
func (config *NodeYaml) DelNode(nodeID string) error {
if _, ok := config.Nodes[nodeID]; !ok {
return errors.New("nodename does not exist: " + nodeID)
}
wwlog.Verbose("Deleting node: %s", nodeID)
delete(config.Nodes, nodeID)
return nil
}
/*
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 {
return nil, errors.New("profile already exists: " + profileId)
} else {
config.NodeProfiles[profileId] = &profile
}
return &profile, nil
}
/*
delete node with the given id
*/
func (config *NodeYaml) DelProfile(nodeID string) error {
if _, ok := config.Nodes[nodeID]; !ok {
return errors.New("profile does not exist: " + nodeID)
}
wwlog.Verbose("deleting profile: %s", nodeID)
delete(config.Nodes, nodeID)
return nil
}
/*
Write the the NodeYaml to disk.
*/
func (config *NodeYaml) Persist() error {
out, dumpErr := config.Dump()
if dumpErr != nil {
wwlog.Error("%s", dumpErr)
os.Exit(1)
}
file, err := os.OpenFile(ConfigFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
wwlog.Error("%s", err)
os.Exit(1)
}
defer file.Close()
_, err = file.WriteString(string(out))
if err != nil {
return err
}
return nil
}
/*
Dump returns a YAML document representing the nodeDb
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 {
val.Flatten()
}
for _, val := range config.Nodes {
val.Flatten()
}
return yaml.Marshal(config)
}