Initial implementation of config file

This commit is contained in:
David J. Allen 2023-10-19 17:08:36 -06:00
parent 1db4349eb0
commit 2baae37f5f
9 changed files with 672 additions and 19 deletions

View file

@ -8,6 +8,7 @@ import (
"github.com/cznic/mathutil"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
@ -66,5 +67,20 @@ func init() {
collectCmd.PersistentFlags().StringVar(&ipmitoolPath, "ipmitool.path", "/usr/bin/ipmitool", "set the path for ipmitool")
collectCmd.PersistentFlags().BoolVar(&withSecureTLS, "secure-tls", false, "enable secure TLS")
collectCmd.PersistentFlags().StringVar(&certPoolFile, "cert-pool", "", "path to CA cert. (defaults to system CAs; used with --secure-tls=true)")
viper.BindPFlag("collect.driver", collectCmd.Flags().Lookup("driver"))
viper.BindPFlag("collect.host", collectCmd.Flags().Lookup("host"))
viper.BindPFlag("collect.port", collectCmd.Flags().Lookup("port"))
viper.BindPFlag("collect.user", collectCmd.Flags().Lookup("user"))
viper.BindPFlag("collect.pass", collectCmd.Flags().Lookup("pass"))
viper.BindPFlag("collect.protocol", collectCmd.Flags().Lookup("protocol"))
viper.BindPFlag("collect.output", collectCmd.Flags().Lookup("output"))
viper.BindPFlag("collect.force-update", collectCmd.Flags().Lookup("force-update"))
viper.BindPFlag("collect.preferred-driver", collectCmd.Flags().Lookup("preferred-driver"))
viper.BindPFlag("collect.ipmitool.path", collectCmd.Flags().Lookup("ipmitool.path"))
viper.BindPFlag("collect.secure-tls", collectCmd.Flags().Lookup("secure-tls"))
viper.BindPFlag("collect.cert-pool", collectCmd.Flags().Lookup("cert-pool"))
rootCmd.AddCommand(collectCmd)
}

View file

@ -2,9 +2,13 @@ package cmd
import (
"fmt"
"net"
"os"
magellan "github.com/bikeshack/magellan/internal"
"github.com/bikeshack/magellan/internal/api/smd"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
@ -22,6 +26,7 @@ var (
preferredDriver string
ipmitoolPath string
outputPath string
configPath string
verbose bool
)
@ -50,8 +55,61 @@ func Execute() {
}
func init() {
cobra.OnInitialize(InitializeConfig)
rootCmd.PersistentFlags().IntVar(&threads, "threads", -1, "set the number of threads")
rootCmd.PersistentFlags().IntVar(&timeout, "timeout", 30, "set the timeout")
rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "", "set the config file path")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", true, "set verbose flag")
rootCmd.PersistentFlags().StringVar(&dbpath, "db.path", "/tmp/magellan/magellan.db", "set the probe storage path")
// bind viper config flags with cobra
viper.BindPFlag("threads", rootCmd.Flags().Lookup("threads"))
viper.BindPFlag("timeout", rootCmd.Flags().Lookup("timeout"))
viper.BindPFlag("verbose", rootCmd.Flags().Lookup("verbose"))
viper.BindPFlag("db.path", rootCmd.Flags().Lookup("db.path"))
// viper.BindPFlags(rootCmd.Flags())
}
func InitializeConfig() {
if configPath != "" {
magellan.LoadConfig(configPath)
fmt.Printf("subnets: %v\n", viper.Get("scan.subnets"))
}
}
func SetDefaults() {
viper.SetDefault("threads", 1)
viper.SetDefault("timeout", 30)
viper.SetDefault("config", "")
viper.SetDefault("verbose", true)
viper.SetDefault("db.path", "/tmp/magellan/magellan.db")
viper.SetDefault("scan.hosts", []string{})
viper.SetDefault("scan.ports", []int{})
viper.SetDefault("scan.subnets", []string{})
viper.SetDefault("scan.subnet-masks", []net.IP{})
viper.SetDefault("scan.disable-probing", false)
viper.SetDefault("collect.driver", []string{"redfish"})
viper.SetDefault("collect.host", smd.Host)
viper.SetDefault("collect.port", smd.Port)
viper.SetDefault("collect.user", "")
viper.SetDefault("collect.pass", "")
viper.SetDefault("collect.protocol", "https")
viper.SetDefault("collect.output", "/tmp/magellan/data/")
viper.SetDefault("collect.force-update", false)
viper.SetDefault("collect.preferred-driver", "ipmi")
viper.SetDefault("collect.ipmitool.path", "/usr/bin/ipmitool")
viper.SetDefault("collect.secure-tls", false)
viper.SetDefault("collect.cert-pool", "")
viper.SetDefault("bmc-host", "")
viper.SetDefault("bmc-port", 443)
viper.SetDefault("user", "")
viper.SetDefault("pass", "")
viper.SetDefault("transfer-protocol", "HTTP")
viper.SetDefault("protocol", "https")
viper.SetDefault("firmware-url", "")
viper.SetDefault("firmware-version", "")
viper.SetDefault("component", "")
viper.SetDefault("secure-tls", false)
viper.SetDefault("status", false)
}

View file

@ -11,6 +11,7 @@ import (
"github.com/cznic/mathutil"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
@ -25,6 +26,7 @@ var scanCmd = &cobra.Command{
Use: "scan",
Short: "Scan for BMC nodes on a network",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("subnets in cmd: %v\n", subnets)
// set hosts to use for scanning
hostsToScan := []string{}
if len(hosts) > 0 {
@ -79,5 +81,11 @@ func init() {
scanCmd.Flags().IPSliceVar(&subnetMasks, "subnet-mask", []net.IP{}, "set the subnet masks to use for network")
scanCmd.Flags().BoolVar(&disableProbing, "disable-probing", false, "disable probing scanned results for BMC nodes")
viper.BindPFlag("scan.hosts", scanCmd.Flags().Lookup("host"))
viper.BindPFlag("scan.ports", scanCmd.Flags().Lookup("port"))
viper.BindPFlag("scan.subnets", scanCmd.Flags().Lookup("subnet"))
viper.BindPFlag("scan.subnet-masks", scanCmd.Flags().Lookup("subnet-mask"))
viper.BindPFlag("scan.disable-probing", scanCmd.Flags().Lookup("disable-probing"))
rootCmd.AddCommand(scanCmd)
}

View file

@ -5,6 +5,7 @@ import (
"github.com/bikeshack/magellan/internal/log"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
@ -78,5 +79,18 @@ func init() {
updateCmd.Flags().StringVar(&component, "component", "", "set the component to upgrade")
updateCmd.Flags().BoolVar(&withSecureTLS, "secure-tls", false, "enable secure TLS")
updateCmd.Flags().BoolVar(&status, "status", false, "get the status of the update")
viper.BindPFlag("bmc-host", updateCmd.Flags().Lookup("bmc-host"))
viper.BindPFlag("bmc-port", updateCmd.Flags().Lookup("bmc-port"))
viper.BindPFlag("user", updateCmd.Flags().Lookup("user"))
viper.BindPFlag("pass", updateCmd.Flags().Lookup("pass"))
viper.BindPFlag("transfer-protocol", updateCmd.Flags().Lookup("transfer-protocol"))
viper.BindPFlag("protocol", updateCmd.Flags().Lookup("protocol"))
viper.BindPFlag("firmware-url", updateCmd.Flags().Lookup("firmware-url"))
viper.BindPFlag("firmware-version", updateCmd.Flags().Lookup("firmware-version"))
viper.BindPFlag("component", updateCmd.Flags().Lookup("component"))
viper.BindPFlag("secure-tls", updateCmd.Flags().Lookup("secure-tls"))
viper.BindPFlag("status", updateCmd.Flags().Lookup("status"))
rootCmd.AddCommand(updateCmd)
}