From af5e330731c9217655076f3297791742cd81f09f Mon Sep 17 00:00:00 2001 From: David Allen Date: Wed, 3 Jul 2024 12:46:35 -0600 Subject: [PATCH 1/4] Updated example Jinja templates --- examples/templates/conman.jinja | 5 ++++- examples/templates/dhcpd.jinja | 5 ++++- examples/templates/dnsmasq.jinja | 7 +++++-- examples/templates/powerman.jinja | 8 +++++++- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/examples/templates/conman.jinja b/examples/templates/conman.jinja index 6f21d7a..249994c 100644 --- a/examples/templates/conman.jinja +++ b/examples/templates/conman.jinja @@ -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 diff --git a/examples/templates/dhcpd.jinja b/examples/templates/dhcpd.jinja index 377b244..5613f73 100644 --- a/examples/templates/dhcpd.jinja +++ b/examples/templates/dhcpd.jinja @@ -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 diff --git a/examples/templates/dnsmasq.jinja b/examples/templates/dnsmasq.jinja index 0b50fac..c522039 100644 --- a/examples/templates/dnsmasq.jinja +++ b/examples/templates/dnsmasq.jinja @@ -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 }} diff --git a/examples/templates/powerman.jinja b/examples/templates/powerman.jinja index 11b1e8c..5808124 100644 --- a/examples/templates/powerman.jinja +++ b/examples/templates/powerman.jinja @@ -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" From c63e9745bbb2475fc50df0a70bef287d435badb3 Mon Sep 17 00:00:00 2001 From: David Allen Date: Wed, 3 Jul 2024 12:47:14 -0600 Subject: [PATCH 2/4] Added access-token CLI flag for fetch command --- cmd/fetch.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/cmd/fetch.go b/cmd/fetch.go index 110f03c..956e3bc 100644 --- a/cmd/fetch.go +++ b/cmd/fetch.go @@ -13,8 +13,9 @@ import ( ) var ( - remoteHost string - remotePort int + accessToken string + remoteHost string + remotePort int ) var fetchCmd = &cobra.Command{ @@ -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) } From 7c266e6406b5ca6e6ab77c61c40cd621205e8717 Mon Sep 17 00:00:00 2001 From: David Allen Date: Wed, 3 Jul 2024 12:47:35 -0600 Subject: [PATCH 3/4] Updated README.md --- README.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 78a971a..282b218 100644 --- a/README.md +++ b/README.md @@ -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 ``` From f8f76342ac19a6ca3cb00c6b65e39d524bcb207e Mon Sep 17 00:00:00 2001 From: David Allen Date: Wed, 3 Jul 2024 12:48:39 -0600 Subject: [PATCH 4/4] Updated generator plugins --- internal/generator/plugins/conman/conman.go | 7 +++++-- internal/generator/plugins/dhcpd/dhcpd.go | 7 +++++-- internal/generator/plugins/dnsmasq/dnsmasq.go | 6 ++++-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/internal/generator/plugins/conman/conman.go b/internal/generator/plugins/conman/conman.go index 4fcef2c..c83d26a 100644 --- a/internal/generator/plugins/conman/conman.go +++ b/internal/generator/plugins/conman/conman.go @@ -61,8 +61,11 @@ 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{ - "server_opts": "", - "global_opts": "", + "plugin_name": g.GetName(), + "plugin_version": g.GetVersion(), + "plugin_description": g.GetDescription(), + "server_opts": "", + "global_opts": "", }, target.Templates...) } diff --git a/internal/generator/plugins/dhcpd/dhcpd.go b/internal/generator/plugins/dhcpd/dhcpd.go index a56732e..1990abc 100644 --- a/internal/generator/plugins/dhcpd/dhcpd.go +++ b/internal/generator/plugins/dhcpd/dhcpd.go @@ -65,8 +65,11 @@ func (g *Dhcpd) Generate(config *configurator.Config, opts ...util.Option) (gene } } return generator.ApplyTemplates(generator.Mappings{ - "compute_nodes": compute_nodes, - "node_entries": "", + "plugin_name": g.GetName(), + "plugin_version": g.GetVersion(), + "plugin_description": g.GetDescription(), + "compute_nodes": compute_nodes, + "node_entries": "", }, target.Templates...) } diff --git a/internal/generator/plugins/dnsmasq/dnsmasq.go b/internal/generator/plugins/dnsmasq/dnsmasq.go index b1b71a7..0b13bf5 100644 --- a/internal/generator/plugins/dnsmasq/dnsmasq.go +++ b/internal/generator/plugins/dnsmasq/dnsmasq.go @@ -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...) }