Added project files

This commit is contained in:
David J. Allen 2024-03-07 17:00:39 -07:00
parent 40615a97f8
commit be40387f4a
No known key found for this signature in database
GPG key ID: 717C593FF60A2ACC
10 changed files with 486 additions and 0 deletions

30
cmd/config.go Normal file
View file

@ -0,0 +1,30 @@
package cmd
import (
"fmt"
"github.com/spf13/cobra"
configurator "github.com/OpenCHAMI/configurator/internal"
"github.com/OpenCHAMI/configurator/internal/util"
)
var configCmd = &cobra.Command{
Use: "config",
Short: "Create a new default config file",
Run: func(cmd *cobra.Command, args []string) {
// create a new config at all args (paths)
for _, path := range args {
// check and make sure something doesn't exist first
if exists, err := util.PathExists(path); exists || err != nil {
fmt.Printf("file or directory exists\n")
continue
}
configurator.SaveDefaultConfig(path)
}
},
}
func init() {
rootCmd.AddCommand(configCmd)
}

85
cmd/generate.go Normal file
View 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)
}

54
cmd/root.go Normal file
View file

@ -0,0 +1,54 @@
package cmd
import (
"fmt"
"os"
configurator "github.com/OpenCHAMI/configurator/internal"
"github.com/OpenCHAMI/configurator/internal/util"
"github.com/spf13/cobra"
)
var (
configPath string
config *configurator.Config
)
var rootCmd = &cobra.Command{
Use: "configurator",
Short: "Tool for building common config files",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Help()
os.Exit(0)
}
},
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&configPath, "config", "", "set the config path")
}
func initConfig() {
if configPath != "" {
exists, err := util.PathExists(configPath)
if err != nil {
fmt.Printf("failed to load config")
os.Exit(1)
} else if exists {
config = configurator.LoadConfig(configPath)
} else {
config = configurator.NewConfig()
}
} else {
config = configurator.NewConfig()
}
}