Fix Nodeadd with ipaddr count

This commit is contained in:
Christian Goll
2022-07-20 15:14:30 +02:00
parent 99e9316011
commit d252b974bf
5 changed files with 49 additions and 43 deletions

View File

@@ -9,8 +9,8 @@ import (
) )
func CobraRunE(cmd *cobra.Command, args []string) error { func CobraRunE(cmd *cobra.Command, args []string) error {
OptionStrMap, haveNetname := apinode.AddNetname(OptionStrMap) OptionStrMap, netWithoutName := apinode.AddNetname(OptionStrMap)
if !haveNetname { if netWithoutName {
return errors.New("a netname must be given for any network related configuration") return errors.New("a netname must be given for any network related configuration")
} }
realMap := make(map[string]string) realMap := make(map[string]string)

View File

@@ -13,8 +13,8 @@ import (
) )
func CobraRunE(cmd *cobra.Command, args []string) (err error) { func CobraRunE(cmd *cobra.Command, args []string) (err error) {
OptionStrMap, haveNetname := apinode.AddNetname(OptionStrMap) OptionStrMap, netWithoutName := apinode.AddNetname(OptionStrMap)
if !haveNetname { if netWithoutName {
return errors.New("a netname must be given for any network related configuration") return errors.New("a netname must be given for any network related configuration")
} }
realMap := make(map[string]string) realMap := make(map[string]string)

View File

@@ -12,8 +12,8 @@ import (
) )
func CobraRunE(cmd *cobra.Command, args []string) (err error) { func CobraRunE(cmd *cobra.Command, args []string) (err error) {
OptionStrMap, haveNetname := apinode.AddNetname(OptionStrMap) OptionStrMap, netWithoutName := apinode.AddNetname(OptionStrMap)
if !haveNetname { if netWithoutName {
return errors.New("a netname must be given for any network related configuration") return errors.New("a netname must be given for any network related configuration")
} }
realMap := make(map[string]string) realMap := make(map[string]string)

View File

@@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
"strings"
"github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1" "github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1"
"github.com/hpcng/warewulf/internal/pkg/node" "github.com/hpcng/warewulf/internal/pkg/node"
@@ -25,7 +24,6 @@ func NodeAdd(nap *wwapiv1.NodeAddParameter) (err error) {
return fmt.Errorf("NodeAddParameter is nil") return fmt.Errorf("NodeAddParameter is nil")
} }
var count uint
nodeDB, err := node.New() nodeDB, err := node.New()
if err != nil { if err != nil {
return errors.Wrap(err, "failed to open node database") return errors.Wrap(err, "failed to open node database")
@@ -40,19 +38,20 @@ func NodeAdd(nap *wwapiv1.NodeAddParameter) (err error) {
return errors.Wrap(err, "failed to add node") return errors.Wrap(err, "failed to add node")
} }
wwlog.Printf(wwlog.INFO, "Added node: %s\n", a) wwlog.Printf(wwlog.INFO, "Added node: %s\n", a)
if nap.OptionsStrMap["ipaddr"] != "" { netName := nap.OptionsStrMap["NetDevs"]
if nap.OptionsStrMap["NetDevs."+netName+".Ipaddr"] != "" {
// if more nodes are added increment IPv4 address // if more nodes are added increment IPv4 address
nap.OptionsStrMap["ipaddr"] = util.IncrementIPv4(nap.OptionsStrMap["ipaddr"], count) nap.OptionsStrMap["NetDevs."+netName+".Ipaddr"] = util.IncrementIPv4(nap.OptionsStrMap["NetDevs."+netName+".Ipaddr"], 1)
wwlog.Verbose("Node: %s:%s, Setting Ipaddr to: %s\n", wwlog.Verbose("Node: %s:%s, Setting Ipaddr to: %s\n",
n.Id.Get(), nap.OptionsStrMap["netname"], nap.OptionsStrMap["ipaddr"]) n.Id.Get(), nap.OptionsStrMap["NetDevs"], nap.OptionsStrMap["NetDevs."+netName+".Ipaddr"])
} }
if nap.OptionsStrMap["ipmiaddr"] != "" { if nap.OptionsStrMap["Ipmi.Ipaddr"] != "" {
// if more nodes are added increment IPv4 address // if more nodes are added increment IPv4 address
nap.OptionsStrMap["ipmiaddr"] = util.IncrementIPv4(nap.OptionsStrMap["ipmiaddr"], count) nap.OptionsStrMap["Ipmi.Ipaddr"] = util.IncrementIPv4(nap.OptionsStrMap["Ipmi.Ipaddr"], 1)
wwlog.Verbose("Node: %s:, Setting IPMIIpaddr to: %s\n", wwlog.Verbose("Node: %s:, Setting IPMIIpaddr to: %s\n",
n.Id.Get(), nap.OptionsStrMap["ipmiaddr"]) n.Id.Get(), nap.OptionsStrMap["Ipmi.Ipaddr"])
} }
// Now set all the rest // Now set all the rest
for key, val := range nap.OptionsStrMap { for key, val := range nap.OptionsStrMap {
@@ -67,7 +66,6 @@ func NodeAdd(nap *wwapiv1.NodeAddParameter) (err error) {
return errors.Wrap(err, "failed to update nodedb") return errors.Wrap(err, "failed to update nodedb")
} }
count++
} }
err = nodeDB.Persist() err = nodeDB.Persist()
@@ -372,31 +370,3 @@ func NodeStatus(nodeNames []string) (nodeStatusResponse *wwapiv1.NodeStatusRespo
} }
return return
} }
/*
Add the netname to the options map, as its only known after the map
command line options have been read out.
*/
func AddNetname(theMap map[string]*string) (map[string]*string, bool) {
foundNetname := false
netname := ""
retMap := make(map[string]*string)
for key, val := range theMap {
if key == "NetDevs" {
foundNetname = true
netname = *val
}
}
if foundNetname {
for key, val := range theMap {
keys := strings.Split(key, ".")
myVal := *val
if len(keys) >= 2 && keys[0] == "NetDevs" {
retMap[keys[0]+"."+netname+"."+strings.Join(keys[1:], ".")] = &myVal
} else {
retMap[key] = &myVal
}
}
}
return retMap, foundNetname
}

View File

@@ -1,6 +1,8 @@
package apinode package apinode
import ( import (
"strings"
"github.com/hpcng/warewulf/internal/pkg/node" "github.com/hpcng/warewulf/internal/pkg/node"
"github.com/hpcng/warewulf/internal/pkg/warewulfd" "github.com/hpcng/warewulf/internal/pkg/warewulfd"
"github.com/pkg/errors" "github.com/pkg/errors"
@@ -23,3 +25,37 @@ func DbSave(nodeDB *node.NodeYaml) (err error) {
} }
return return
} }
/*
Add the netname to the options map, as its only known after the
command line options have been read out, but its needing for setting
the values. Returns the manipulated map and a bool which is true if
networks specific values were set, but no netname was given.
*/
func AddNetname(theMap map[string]*string) (map[string]*string, bool) {
netname := ""
netvalues := false
retMap := make(map[string]*string)
for key, val := range theMap {
if key == "NetDevs" && *val != "" {
netname = *val
}
}
for key, val := range theMap {
keys := strings.Split(key, ".")
myVal := *val
if len(keys) >= 2 && keys[0] == "NetDevs" {
if *val != "" {
netvalues = true
}
if netname != "" {
retMap[keys[0]+"."+netname+"."+strings.Join(keys[1:], ".")] = &myVal
}
} else {
retMap[key] = &myVal
}
}
return retMap, netvalues && netname == ""
}