Added project files
This commit is contained in:
parent
40615a97f8
commit
be40387f4a
10 changed files with 486 additions and 0 deletions
85
cmd/generate.go
Normal file
85
cmd/generate.go
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
configurator "github.com/OpenCHAMI/configurator/internal"
|
||||
"github.com/OpenCHAMI/configurator/internal/generator"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
targets []string
|
||||
tokenFetchRetries int
|
||||
)
|
||||
var generateCmd = &cobra.Command{
|
||||
Use: "generate",
|
||||
Short: "Create a config file from current system state",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
client := configurator.SmdClient{
|
||||
Host: config.SmdHost,
|
||||
Port: config.SmdPort,
|
||||
AccessToken: config.AccessToken,
|
||||
}
|
||||
|
||||
// make sure that we have a token present before trying to make request
|
||||
if config.AccessToken == "" {
|
||||
// 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")
|
||||
if accessToken != "" {
|
||||
config.AccessToken = accessToken
|
||||
} else {
|
||||
fmt.Printf("No token found. Attempting to generate config without one...\n")
|
||||
}
|
||||
}
|
||||
|
||||
if targets == nil {
|
||||
logrus.Errorf("no target supplied (--target type:template)")
|
||||
} else {
|
||||
for _, target := range targets {
|
||||
// split the target and type
|
||||
tmp := strings.Split(target, ":")
|
||||
configType := tmp[0]
|
||||
configTemplate := tmp[1]
|
||||
|
||||
// NOTE: we probably don't want to hardcode the types, but should do for now
|
||||
if configType == "dhcp" {
|
||||
// fetch eths from SMD
|
||||
eths, err := client.FetchEthernetInterfaces()
|
||||
if err != nil {
|
||||
logrus.Errorf("failed to fetch DHCP metadata: %v\n", err)
|
||||
}
|
||||
if len(eths) <= 0 {
|
||||
break
|
||||
}
|
||||
// generate a new config from that data
|
||||
g := generator.New()
|
||||
g.GenerateDHCP(config, configTemplate, eths)
|
||||
} else if configType == "dns" {
|
||||
// TODO: fetch from SMD
|
||||
// TODO: generate config from pulled info
|
||||
|
||||
} else if configType == "syslog" {
|
||||
|
||||
} else if configType == "ansible" {
|
||||
|
||||
} else if configType == "warewulf" {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
generateCmd.Flags().StringSliceVar(&targets, "target", nil, "set the target configs to make")
|
||||
generateCmd.Flags().IntVar(&tokenFetchRetries, "fetch-retries", 5, "set the number of retries to fetch an access token")
|
||||
|
||||
rootCmd.AddCommand(generateCmd)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue