Merge branch 'development' into RefactorFlags

This commit is contained in:
Christian Goll
2023-04-11 14:17:13 +02:00
committed by GitHub
34 changed files with 2342 additions and 629 deletions

View File

@@ -24,6 +24,7 @@ func FindAllNodeConfs() *wwapiv1.NodeYaml {
buffer, _ := yaml.Marshal(nodeMap)
retVal := wwapiv1.NodeYaml{
NodeConfMapYaml: string(buffer),
Hash: nodeDB.StringHash(),
}
return &retVal
}
@@ -42,6 +43,7 @@ func FilteredNodes(nodeList *wwapiv1.NodeList) *wwapiv1.NodeYaml {
buffer, _ := yaml.Marshal(nodeMap)
retVal := wwapiv1.NodeYaml{
NodeConfMapYaml: string(buffer),
Hash: nodeDB.StringHash(),
}
return &retVal
}

View File

@@ -0,0 +1,20 @@
package apinode
import (
"encoding/hex"
"github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1"
"github.com/hpcng/warewulf/internal/pkg/node"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
)
func Hash() *wwapiv1.NodeDBHash {
config, err := node.New()
if err != nil {
wwlog.Warn("couldb't read config")
}
hash := config.Hash()
return &wwapiv1.NodeDBHash{
Hash: hex.EncodeToString(hash[:]),
}
}

View File

