Skip to content

Commit

Permalink
Merge pull request #21 from OpenCHAMI/20-convert-plugins
Browse files Browse the repository at this point in the history
Build default plugins directly into the binary executable
  • Loading branch information
alexlovelltroy authored Nov 14, 2024
2 parents 9328d2a + 9f6a8ac commit a55ecf2
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 92 deletions.
8 changes: 5 additions & 3 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ before:
- go mod download
- make plugins
builds:
- env:
- CGO_ENABLED=1
- id: "configurator"
goos:
- linux
- 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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
Expand Down Expand Up @@ -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(),
Expand All @@ -65,5 +64,3 @@ func (g *Conman) Generate(config *configurator.Config, opts ...util.Option) (gen
"consoles": consoles,
}, target.TemplatePaths...)
}

var Generator Conman
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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
21 changes: 9 additions & 12 deletions pkg/generator/plugins/dhcpd/dhcpd.go → pkg/generator/dhcpd.go
Original file line number Diff line number Diff line change
@@ -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 = ""
Expand Down Expand Up @@ -64,13 +63,11 @@ 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(),
"compute_nodes": compute_nodes,
"node_entries": "",
}, target.TemplatePaths...)
}

var Generator Dhcpd
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
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)")
}

// 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
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package main
//go:build example || plugins
// +build example plugins

package generator

import (
"fmt"
Expand Down
77 changes: 51 additions & 26 deletions pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

// Generator interface used to define how files are created. Plugins can
// be created entirely independent of the main driver program.
type Generator interface {
GetName() string
GetVersion() string
GetDescription() string
Generate(config *configurator.Config, opts ...util.Option) (FileMap, error)
}
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.
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 {
Args []string
Generators map[string]Generator
TemplatePaths []string
PluginPath string
Target string
Verbose bool
// Params defined and used by the "generate" subcommand.
Params struct {
Args []string
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.
Expand Down Expand Up @@ -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
Expand All @@ -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")
}
Expand All @@ -415,10 +436,14 @@ 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)
if err != nil {
return nil, fmt.Errorf("failed to load plugin: %w", err)
// 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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
Loading

0 comments on commit a55ecf2

Please sign in to comment.