mirror of
https://github.com/davidallendj/configurator.git
synced 2025-12-20 11:37:00 -07:00
Minor changes
This commit is contained in:
parent
e14a27cf84
commit
02406dec9f
6 changed files with 41 additions and 43 deletions
|
|
@ -11,7 +11,7 @@ import (
|
||||||
type Options struct{}
|
type Options struct{}
|
||||||
|
|
||||||
type Target struct {
|
type Target struct {
|
||||||
Templates []string `yaml:"templates,omitempty"`
|
TemplatePaths []string `yaml:"templates,omitempty"`
|
||||||
FilePaths []string `yaml:"files,omitempty"`
|
FilePaths []string `yaml:"files,omitempty"`
|
||||||
RunTargets []string `yaml:"targets,omitempty"`
|
RunTargets []string `yaml:"targets,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
@ -48,13 +48,13 @@ func NewConfig() Config {
|
||||||
},
|
},
|
||||||
Targets: map[string]Target{
|
Targets: map[string]Target{
|
||||||
"dnsmasq": Target{
|
"dnsmasq": Target{
|
||||||
Templates: []string{},
|
TemplatePaths: []string{},
|
||||||
},
|
},
|
||||||
"conman": Target{
|
"conman": Target{
|
||||||
Templates: []string{},
|
TemplatePaths: []string{},
|
||||||
},
|
},
|
||||||
"warewulf": Target{
|
"warewulf": Target{
|
||||||
Templates: []string{
|
TemplatePaths: []string{
|
||||||
"templates/warewulf/defaults/node.jinja",
|
"templates/warewulf/defaults/node.jinja",
|
||||||
"templates/warewulf/defaults/provision.jinja",
|
"templates/warewulf/defaults/provision.jinja",
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,6 @@ import (
|
||||||
"github.com/OpenCHAMI/configurator/pkg/util"
|
"github.com/OpenCHAMI/configurator/pkg/util"
|
||||||
"github.com/nikolalohinski/gonja/v2"
|
"github.com/nikolalohinski/gonja/v2"
|
||||||
"github.com/nikolalohinski/gonja/v2/exec"
|
"github.com/nikolalohinski/gonja/v2/exec"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Mappings map[string]any
|
type Mappings map[string]any
|
||||||
|
|
@ -32,6 +31,7 @@ type Generator interface {
|
||||||
type Params struct {
|
type Params struct {
|
||||||
Args []string
|
Args []string
|
||||||
PluginPaths []string
|
PluginPaths []string
|
||||||
|
Generators map[string]Generator
|
||||||
Target string
|
Target string
|
||||||
Verbose bool
|
Verbose bool
|
||||||
}
|
}
|
||||||
|
|
@ -259,14 +259,14 @@ func ApplyTemplateFromFiles(mappings Mappings, paths ...string) (FileMap, error)
|
||||||
|
|
||||||
// Main function to generate a collection of files as a map with the path as the key and
|
// Main function to generate a collection of files as a map with the path as the key and
|
||||||
// the contents of the file as the value. This function currently expects a list of plugin
|
// the contents of the file as the value. This function currently expects a list of plugin
|
||||||
// paths to load all plugins within a directory. Then, each plugin's generator.Generate()
|
// paths to load all plugins within a directory. Then, each plugin's generator.GenerateWithTarget()
|
||||||
// function is called for each target specified.
|
// function is called for each target specified.
|
||||||
//
|
//
|
||||||
// This function is the corresponding implementation for the "generate" CLI subcommand.
|
// This function is the corresponding implementation for the "generate" CLI subcommand.
|
||||||
// It is also call when running the configurator as a service with the "/generate" route.
|
// It is also call when running the configurator as a service with the "/generate" route.
|
||||||
//
|
//
|
||||||
// TODO: Separate loading plugins so we can load them once when running as a service.
|
// TODO: Separate loading plugins so we can load them once when running as a service.
|
||||||
func Generate(config *configurator.Config, params Params) (FileMap, error) {
|
func GenerateWithTarget(config *configurator.Config, params Params) (FileMap, error) {
|
||||||
// load generator plugins to generate configs or to print
|
// load generator plugins to generate configs or to print
|
||||||
var (
|
var (
|
||||||
generators = make(map[string]Generator)
|
generators = make(map[string]Generator)
|
||||||
|
|
@ -278,12 +278,12 @@ func Generate(config *configurator.Config, params Params) (FileMap, error) {
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
// load all plugins from params
|
// load all plugins from supplied arguments
|
||||||
for _, path := range params.PluginPaths {
|
for _, path := range params.PluginPaths {
|
||||||
if params.Verbose {
|
if params.Verbose {
|
||||||
fmt.Printf("loading plugins from '%s'\n", path)
|
fmt.Printf("loading plugins from '%s'\n", path)
|
||||||
}
|
}
|
||||||
gens, err := LoadPlugins(path)
|
plugins, err := LoadPlugins(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("failed to load plugins: %v\n", err)
|
fmt.Printf("failed to load plugins: %v\n", err)
|
||||||
err = nil
|
err = nil
|
||||||
|
|
@ -291,9 +291,12 @@ func Generate(config *configurator.Config, params Params) (FileMap, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// add loaded generator plugins to set
|
// add loaded generator plugins to set
|
||||||
maps.Copy(generators, gens)
|
maps.Copy(generators, plugins)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// copy all generators supplied from arguments
|
||||||
|
maps.Copy(generators, params.Generators)
|
||||||
|
|
||||||
// show available targets then exit
|
// show available targets then exit
|
||||||
if len(params.Args) == 0 && params.Target == "" {
|
if len(params.Args) == 0 && params.Target == "" {
|
||||||
for g := range generators {
|
for g := range generators {
|
||||||
|
|
@ -302,9 +305,6 @@ func Generate(config *configurator.Config, params Params) (FileMap, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.Target == "" {
|
|
||||||
logrus.Errorf("no target supplied (--target name)")
|
|
||||||
} else {
|
|
||||||
// run the generator plugin from target passed
|
// run the generator plugin from target passed
|
||||||
gen := generators[params.Target]
|
gen := generators[params.Target]
|
||||||
if gen == nil {
|
if gen == nil {
|
||||||
|
|
@ -316,5 +316,3 @@ func Generate(config *configurator.Config, params Params) (FileMap, error) {
|
||||||
WithClient(client),
|
WithClient(client),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("an unknown error has occurred")
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -62,7 +62,7 @@ func (g *Conman) Generate(config *configurator.Config, opts ...util.Option) (gen
|
||||||
"plugin_description": g.GetDescription(),
|
"plugin_description": g.GetDescription(),
|
||||||
"server_opts": "",
|
"server_opts": "",
|
||||||
"global_opts": "",
|
"global_opts": "",
|
||||||
}, target.Templates...)
|
}, target.TemplatePaths...)
|
||||||
}
|
}
|
||||||
|
|
||||||
var Generator Conman
|
var Generator Conman
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ func (g *DnsMasq) Generate(config *configurator.Config, opts ...util.Option) (ge
|
||||||
// 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("template: \n%s\nethernet interfaces found: %v\n", strings.Join(target.Templates, "\n\t"), len(eths))
|
fmt.Printf("template: \n%s\nethernet interfaces found: %v\n", strings.Join(target.TemplatePaths, "\n\t"), len(eths))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -79,7 +79,7 @@ func (g *DnsMasq) Generate(config *configurator.Config, opts ...util.Option) (ge
|
||||||
"plugin_version": g.GetVersion(),
|
"plugin_version": g.GetVersion(),
|
||||||
"plugin_description": g.GetDescription(),
|
"plugin_description": g.GetDescription(),
|
||||||
"dhcp-hosts": output,
|
"dhcp-hosts": output,
|
||||||
}, target.Templates...)
|
}, target.TemplatePaths...)
|
||||||
}
|
}
|
||||||
|
|
||||||
var Generator DnsMasq
|
var Generator DnsMasq
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,9 @@ import (
|
||||||
"maps"
|
"maps"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
configurator "github.com/OpenCHAMI/configurator/internal"
|
configurator "github.com/OpenCHAMI/configurator/pkg"
|
||||||
"github.com/OpenCHAMI/configurator/internal/generator"
|
"github.com/OpenCHAMI/configurator/pkg/generator"
|
||||||
"github.com/OpenCHAMI/configurator/internal/util"
|
"github.com/OpenCHAMI/configurator/pkg/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Warewulf struct{}
|
type Warewulf struct{}
|
||||||
|
|
@ -30,7 +30,7 @@ func (g *Warewulf) Generate(config *configurator.Config, opts ...util.Option) (g
|
||||||
client = generator.GetClient(params)
|
client = generator.GetClient(params)
|
||||||
targetKey = params["target"].(string)
|
targetKey = params["target"].(string)
|
||||||
target = config.Targets[targetKey]
|
target = config.Targets[targetKey]
|
||||||
outputs = make(generator.FileMap, len(target.FilePaths)+len(target.Templates))
|
outputs = make(generator.FileMap, len(target.FilePaths)+len(target.TemplatePaths))
|
||||||
)
|
)
|
||||||
|
|
||||||
// check if our client is included and is valid
|
// check if our client is included and is valid
|
||||||
|
|
@ -55,7 +55,7 @@ func (g *Warewulf) Generate(config *configurator.Config, opts ...util.Option) (g
|
||||||
// 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("template: \n%s\n ethernet interfaces found: %v\n", strings.Join(target.Templates, "\n\t"), len(eths))
|
fmt.Printf("template: \n%s\n ethernet interfaces found: %v\n", strings.Join(target.TemplatePaths, "\n\t"), len(eths))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -78,7 +78,7 @@ func (g *Warewulf) Generate(config *configurator.Config, opts ...util.Option) (g
|
||||||
}
|
}
|
||||||
templates, err := generator.ApplyTemplateFromFiles(generator.Mappings{
|
templates, err := generator.ApplyTemplateFromFiles(generator.Mappings{
|
||||||
"node_entries": nodeEntries,
|
"node_entries": nodeEntries,
|
||||||
}, target.Templates...)
|
}, target.TemplatePaths...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to load templates: %v", err)
|
return nil, fmt.Errorf("failed to load templates: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -119,14 +119,14 @@ func (s *Server) Generate(w http.ResponseWriter, r *http.Request) {
|
||||||
// get all of the expect query URL params and validate
|
// get all of the expect query URL params and validate
|
||||||
s.GeneratorParams.Target = r.URL.Query().Get("target")
|
s.GeneratorParams.Target = r.URL.Query().Get("target")
|
||||||
if s.GeneratorParams.Target == "" {
|
if s.GeneratorParams.Target == "" {
|
||||||
writeError(w, "no targets supplied")
|
writeErrorResponse(w, "no targets supplied")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// generate a new config file from supplied params
|
// generate a new config file from supplied params
|
||||||
outputs, err := generator.Generate(s.Config, s.GeneratorParams)
|
outputs, err := generator.GenerateWithTarget(s.Config, s.GeneratorParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, "failed to generate config: %v", err)
|
writeErrorResponse(w, "failed to generate config: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -134,12 +134,12 @@ func (s *Server) Generate(w http.ResponseWriter, r *http.Request) {
|
||||||
tmp := generator.ConvertContentsToString(outputs)
|
tmp := generator.ConvertContentsToString(outputs)
|
||||||
b, err := json.Marshal(tmp)
|
b, err := json.Marshal(tmp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, "failed to marshal output: %v", err)
|
writeErrorResponse(w, "failed to marshal output: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, err = w.Write(b)
|
_, err = w.Write(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, "failed to write response: %v", err)
|
writeErrorResponse(w, "failed to write response: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -151,15 +151,15 @@ func (s *Server) Generate(w http.ResponseWriter, r *http.Request) {
|
||||||
func (s *Server) ManageTemplates(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) ManageTemplates(w http.ResponseWriter, r *http.Request) {
|
||||||
_, err := w.Write([]byte("this is not implemented yet"))
|
_, err := w.Write([]byte("this is not implemented yet"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, "failed to write response: %v", err)
|
writeErrorResponse(w, "failed to write response: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wrapper function to simplify writting error message responses. This function
|
// Wrapper function to simplify writting error message responses. This function
|
||||||
// is only intended to be used with the service and nothing else.
|
// is only intended to be used with the service and nothing else.
|
||||||
func writeError(w http.ResponseWriter, format string, a ...any) {
|
func writeErrorResponse(w http.ResponseWriter, format string, a ...any) error {
|
||||||
errmsg := fmt.Sprintf(format, a...)
|
errmsg := fmt.Sprintf(format, a...)
|
||||||
fmt.Printf(errmsg)
|
|
||||||
w.Write([]byte(errmsg))
|
w.Write([]byte(errmsg))
|
||||||
|
return fmt.Errorf(errmsg)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue