Updated generator plugins to reflect generator changes

This commit is contained in:
David Allen 2024-06-26 10:17:59 -06:00
parent 56be39ff99
commit f443558b50
No known key found for this signature in database
GPG key ID: 717C593FF60A2ACC
6 changed files with 68 additions and 65 deletions

View file

@ -15,17 +15,17 @@ func (g *Conman) GetName() string {
}
func (g *Conman) GetGroups() []string {
return []string{"conman"}
return []string{""}
}
func (g *Conman) Generate(config *configurator.Config, opts ...util.Option) ([]byte, error) {
func (g *Conman) Generate(config *configurator.Config, opts ...util.Option) (map[string][]byte, error) {
var (
params = generator.GetParams(opts...)
client = generator.GetClient(params)
template = params["template"].(string) // required param
path = config.TemplatePaths[template]
eps []configurator.RedfishEndpoint = nil
err error = nil
params = generator.GetParams(opts...)
client = generator.GetClient(params)
targetKey = params["targets"].(string) // required param
target = config.Targets[targetKey]
eps []configurator.RedfishEndpoint = nil
err error = nil
// serverOpts = ""
// globalOpts = ""
consoles = ""
@ -52,10 +52,10 @@ func (g *Conman) Generate(config *configurator.Config, opts ...util.Option) ([]b
consoles += "# ====================================================================="
// apply template substitutions and return output as byte array
return generator.ApplyTemplate(path, generator.Mappings{
return generator.ApplyTemplates(generator.Mappings{
"server_opts": "",
"global_opts": "",
})
}, target.Templates...)
}
var Generator Conman

View file

@ -13,11 +13,7 @@ func (g *CoreDhcp) GetName() string {
return "coredhcp"
}
func (g *CoreDhcp) GetGroups() []string {
return []string{"coredhcp"}
}
func (g *CoreDhcp) Generate(config *configurator.Config, opts ...util.Option) ([]byte, error) {
func (g *CoreDhcp) Generate(config *configurator.Config, opts ...util.Option) (map[string][]byte, error) {
return nil, fmt.Errorf("plugin does not implement generation function")
}

View file

@ -2,6 +2,7 @@ package main
import (
"fmt"
"strings"
configurator "github.com/OpenCHAMI/configurator/internal"
"github.com/OpenCHAMI/configurator/internal/generator"
@ -10,28 +11,11 @@ import (
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) {
func (g *DnsMasq) Generate(config *configurator.Config, opts ...util.Option) (map[string][]byte, error) {
// make sure we have a valid config first
if config == nil {
return nil, fmt.Errorf("invalid config (config is nil)")
@ -39,12 +23,12 @@ func (g *DnsMasq) Generate(config *configurator.Config, opts ...util.Option) ([]
// set all the defaults for variables
var (
params = generator.GetParams(opts...)
client = generator.GetClient(params)
template = params["template"].(string) // required param
path = config.TemplatePaths[template]
eths []configurator.EthernetInterface = nil
err error = nil
params = generator.GetParams(opts...)
client = generator.GetClient(params)
targetKey = params["target"].(string) // required param
target = config.Targets[targetKey]
eths []configurator.EthernetInterface = nil
err error = nil
)
// if we have a client, try making the request for the ethernet interfaces
@ -66,7 +50,7 @@ func (g *DnsMasq) Generate(config *configurator.Config, opts ...util.Option) ([]
// 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))
fmt.Printf("template: \n%s\n ethernet interfaces found: %v\n", strings.Join(target.Templates, "\n\t"), len(eths))
}
}
@ -82,10 +66,10 @@ func (g *DnsMasq) Generate(config *configurator.Config, opts ...util.Option) ([]
output += "# ====================================================================="
// apply template substitutions and return output as byte array
return generator.ApplyTemplate(path, generator.Mappings{
return generator.ApplyTemplates(generator.Mappings{
"name": g.GetName(),
"output": output,
})
}, target.Templates...)
}
var Generator DnsMasq

View file

@ -17,7 +17,7 @@ func (g *Powerman) GetGroups() []string {
return []string{"powerman"}
}
func (g *Powerman) Generate(config *configurator.Config, opts ...util.Option) ([]byte, error) {
func (g *Powerman) Generate(config *configurator.Config, opts ...util.Option) (map[string][]byte, error) {
return nil, fmt.Errorf("plugin does not implement generation function")
}

View file

@ -13,11 +13,7 @@ func (g *Syslog) GetName() string {
return "syslog"
}
func (g *Syslog) GetGroups() []string {
return []string{"log"}
}
func (g *Syslog) Generate(config *configurator.Config, opts ...util.Option) ([]byte, error) {
func (g *Syslog) Generate(config *configurator.Config, opts ...util.Option) (map[string][]byte, error) {
return nil, fmt.Errorf("plugin does not implement generation function")
}

View file

@ -1,12 +1,12 @@
package main
import (
"bytes"
"fmt"
"maps"
configurator "github.com/OpenCHAMI/configurator/internal"
"github.com/nikolalohinski/gonja/v2"
"github.com/nikolalohinski/gonja/v2/exec"
"github.com/OpenCHAMI/configurator/internal/generator"
"github.com/OpenCHAMI/configurator/internal/util"
)
type Warewulf struct{}
@ -19,26 +19,53 @@ func (g *Warewulf) GetGroups() []string {
return []string{"warewulf"}
}
func (g *Warewulf) Generate(config *configurator.Config, template string) ([]byte, error) {
func (g *Warewulf) Generate(config *configurator.Config, opts ...util.Option) (generator.Files, error) {
var (
path = config.TemplatePaths[template]
params = generator.GetParams(opts...)
client = generator.GetClient(params)
targetKey = params["target"].(string)
target = config.Targets[targetKey]
outputs = make(generator.Files, len(target.FilePaths)+len(target.Templates))
)
t, err := gonja.FromFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read template from file: %v", err)
// check if our client is included and is valid
if client == nil {
return nil, fmt.Errorf("invalid client (client is nil)")
}
output := "# ========== GENERATED BY OCHAMI CONFIGURATOR ==========\n"
output += "# ======================================================"
data := exec.NewContext(map[string]any{
"hosts": output,
})
b := bytes.Buffer{}
if err = t.Execute(&b, data); err != nil {
return nil, fmt.Errorf("failed to execute: %v", err)
// fetch redfish endpoints and handle errors
eps, err := client.FetchRedfishEndpoints(opts...)
if err != nil {
return nil, fmt.Errorf("failed to fetch redfish endpoints: %v", err)
}
return nil, nil
if len(eps) <= 0 {
return nil, fmt.Errorf("no redfish endpoints found")
}
// load files and templates and copy to outputs
files, err := generator.LoadFiles(target.FilePaths...)
if err != nil {
return nil, fmt.Errorf("failed to load files: %v", err)
}
templates, err := generator.ApplyTemplates(generator.Mappings{}, target.Templates...)
if err != nil {
return nil, fmt.Errorf("failed to load templates: %v", err)
}
maps.Copy(outputs, files)
maps.Copy(outputs, templates)
// print message if verbose param is found
if verbose, ok := params["verbose"].(bool); ok {
if verbose {
fmt.Printf("templates and files loaded: \n")
for path, _ := range outputs {
fmt.Printf("\t%s", path)
}
}
}
return outputs, err
}
var Generator Warewulf