Files
warewulf/internal/pkg/node/modifiers.go
Jonathon Anderson 081d2ec61e Update linter for golang v1.25 compatibility
Signed-off-by: Jonathon Anderson <janderson@ciq.com>
2026-03-11 09:32:49 +01:00

155 lines
3.4 KiB
Go

package node
import (
"bytes"
"encoding/gob"
"os"
"github.com/pkg/errors"
warewulfconf "github.com/warewulf/warewulf/internal/pkg/config"
"github.com/warewulf/warewulf/internal/pkg/util"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
)
/*
Add a node with the given ID and return a pointer to it
*/
func (config *NodesYaml) AddNode(nodeID string) (*Node, 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 *NodesYaml) 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
}
/*
set node for the node with id the values of vals
*/
func (config *NodesYaml) SetNode(nodeID string, vals Node) 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 *NodesYaml) SetProfile(profileId string, vals Profile) 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 *NodesYaml) AddProfile(profileId string) (*Profile, error) {
profile := EmptyProfile()
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 profile with the given id
*/
func (config *NodesYaml) DelProfile(profileID string) error {
if _, ok := config.NodeProfiles[profileID]; !ok {
return errors.New("profile does not exist: " + profileID)
}
wwlog.Verbose("deleting profile: %s", profileID)
delete(config.NodeProfiles, profileID)
return nil
}
/*
Write the the NodeYaml to disk.
*/
func (config *NodesYaml) Persist() error {
return config.PersistToFile(warewulfconf.Get().Paths.NodesConf())
}
func (config *NodesYaml) PersistToFile(configFile string) (err error) {
if configFile == "" {
configFile = warewulfconf.Get().Paths.NodesConf()
}
out, dumpErr := config.Dump()
if dumpErr != nil {
wwlog.Error("%s", dumpErr)
return dumpErr
}
file, err := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
wwlog.Error("%s", err)
return err
}
defer func() {
if cerr := file.Close(); cerr != nil && err == nil {
err = cerr
}
}()
_, err = file.WriteString(string(out))
if err != nil {
return err
}
wwlog.Debug("persisted: %s", configFile)
return nil
}
// Dump returns a YAML document representing the nodeDb instance. Passes through any errors
// generated by yaml encoding.
func (config *NodesYaml) Dump() ([]byte, error) {
// flatten out profiles and nodes
for _, val := range config.NodeProfiles {
val.Flatten()
}
for _, val := range config.Nodes {
val.Flatten()
}
return util.EncodeYaml(config)
}