From efd761f7c380a546de4f5f7435027b5816e02686 Mon Sep 17 00:00:00 2001 From: Christian Goll Date: Thu, 15 Sep 2022 10:33:27 +0200 Subject: [PATCH] Read the defaults for anode from defaults.conf --- .gitignore | 1 + Makefile | 7 +++- cmd/print_defaults/print_defaults.go | 17 ++++++++++ internal/pkg/node/constructors.go | 50 +++++++++++++++++++++++----- internal/pkg/node/datastructure.go | 8 ++--- internal/pkg/node/transformers.go | 14 ++++++++ 6 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 cmd/print_defaults/print_defaults.go diff --git a/.gitignore b/.gitignore index 35665e4d..cc7db570 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,7 @@ /wwapic /wwapid /wwapird +/print_defaults # other created files /man_pages diff --git a/Makefile b/Makefile index 00a48716..add0a600 100644 --- a/Makefile +++ b/Makefile @@ -95,7 +95,7 @@ export GOPROXY # built tags needed for wwbuild binary WW_GO_BUILD_TAGS := containers_image_openpgp containers_image_ostree -all: config vendor wwctl wwclient bash_completion.d man_pages config_defaults wwapid wwapic wwapird +all: config vendor wwctl wwclient bash_completion.d man_pages config_defaults print_defaults wwapid wwapic wwapird build: lint test-it vet all @@ -174,6 +174,7 @@ files: all test -f $(DESTDIR)$(SYSCONFDIR)/warewulf/wwapic.conf || install -m 644 etc/wwapic.conf $(DESTDIR)$(SYSCONFDIR)/warewulf/ test -f $(DESTDIR)$(SYSCONFDIR)/warewulf/wwapid.conf || install -m 644 etc/wwapid.conf $(DESTDIR)$(SYSCONFDIR)/warewulf/ test -f $(DESTDIR)$(SYSCONFDIR)/warewulf/wwapird.conf || install -m 644 etc/wwapird.conf $(DESTDIR)$(SYSCONFDIR)/warewulf/ + test -f $(DESTDIR)$(SYSCONFDIR)/warewulf/defaults.conf || ./print_defaults > $(DESTDIR)$(SYSCONFDIR)/warewulf/defaults.conf cp -r etc/examples $(DESTDIR)$(WWCONFIGDIR)/ cp -r etc/ipxe $(DESTDIR)$(WWCONFIGDIR)/ cp -r overlays/* $(DESTDIR)$(WWOVERLAYDIR)/ @@ -237,6 +238,9 @@ config_defaults: vendor cmd/config_defaults/config_defaults.go -X 'github.com/hpcng/warewulf/internal/pkg/node.ConfigFile=./etc/nodes.conf'"\ -mod vendor -tags "$(WW_GO_BUILD_TAGS)" -o ../../config_defaults +print_defaults: vendor cmd/print_defaults/print_defaults.go + cd cmd/print_defaults && go build -ldflags="-X 'github.com/hpcng/warewulf/internal/pkg/warewulfconf.ConfigFile=./etc/warewulf.conf'" -o ../../print_defaults + update_configuration: vendor cmd/update_configuration/update_configuration.go cd cmd/update_configuration && go build -ldflags="-X 'github.com/hpcng/warewulf/internal/pkg/warewulfconf.ConfigFile=./etc/warewulf.conf'\ -X 'github.com/hpcng/warewulf/internal/pkg/node.ConfigFile=./etc/nodes.conf'"\ @@ -297,6 +301,7 @@ clean: rm -rf $(TOOLS_DIR) rm -f config_defaults rm -f update_configuration + rm -f print_defaults install: files install_wwclient diff --git a/cmd/print_defaults/print_defaults.go b/cmd/print_defaults/print_defaults.go new file mode 100644 index 00000000..397294dd --- /dev/null +++ b/cmd/print_defaults/print_defaults.go @@ -0,0 +1,17 @@ +package main + +import ( + "fmt" + + "github.com/hpcng/warewulf/internal/pkg/node" +) + +/* +Print the build in defaults for the nodes. +Called via Makefile so that there is single upstream +source of the defaults which is FallBackConf +*/ + +func main() { + fmt.Println(node.FallBackConf) +} diff --git a/internal/pkg/node/constructors.go b/internal/pkg/node/constructors.go index 6237df47..1ea70def 100644 --- a/internal/pkg/node/constructors.go +++ b/internal/pkg/node/constructors.go @@ -14,11 +14,28 @@ import ( ) var ConfigFile string +var DefaultConfig string + +// used as fallback if DefaultConfig can't be read +var FallBackConf = ` +runtime overlay: +- generic +system overlay: +- wwinit +kernel: + args: quiet crashkernel=no vga=791 +init: /sbin/init +root: initramfs +profiles: +- default` func init() { if ConfigFile == "" { ConfigFile = path.Join(buildconfig.SYSCONFDIR(), "warewulf/nodes.conf") } + if DefaultConfig == "" { + DefaultConfig = path.Join(buildconfig.SYSCONFDIR(), "warewulf/defaults.conf") + } } func New() (NodeYaml, error) { @@ -54,6 +71,27 @@ func (config *NodeYaml) FindAllNodes() ([]NodeInfo, error) { return ret, err } */ + var defConf NodeConf + readBuildin := false + wwlog.Verbose("Opening defaults failed %s\n", DefaultConfig) + defData, err := ioutil.ReadFile(DefaultConfig) + if err != nil { + wwlog.Verbose("Couldn't read DefaultConfig :%s\n", err) + } + wwlog.Debug("Unmarshalling default config\n") + err = yaml.Unmarshal(defData, &defConf) + if err != nil { + readBuildin = true + wwlog.Verbose("Couldn't unmarshall defaults from file :%s\n", err) + } + if readBuildin { + wwlog.Verbose("Using building defaults") + err = yaml.Unmarshal([]byte(FallBackConf), &defConf) + if err != nil { + wwlog.Warn("Could not get any defaults") + } + } + wwlog.Debug("Finding all nodes...\n") for nodename, node := range config.Nodes { var n NodeInfo @@ -63,13 +101,7 @@ func (config *NodeYaml) FindAllNodes() ([]NodeInfo, error) { n.Tags = make(map[string]*Entry) n.Kernel = new(KernelEntry) n.Ipmi = new(IpmiEntry) - n.SystemOverlay.SetDefault("wwinit") - n.RuntimeOverlay.SetDefault("generic") - n.Ipxe.SetDefault("default") - n.Init.SetDefault("/sbin/init") - n.Root.SetDefault("initramfs") - n.Kernel.Args.SetDefault("quiet crashkernel=no vga=791") - + n.SetDefFrom(&defConf) fullname := strings.SplitN(nodename, ".", 2) if len(fullname) > 1 { n.ClusterName.SetDefault(fullname[1]) @@ -80,9 +112,9 @@ func (config *NodeYaml) FindAllNodes() ([]NodeInfo, error) { } else { n.Profiles.SetSlice(node.Profiles) } - // node explciti nodename field in NodeConf + // node explicitly nodename field in NodeConf n.Id.Set(nodename) - // backward compatibilty + // backward compatibility for keyname, key := range node.Keys { node.Tags[keyname] = key delete(node.Keys, keyname) diff --git a/internal/pkg/node/datastructure.go b/internal/pkg/node/datastructure.go index fa9fd33d..f16eb163 100644 --- a/internal/pkg/node/datastructure.go +++ b/internal/pkg/node/datastructure.go @@ -1,10 +1,5 @@ package node -import ( - "github.com/hpcng/warewulf/internal/pkg/util" - "github.com/hpcng/warewulf/internal/pkg/wwlog" -) - /****** * YAML data representations ******/ @@ -163,6 +158,8 @@ type NetDevEntry struct { // string which is printed if no value is set const NoValue = "--" +/* +Has no real purpose as only New() needs it func init() { // Check that nodes.conf is found if !util.IsFile(ConfigFile) { @@ -171,3 +168,4 @@ func init() { return } } +*/ diff --git a/internal/pkg/node/transformers.go b/internal/pkg/node/transformers.go index a50ca263..86177650 100644 --- a/internal/pkg/node/transformers.go +++ b/internal/pkg/node/transformers.go @@ -303,6 +303,20 @@ func (node *NodeInfo) SetAltFrom(n *NodeConf, profileName string) { node.setterFrom(n, profileName, (*Entry).SetAlt, (*Entry).SetAltSlice) } +/* +Populates all fields of NodeInfo with SetDefault from the +values of NodeConf. +*/ +func (node *NodeInfo) SetDefFrom(n *NodeConf) { + setWrap := func(entr *Entry, val string, nameArg string) { + entr.SetDefault(val) + } + setSliceWrap := func(entr *Entry, val []string, nameArg string) { + entr.SetDefaultSlice(val) + } + node.setterFrom(n, "", setWrap, setSliceWrap) +} + /* Abstract function which populates a NodeInfo from a NodeConf via setter functionns.