refactor: more code cleanup and simplification

This commit is contained in:
David Allen 2024-11-21 14:14:44 -07:00
parent 32065dc163
commit a7b8fb0de5
Signed by: towk
GPG key ID: 793B2924A49B3A3F
11 changed files with 268 additions and 398 deletions

View file

@ -4,6 +4,8 @@ import (
"fmt"
configurator "github.com/OpenCHAMI/configurator/pkg"
"github.com/OpenCHAMI/configurator/pkg/client"
"github.com/OpenCHAMI/configurator/pkg/config"
"github.com/OpenCHAMI/configurator/pkg/util"
)
@ -21,23 +23,18 @@ func (g *DHCPd) GetDescription() string {
return fmt.Sprintf("Configurator generator plugin for '%s'.", g.GetName())
}
func (g *DHCPd) Generate(config *configurator.Config, opts ...util.Option) (FileMap, error) {
func (g *DHCPd) Generate(config *config.Config, params Params) (FileMap, error) {
var (
params = GetParams(opts...)
client = GetClient(params)
targetKey = params["target"].(string)
target = config.Targets[targetKey]
compute_nodes = ""
eths []configurator.EthernetInterface = nil
err error = nil
smdClient = client.NewSmdClient(params.ClientOpts...)
eths = []configurator.EthernetInterface{}
computeNodes = ""
err error = nil
)
//
if client != nil {
eths, err = client.FetchEthernetInterfaces(opts...)
if err != nil {
return nil, fmt.Errorf("failed to fetch ethernet interfaces with client: %w", err)
}
eths, err = smdClient.FetchEthernetInterfaces(params.Verbose)
if err != nil {
return nil, fmt.Errorf("failed to fetch ethernet interfaces with client: %w", err)
}
// check if we have the required params first
@ -49,25 +46,23 @@ func (g *DHCPd) Generate(config *configurator.Config, opts ...util.Option) (File
}
// format output to write to config file
compute_nodes = "# ========== DYNAMICALLY GENERATED BY OPENCHAMI CONFIGURATOR ==========\n"
computeNodes = "# ========== DYNAMICALLY GENERATED BY OPENCHAMI CONFIGURATOR ==========\n"
for _, eth := range eths {
if len(eth.IpAddresses) == 0 {
continue
}
compute_nodes += fmt.Sprintf("host %s { hardware ethernet %s; fixed-address %s} ", eth.ComponentId, eth.MacAddress, eth.IpAddresses[0])
computeNodes += fmt.Sprintf("host %s { hardware ethernet %s; fixed-address %s} ", eth.ComponentId, eth.MacAddress, eth.IpAddresses[0])
}
compute_nodes += "# ====================================================================="
computeNodes += "# ====================================================================="
if verbose, ok := params["verbose"].(bool); ok {
if verbose {
fmt.Printf("")
}
if params.Verbose {
fmt.Printf("")
}
return ApplyTemplateFromFiles(Mappings{
return ApplyTemplates(Mappings{
"plugin_name": g.GetName(),
"plugin_version": g.GetVersion(),
"plugin_description": g.GetDescription(),
"compute_nodes": compute_nodes,
"compute_nodes": computeNodes,
"node_entries": "",
}, target.TemplatePaths...)
}, params.Templates)
}