Merge pull request #7 from OpenCHAMI/improvements

Small miscellaneous improvements
This commit is contained in:
David Allen 2024-07-03 12:49:48 -06:00 committed by GitHub
commit c197afddce
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 53 additions and 20 deletions

View file

@ -62,7 +62,7 @@ type Generator interface {
} }
``` ```
A new plugin can be created by implementing the methods from interface and exporting a symbol with `Generator` as the name and the plugin struct as the type. The `GetName()` function returns the name that is used for looking up the corresponding template set in your config file. The `GetGroups()` function is used to look all of the groups that the plugin is included. The `Generate` function is where the magic happens to build the config file from a template. A new plugin can be created by implementing the methods from interface and exporting a symbol with `Generator` as the name and the plugin struct as the type. The `GetName()` function returns the name that is used for looking up the corresponding template set in your config file. It can also be included in the templated files with the default plugins using the `{{ plugin_name }}` in your template. The `GetVersion()` and `GetDescription()` functions returns the version and description of the plugin which can be included in the templated files using `{{ plugin_version }}` and `{{ plugin_description }}` respectively with the default plugins. The `Generate` function is where the magic happens to build the config file from a template.
```go ```go
package main package main
@ -83,7 +83,7 @@ func (g *MyGenerator) GetDescription() string {
return "This is an example plugin." return "This is an example plugin."
} }
func (g *MyGenerator) Generate(config *configurator.Config, opts ...util.Option) (map[string][]byte, error) { func (g *MyGenerator) Generate(config *configurator.Config, opts ...util.Option) (generator.Files, error) {
// do config generation stuff here... // do config generation stuff here...
var ( var (
params = generator.GetParams(opts...) params = generator.GetParams(opts...)
@ -92,16 +92,19 @@ func (g *MyGenerator) Generate(config *configurator.Config, opts ...util.Option)
) )
if client { if client {
eths, err := client.FetchEthernetInterfaces(opts...) eths, err := client.FetchEthernetInterfaces(opts...)
// ... blah, blah, blah, format output, and so on... // ... blah, blah, blah, check error, format output, and so on...
} }
// apply the substitutions to Jinja template and return output as byte array // apply the substitutions to Jinja template and return output as byte array
return generator.ApplyTemplate(path, generator.Mappings{ return generator.ApplyTemplate(path, generator.Mappings{
"hosts": output, "plugin_name": g.GetName(),
"plugin_version": g.GetVersion(),
"plugin_description": g.GetDescription(),
"output": output,
}) })
} }
// this MUST be named "Generator" for symbol lookup // this MUST be named "Generator" for symbol lookup in main driver
var Generator MyGenerator var Generator MyGenerator
``` ```

View file

