Merge pull request #3 from OpenCHAMI/improvements
General improvements to generator
This commit is contained in:
commit
319f29dbb0
4 changed files with 77 additions and 15 deletions
|
|
@ -6,6 +6,7 @@ package cmd
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
configurator "github.com/OpenCHAMI/configurator/internal"
|
configurator "github.com/OpenCHAMI/configurator/internal"
|
||||||
|
|
@ -15,12 +16,12 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
targets []string
|
|
||||||
tokenFetchRetries int
|
tokenFetchRetries int
|
||||||
)
|
)
|
||||||
|
|
||||||
var generateCmd = &cobra.Command{
|
var generateCmd = &cobra.Command{
|
||||||
Use: "generate",
|
Use: "generate",
|
||||||
Short: "Create a config file from current system state",
|
Short: "Generate a config file from system state",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
client := configurator.SmdClient{
|
client := configurator.SmdClient{
|
||||||
Host: config.SmdHost,
|
Host: config.SmdHost,
|
||||||
|
|
@ -30,12 +31,14 @@ var generateCmd = &cobra.Command{
|
||||||
|
|
||||||
// make sure that we have a token present before trying to make request
|
// make sure that we have a token present before trying to make request
|
||||||
if config.AccessToken == "" {
|
if config.AccessToken == "" {
|
||||||
// check if OCHAMI_ACCESS_TOKEN env var is set if no access token is provided and use that instead
|
// TODO: make request to check if request will need token
|
||||||
|
|
||||||
|
// check if OCHAMI_ACCESS_TOKEN env var is set if no access token is provided and use that instead
|
||||||
accessToken := os.Getenv("OCHAMI_ACCESS_TOKEN")
|
accessToken := os.Getenv("OCHAMI_ACCESS_TOKEN")
|
||||||
if accessToken != "" {
|
if accessToken != "" {
|
||||||
config.AccessToken = accessToken
|
config.AccessToken = accessToken
|
||||||
} else {
|
} else {
|
||||||
|
// TODO: try and fetch token first if it is needed
|
||||||
fmt.Printf("No token found. Attempting to generate config without one...\n")
|
fmt.Printf("No token found. Attempting to generate config without one...\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -43,27 +46,61 @@ var generateCmd = &cobra.Command{
|
||||||
if targets == nil {
|
if targets == nil {
|
||||||
logrus.Errorf("no target supplied (--target type:template)")
|
logrus.Errorf("no target supplied (--target type:template)")
|
||||||
} else {
|
} else {
|
||||||
|
// if we have more than one target and output is set, create configs in directory
|
||||||
|
targetCount := len(targets)
|
||||||
|
if outputPath != "" && targetCount > 1 {
|
||||||
|
err := os.MkdirAll(outputPath, 0o755)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Errorf("failed to make output directory: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for _, target := range targets {
|
for _, target := range targets {
|
||||||
// split the target and type
|
// split the target and type
|
||||||
tmp := strings.Split(target, ":")
|
tmp := strings.Split(target, ":")
|
||||||
|
|
||||||
|
// make sure each target has at least two args
|
||||||
|
if len(tmp) < 2 {
|
||||||
|
message := "target"
|
||||||
|
if len(tmp) == 1 {
|
||||||
|
message += fmt.Sprintf(" '%s'", tmp[1])
|
||||||
|
}
|
||||||
|
message += " does not provide enough arguments (args: \"type:template\")"
|
||||||
|
logrus.Errorf(message)
|
||||||
|
continue
|
||||||
|
}
|
||||||
g := generator.Generator{
|
g := generator.Generator{
|
||||||
Type: tmp[0],
|
Type: tmp[0],
|
||||||
Template: tmp[1],
|
Template: tmp[1],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if another param is specified
|
||||||
|
targetPath := ""
|
||||||
|
if len(tmp) > 2 {
|
||||||
|
targetPath = tmp[2]
|
||||||
|
}
|
||||||
|
|
||||||
// NOTE: we probably don't want to hardcode the types, but should do for now
|
// NOTE: we probably don't want to hardcode the types, but should do for now
|
||||||
|
ext := ""
|
||||||
|
contents := []byte{}
|
||||||
if g.Type == "dhcp" {
|
if g.Type == "dhcp" {
|
||||||
// fetch eths from SMD
|
// fetch eths from SMD
|
||||||
eths, err := client.FetchEthernetInterfaces()
|
eths, err := client.FetchEthernetInterfaces()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("failed to fetch DHCP metadata: %v\n", err)
|
logrus.Errorf("failed to fetch DHCP metadata: %v\n", err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
if len(eths) <= 0 {
|
if len(eths) <= 0 {
|
||||||
break
|
continue
|
||||||
}
|
}
|
||||||
// generate a new config from that data
|
// generate a new config from that data
|
||||||
|
contents, err = g.GenerateDHCP(&config, eths)
|
||||||
g.GenerateDHCP(&config, eths)
|
if err != nil {
|
||||||
|
logrus.Errorf("failed to generate DHCP config file: %v\n", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ext = "conf"
|
||||||
} else if g.Type == "dns" {
|
} else if g.Type == "dns" {
|
||||||
// TODO: fetch from SMD
|
// TODO: fetch from SMD
|
||||||
// TODO: generate config from pulled info
|
// TODO: generate config from pulled info
|
||||||
|
|
@ -76,14 +113,35 @@ var generateCmd = &cobra.Command{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// write config output if no specific targetPath is set
|
||||||
|
if targetPath == "" {
|
||||||
|
if outputPath == "" {
|
||||||
|
// write only to stdout
|
||||||
|
fmt.Printf("%s\n", "")
|
||||||
|
} else if outputPath != "" && targetCount == 1 {
|
||||||
|
// write just a single file using template name
|
||||||
|
err := os.WriteFile(outputPath, contents, 0o644)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Errorf("failed to write config to file: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
} else if outputPath != "" && targetCount > 1 {
|
||||||
|
// write multiple files in directory using template name
|
||||||
|
err := os.WriteFile(fmt.Sprintf("%s/%s.%s", filepath.Clean(outputPath), g.Template, ext), contents, 0o644)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Errorf("failed to write config to file: %v", err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
} // for targets
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
generateCmd.Flags().StringSliceVar(&targets, "target", nil, "set the target configs to make")
|
generateCmd.Flags().StringSliceVar(&targets, "target", nil, "set the target configs to make")
|
||||||
|
generateCmd.Flags().StringVarP(&outputPath, "output", "o", "", "set the output path for config targets")
|
||||||
generateCmd.Flags().IntVar(&tokenFetchRetries, "fetch-retries", 5, "set the number of retries to fetch an access token")
|
generateCmd.Flags().IntVar(&tokenFetchRetries, "fetch-retries", 5, "set the number of retries to fetch an access token")
|
||||||
|
|
||||||
rootCmd.AddCommand(generateCmd)
|
rootCmd.AddCommand(generateCmd)
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ import (
|
||||||
var (
|
var (
|
||||||
configPath string
|
configPath string
|
||||||
config configurator.Config
|
config configurator.Config
|
||||||
|
targets []string
|
||||||
|
outputPath string
|
||||||
)
|
)
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
package generator
|
package generator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"bytes"
|
||||||
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
|
@ -26,13 +26,13 @@ func (g *Generator) GenerateDNS(config *configurator.Config) {
|
||||||
// TODO: print generated config file to STDOUT
|
// TODO: print generated config file to STDOUT
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Generator) GenerateDHCP(config *configurator.Config, eths []configurator.EthernetInterface) error {
|
func (g *Generator) GenerateDHCP(config *configurator.Config, eths []configurator.EthernetInterface) ([]byte, error) {
|
||||||
// generate file using gonja template
|
// generate file using gonja template
|
||||||
path := config.TemplatePaths[g.Template]
|
path := config.TemplatePaths[g.Template]
|
||||||
fmt.Printf("path: %s\neth count: %v\n", path, len(eths))
|
fmt.Printf("path: %s\neth count: %v\n", path, len(eths))
|
||||||
t, err := gonja.FromFile(path)
|
t, err := gonja.FromFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to read template from file: %v", err)
|
return nil, fmt.Errorf("failed to read template from file: %v", err)
|
||||||
}
|
}
|
||||||
template := "# ========== GENERATED BY OCHAMI CONFIGURATOR ==========\n"
|
template := "# ========== GENERATED BY OCHAMI CONFIGURATOR ==========\n"
|
||||||
for _, eth := range eths {
|
for _, eth := range eths {
|
||||||
|
|
@ -46,9 +46,10 @@ func (g *Generator) GenerateDHCP(config *configurator.Config, eths []configurato
|
||||||
data := exec.NewContext(map[string]any{
|
data := exec.NewContext(map[string]any{
|
||||||
"hosts": template,
|
"hosts": template,
|
||||||
})
|
})
|
||||||
if err = t.Execute(os.Stdout, data); err != nil {
|
b := bytes.Buffer{}
|
||||||
return fmt.Errorf("failed to execute: %v", err)
|
if err = t.Execute(&b, data); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to execute: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return b.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -86,12 +86,13 @@ func (s *Server) Start(config *configurator.Config) error {
|
||||||
}
|
}
|
||||||
// generate a new config from that data
|
// generate a new config from that data
|
||||||
|
|
||||||
err = g.GenerateDHCP(config, eths)
|
b, err := g.GenerateDHCP(config, eths)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("failed to generate DHCP: %v", err)
|
logrus.Errorf("failed to generate DHCP: %v", err)
|
||||||
w.Write([]byte("An error has occurred."))
|
w.Write([]byte("An error has occurred."))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
w.Write(b)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
r.HandleFunc("/templates", func(w http.ResponseWriter, r *http.Request) {
|
r.HandleFunc("/templates", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue