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

@ -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