@ -13,8 +13,9 @@ import (
) )
var ( var (
remoteHost string accessToken string
remotePort int remoteHost string
remotePort int
) )
var fetchCmd = &cobra.Command{ var fetchCmd = &cobra.Command{
@ -26,11 +27,16 @@ var fetchCmd = &cobra.Command{
logrus.Errorf("no '--host' argument set") logrus.Errorf("no '--host' argument set")
return return
} }
for _, target := range targets {
headers := map[string]string{}
if accessToken != "" {
headers["Authorization"] = "Bearer " + accessToken
}
for _, target := range targets {
// make a request for each target // make a request for each target
url := fmt.Sprintf("%s:%d/generate?target=%s", remoteHost, remotePort, target) url := fmt.Sprintf("%s:%d/generate?target=%s", remoteHost, remotePort, target)
res, body, err := util.MakeRequest(url, http.MethodGet, nil, nil) res, body, err := util.MakeRequest(url, http.MethodGet, nil, headers)
if err != nil { if err != nil {
logrus.Errorf("failed to make request: %v", err) logrus.Errorf("failed to make request: %v", err)
return return
@ -49,6 +55,7 @@ func init() {
fetchCmd.Flags().IntVar(&remotePort, "port", 3334, "set the remote configurator port") fetchCmd.Flags().IntVar(&remotePort, "port", 3334, "set the remote configurator port")
fetchCmd.Flags().StringSliceVar(&targets, "target", nil, "set the target configs to make") fetchCmd.Flags().StringSliceVar(&targets, "target", nil, "set the target configs to make")
fetchCmd.Flags().StringVarP(&outputPath, "output", "o", "", "set the output path for config targets") fetchCmd.Flags().StringVarP(&outputPath, "output", "o", "", "set the output path for config targets")
fetchCmd.Flags().StringVar(&accessToken, "access-token", "o", "", "set the output path for config targets")
rootCmd.AddCommand(fetchCmd) rootCmd.AddCommand(fetchCmd)
} }

View file

@ -1,5 +1,8 @@
# #
# This file was auto-generated by the OpenCHAMI "configurator" tool using the "{{name}}" plugin. # This file was auto-generated by the OpenCHAMI "configurator" tool using the following plugin:
# Name: {{ plugin_name }}
# Version: {{ plugin_version }}
# Description: {{ plugin_description }}
# #
# Source code: https://github.com/OpenCHAMI/configurator # Source code: https://github.com/OpenCHAMI/configurator
# Creating plugins: https://github.com/OpenCHAMI/configurator/blob/main/README.md#creating-generator-plugins # Creating plugins: https://github.com/OpenCHAMI/configurator/blob/main/README.md#creating-generator-plugins

View file

@ -1,5 +1,8 @@
# #
# This file was auto-generated by the OpenCHAMI "configurator" tool using the "{{name}}" plugin. # This file was auto-generated by the OpenCHAMI "configurator" tool using the following plugin:
# Name: {{ plugin_name }}
# Version: {{ plugin_version }}
# Description: {{ plugin_description }}
# #
# Source code: https://github.com/OpenCHAMI/configurator # Source code: https://github.com/OpenCHAMI/configurator
# Creating plugins: https://github.com/OpenCHAMI/configurator/blob/main/README.md#creating-generator-plugins # Creating plugins: https://github.com/OpenCHAMI/configurator/blob/main/README.md#creating-generator-plugins

View file

@ -1,7 +1,10 @@
# #
# This file was auto-generated by the OpenCHAMI "configurator" tool using the "{{name}}" plugin. # This file was auto-generated by the OpenCHAMI "configurator" tool using the following plugin:
# Name: {{ plugin_name }}
# Version: {{ plugin_version }}
# Description: {{ plugin_description }}
# #
# Source code: https://github.com/OpenCHAMI/configurator # Source code: https://github.com/OpenCHAMI/configurator
# Creating plugins: https://github.com/OpenCHAMI/configurator/blob/main/README.md#creating-generator-plugins # Creating plugins: https://github.com/OpenCHAMI/configurator/blob/main/README.md#creating-generator-plugins
# #
{{ output }} {{ dhcp-hosts }}

View file

@ -1,5 +1,11 @@
# #
# Ansible managed # This file was auto-generated by the OpenCHAMI "configurator" tool using the following plugin:
# Name: {{ plugin_name }}
# Version: {{ plugin_version }}
# Description: {{ plugin_description }}
#
# Source code: https://github.com/OpenCHAMI/configurator
# Creating plugins: https://github.com/OpenCHAMI/configurator/blob/main/README.md#creating-generator-plugins
# #
include "/etc/powerman/ipmipower.dev" include "/etc/powerman/ipmipower.dev"
include "/etc/powerman/ipmi.dev" include "/etc/powerman/ipmi.dev"

View file

@ -61,8 +61,11 @@ func (g *Conman) Generate(config *configurator.Config, opts ...util.Option) (map
// apply template substitutions and return output as byte array // apply template substitutions and return output as byte array
return generator.ApplyTemplates(generator.Mappings{ return generator.ApplyTemplates(generator.Mappings{
"server_opts": "", "plugin_name": g.GetName(),
"global_opts": "", "plugin_version": g.GetVersion(),
"plugin_description": g.GetDescription(),
"server_opts": "",
"global_opts": "",
}, target.Templates...) }, target.Templates...)
} }

View file

@ -65,8 +65,11 @@ func (g *Dhcpd) Generate(config *configurator.Config, opts ...util.Option) (gene
} }
} }
return generator.ApplyTemplates(generator.Mappings{ return generator.ApplyTemplates(generator.Mappings{
"compute_nodes": compute_nodes, "plugin_name": g.GetName(),
"node_entries": "", "plugin_version": g.GetVersion(),
"plugin_description": g.GetDescription(),
"compute_nodes": compute_nodes,
"node_entries": "",
}, target.Templates...) }, target.Templates...)
} }

View file

@ -75,8 +75,10 @@ func (g *DnsMasq) Generate(config *configurator.Config, opts ...util.Option) (ma
// apply template substitutions and return output as byte array // apply template substitutions and return output as byte array
return generator.ApplyTemplates(generator.Mappings{ return generator.ApplyTemplates(generator.Mappings{
"name": g.GetName(), "plugin_name": g.GetName(),
"output": output, "plugin_version": g.GetVersion(),
"plugin_description": g.GetDescription(),
"dhcp-hosts": output,
}, target.Templates...) }, target.Templates...)
} }