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

View file

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

View file

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

View file

@ -17,7 +17,7 @@ func (g *Powerman) GetGroups() []string {
return []string{"powerman"} 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") return nil, fmt.Errorf("plugin does not implement generation function")
} }

View file

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

View file

@ -1,12 +1,12 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"maps"
configurator "github.com/OpenCHAMI/configurator/internal" configurator "github.com/OpenCHAMI/configurator/internal"
"github.com/nikolalohinski/gonja/v2" "github.com/OpenCHAMI/configurator/internal/generator"
"github.com/nikolalohinski/gonja/v2/exec" "github.com/OpenCHAMI/configurator/internal/util"
) )
type Warewulf struct{} type Warewulf struct{}
@ -19,26 +19,53 @@ func (g *Warewulf) GetGroups() []string {
return []string{"warewulf"} 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 ( 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) // check if our client is included and is valid
if err != nil { if client == nil {
return nil, fmt.Errorf("failed to read template from file: %v", err) return nil, fmt.Errorf("invalid client (client is nil)")
} }
output := "# ========== GENERATED BY OCHAMI CONFIGURATOR ==========\n"
output += "# ======================================================" // fetch redfish endpoints and handle errors
data := exec.NewContext(map[string]any{ eps, err := client.FetchRedfishEndpoints(opts...)
"hosts": output, if err != nil {
}) return nil, fmt.Errorf("failed to fetch redfish endpoints: %v", err)
b := bytes.Buffer{}
if err = t.Execute(&b, data); err != nil {
return nil, fmt.Errorf("failed to execute: %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 var Generator Warewulf