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
package main
@ -83,7 +83,7 @@ func (g *MyGenerator) GetDescription() string {
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...
var (
params = generator.GetParams(opts...)
@ -92,16 +92,19 @@ func (g *MyGenerator) Generate(config *configurator.Config, opts ...util.Option)
)
if client {
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
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
```

View file

@ -13,6 +13,7 @@ import (
)
var (
accessToken string
remoteHost string
remotePort int
)
@ -26,11 +27,16 @@ var fetchCmd = &cobra.Command{
logrus.Errorf("no '--host' argument set")
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
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 {
logrus.Errorf("failed to make request: %v", err)
return
@ -49,6 +55,7 @@ func init() {
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().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)
}

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
# 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
# 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
# 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/ipmi.dev"

View file

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

View file

@ -65,6 +65,9 @@ func (g *Dhcpd) Generate(config *configurator.Config, opts ...util.Option) (gene
}
}
return generator.ApplyTemplates(generator.Mappings{
"plugin_name": g.GetName(),
"plugin_version": g.GetVersion(),
"plugin_description": g.GetDescription(),
"compute_nodes": compute_nodes,
"node_entries": "",
}, 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
return generator.ApplyTemplates(generator.Mappings{
"name": g.GetName(),
"output": output,
"plugin_name": g.GetName(),
"plugin_version": g.GetVersion(),
"plugin_description": g.GetDescription(),
"dhcp-hosts": output,
}, target.Templates...)
}