add profile edit (copy&paste) from node edit

This commit is contained in:
Christian Goll
2022-09-19 15:58:03 +02:00
parent 3cc9e12e69
commit 8e0fd64c7e
7 changed files with 347 additions and 11 deletions

View File

@@ -0,0 +1,94 @@
package apiprofile
import (
"fmt"
"os"
"github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1"
"github.com/hpcng/warewulf/internal/pkg/node"
"github.com/hpcng/warewulf/internal/pkg/warewulfd"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
"github.com/hpcng/warewulf/pkg/hostlist"
"github.com/pkg/errors"
)
// ProfileDelete adds profile deletion for management by Warewulf.
func ProfileDelete(ndp *wwapiv1.NodeDeleteParameter) (err error) {
var profileList []node.NodeInfo
profileList, err = ProfileDeleteParameterCheck(ndp, false)
if err != nil {
return
}
nodeDB, err := node.New()
if err != nil {
wwlog.Error("Failed to open node database: %s\n", err)
return
}
for _, p := range profileList {
err := nodeDB.DelProfile(p.Id.Get())
if err != nil {
wwlog.Error("%s\n", err)
} else {
//count++
wwlog.Verbose("Deleting node: %s\n", p.Id.Print())
}
}
err = nodeDB.Persist()
if err != nil {
return errors.Wrap(err, "failed to persist nodedb")
}
err = warewulfd.DaemonReload()
if err != nil {
return errors.Wrap(err, "failed to reload warewulf daemon")
}
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) (profileList []node.NodeInfo, 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
}
profiles, err := nodeDB.FindAllProfiles()
if err != nil {
wwlog.Error("Could not get node list: %s\n", err)
return
}
node_args := hostlist.Expand(ndp.NodeNames)
for _, r := range node_args {
var match bool
for _, p := range profiles {
if p.Id.Get() == r {
profileList = append(profileList, p)
match = true
}
}
if !match {
fmt.Fprintf(os.Stderr, "ERROR: No match for node: %s\n", r)
}
}
if len(profileList) == 0 {
fmt.Printf("No s found\n")
}
return
}

View File

@@ -0,0 +1,70 @@
package apiprofile
import (
"os"
"github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1"
"github.com/hpcng/warewulf/internal/pkg/node"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
/*
Returns the nodes as a yaml string
*/
func FindAllProfileConfs() *wwapiv1.NodeYaml {
nodeDB, err := node.New()
if err != nil {
wwlog.Error("Could not open nodeDB: %s\n", err)
os.Exit(1)
}
profileMap := nodeDB.NodeProfiles
// ignore err as nodeDB should always be correct
buffer, _ := yaml.Marshal(profileMap)
retVal := wwapiv1.NodeYaml{
NodeConfMapYaml: string(buffer),
}
return &retVal
}
/*
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)
}
profileMap := nodeDB.NodeProfiles
profileMap = node.FilterMapByName(profileMap, profileList.Output)
buffer, _ := yaml.Marshal(profileMap)
retVal := wwapiv1.NodeYaml{
NodeConfMapYaml: string(buffer),
}
return &retVal
}
/*
Add profiles from yaml
*/
func ProfileAddFromYaml(nodeList *wwapiv1.NodeYaml) (err error) {
nodeDB, err := node.New()
if err != nil {
return errors.Wrap(err, "Could not open NodeDB: %s\n")
}
profileMap := make(map[string]*node.NodeConf)
err = yaml.Unmarshal([]byte(nodeList.NodeConfMapYaml), profileMap)
if err != nil {
return errors.Wrap(err, "Could not unmarshall Yaml: %s\n")
}
for profileName, profile := range profileMap {
nodeDB.NodeProfiles[profileName] = profile
}
err = nodeDB.Persist()
if err != nil {
return errors.Wrap(err, "failed to persist nodedb")
}
return nil
}

View File

@@ -25,8 +25,10 @@ func ConfirmationPrompt(label string) (yes bool) {
return
}
func CanWriteConfig() (canwrite wwapiv1.CanWriteConfig) {
/*
Simple check if the config can be written in case wwctl isn't run as root
*/
func CanWriteConfig() (canwrite *wwapiv1.CanWriteConfig) {
err := syscall.Access(node.ConfigFile, syscall.O_RDWR)
if err != nil {
wwlog.Warn("Couldn't open %s:%s", node.ConfigFile, err)