Rewrote generators to use plugin system with default plugins
This commit is contained in:
parent
8036a5a8c0
commit
d77a31c7fe
15 changed files with 712 additions and 179 deletions
89
internal/generator/plugins/dnsmasq/dnsmasq.go
Normal file
89
internal/generator/plugins/dnsmasq/dnsmasq.go
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
configurator "github.com/OpenCHAMI/configurator/internal"
|
||||
"github.com/OpenCHAMI/configurator/internal/generator"
|
||||
"github.com/OpenCHAMI/configurator/internal/util"
|
||||
)
|
||||
|
||||
type DnsMasq struct{}
|
||||
|
||||
func TestGenerateDnsMasq() {
|
||||
var (
|
||||
g = DnsMasq{}
|
||||
config = &configurator.Config{}
|
||||
client = configurator.SmdClient{}
|
||||
)
|
||||
g.Generate(
|
||||
config,
|
||||
generator.WithTemplate("dnsmasq"),
|
||||
generator.WithClient(client),
|
||||
)
|
||||
}
|
||||
|
||||
func (g *DnsMasq) GetName() string {
|
||||
return "dnsmasq"
|
||||
}
|
||||
|
||||
func (g *DnsMasq) GetGroups() []string {
|
||||
return []string{"dnsmasq"}
|
||||
}
|
||||
|
||||
func (g *DnsMasq) Generate(config *configurator.Config, opts ...util.Option) ([]byte, error) {
|
||||
// make sure we have a valid config first
|
||||
if config == nil {
|
||||
return nil, fmt.Errorf("invalid config (config is nil)")
|
||||
}
|
||||
|
||||
// set all the defaults for variables
|
||||
var (
|
||||
params = generator.GetParams(opts...)
|
||||
template = params["template"].(string) // required param
|
||||
path = config.TemplatePaths[template]
|
||||
eths []configurator.EthernetInterface = nil
|
||||
err error = nil
|
||||
)
|
||||
|
||||
// if we have a client, try making the request for the ethernet interfaces
|
||||
if client, ok := params["client"].(configurator.SmdClient); ok {
|
||||
eths, err = client.FetchEthernetInterfaces(opts...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to fetch ethernet interfaces with client: %v", 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")
|
||||
}
|
||||
|
||||
// print message if verbose param found
|
||||
if verbose, ok := params["verbose"].(bool); ok {
|
||||
if verbose {
|
||||
fmt.Printf("path: %s\neth count: %v\n", path, len(eths))
|
||||
}
|
||||
}
|
||||
|
||||
// format output to write to config file
|
||||
output := "# ========== GENERATED BY OCHAMI CONFIGURATOR ==========\n"
|
||||
for _, eth := range eths {
|
||||
if eth.Type == "NodeBMC" {
|
||||
output += "dhcp-host=" + eth.MacAddress + "," + eth.ComponentId + "," + eth.IpAddresses[0].IpAddress + "\n"
|
||||
} else {
|
||||
output += "dhcp-host=" + eth.MacAddress + "," + eth.ComponentId + "," + eth.IpAddresses[0].IpAddress + "\n"
|
||||
}
|
||||
}
|
||||
output += "# ======================================================"
|
||||
|
||||
// apply template substitutions and return output as byte array
|
||||
return generator.ApplyTemplate(path, generator.Mappings{
|
||||
"hosts": output,
|
||||
})
|
||||
}
|
||||
|
||||
var Generator DnsMasq
|
||||
Loading…
Add table
Add a link
Reference in a new issue