Rewrote generators to use plugin system with default plugins

This commit is contained in:
David Allen 2024-06-19 14:19:42 -06:00
parent 8036a5a8c0
commit d77a31c7fe
No known key found for this signature in database
GPG key ID: 717C593FF60A2ACC
15 changed files with 712 additions and 179 deletions

View file

@ -2,50 +2,162 @@ package generator
import (
"bytes"
"fmt"
"os"
"plugin"
configurator "github.com/OpenCHAMI/configurator/internal"
"github.com/OpenCHAMI/configurator/internal/util"
"github.com/nikolalohinski/gonja/v2"
"github.com/nikolalohinski/gonja/v2/exec"
)
type Generator struct {
Type string
Template string
type Mappings = map[string]any
type Generator interface {
GetName() string
GetGroups() []string
Generate(config *configurator.Config, opts ...util.Option) ([]byte, error)
}
func New() *Generator {
return &Generator{}
func LoadPlugin(path string) (Generator, error) {
p, err := plugin.Open(path)
if err != nil {
return nil, fmt.Errorf("failed to load plugin: %v", err)
}
symbol, err := p.Lookup("Generator")
if err != nil {
return nil, fmt.Errorf("failed to look up symbol: %v", err)
}
gen, ok := symbol.(Generator)
if !ok {
return nil, fmt.Errorf("failed to load the correct symbol type")
}
return gen, nil
}
func (g *Generator) GenerateDNS(config *configurator.Config) {
// generate file using jinja template
// TODO: load template file for DNS
// TODO: substitute DNS data fetched from SMD
// TODO: print generated config file to STDOUT
func LoadPlugins(dirpath string, opts ...util.Option) (map[string]Generator, error) {
// check if verbose option is supplied
var (
gens = make(map[string]Generator)
params = util.GetParams(opts...)
)
items, _ := os.ReadDir(dirpath)
var LoadGenerator = func(path string) (Generator, error) {
// load each generator plugin
p, err := plugin.Open(path)
if err != nil {
return nil, fmt.Errorf("failed to load plugin: %v", err)
}
// lookup symbol in plugin
symbol, err := p.Lookup("Generator")
if err != nil {
return nil, fmt.Errorf("failed to look up symbol: %v", err)
}
// assert that the loaded symbol is the correct type
gen, ok := symbol.(Generator)
if !ok {
return nil, fmt.Errorf("failed to load the correct symbol type")
}
return gen, nil
}
for _, item := range items {
if item.IsDir() {
subitems, _ := os.ReadDir(item.Name())
for _, subitem := range subitems {
if !subitem.IsDir() {
gen, err := LoadGenerator(subitem.Name())
if err != nil {
fmt.Printf("failed to load generator in directory '%s': %v\n", item.Name(), err)
continue
}
if verbose, ok := params["verbose"].(bool); ok {
if verbose {
fmt.Printf("found plugin '%s'\n", item.Name())
}
}
gens[gen.GetName()] = gen
}
}
} else {
gen, err := LoadGenerator(dirpath + item.Name())
if err != nil {
fmt.Printf("failed to load generator: %v\n", err)
continue
}
if verbose, ok := params["verbose"].(bool); ok {
if verbose {
fmt.Printf("found plugin '%s'\n", dirpath+item.Name())
}
}
gens[gen.GetName()] = gen
}
}
return gens, nil
}
func (g *Generator) GenerateDHCP(config *configurator.Config, eths []configurator.EthernetInterface) ([]byte, error) {
// generate file using gonja template
path := config.TemplatePaths[g.Template]
fmt.Printf("path: %s\neth count: %v\n", path, len(eths))
func WithTemplate(_template string) util.Option {
return func(p util.Params) {
if p != nil {
p["template"] = _template
}
}
}
func WithType(_type string) util.Option {
return func(p util.Params) {
if p != nil {
p["type"] = _type
}
}
}
func WithClient(client configurator.SmdClient) util.Option {
return func(p util.Params) {
p["client"] = client
}
}
// Syntactic sugar generic function to get parameter from util.Params.
func Get[T any](params util.Params, key string) *T {
if v, ok := params[key].(T); ok {
return &v
}
return nil
}
// Helper function to get client in generator plugins.
func GetClient(params util.Params) *configurator.SmdClient {
return Get[configurator.SmdClient](params, "client")
}
func GetParams(opts ...util.Option) util.Params {
params := util.Params{}
for _, opt := range opts {
opt(params)
}
return params
}
func Generate(g Generator, config *configurator.Config, opts ...util.Option) {
g.Generate(config, opts...)
}
func ApplyTemplate(path string, mappings map[string]any) ([]byte, error) {
data := exec.NewContext(mappings)
// load jinja template from file
t, err := gonja.FromFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read template from file: %v", err)
}
template := "# ========== GENERATED BY OCHAMI CONFIGURATOR ==========\n"
for _, eth := range eths {
if eth.Type == "NodeBMC" {
template += "dhcp-host=" + eth.MacAddress + "," + eth.ComponentId + "," + eth.IpAddresses[0].IpAddress + "\n"
} else {
template += "dhcp-host=" + eth.MacAddress + "," + eth.ComponentId + "," + eth.IpAddresses[0].IpAddress + "\n"
}
}
template += "# ======================================================"
data := exec.NewContext(map[string]any{
"hosts": template,
})
// execute/render jinja template
b := bytes.Buffer{}
if err = t.Execute(&b, data); err != nil {
return nil, fmt.Errorf("failed to execute: %v", err)

View file

@ -0,0 +1,46 @@
package main
import (
"bytes"
"fmt"
configurator "github.com/OpenCHAMI/configurator/internal"
"github.com/OpenCHAMI/configurator/internal/generator"
"github.com/OpenCHAMI/configurator/internal/util"
"github.com/nikolalohinski/gonja/v2"
"github.com/nikolalohinski/gonja/v2/exec"
)
type Conman struct{}
func (g *Conman) GetName() string {
return "conman"
}
func (g *Conman) GetGroups() []string {
return []string{"conman"}
}
func (g *Conman) Generate(config *configurator.Config, opts ...util.Option) ([]byte, error) {
params := generator.GetParams(opts...)
var (
template = params["template"].(string)
path = config.TemplatePaths[template]
)
data := exec.NewContext(map[string]any{})
t, err := gonja.FromFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read template from file: %v", err)
}
output := "# ========== GENERATED BY OCHAMI CONFIGURATOR ==========\n"
output += "# ======================================================"
b := bytes.Buffer{}
if err = t.Execute(&b, data); err != nil {
return nil, fmt.Errorf("failed to execute: %v", err)
}
return b.Bytes(), nil
}
var Generator Conman

View file

@ -0,0 +1,22 @@
package main
import (
configurator "github.com/OpenCHAMI/configurator/internal"
"github.com/OpenCHAMI/configurator/internal/util"
)
type CoreDhcp struct{}
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) {
return nil, nil
}
var Generator CoreDhcp

View 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

View file

@ -0,0 +1,22 @@
package main
import (
configurator "github.com/OpenCHAMI/configurator/internal"
"github.com/OpenCHAMI/configurator/internal/util"
)
type Powerman struct{}
func (g *Powerman) GetName() string {
return "powerman"
}
func (g *Powerman) GetGroups() []string {
return []string{"powerman"}
}
func (g *Powerman) Generate(config *configurator.Config, opts ...util.Option) ([]byte, error) {
return nil, nil
}
var Generator Powerman

View file

@ -0,0 +1,22 @@
package main
import (
configurator "github.com/OpenCHAMI/configurator/internal"
"github.com/OpenCHAMI/configurator/internal/util"
)
type Syslog struct{}
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) {
return nil, nil
}
var Generator Syslog

View file

@ -0,0 +1,44 @@
package main
import (
"bytes"
"fmt"
configurator "github.com/OpenCHAMI/configurator/internal"
"github.com/nikolalohinski/gonja/v2"
"github.com/nikolalohinski/gonja/v2/exec"
)
type Warewulf struct{}
func (g *Warewulf) GetName() string {
return "warewulf"
}
func (g *Warewulf) GetGroups() []string {
return []string{"warewulf"}
}
func (g *Warewulf) Generate(config *configurator.Config, template string) ([]byte, error) {
var (
path = config.TemplatePaths[template]
)
t, err := gonja.FromFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read template from file: %v", err)
}
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)
}
return nil, nil
}
var Generator Warewulf