diff --git a/internal/app/wwctl/node/add/main.go b/internal/app/wwctl/node/add/main.go index 571783e3..2b63ad94 100644 --- a/internal/app/wwctl/node/add/main.go +++ b/internal/app/wwctl/node/add/main.go @@ -9,8 +9,8 @@ import ( ) func CobraRunE(cmd *cobra.Command, args []string) error { - OptionStrMap, haveNetname := apinode.AddNetname(OptionStrMap) - if !haveNetname { + OptionStrMap, netWithoutName := apinode.AddNetname(OptionStrMap) + if netWithoutName { return errors.New("a netname must be given for any network related configuration") } realMap := make(map[string]string) diff --git a/internal/app/wwctl/profile/add/main.go b/internal/app/wwctl/profile/add/main.go index fb502fd3..6871520b 100644 --- a/internal/app/wwctl/profile/add/main.go +++ b/internal/app/wwctl/profile/add/main.go @@ -13,8 +13,8 @@ import ( ) func CobraRunE(cmd *cobra.Command, args []string) (err error) { - OptionStrMap, haveNetname := apinode.AddNetname(OptionStrMap) - if !haveNetname { + OptionStrMap, netWithoutName := apinode.AddNetname(OptionStrMap) + if netWithoutName { return errors.New("a netname must be given for any network related configuration") } realMap := make(map[string]string) diff --git a/internal/app/wwctl/profile/set/main.go b/internal/app/wwctl/profile/set/main.go index e206c13f..b177a6b5 100644 --- a/internal/app/wwctl/profile/set/main.go +++ b/internal/app/wwctl/profile/set/main.go @@ -12,8 +12,8 @@ import ( ) func CobraRunE(cmd *cobra.Command, args []string) (err error) { - OptionStrMap, haveNetname := apinode.AddNetname(OptionStrMap) - if !haveNetname { + OptionStrMap, netWithoutName := apinode.AddNetname(OptionStrMap) + if netWithoutName { return errors.New("a netname must be given for any network related configuration") } realMap := make(map[string]string) diff --git a/internal/pkg/api/node/node.go b/internal/pkg/api/node/node.go index 5e7e902a..14263082 100644 --- a/internal/pkg/api/node/node.go +++ b/internal/pkg/api/node/node.go @@ -5,7 +5,6 @@ import ( "fmt" "net/http" "os" - "strings" "github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1" "github.com/hpcng/warewulf/internal/pkg/node" @@ -25,7 +24,6 @@ func NodeAdd(nap *wwapiv1.NodeAddParameter) (err error) { return fmt.Errorf("NodeAddParameter is nil") } - var count uint nodeDB, err := node.New() if err != nil { 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") } 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 - 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", - 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 - 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", - n.Id.Get(), nap.OptionsStrMap["ipmiaddr"]) + n.Id.Get(), nap.OptionsStrMap["Ipmi.Ipaddr"]) } // Now set all the rest 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") } - count++ } err = nodeDB.Persist() @@ -372,31 +370,3 @@ func NodeStatus(nodeNames []string) (nodeStatusResponse *wwapiv1.NodeStatusRespo } 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 -} diff --git a/internal/pkg/api/node/utils.go b/internal/pkg/api/node/utils.go index e2643f77..cf46d4bb 100644 --- a/internal/pkg/api/node/utils.go +++ b/internal/pkg/api/node/utils.go @@ -1,6 +1,8 @@ package apinode import ( + "strings" + "github.com/hpcng/warewulf/internal/pkg/node" "github.com/hpcng/warewulf/internal/pkg/warewulfd" "github.com/pkg/errors" @@ -23,3 +25,37 @@ func DbSave(nodeDB *node.NodeYaml) (err error) { } 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 == "" +}