Refactored flags and added xnames to add endpoint to hms-smd

This commit is contained in:
David J. Allen 2023-09-05 14:26:53 -06:00
parent de07b119b5
commit 2edb9fdbb0
18 changed files with 270 additions and 175 deletions

View file

@ -1,9 +1,12 @@
package cmd
import (
"davidallendj/magellan/api/smd"
magellan "davidallendj/magellan/internal"
"davidallendj/magellan/internal/db/sqlite"
"fmt"
"github.com/bombsimon/logrusr/v2"
"github.com/Cray-HPE/hms-xname/xnames"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
@ -13,19 +16,26 @@ 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)
// make application logger
l := magellan.NewLogger(logrus.New(), logrus.DebugLevel)
// get probe states stored in db from scan
probeStates, err := magellan.GetStates(dbpath)
probeStates, err := sqlite.GetProbeResults(dbpath)
if err != nil {
l.Errorf("could not get states: %v", err)
l.Log.Errorf("could not get states: %v", err)
}
// generate custom xnames for bmcs
node := xnames.Node{
Cabinet: 1000,
Chassis: 1,
ComputeModule: 7,
NodeBMC: 1,
Node: 0,
}
// use the found results to query bmc information
inventories := [][]byte{}
users := [][]byte{}
for _, ps := range probeStates {
if !ps.State {
@ -43,61 +53,80 @@ var collectCmd = &cobra.Command{
WithSecureTLS: withSecureTLS,
}
client, err := magellan.NewClient(&logger, &q)
client, err := magellan.NewClient(l, &q)
if err != nil {
l.Errorf("could not make client: %v", err)
l.Log.Errorf("could not make client: %v", err)
return
}
// metadata
_, err = magellan.QueryMetadata(client, &logger, &q)
_, err = magellan.QueryMetadata(client, l, &q)
if err != nil {
l.Errorf("could not query metadata: %v\n", err)
l.Log.Errorf("could not query metadata: %v\n", err)
}
// inventories
inventory, err := magellan.QueryInventory(client, &logger, &q)
inventory, err := magellan.QueryInventory(client, l, &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)
l.Log.Errorf("could not query inventory: %v\n", err)
}
node.NodeBMC += 1
data := make(map[string]any)
data["ID"] = fmt.Sprintf("%v", node)
data["FQDN"] = ps.Host
data["RediscoverOnUpdate"] = false
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
// add all endpoints to smd
err = smd.AddRedfishEndpoint(inventory, headers)
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)
}
inventories = append(inventories, inventory)
// users
user, err := magellan.QueryUsers(client, &logger, &q)
user, err := magellan.QueryUsers(client, l, &q)
if err != nil {
l.Errorf("could not query users: %v\n", err)
l.Log.Errorf("could not query users: %v\n", err)
}
users = append(users, user)
// // bios
_, err = magellan.QueryBios(client, &logger, &q)
// bios
_, err = magellan.QueryBios(client, l, &q)
if err != nil {
l.Errorf("could not query bios: %v\n", err)
l.Log.Errorf("could not query bios: %v\n", err)
}
_, err = magellan.QueryPowerState(client, &logger, &q)
_, err = magellan.QueryPowerState(client, l, &q)
if err != nil {
l.Errorf("could not query power state: %v\n", err)
l.Log.Errorf("could not query power state: %v\n", err)
}
}
// 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(){
collectCmd.PersistentFlags().StringSliceVar(&drivers, "driver", []string{"redfish"}, "set the driver(s) and fallback drivers to use")
collectCmd.PersistentFlags().StringVar(&smd.Host, "host", smd.Host, "set the host to the smd API")
collectCmd.PersistentFlags().IntVar(&smd.Port, "port", smd.Port, "set the port to the smd API")
collectCmd.PersistentFlags().StringVar(&user, "user", "", "set the BMC user")
collectCmd.PersistentFlags().StringVar(&pass, "pass", "", "set the BMC password")
collectCmd.PersistentFlags().StringVar(&pass, "password", "", "set the BMC password")
collectCmd.PersistentFlags().StringVar(&preferredDriver, "preferred-driver", "ipmi", "set the preferred driver to use")
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)")
rootCmd.AddCommand(collectCmd)
}

View file

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

View file

@ -1,7 +1,6 @@
package cmd
import (
"davidallendj/magellan/api/smd"
"fmt"
"os"
@ -26,6 +25,7 @@ var (
// TODO: discover bmc's on network (dora)
// TODO: query bmc component information and store in db (?)
// TODO: send bmc component information to smd
// TODO: set ports to scan automatically with set driver
var rootCmd = &cobra.Command{
Use: "magellan",
@ -47,17 +47,9 @@ func Execute() {
}
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 driver(s) and fallback drivers to use")
rootCmd.PersistentFlags().StringVar(&preferredDriver, "preferred-driver", "ipmi", "set the preferred driver to use")
rootCmd.PersistentFlags().StringVar(&dbpath, "dbpath", ":memory:", "set the probe storage path")
rootCmd.PersistentFlags().StringVar(&ipmitoolPath, "ipmitool", "/usr/bin/ipmitool", "set the path for ipmitool")
rootCmd.PersistentFlags().BoolVar(&withSecureTLS, "secure-tls", false, "enable secure TLS")
rootCmd.PersistentFlags().StringVar(&certPoolFile, "cert-pool", "", "path to CA cert. (defaults to system CAs; used with --secure-tls=true)")
rootCmd.PersistentFlags().StringVar(&dbpath, "db.path", "/tmp/magellan.db", "set the probe storage path")
}

View file

@ -2,6 +2,7 @@ package cmd
import (
magellan "davidallendj/magellan/internal"
"davidallendj/magellan/internal/db/sqlite"
"fmt"
"github.com/cznic/mathutil"
@ -42,11 +43,13 @@ var scanCmd = &cobra.Command{
}
probeStates := magellan.ScanForAssets(hostsToScan, portsToScan, threads, timeout)
fmt.Printf("probe states: %v\n", probeStates)
magellan.StoreStates(dbpath, &probeStates)
sqlite.InsertProbeResults(dbpath, &probeStates)
},
}
func init() {
scanCmd.PersistentFlags().StringSliceVar(&hosts, "host", []string{}, "set additional hosts to scan")
scanCmd.PersistentFlags().IntSliceVar(&ports, "port", []int{}, "set the ports to scan")
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{}, "set additional subnets")