Merge pull request #21 from OpenCHAMI/20-convert-plugins

Build default plugins directly into the binary executable
This commit is contained in:
Alex Lovell-Troy 2024-11-14 15:26:15 -05:00 committed by GitHub
commit a55ecf2b88
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 100 additions and 91 deletions

View file

@ -5,12 +5,14 @@ before:
- go mod download
- make plugins
builds:
- env:
- CGO_ENABLED=1
- id: "configurator"
goos:
- linux
goarch:
- amd64
- arm64
flags:
- -tags:all
archives:
- format: tar.gz
# this name template makes the OS and Arch compatible with the results of uname.

View file

@ -1,10 +1,9 @@
package main
package generator
import (
"fmt"
configurator "github.com/OpenCHAMI/configurator/pkg"
"github.com/OpenCHAMI/configurator/pkg/generator"
"github.com/OpenCHAMI/configurator/pkg/util"
)
@ -22,10 +21,10 @@ func (g *Conman) GetDescription() string {
return fmt.Sprintf("Configurator generator plugin for '%s'.", g.GetName())
}
func (g *Conman) Generate(config *configurator.Config, opts ...util.Option) (generator.FileMap, error) {
func (g *Conman) Generate(config *configurator.Config, opts ...util.Option) (FileMap, error) {
var (
params = generator.GetParams(opts...)
client = generator.GetClient(params)
params = GetParams(opts...)
client = GetClient(params)
targetKey = params["target"].(string) // required param
target = config.Targets[targetKey]
eps []configurator.RedfishEndpoint = nil
@ -56,7 +55,7 @@ func (g *Conman) Generate(config *configurator.Config, opts ...util.Option) (gen
consoles += "# ====================================================================="
// apply template substitutions and return output as byte array
return generator.ApplyTemplateFromFiles(generator.Mappings{
return ApplyTemplateFromFiles(Mappings{
"plugin_name": g.GetName(),
"plugin_version": g.GetVersion(),
"plugin_description": g.GetDescription(),
@ -65,5 +64,3 @@ func (g *Conman) Generate(config *configurator.Config, opts ...util.Option) (gen
"consoles": consoles,
}, target.TemplatePaths...)
}
var Generator Conman

View file

@ -1,10 +1,12 @@
package main
//go:build coredhcp || plugins
// +build coredhcp plugins
package generator
import (
"fmt"
configurator "github.com/OpenCHAMI/configurator/pkg"
"github.com/OpenCHAMI/configurator/pkg/generator"
"github.com/OpenCHAMI/configurator/pkg/util"
)
@ -22,8 +24,6 @@ func (g *CoreDhcp) GetDescription() string {
return fmt.Sprintf("Configurator generator plugin for '%s' to generate config files. (WIP)", g.GetName())
}
func (g *CoreDhcp) Generate(config *configurator.Config, opts ...util.Option) (generator.FileMap, error) {
func (g *CoreDhcp) Generate(config *configurator.Config, opts ...util.Option) (FileMap, error) {
return nil, fmt.Errorf("plugin does not implement generation function")
}
var Generator CoreDhcp

View file

@ -1,31 +1,30 @@
package main
package generator
import (
"fmt"
configurator "github.com/OpenCHAMI/configurator/pkg"
"github.com/OpenCHAMI/configurator/pkg/generator"
"github.com/OpenCHAMI/configurator/pkg/util"
)
type Dhcpd struct{}
type DHCPd struct{}
func (g *Dhcpd) GetName() string {
func (g *DHCPd) GetName() string {
return "dhcpd"
}
func (g *Dhcpd) GetVersion() string {
func (g *DHCPd) GetVersion() string {
return util.GitCommit()
}
func (g *Dhcpd) GetDescription() string {
func (g *DHCPd) GetDescription() string {
return fmt.Sprintf("Configurator generator plugin for '%s'.", g.GetName())
}
func (g *Dhcpd) Generate(config *configurator.Config, opts ...util.Option) (generator.FileMap, error) {
func (g *DHCPd) Generate(config *configurator.Config, opts ...util.Option) (FileMap, error) {
var (
params = generator.GetParams(opts...)
client = generator.GetClient(params)
params = GetParams(opts...)
client = GetClient(params)
targetKey = params["target"].(string)
target = config.Targets[targetKey]
compute_nodes = ""
@ -64,7 +63,7 @@ func (g *Dhcpd) Generate(config *configurator.Config, opts ...util.Option) (gene
fmt.Printf("")
}
}
return generator.ApplyTemplateFromFiles(generator.Mappings{
return ApplyTemplateFromFiles(Mappings{
"plugin_name": g.GetName(),
"plugin_version": g.GetVersion(),
"plugin_description": g.GetDescription(),
@ -72,5 +71,3 @@ func (g *Dhcpd) Generate(config *configurator.Config, opts ...util.Option) (gene
"node_entries": "",
}, target.TemplatePaths...)
}
var Generator Dhcpd

View file

@ -1,29 +1,28 @@
package main
package generator
import (
"fmt"
"strings"
configurator "github.com/OpenCHAMI/configurator/pkg"
"github.com/OpenCHAMI/configurator/pkg/generator"
"github.com/OpenCHAMI/configurator/pkg/util"
)
type DnsMasq struct{}
type DNSMasq struct{}
func (g *DnsMasq) GetName() string {
func (g *DNSMasq) GetName() string {
return "dnsmasq"
}
func (g *DnsMasq) GetVersion() string {
func (g *DNSMasq) GetVersion() string {
return util.GitCommit()
}
func (g *DnsMasq) GetDescription() string {
func (g *DNSMasq) GetDescription() string {
return fmt.Sprintf("Configurator generator plugin for '%s'.", g.GetName())
}
func (g *DnsMasq) Generate(config *configurator.Config, opts ...util.Option) (generator.FileMap, error) {
func (g *DNSMasq) Generate(config *configurator.Config, opts ...util.Option) (FileMap, error) {
// make sure we have a valid config first
if config == nil {
return nil, fmt.Errorf("invalid config (config is nil)")
@ -31,8 +30,8 @@ func (g *DnsMasq) Generate(config *configurator.Config, opts ...util.Option) (ge
// set all the defaults for variables
var (
params = generator.GetParams(opts...)
client = generator.GetClient(params)
params = GetParams(opts...)
client = GetClient(params)
targetKey = params["target"].(string) // required param
target = config.Targets[targetKey]
eths []configurator.EthernetInterface = nil
@ -74,12 +73,10 @@ func (g *DnsMasq) Generate(config *configurator.Config, opts ...util.Option) (ge
output += "# ====================================================================="
// apply template substitutions and return output as byte array
return generator.ApplyTemplateFromFiles(generator.Mappings{
return ApplyTemplateFromFiles(Mappings{
"plugin_name": g.GetName(),
"plugin_version": g.GetVersion(),
"plugin_description": g.GetDescription(),
"dhcp-hosts": output,
}, target.TemplatePaths...)
}
var Generator DnsMasq

View file

@ -1,4 +1,7 @@
package main
//go:build example || plugins
// +build example plugins
package generator
import (
"fmt"

View file

@ -14,28 +14,45 @@ import (
"github.com/nikolalohinski/gonja/v2/exec"
)
type Mappings map[string]any
type FileMap map[string][]byte
type FileList [][]byte
type Template []byte
type (
Mappings map[string]any
FileMap map[string][]byte
FileList [][]byte
Template []byte
// Generator interface used to define how files are created. Plugins can
// be created entirely independent of the main driver program.
type Generator interface {
// Generator interface used to define how files are created. Plugins can
// be created entirely independent of the main driver program.
Generator interface {
GetName() string
GetVersion() string
GetDescription() string
Generate(config *configurator.Config, opts ...util.Option) (FileMap, error)
}
}
// Params defined and used by the "generate" subcommand.
type Params struct {
// Params defined and used by the "generate" subcommand.
Params struct {
Args []string
Generators map[string]Generator
TemplatePaths []string
PluginPath string
Target string
Verbose bool
}
)
var DefaultGenerators = createDefaultGenerators()
func createDefaultGenerators() map[string]Generator {
var (
generatorMap = map[string]Generator{}
generators = []Generator{
&Conman{}, &DHCPd{}, &DNSMasq{}, &Hostfile{},
&Powerman{}, &Syslog{}, &Warewulf{},
}
)
for _, g := range generators {
generatorMap[g.GetName()] = g
}
return generatorMap
}
// Converts the file outputs from map[string][]byte to map[string]string.
@ -397,6 +414,10 @@ func GenerateWithTarget(config *configurator.Config, params Params) (FileMap, er
configurator.WithAccessToken(config.AccessToken),
configurator.WithCertPoolFile(config.CertPath),
)
target configurator.Target
generator Generator
err error
ok bool
)
// check if a target is supplied
@ -405,7 +426,7 @@ func GenerateWithTarget(config *configurator.Config, params Params) (FileMap, er
}
// load target information from config
target, ok := config.Targets[params.Target]
target, ok = config.Targets[params.Target]
if !ok {
return nil, fmt.Errorf("target not found in config")
}
@ -415,11 +436,15 @@ func GenerateWithTarget(config *configurator.Config, params Params) (FileMap, er
target.PluginPath = params.PluginPath
}
// only load the plugin needed for this target
generator, err := LoadPlugin(target.PluginPath)
// check if generator is built-in first before loading
generator, ok = DefaultGenerators[params.Target]
if !ok {
// only load the plugin needed for this target if we don't find default
generator, err = LoadPlugin(target.PluginPath)
if err != nil {
return nil, fmt.Errorf("failed to load plugin: %w", err)
}
}
// run the generator plugin from target passed
return generator.Generate(

View file

@ -1,10 +1,9 @@
package main
package generator
import (
"fmt"
configurator "github.com/OpenCHAMI/configurator/pkg"
"github.com/OpenCHAMI/configurator/pkg/generator"
"github.com/OpenCHAMI/configurator/pkg/util"
)
@ -22,8 +21,6 @@ func (g *Hostfile) GetDescription() string {
return fmt.Sprintf("Configurator generator plugin for '%s'.", g.GetName())
}
func (g *Hostfile) Generate(config *configurator.Config, opts ...util.Option) (generator.FileMap, error) {
func (g *Hostfile) Generate(config *configurator.Config, opts ...util.Option) (FileMap, error) {
return nil, fmt.Errorf("plugin does not implement generation function")
}
var Generator Hostfile

View file

@ -1,10 +1,9 @@
package main
package generator
import (
"fmt"
configurator "github.com/OpenCHAMI/configurator/pkg"
"github.com/OpenCHAMI/configurator/pkg/generator"
"github.com/OpenCHAMI/configurator/pkg/util"
)
@ -22,8 +21,6 @@ func (g *Powerman) GetDescription() string {
return fmt.Sprintf("Configurator generator plugin for '%s'.", g.GetName())
}
func (g *Powerman) Generate(config *configurator.Config, opts ...util.Option) (generator.FileMap, error) {
func (g *Powerman) Generate(config *configurator.Config, opts ...util.Option) (FileMap, error) {
return nil, fmt.Errorf("plugin does not implement generation function")
}
var Generator Powerman

View file

@ -1,10 +1,9 @@
package main
package generator
import (
"fmt"
configurator "github.com/OpenCHAMI/configurator/pkg"
"github.com/OpenCHAMI/configurator/pkg/generator"
"github.com/OpenCHAMI/configurator/pkg/util"
)
@ -22,8 +21,6 @@ func (g *Syslog) GetDescription() string {
return fmt.Sprintf("Configurator generator plugin for '%s'.", g.GetName())
}
func (g *Syslog) Generate(config *configurator.Config, opts ...util.Option) (generator.FileMap, error) {
func (g *Syslog) Generate(config *configurator.Config, opts ...util.Option) (FileMap, error) {
return nil, fmt.Errorf("plugin does not implement generation function")
}
var Generator Syslog

View file

@ -1,4 +1,4 @@
package main
package generator
import (
"fmt"
@ -6,7 +6,6 @@ import (
"strings"
configurator "github.com/OpenCHAMI/configurator/pkg"
"github.com/OpenCHAMI/configurator/pkg/generator"
"github.com/OpenCHAMI/configurator/pkg/util"
)
@ -24,13 +23,13 @@ func (g *Warewulf) GetDescription() string {
return "Configurator generator plugin for 'warewulf' config files."
}
func (g *Warewulf) Generate(config *configurator.Config, opts ...util.Option) (generator.FileMap, error) {
func (g *Warewulf) Generate(config *configurator.Config, opts ...util.Option) (FileMap, error) {
var (
params = generator.GetParams(opts...)
client = generator.GetClient(params)
params = GetParams(opts...)
client = GetClient(params)
targetKey = params["target"].(string)
target = config.Targets[targetKey]
outputs = make(generator.FileMap, len(target.FilePaths)+len(target.TemplatePaths))
outputs = make(FileMap, len(target.FilePaths)+len(target.TemplatePaths))
)
// check if our client is included and is valid
@ -72,11 +71,11 @@ func (g *Warewulf) Generate(config *configurator.Config, opts ...util.Option) (g
nodeEntries := ""
// load files and templates and copy to outputs
files, err := generator.LoadFiles(target.FilePaths...)
files, err := LoadFiles(target.FilePaths...)
if err != nil {
return nil, fmt.Errorf("failed to load files: %v", err)
}
templates, err := generator.ApplyTemplateFromFiles(generator.Mappings{
templates, err := ApplyTemplateFromFiles(Mappings{
"node_entries": nodeEntries,
}, target.TemplatePaths...)
if err != nil {
@ -98,5 +97,3 @@ func (g *Warewulf) Generate(config *configurator.Config, opts ...util.Option) (g
return outputs, err
}
var Generator Warewulf