@@ -29,8 +29,7 @@ func NodeList(nodeGet *wwapiv1.GetNodeList) (nodeList wwapiv1.NodeList, err erro
sort.Strings(nodeGet.Nodes)
if nodeGet.Type == wwapiv1.GetNodeList_Simple {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-22s %-26s %s", "NODE NAME", "PROFILES", "NETWORK"))
nodeList.Output = append(nodeList.Output, (strings.Repeat("=", 80)))
fmt.Sprintf("%s=%s=%s", "NODE NAME", "PROFILES", "NETWORK"))
for _, n := range node.FilterByName(nodes, nodeGet.Nodes) {
var netNames []string
for k := range n.NetDevs {
@@ -38,33 +37,31 @@ func NodeList(nodeGet *wwapiv1.GetNodeList) (nodeList wwapiv1.NodeList, err erro
}
sort.Strings(netNames)
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-22s %-26s %s", n.Id.Print(), n.Profiles.Print(), strings.Join(netNames, ", ")))
fmt.Sprintf("%s=%s=%s", n.Id.Print(), n.Profiles.Print(), strings.Join(netNames, ", ")))
}
} else if nodeGet.Type == wwapiv1.GetNodeList_Network {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-22s %-8s %-18s %-15s %-15s %-15s", "NODE NAME", "NAME", "HWADDR", "IPADDR", "GATEWAY", "DEVICE"),
strings.Repeat("=", 90))
fmt.Sprintf("%s=%s=%s=%s=%s=%s", "NODE NAME", "NAME", "HWADDR", "IPADDR", "GATEWAY", "DEVICE"))
for _, n := range node.FilterByName(nodes, nodeGet.Nodes) {
if len(n.NetDevs) > 0 {
for name := range n.NetDevs {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-22s %-8s %-18s %-15s %-15s %-15s", n.Id.Print(), name,
fmt.Sprintf("%s=%s=%s=%s=%s=%s", n.Id.Print(), name,
n.NetDevs[name].Hwaddr.Print(),
n.NetDevs[name].Ipaddr.Print(),
n.NetDevs[name].Gateway.Print(),
n.NetDevs[name].Device.Print()))
}
} else {
fmt.Printf("%-22s %-6s %-18s %-15s %-15s", n.Id.Print(), "--", "--", "--", "--")
fmt.Printf("%s=%s=%s=%s=%s=%s", n.Id.Print(), "--", "--", "--", "--", "--")
}
}
} else if nodeGet.Type == wwapiv1.GetNodeList_Ipmi {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-22s %-16s %-10s %-20s %-14s", "NODE NAME", "IPMI IPADDR", "IPMI PORT", "IPMI USERNAME", "IPMI INTERFACE"),
strings.Repeat("=", 98))
fmt.Sprintf("%s=%s=%s=%s=%s", "NODE NAME", "IPMI IPADDR", "IPMI PORT", "IPMI USERNAME", "IPMI INTERFACE"))
for _, n := range node.FilterByName(nodes, nodeGet.Nodes) {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-22s %-16s %-10s %-20s %-14s", n.Id.Print(),
fmt.Sprintf("%s=%s=%s=%s=%s", n.Id.Print(),
n.Ipmi.Ipaddr.Print(),
n.Ipmi.Port.Print(),
n.Ipmi.UserName.Print(),
@@ -72,19 +69,18 @@ func NodeList(nodeGet *wwapiv1.GetNodeList) (nodeList wwapiv1.NodeList, err erro
}
} else if nodeGet.Type == wwapiv1.GetNodeList_Long {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-22s %-16s %-16s %s", "NODE NAME", "KERNEL OVERRIDE", "CONTAINER", "OVERLAYS (S/R)"),
strings.Repeat("=", 85))
fmt.Sprintf("%s=%s=%s=%s", "NODE NAME", "KERNEL OVERRIDE", "CONTAINER", "OVERLAYS (S/R)"))
for _, n := range node.FilterByName(nodes, nodeGet.Nodes) {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-22s %-16s %-16s %s", n.Id.Print(),
fmt.Sprintf("%s=%s=%s=%s", n.Id.Print(),
n.Kernel.Override.Print(),
n.ContainerName.Print(),
n.SystemOverlay.Print()+"/"+n.RuntimeOverlay.Print()))
}
} else if nodeGet.Type == wwapiv1.GetNodeList_All {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%s=%s=%s=%s", "NODE", "FIELD", "PROFILE", "VALUE"))
for _, n := range node.FilterByName(nodes, nodeGet.Nodes) {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", "NODE", "FIELD", "PROFILE", "VALUE"), strings.Repeat("=", 85))
nType := reflect.TypeOf(n)
nVal := reflect.ValueOf(n)
nConfType := reflect.TypeOf(node.NodeConf{})
@@ -101,12 +97,12 @@ func NodeList(nodeGet *wwapiv1.GetNodeList) (nodeList wwapiv1.NodeList, err erro
fieldSource = entr.Source()
fieldVal = entr.Print()
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", n.Id.Print(), fieldName, fieldSource, fieldVal))
fmt.Sprintf("%s=%s=%s=%s", n.Id.Print(), fieldName, fieldSource, fieldVal))
} else if nType.Field(i).Type == reflect.TypeOf(map[string]*node.Entry{}) {
entrMap := nVal.Field(i).Interface().(map[string]*node.Entry)
for key, val := range entrMap {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", n.Id.Print(), key, val.Source(), val.Print()))
fmt.Sprintf("%s=%s=%s=%s", n.Id.Print(), key, val.Source(), val.Print()))
}
} else if nType.Field(i).Type == reflect.TypeOf(map[string]*node.NetDevEntry{}) {
netDevs := nVal.Field(i).Interface().(map[string]*node.NetDevEntry)
@@ -132,7 +128,7 @@ func NodeList(nodeGet *wwapiv1.GetNodeList) (nodeList wwapiv1.NodeList, err erro
// only print fields with lopt
if netConfField.Tag.Get("lopt") != "" {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", n.Id.Print(), fieldName, fieldSource, fieldVal))
fmt.Sprintf("%s=%s=%s=%s", n.Id.Print(), fieldName, fieldSource, fieldVal))
}
} else if netInfoType.Field(j).Type == reflect.TypeOf(map[string]*node.Entry{}) {
for key, val := range netInfoVal.Field(j).Interface().(map[string]*node.Entry) {
@@ -140,7 +136,7 @@ func NodeList(nodeGet *wwapiv1.GetNodeList) (nodeList wwapiv1.NodeList, err erro
fieldSource = val.Source()
fieldVal = val.Print()
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", n.Id.Print(), keyfieldName, fieldSource, fieldVal))
fmt.Sprintf("%s=%s=%s=%s", n.Id.Print(), keyfieldName, fieldSource, fieldVal))
}
}
@@ -162,14 +158,14 @@ func NodeList(nodeGet *wwapiv1.GetNodeList) (nodeList wwapiv1.NodeList, err erro
fieldSource = entr.Source()
fieldVal = entr.Print()
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", n.Id.Print(), fieldName, fieldSource, fieldVal))
fmt.Sprintf("%s=%s=%s=%s", n.Id.Print(), fieldName, fieldSource, fieldVal))
} else if nestInfoType.Elem().Field(j).Type == reflect.TypeOf(map[string]*node.Entry{}) {
for key, val := range nestInfoVal.Elem().Field(j).Interface().(map[string]*node.Entry) {
fieldName = fieldName + ":" + key
fieldSource = val.Source()
fieldVal = val.Print()
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", n.Id.Print(), fieldName, fieldSource, fieldVal))
fmt.Sprintf("%s=%s=%s=%s", n.Id.Print(), fieldName, fieldSource, fieldVal))
}
}
}
@@ -177,7 +173,6 @@ func NodeList(nodeGet *wwapiv1.GetNodeList) (nodeList wwapiv1.NodeList, err erro
}
}
}
return
}

