hash checking for profile edit
Signed-off-by: Christian Goll <cgoll@suse.de>
This commit is contained in:
@@ -8,6 +8,8 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
apinode "github.com/hpcng/warewulf/internal/pkg/api/node"
|
||||
|
||||
apiprofile "github.com/hpcng/warewulf/internal/pkg/api/profile"
|
||||
"github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1"
|
||||
apiutil "github.com/hpcng/warewulf/internal/pkg/api/util"
|
||||
@@ -88,12 +90,19 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
if !yes {
|
||||
break
|
||||
}
|
||||
err = apiprofile.ProfileDelete(&wwapiv1.NodeDeleteParameter{NodeNames: nodeList, Force: true})
|
||||
err = apiprofile.ProfileDelete(&wwapiv1.NodeDeleteParameter{
|
||||
NodeNames: nodeList,
|
||||
Hash: profileListMsg.Hash,
|
||||
})
|
||||
if err != nil {
|
||||
wwlog.Verbose("Problem deleting nodes before modification %s")
|
||||
}
|
||||
buffer, _ = yaml.Marshal(modifiedProfileMap)
|
||||
err = apiprofile.ProfileAddFromYaml(&wwapiv1.NodeYaml{NodeConfMapYaml: string(buffer)})
|
||||
newHash := apinode.Hash()
|
||||
err = apiprofile.ProfileAddFromYaml(&wwapiv1.NodeAddParameter{
|
||||
NodeConfYaml: string(buffer),
|
||||
Hash: newHash.Hash,
|
||||
})
|
||||
if err != nil {
|
||||
wwlog.Error("Got following problem when writing back yaml: %s", err)
|
||||
os.Exit(1)
|
||||
|
||||
@@ -26,14 +26,16 @@ func ProfileDelete(ndp *wwapiv1.NodeDeleteParameter) (err error) {
|
||||
wwlog.Error("Failed to open node database: %s\n", err)
|
||||
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.Id.Get())
|
||||
if err != nil {
|
||||
wwlog.Error("%s\n", err)
|
||||
} else {
|
||||
//count++
|
||||
wwlog.Verbose("Deleting node: %s\n", p.Id.Print())
|
||||
wwlog.Verbose("Deleting profile: %s\n", p.Id.Print())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package apiprofile
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1"
|
||||
@@ -49,13 +50,17 @@ func FilteredProfiles(profileList *wwapiv1.NodeList) *wwapiv1.NodeYaml {
|
||||
/*
|
||||
Add profiles from yaml
|
||||
*/
|
||||
func ProfileAddFromYaml(nodeList *wwapiv1.NodeYaml) (err error) {
|
||||
func ProfileAddFromYaml(nodeList *wwapiv1.NodeAddParameter) (err error) {
|
||||
nodeDB, err := node.New()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Could not open NodeDB: %s\n")
|
||||
}
|
||||
if nodeDB.StringHash() != nodeList.Hash && !nodeList.Force {
|
||||
return fmt.Errorf("got wrong hash, not modifying profile database")
|
||||
}
|
||||
|
||||
profileMap := make(map[string]*node.NodeConf)
|
||||
err = yaml.Unmarshal([]byte(nodeList.NodeConfMapYaml), profileMap)
|
||||
err = yaml.Unmarshal([]byte(nodeList.NodeConfYaml), profileMap)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Could not unmarshall Yaml: %s\n")
|
||||
}
|
||||
|
||||
@@ -88,9 +88,11 @@ nodes:
|
||||
|
||||
t.Run("Same NodeYaml with same conf", func(t *testing.T) {
|
||||
var testConf NodeYaml
|
||||
yaml.Unmarshal([]byte(nodeConfYml1), &testConf)
|
||||
err = yaml.Unmarshal([]byte(nodeConfYml1), &testConf)
|
||||
assert.NoError(t, err)
|
||||
if testConf.Hash() != nodeConf1.Hash() {
|
||||
yaml.Unmarshal([]byte(nodeConfYml1), nodeConf1)
|
||||
err = yaml.Unmarshal([]byte(nodeConfYml1), nodeConf1)
|
||||
assert.NoError(t, err)
|
||||
t.Errorf("Hashes for same configuration differs: %x != %x", nodeConf1.Hash(), nodeConf1.Hash())
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user