mirror of
https://github.com/davidallendj/configurator.git
synced 2025-12-20 11:37:00 -07:00
64 lines
1.8 KiB
Go
64 lines
1.8 KiB
Go
package generator
|
|
|
|
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"
|
|
)
|
|
|
|
type DHCPd struct{}
|
|
|
|
func (g *DHCPd) GetName() string {
|
|
return "dhcpd"
|
|
}
|
|
|
|
func (g *DHCPd) GetVersion() string {
|
|
return util.GitCommit()
|
|
}
|
|
|
|
func (g *DHCPd) GetDescription() string {
|
|
return fmt.Sprintf("Configurator generator plugin for '%s'.", g.GetName())
|
|
}
|
|
|
|
func (g *DHCPd) Generate(config *config.Config, params Params) (FileMap, error) {
|
|
var (
|
|
smdClient = client.NewSmdClient(params.ClientOpts...)
|
|
eths = []configurator.EthernetInterface{}
|
|
computeNodes = ""
|
|
err error = nil
|
|
)
|
|
|
|
//
|
|
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
|
|
if eths == nil {
|
|
return nil, fmt.Errorf("invalid ethernet interfaces (variable is nil)")
|
|
}
|
|
if len(eths) <= 0 {
|
|
return nil, fmt.Errorf("no ethernet interfaces found")
|
|
}
|
|
|
|
// format output to write to config file
|
|
computeNodes = "# ========== DYNAMICALLY GENERATED BY OPENCHAMI CONFIGURATOR ==========\n"
|
|
for _, eth := range eths {
|
|
if len(eth.IpAddresses) == 0 {
|
|
continue
|
|
}
|
|
computeNodes += fmt.Sprintf("host %s { hardware ethernet %s; fixed-address %s} ", eth.ComponentId, eth.MacAddress, eth.IpAddresses[0])
|
|
}
|
|
computeNodes += "# ====================================================================="
|
|
return ApplyTemplates(Mappings{
|
|
"plugin_name": g.GetName(),
|
|
"plugin_version": g.GetVersion(),
|
|
"plugin_description": g.GetDescription(),
|
|
"compute_nodes": computeNodes,
|
|
"node_entries": "",
|
|
}, params.Templates)
|
|
}
|