View File

@@ -1,6 +1,7 @@
package apinode
import (
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
@@ -29,7 +30,10 @@ func NodeAdd(nap *wwapiv1.NodeAddParameter) (err error) {
if err != nil {
return errors.Wrap(err, "failed to open node database")
}
dbHash := nodeDB.Hash()
if hex.EncodeToString(dbHash[:]) != nap.Hash && !nap.Force {
return fmt.Errorf("got wrong hash, not modifying node database")
}
node_args := hostlist.Expand(nap.NodeNames)
var nodeConf node.NodeConf
err = yaml.Unmarshal([]byte(nap.NodeConfYaml), &nodeConf)
@@ -101,6 +105,10 @@ func NodeDelete(ndp *wwapiv1.NodeDeleteParameter) (err error) {
wwlog.Error("Failed to open node database: %s", err)
return
}
dbHash := nodeDB.Hash()
if hex.EncodeToString(dbHash[:]) != ndp.Hash && !ndp.Force {
return fmt.Errorf("got wrong hash, not modifying node database")
}
for _, n := range nodeList {
err := nodeDB.DelNode(n.Id.Get())
@@ -139,6 +147,13 @@ func NodeDeleteParameterCheck(ndp *wwapiv1.NodeDeleteParameter, console bool) (n
wwlog.Error("Failed to open node database: %s", err)
return
}
dbHash := nodeDB.Hash()
if hex.EncodeToString(dbHash[:]) != ndp.Hash && !ndp.Force {
wwlog.Debug("got hash: %s", ndp.Hash)
wwlog.Debug("actual hash: %s", hex.EncodeToString(dbHash[:]))
err = fmt.Errorf("got wrong hash, not modifying node database")
return
}
nodes, err := nodeDB.FindAllNodes()
if err != nil {

View File

@@ -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())
}
}

View File

@@ -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")
}

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"reflect"
"sort"
"strings"
"github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1"
"github.com/hpcng/warewulf/internal/pkg/node"
@@ -34,7 +33,7 @@ func ProfileList(ShowOpt *wwapiv1.GetProfileList) (profileList wwapiv1.ProfileLi
if ShowOpt.ShowAll {
for _, p := range profiles {
profileList.Output = append(profileList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", "PROFILE", "FIELD", "PROFILE", "VALUE"), strings.Repeat("=", 85))
fmt.Sprintf("%s=%s=%s=%s", "PROFILE", "FIELD", "PROFILE", "VALUE"))
nType := reflect.TypeOf(p)
nVal := reflect.ValueOf(p)
nConfType := reflect.TypeOf(node.NodeConf{})
@@ -51,12 +50,12 @@ func ProfileList(ShowOpt *wwapiv1.GetProfileList) (profileList wwapiv1.ProfileLi
fieldSource = entr.Source()
fieldVal = entr.Print()
profileList.Output = append(profileList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", p.Id.Print(), fieldName, fieldSource, fieldVal))
fmt.Sprintf("%s=%s=%s=%s", p.Id.Print(), fieldName, fieldSource, fieldVal))
} else if nType.Field(i).Type == reflect.TypeOf(map[string]*node.Entry{}) {
entrMap := nVal.Field(i).Interface().(map[string]*node.Entry)
for key, val := range entrMap {
profileList.Output = append(profileList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", p.Id.Print(), key, val.Source(), val.Print()))
fmt.Sprintf("%s=%s=%s=%s", p.Id.Print(), key, val.Source(), val.Print()))
}
} else if nType.Field(i).Type == reflect.TypeOf(map[string]*node.NetDevEntry{}) {
netDevs := nVal.Field(i).Interface().(map[string]*node.NetDevEntry)
@@ -82,7 +81,7 @@ func ProfileList(ShowOpt *wwapiv1.GetProfileList) (profileList wwapiv1.ProfileLi
// only print fields with lopt
if netConfField.Tag.Get("lopt") != "" {
profileList.Output = append(profileList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", p.Id.Print(), fieldName, fieldSource, fieldVal))
fmt.Sprintf("%s=%s=%s=%s", p.Id.Print(), fieldName, fieldSource, fieldVal))
}
} else if netInfoType.Field(j).Type == reflect.TypeOf(map[string]*node.Entry{}) {
for key, val := range netInfoVal.Field(j).Interface().(map[string]*node.Entry) {
@@ -90,7 +89,7 @@ func ProfileList(ShowOpt *wwapiv1.GetProfileList) (profileList wwapiv1.ProfileLi
fieldSource = val.Source()
fieldVal = val.Print()
profileList.Output = append(profileList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", p.Id.Print(), keyfieldName, fieldSource, fieldVal))
fmt.Sprintf("%s=%s=%s=%s", p.Id.Print(), keyfieldName, fieldSource, fieldVal))
}
}
@@ -112,14 +111,14 @@ func ProfileList(ShowOpt *wwapiv1.GetProfileList) (profileList wwapiv1.ProfileLi
fieldSource = entr.Source()
fieldVal = entr.Print()
profileList.Output = append(profileList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", p.Id.Print(), fieldName, fieldSource, fieldVal))
fmt.Sprintf("%s=%s=%s=%s", p.Id.Print(), fieldName, fieldSource, fieldVal))
} else if nestInfoType.Elem().Field(j).Type == reflect.TypeOf(map[string]*node.Entry{}) {
for key, val := range nestInfoVal.Elem().Field(j).Interface().(map[string]*node.Entry) {
fieldName = fieldName + ":" + key
fieldSource = val.Source()
fieldVal = val.Print()
profileList.Output = append(profileList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", p.Id.Print(), fieldName, fieldSource, fieldVal))
fmt.Sprintf("%s=%s=%s=%s", p.Id.Print(), fieldName, fieldSource, fieldVal))
}
}
}
@@ -128,12 +127,11 @@ func ProfileList(ShowOpt *wwapiv1.GetProfileList) (profileList wwapiv1.ProfileLi
}
} else {
profileList.Output = append(profileList.Output,
fmt.Sprintf("%-20s %s", "PROFILE NAME", "COMMENT/DESCRIPTION"))
profileList.Output = append(profileList.Output, strings.Repeat("=", 80))
fmt.Sprintf("%s=%s", "PROFILE NAME", "COMMENT/DESCRIPTION"))
for _, profile := range profiles {
profileList.Output = append(profileList.Output,
fmt.Sprintf("%-20s %s", profile.Id.Print(), profile.Comment.Print()))
fmt.Sprintf("%s=%s", profile.Id.Print(), profile.Comment.Print()))
}
}
return

View File

@@ -10,6 +10,12 @@ package wwapi.v1;
import "google/protobuf/empty.proto";
import "google/api/annotations.proto";
// Information about the database
message NodeDBHash {
string hash = 1;
}
// Container
// ContainerBuildParameter contains input for building zero or more containers.
@@ -133,9 +139,12 @@ message ProfileList {
}
// NodeAddParameter contains all input for adding a node to be managed by
// Warewulf.
// Warewulf. Only adds nodes if the hash matches the actual hash of the
// configuration.
message NodeAddParameter {
string nodeConfYaml = 1;
bool force = 2;
string hash = 3;
repeated string nodeNames = 10;
}
@@ -143,13 +152,17 @@ message NodeAddParameter {
// to nodes.conf (is resused for profile edit)
message NodeYaml {
string nodeConfMapYaml = 1;
string hash = 2;
}
// NodeDeleteParameter contains input for removing nodes from Warewulf
// management.
// management. If the given hash differs with the actual hash of the
// configuration, no node is deleted. The force option allows the deletion
// of nodes with a correct hash.
message NodeDeleteParameter {
bool force = 1;
repeated string nodeNames = 2;
string hash = 3;
}
// NodeSetParameter contains all fields for updating aspects of nodes managed

File diff suppressed because it is too large Load Diff