Refactor wwctl <node|profile> edit and fix bugs
`wwctl node edit` appears to not be working properly since MergeNode. This refactor, partly towards #918, resolves the issues with `node edit` and updates `profile edit` to match. Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
@@ -1,75 +0,0 @@
|
||||
package apiprofile
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
|
||||
"github.com/warewulf/warewulf/internal/pkg/hostlist"
|
||||
"github.com/warewulf/warewulf/internal/pkg/node"
|
||||
"github.com/warewulf/warewulf/internal/pkg/warewulfd"
|
||||
"github.com/warewulf/warewulf/internal/pkg/wwlog"
|
||||
)
|
||||
|
||||
// ProfileDelete adds profile deletion for management by Warewulf.
|
||||
func ProfileDelete(ndp *wwapiv1.NodeDeleteParameter) (err error) {
|
||||
nodeDB, profileList, err := ProfileDeleteParameterCheck(ndp, false)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if nodeDB.StringHash() != ndp.Hash && !ndp.Force {
|
||||
return fmt.Errorf("got wrong hash, not modifying profile database")
|
||||
}
|
||||
for _, p := range profileList {
|
||||
err = nodeDB.DelProfile(p)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
err = nodeDB.Persist()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to persist nodedb: %w", err)
|
||||
}
|
||||
err = warewulfd.DaemonReload()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to reload warewulf daemon: %w", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// ProfileDeleteParameterCheck does error checking on ProfileDeleteParameter.
|
||||
// Output to the console if console is true.
|
||||
// Returns the profiles to delete.
|
||||
func ProfileDeleteParameterCheck(ndp *wwapiv1.NodeDeleteParameter, console bool) (nodeDB node.NodesYaml, profileList []string, err error) {
|
||||
|
||||
if ndp == nil {
|
||||
err = fmt.Errorf("profileDeleteParameter is nil")
|
||||
return
|
||||
}
|
||||
|
||||
nodeDB, err = node.New()
|
||||
if err != nil {
|
||||
wwlog.Error("failed to open node database: %s\n", err)
|
||||
return
|
||||
}
|
||||
profileList = nodeDB.ListAllProfiles()
|
||||
profileArgs := hostlist.Expand(ndp.NodeNames)
|
||||
for _, r := range profileArgs {
|
||||
match := false
|
||||
for _, p := range profileList {
|
||||
if p == r {
|
||||
profileList = append(profileList, p)
|
||||
match = true
|
||||
}
|
||||
}
|
||||
|
||||
if !match {
|
||||
wwlog.Error("no match for profile: %s", r)
|
||||
}
|
||||
}
|
||||
|
||||
if len(profileList) == 0 {
|
||||
wwlog.Warn("no profiles found\n")
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
package apiprofile
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
|
||||
"github.com/warewulf/warewulf/internal/pkg/node"
|
||||
"github.com/warewulf/warewulf/internal/pkg/wwlog"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
/*
|
||||
Returns filtered list of nodes
|
||||
*/
|
||||
func FilteredProfiles(profileList *wwapiv1.NodeList) *wwapiv1.NodeYaml {
|
||||
nodeDB, err := node.New()
|
||||
if err != nil {
|
||||
wwlog.Error("Could not open nodeDB: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
profiles, _ := nodeDB.FindAllProfiles()
|
||||
profiles = node.FilterProfileListByName(profiles, profileList.Output)
|
||||
buffer, _ := yaml.Marshal(profiles)
|
||||
retVal := wwapiv1.NodeYaml{
|
||||
NodeConfMapYaml: string(buffer),
|
||||
}
|
||||
return &retVal
|
||||
}
|
||||
|
||||
/*
|
||||
Add profiles from yaml
|
||||
*/
|
||||
func ProfileAddFromYaml(nodeList *wwapiv1.NodeAddParameter) (err error) {
|
||||
nodeDB, err := node.New()
|
||||
if err != nil {
|
||||
return fmt.Errorf("couldn't open NodeDB: %w", err)
|
||||
}
|
||||
if nodeDB.StringHash() != nodeList.Hash && !nodeList.Force {
|
||||
return fmt.Errorf("got wrong hash, not modifying profile database")
|
||||
}
|
||||
|
||||
profileMap := make(map[string]*node.Profile)
|
||||
err = yaml.Unmarshal([]byte(nodeList.NodeConfYaml), profileMap)
|
||||
if err != nil {
|
||||
return fmt.Errorf("couldn't unmarshall Yaml: %w", err)
|
||||
}
|
||||
for profileName, profile := range profileMap {
|
||||
err = nodeDB.SetProfile(profileName, *profile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("couldn't set profile: %w", err)
|
||||
}
|
||||
}
|
||||
err = nodeDB.Persist()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to persist nodedb: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user