From 255ff61dc8fcbe753301591f12ccf525348287f3 Mon Sep 17 00:00:00 2001 From: Christian Goll Date: Wed, 31 Aug 2022 15:42:57 +0200 Subject: [PATCH] adding netname option back --- internal/app/wwctl/node/add/main.go | 17 ++++++++--------- internal/app/wwctl/node/add/root.go | 9 ++++++--- internal/app/wwctl/node/set/main.go | 17 ++++++++--------- internal/app/wwctl/node/set/root.go | 8 +++++--- internal/app/wwctl/profile/add/main.go | 17 ++++++++--------- internal/app/wwctl/profile/add/root.go | 8 +++++--- internal/app/wwctl/profile/set/main.go | 15 ++++++++++----- internal/app/wwctl/profile/set/root.go | 8 +++++--- internal/pkg/node/datastructure.go | 8 ++++---- internal/pkg/node/methods.go | 2 ++ internal/pkg/node/transformers.go | 11 +++++++---- 11 files changed, 68 insertions(+), 52 deletions(-) diff --git a/internal/app/wwctl/node/add/main.go b/internal/app/wwctl/node/add/main.go index 4837809d..762907bf 100644 --- a/internal/app/wwctl/node/add/main.go +++ b/internal/app/wwctl/node/add/main.go @@ -12,16 +12,15 @@ import ( ) func CobraRunE(cmd *cobra.Command, args []string) error { - // 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) + // remove the default network as the all network values are assigned + // to this network + if NetName != "" { + netDev := *NodeConf.NetDevs["default"] + NodeConf.NetDevs[NetName] = &netDev + delete(NodeConf.NetDevs, "default") - // for key, val := range OptionStrMap { - // realMap[key] = *val - // } - buffer, err := yaml.Marshal(nodeConf) + } + buffer, err := yaml.Marshal(NodeConf) if err != nil { wwlog.Error("Cant marshall nodeInfo", err) os.Exit(1) diff --git a/internal/app/wwctl/node/add/root.go b/internal/app/wwctl/node/add/root.go index 18dee9ae..76297f9a 100644 --- a/internal/app/wwctl/node/add/root.go +++ b/internal/app/wwctl/node/add/root.go @@ -19,12 +19,15 @@ var ( RunE: CobraRunE, Args: cobra.MinimumNArgs(1), } - nodeConf node.NodeConf + NodeConf node.NodeConf + NetName string ) func init() { - nodeConf := node.NewConf() - nodeConf.CreateFlags(baseCmd, []string{}) + NodeConf = node.NewConf() + NodeConf.CreateFlags(baseCmd, []string{}) + baseCmd.PersistentFlags().StringVar(&NetName, "netname", "", "Set network name for network options") + // register the command line completions if err := baseCmd.RegisterFlagCompletionFunc("container", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { list, _ := container.ListSources() diff --git a/internal/app/wwctl/node/set/main.go b/internal/app/wwctl/node/set/main.go index c7c6be47..e71a5d10 100644 --- a/internal/app/wwctl/node/set/main.go +++ b/internal/app/wwctl/node/set/main.go @@ -13,16 +13,15 @@ import ( ) func CobraRunE(cmd *cobra.Command, args []string) (err error) { - // 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) + // remove the default network as the all network values are assigned + // to this network + if NetName != "" { + netDev := *NodeConf.NetDevs["default"] + NodeConf.NetDevs[NetName] = &netDev + delete(NodeConf.NetDevs, "default") - // for key, val := range OptionStrMap { - // realMap[key] = *val - // } - buffer, err := yaml.Marshal(nodeConf) + } + buffer, err := yaml.Marshal(NodeConf) if err != nil { wwlog.Error("Cant marshall nodeInfo", err) os.Exit(1) diff --git a/internal/app/wwctl/node/set/root.go b/internal/app/wwctl/node/set/root.go index 3e0a4675..b3087897 100644 --- a/internal/app/wwctl/node/set/root.go +++ b/internal/app/wwctl/node/set/root.go @@ -33,16 +33,18 @@ var ( }, } SetNetDevDel string + NetName string SetNodeAll bool SetYes bool SetForce bool - nodeConf node.NodeConf + NodeConf node.NodeConf ) func init() { - nodeConf := node.NewConf() - nodeConf.CreateFlags(baseCmd, []string{}) + NodeConf = node.NewConf() + NodeConf.CreateFlags(baseCmd, []string{}) baseCmd.PersistentFlags().StringVarP(&SetNetDevDel, "netdel", "D", "", "Delete the node's network device") + baseCmd.PersistentFlags().StringVar(&NetName, "netname", "", "Set network name for network options") baseCmd.PersistentFlags().BoolVarP(&SetNodeAll, "all", "a", false, "Set all nodes") baseCmd.PersistentFlags().BoolVarP(&SetYes, "yes", "y", false, "Set 'yes' to all questions asked") baseCmd.PersistentFlags().BoolVarP(&SetForce, "force", "f", false, "Force configuration (even on error)") diff --git a/internal/app/wwctl/profile/add/main.go b/internal/app/wwctl/profile/add/main.go index 3e689a3d..0260f232 100644 --- a/internal/app/wwctl/profile/add/main.go +++ b/internal/app/wwctl/profile/add/main.go @@ -14,16 +14,15 @@ import ( ) func CobraRunE(cmd *cobra.Command, args []string) (err error) { - // 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) + // remove the default network as the all network values are assigned + // to this network + if NetName != "" { + netDev := *ProfileConf.NetDevs["default"] + ProfileConf.NetDevs[NetName] = &netDev + delete(ProfileConf.NetDevs, "default") - // for key, val := range OptionStrMap { - // realMap[key] = *val - // } - buffer, err := yaml.Marshal(profileConf) + } + buffer, err := yaml.Marshal(ProfileConf) if err != nil { wwlog.Error("Cant marshall nodeInfo", err) os.Exit(1) diff --git a/internal/app/wwctl/profile/add/root.go b/internal/app/wwctl/profile/add/root.go index a7e60b26..14694477 100644 --- a/internal/app/wwctl/profile/add/root.go +++ b/internal/app/wwctl/profile/add/root.go @@ -23,14 +23,16 @@ var ( SetNodeAll bool SetYes bool SetForce bool - profileConf node.NodeConf + NetName string + ProfileConf node.NodeConf ) // GetRootCommand returns the root cobra.Command for the application. func GetCommand() *cobra.Command { - profileConf = node.NewConf() - profileConf.CreateFlags(baseCmd, + ProfileConf = node.NewConf() + ProfileConf.CreateFlags(baseCmd, []string{"ipaddr", "ipaddr6", "ipmiaddr", "profile"}) + baseCmd.PersistentFlags().StringVar(&NetName, "netname", "", "Set network name for network options") // register the command line completions if err := baseCmd.RegisterFlagCompletionFunc("container", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { list, _ := container.ListSources() diff --git a/internal/app/wwctl/profile/set/main.go b/internal/app/wwctl/profile/set/main.go index 43e0446a..d0d8dcfd 100644 --- a/internal/app/wwctl/profile/set/main.go +++ b/internal/app/wwctl/profile/set/main.go @@ -13,11 +13,16 @@ import ( ) func CobraRunE(cmd *cobra.Command, args []string) (err error) { - // OptionStrMap, netWithoutName := apinode.AddNetname(OptionStrMap) - // if netWithoutName { - // return errors.New("a netname must be given for any network related configuration") - // } - buffer, err := yaml.Marshal(profileConf) + + // remove the default network as the all network values are assigned + // to this network + if NetName != "" { + netDev := *ProfileConf.NetDevs["default"] + ProfileConf.NetDevs[NetName] = &netDev + delete(ProfileConf.NetDevs, "default") + + } + buffer, err := yaml.Marshal(ProfileConf) if err != nil { wwlog.Error("Cant marshall nodeInfo", err) os.Exit(1) diff --git a/internal/app/wwctl/profile/set/root.go b/internal/app/wwctl/profile/set/root.go index 577e0312..807d2367 100644 --- a/internal/app/wwctl/profile/set/root.go +++ b/internal/app/wwctl/profile/set/root.go @@ -36,13 +36,15 @@ var ( SetNodeAll bool SetYes bool SetForce bool - profileConf node.NodeConf + NetName string + ProfileConf node.NodeConf ) func init() { - profileConf = node.NewConf() - profileConf.CreateFlags(baseCmd, + ProfileConf = node.NewConf() + ProfileConf.CreateFlags(baseCmd, []string{"ipaddr", "ipaddr6", "ipmiaddr", "profile"}) + baseCmd.PersistentFlags().StringVar(&NetName, "netname", "", "Set network name for network options") baseCmd.PersistentFlags().StringVarP(&SetNetDevDel, "netdel", "D", "", "Delete the node's network device") baseCmd.PersistentFlags().BoolVarP(&SetNodeAll, "all", "a", false, "Set all nodes") baseCmd.PersistentFlags().BoolVarP(&SetYes, "yes", "y", false, "Set 'yes' to all questions asked") diff --git a/internal/pkg/node/datastructure.go b/internal/pkg/node/datastructure.go index 2e259a5b..6efc2959 100644 --- a/internal/pkg/node/datastructure.go +++ b/internal/pkg/node/datastructure.go @@ -49,8 +49,8 @@ type NodeConf struct { Profiles []string `yaml:"profiles,omitempty" lopt:"profile" sopt:"P" comment:"Set the node's profile members (comma separated)"` NetDevs map[string]*NetDevs `yaml:"network devices,omitempty"` Tags map[string]string `yaml:"tags,omitempty" lopt:"tagadd" comment:"base key"` - TagsDel []string `lopt:"tagdel" comment:"remove this tags"` // does not go to disk only to wire - Keys map[string]string `yaml:"keys,omitempty"` // Reverse compatibility + TagsDel []string `yaml:"tagsdel,omitempty" lopt:"tagdel" comment:"remove this tags"` // should not go to disk only to wire + Keys map[string]string `yaml:"keys,omitempty"` // Reverse compatibility } type IpmiConf struct { @@ -63,7 +63,7 @@ type IpmiConf struct { Interface string `yaml:"interface,omitempty" lopt:"ipmiinterface" comment:"Set the node's IPMI interface (defaults: 'lan')"` Write string `yaml:"write,omitempty" lopt:"ipmiwrite" comment:"Enable the write of impi configuration (yes/no)"` Tags map[string]string `yaml:"tags,omitempty" lopt:"ipmitagadd" comment:"add ipmitags"` - TagsDel []string `lopt:"ipmitagdel" comment:"remove ipmitags"` // does not go to disk only to wire + TagsDel []string `yaml:"tagsdel,omitempty" lopt:"ipmitagdel" comment:"remove ipmitags"` // should not go to disk only to wire } type KernelConf struct { Version string `yaml:"version,omitempty"` @@ -85,7 +85,7 @@ type NetDevs struct { Primary string `yaml:"primary,omitempty" lopt:"primary" comment:"Enable/disable network device as primary (yes/no)"` Default string `yaml:"default,omitempty"` /* backward compatibility */ Tags map[string]string `yaml:"tags,omitempty" lopt:"nettagadd" comment:"network tags"` - TagsDel []string `lopt:"nettagdel" comment:"delete network tags"` // does not go to disk only to wire + TagsDel []string `yaml:"tagsdel,omitempty" lopt:"nettagdel" comment:"delete network tags"` // should not go to disk only to wire } /****** diff --git a/internal/pkg/node/methods.go b/internal/pkg/node/methods.go index 267f265b..f33b7c3a 100644 --- a/internal/pkg/node/methods.go +++ b/internal/pkg/node/methods.go @@ -81,6 +81,8 @@ func (ent *Entry) SetB(val bool) { func (ent *Entry) SetSlice(val []string) { if len(val) == 0 { return + } else if len(val) == 1 && val[0] == "" { // check also for an "empty" slice + return } if val[0] == "UNDEF" || val[0] == "DELETE" || val[0] == "UNSET" || val[0] == "--" { ent.value = []string{} diff --git a/internal/pkg/node/transformers.go b/internal/pkg/node/transformers.go index ce5e039a..4a2369f9 100644 --- a/internal/pkg/node/transformers.go +++ b/internal/pkg/node/transformers.go @@ -142,12 +142,14 @@ func (nodeConf *NodeConf) CreateFlags(baseCmd *cobra.Command, excludeList []stri for i := 0; i < nodeInfoVal.Elem().NumField(); i++ { if nodeInfoType.Elem().Field(i).Tag.Get("comment") != "" && !util.InSlice(excludeList, nodeInfoType.Elem().Field(i).Tag.Get("lopt")) { - createFlags(baseCmd, excludeList, nodeInfoType.Elem().Field(i), nodeInfoVal.Elem().Field(i)) + field := nodeInfoVal.Elem().Field(i) + createFlags(baseCmd, excludeList, nodeInfoType.Elem().Field(i), &field) } else if nodeInfoType.Elem().Field(i).Type.Kind() == reflect.Ptr { nestType := reflect.TypeOf(nodeInfoVal.Elem().Field(i).Interface()) nestVal := reflect.ValueOf(nodeInfoVal.Elem().Field(i).Interface()) for j := 0; j < nestType.Elem().NumField(); j++ { - createFlags(baseCmd, excludeList, nestType.Elem().Field(j), nestVal.Elem().Field(j)) + field := nestVal.Elem().Field(j) + createFlags(baseCmd, excludeList, nestType.Elem().Field(j), &field) } } else if nodeInfoType.Elem().Field(i).Type == reflect.TypeOf(map[string]*NetDevs(nil)) { netMap := nodeInfoVal.Elem().Field(i).Interface().(map[string]*NetDevs) @@ -156,7 +158,8 @@ func (nodeConf *NodeConf) CreateFlags(baseCmd *cobra.Command, excludeList []stri netType := reflect.TypeOf(netMap["default"]) netVal := reflect.ValueOf(netMap["default"]) for j := 0; j < netType.Elem().NumField(); j++ { - createFlags(baseCmd, excludeList, netType.Elem().Field(j), netVal.Elem().Field(j)) + field := netVal.Elem().Field(j) + createFlags(baseCmd, excludeList, netType.Elem().Field(j), &field) } } } @@ -166,7 +169,7 @@ func (nodeConf *NodeConf) CreateFlags(baseCmd *cobra.Command, excludeList []stri Helper function to create the different PerisitantFlags() for different types. */ func createFlags(baseCmd *cobra.Command, excludeList []string, - myType reflect.StructField, myVal reflect.Value) { + myType reflect.StructField, myVal *reflect.Value) { if myType.Tag.Get("lopt") != "" { if myType.Type.Kind() == reflect.String { ptr := myVal.Addr().Interface().(*string)