Reorganized adding cobra commands

This commit is contained in:
David J. Allen 2023-08-29 14:36:57 -06:00
parent 25a885b18c
commit c202469c50
9 changed files with 435 additions and 208 deletions

99
cmd/collect.go Normal file
View file

@ -0,0 +1,99 @@
package cmd
import (
"davidallendj/magellan/api/smd"
magellan "davidallendj/magellan/internal"
"github.com/bombsimon/logrusr/v2"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var collectCmd = &cobra.Command{
Use: "collect",
Short: "Query information about BMC",
Run: func(cmd *cobra.Command, args []string) {
// make application logger
l := logrus.New()
l.Level = logrus.DebugLevel
logger := logrusr.New(l)
// get probe states stored in db from scan
probeStates, err := magellan.GetStates(dbpath)
if err != nil {
l.Errorf("could not get states: %v", err)
}
// use the found results to query bmc information
inventories := [][]byte{}
// users := [][]byte{}
for _, ps := range probeStates {
if !ps.State {
continue
}
logrus.Infof("querying %v\n", ps)
q := magellan.QueryParams{
Host: ps.Host,
Port: ps.Port,
User: user,
Pass: pass,
Drivers: drivers,
Timeout: timeout,
Verbose: true,
WithSecureTLS: withSecureTLS,
}
client, err := magellan.NewClient(&logger, &q)
if err != nil {
l.Errorf("could not make client: %v", err)
return
}
// metadata
_, err = magellan.QueryMetadata(client, &logger, &q)
if err != nil {
l.Errorf("could not query metadata: %v\n", err)
}
// inventories
inventory, err := magellan.QueryInventory(client, &logger, &q)
// inventory, err := magellan.QueryInventoryV2(q.Host, q.Port, q.User, q.Pass)
if err != nil {
l.Errorf("could not query inventory: %v\n", err)
}
inventories = append(inventories, inventory)
// users
// user, err := magellan.QueryUsers(client, &logger, &q)
// if err != nil {
// l.Errorf("could not query users: %v\n", err)
// }
// // bios
// _, err = magellan.QueryBios(client, &logger, &q)
// if err != nil {
// l.Errorf("could not query bios: %v\n", err)
// }
// users = append(users, user)
}
// add all endpoints to smd
for _, inventory := range inventories {
err := smd.AddRedfishEndpoint(inventory)
if err != nil {
logrus.Errorf("could not add redfish endpoint: %v", err)
}
}
// confirm the inventories were added
err = smd.GetRedfishEndpoints()
if err != nil {
logrus.Errorf("could not get redfish endpoints: %v\n", err)
}
},
}
func init(){
rootCmd.AddCommand(collectCmd)
}

26
cmd/list.go Normal file
View file

@ -0,0 +1,26 @@
package cmd
import (
magellan "davidallendj/magellan/internal"
"fmt"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var listCmd = &cobra.Command{
Use: "list",
Short: "List information from scan",
Run: func(cmd *cobra.Command, args []string) {
probeResults, err := magellan.GetStates(dbpath)
if err != nil {
logrus.Errorf("could not get probe results: %v\n", err)
}
fmt.Printf("%v\n", probeResults)
},
}
func init() {
rootCmd.AddCommand(listCmd)
}

59
cmd/root.go Normal file
View file

@ -0,0 +1,59 @@
package cmd
import (
"davidallendj/magellan/api/smd"
"fmt"
"os"
"github.com/spf13/cobra"
)
var (
timeout int
threads int
ports []int
hosts []string
withSecureTLS bool
certPoolFile string
user string
pass string
dbpath string
drivers []string
)
// TODO: discover bmc's on network (dora)
// TODO: query bmc component information and store in db (?)
// TODO: send bmc component information to smd
var rootCmd = &cobra.Command{
Use: "magellan",
Short: "Tool for BMC discovery",
Long: "",
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(){
rootCmd.PersistentFlags().StringVar(&user, "user", "", "set the BMC user")
rootCmd.PersistentFlags().StringVar(&pass, "pass", "", "set the BMC pass")
rootCmd.PersistentFlags().StringSliceVar(&hosts, "host", []string{}, "set additional hosts")
rootCmd.PersistentFlags().StringVar(&smd.Host, "smd-host", "localhost", "set the host to the hms-smd API")
rootCmd.PersistentFlags().IntVar(&threads, "threads", -1, "set the number of threads")
rootCmd.PersistentFlags().IntVar(&timeout, "timeout", 10, "set the timeout")
rootCmd.PersistentFlags().IntSliceVar(&ports, "port", []int{}, "set the ports to scan")
rootCmd.PersistentFlags().StringSliceVar(&drivers, "driver", []string{"redfish"}, "set the BMC driver to use")
rootCmd.PersistentFlags().StringVar(&dbpath, "dbpath", ":memory:", "set the probe storage path")
rootCmd.PersistentFlags().BoolVar(&withSecureTLS, "secure-tls", false, "enable secure TLS")
rootCmd.PersistentFlags().StringVar(&certPoolFile, "cert-pool", "", "path to an file containing x509 CAs. An empty string uses the system CAs. Only takes effect when --secure-tls=true")
}

55
cmd/scan.go Normal file
View file

@ -0,0 +1,55 @@
package cmd
import (
magellan "davidallendj/magellan/internal"
"fmt"
"github.com/cznic/mathutil"
"github.com/spf13/cobra"
)
var (
begin uint8
end uint8
subnets []string
)
var scanCmd = &cobra.Command{
Use: "scan",
Short: "Scan for BMCs",
Run: func(cmd *cobra.Command, args []string) {
// set hosts to use for scanning
hostsToScan := []string{}
if len(hosts) > 0 {
hostsToScan = hosts
} else {
for _, subnet := range subnets {
hostsToScan = append(hostsToScan, magellan.GenerateHosts(subnet, begin, end)...)
}
}
// set ports to use for scanning
portsToScan := []int{}
if len(ports) > 0 {
portsToScan = ports
} else {
portsToScan = append(magellan.GetDefaultPorts(), ports...)
}
// scan and store probe data in dbPath
if threads <= 0 {
threads = mathutil.Clamp(len(hostsToScan), 1, 255)
}
probeStates := magellan.ScanForAssets(hostsToScan, portsToScan, threads, timeout)
fmt.Printf("probe states: %v\n", probeStates)
magellan.StoreStates(dbpath, &probeStates)
},
}
func init() {
scanCmd.Flags().Uint8Var(&begin, "begin", 0, "set the starting point for range of IP addresses")
scanCmd.Flags().Uint8Var(&end, "end", 255, "set the ending point for range of IP addresses")
scanCmd.Flags().StringSliceVar(&subnets, "subnet", []string{"127.0.0.0"}, "set additional subnets")
rootCmd.AddCommand(scanCmd)
}