mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 11:37:01 -07:00
Add support for storage command and crawler output
Partially addresses issue #3 by adding a simple `magellan list devices` command to list storage devices. To close the issue, this PR still requires including storage device information in the `crawler`'s output. Reviewed-on: towk/magellan-ng#5
This commit is contained in:
parent
f02c29917a
commit
a6c445b86f
20 changed files with 449 additions and 18 deletions
|
|
@ -14,6 +14,9 @@ type BMCCredentials struct {
|
|||
|
||||
func GetBMCCredentialsDefault(store secrets.SecretStore) (BMCCredentials, error) {
|
||||
var creds BMCCredentials
|
||||
if store == nil {
|
||||
return creds, fmt.Errorf("invalid secrets store")
|
||||
}
|
||||
if strCreds, err := store.GetSecretByID(secrets.DEFAULT_KEY); err != nil {
|
||||
return creds, fmt.Errorf("get default BMC credentials from secret store: %w", err)
|
||||
} else {
|
||||
|
|
@ -27,6 +30,9 @@ func GetBMCCredentialsDefault(store secrets.SecretStore) (BMCCredentials, error)
|
|||
|
||||
func GetBMCCredentials(store secrets.SecretStore, id string) (BMCCredentials, error) {
|
||||
var creds BMCCredentials
|
||||
if store == nil {
|
||||
return creds, fmt.Errorf("invalid secrets store")
|
||||
}
|
||||
if strCreds, err := store.GetSecretByID(id); err != nil {
|
||||
return creds, fmt.Errorf("get BMC credentials from secret store: %w", err)
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -97,6 +97,7 @@ func CollectInventory(assets *[]RemoteAsset, params *CollectParams, store secret
|
|||
var (
|
||||
systems []crawler.InventoryDetail
|
||||
managers []crawler.Manager
|
||||
storage []crawler.Storage
|
||||
config = crawler.CrawlerConfig{
|
||||
URI: uri,
|
||||
CredentialStore: params.SecretStore,
|
||||
|
|
@ -116,6 +117,11 @@ func CollectInventory(assets *[]RemoteAsset, params *CollectParams, store secret
|
|||
log.Error().Err(err).Str("uri", uri).Msg("failed to crawl BMC for managers")
|
||||
}
|
||||
|
||||
storage, err = crawler.CrawlBMCForStorage(config)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("uri", uri).Msg("failed to crawl BMC for storage")
|
||||
}
|
||||
|
||||
// we didn't find anything so do not proceed
|
||||
if util.IsEmpty(systems) && util.IsEmpty(managers) {
|
||||
continue
|
||||
|
|
@ -144,6 +150,7 @@ func CollectInventory(assets *[]RemoteAsset, params *CollectParams, store secret
|
|||
"RediscoverOnUpdate": false,
|
||||
"Systems": systems,
|
||||
"Managers": managers,
|
||||
"Storage": storage,
|
||||
"SchemaVersion": 1,
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -97,6 +97,26 @@ type InventoryDetail struct {
|
|||
Links Links `json:"links,omitempty"` // Links to specific resources
|
||||
}
|
||||
|
||||
type Storage struct {
|
||||
URI string `json:"uri"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
DrivesCount int `json:"drives_count"`
|
||||
Controllers []StorageController `json:"controllers"`
|
||||
}
|
||||
|
||||
type StorageController struct {
|
||||
Identifiers []string `json:"identifiers"`
|
||||
FirmwareVersion string `json:"firmware_version"`
|
||||
Location string `json:"location"`
|
||||
Manufacturer string `json:"manufacturer"`
|
||||
Model string `json:"model"`
|
||||
PartNumber string `json:"part_number"`
|
||||
SerialNumber string `json:"serial_number"`
|
||||
SpeedGbps float32 `json:"speed_gbps"`
|
||||
}
|
||||
|
||||
// CrawlBMCForSystems pulls all pertinent information from a BMC. It accepts a CrawlerConfig and returns a list of InventoryDetail structs.
|
||||
func CrawlBMCForSystems(config CrawlerConfig) ([]InventoryDetail, error) {
|
||||
var (
|
||||
|
|
@ -240,6 +260,49 @@ func CrawlBMCForManagers(config CrawlerConfig) ([]Manager, error) {
|
|||
return walkManagers(rf_managers, config.URI)
|
||||
}
|
||||
|
||||
func CrawlBMCForStorage(config CrawlerConfig) ([]Storage, error) {
|
||||
// get username and password from secret store
|
||||
bmc_creds, err := loadBMCCreds(config)
|
||||
if err != nil {
|
||||
event := log.Error()
|
||||
event.Err(err)
|
||||
event.Msg("failed to load BMC credentials")
|
||||
return nil, err
|
||||
}
|
||||
// initialize gofish client
|
||||
var storage []Storage
|
||||
client, err := gofish.Connect(gofish.ClientConfig{
|
||||
Endpoint: config.URI,
|
||||
Username: bmc_creds.Username,
|
||||
Password: bmc_creds.Password,
|
||||
Insecure: config.Insecure,
|
||||
BasicAuth: true,
|
||||
})
|
||||
if err != nil {
|
||||
if strings.HasPrefix(err.Error(), "404:") {
|
||||
err = fmt.Errorf("no ServiceRoot found. This is probably not a BMC: %s", config.URI)
|
||||
}
|
||||
if strings.HasPrefix(err.Error(), "401:") {
|
||||
err = fmt.Errorf("authentication failed. Check your username and password: %s", config.URI)
|
||||
}
|
||||
event := log.Error()
|
||||
event.Err(err)
|
||||
event.Msg("failed to connect to BMC")
|
||||
return storage, err
|
||||
}
|
||||
defer client.Logout()
|
||||
|
||||
// Obtain the ServiceRoot
|
||||
rf_service := client.GetService()
|
||||
log.Debug().Msgf("found ServiceRoot %s. Redfish Version %s", rf_service.ID, rf_service.RedfishVersion)
|
||||
|
||||
rf_storage, err := rf_service.Storage()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to get managers from ServiceRoot")
|
||||
}
|
||||
return walkStorage(rf_storage, config.URI)
|
||||
}
|
||||
|
||||
// walkSystems processes a list of Redfish computer systems and their associated chassis,
|
||||
// and returns a list of inventory details for each system.
|
||||
//
|
||||
|
|
@ -494,6 +557,39 @@ func walkManagers(rf_managers []*redfish.Manager, baseURI string) ([]Manager, er
|
|||
// }
|
||||
|
||||
// }
|
||||
func walkStorage(rf_storage []*redfish.Storage, baseURI string) ([]Storage, error) {
|
||||
var storage []Storage
|
||||
for _, rf_storage := range rf_storage {
|
||||
var controllers []StorageController
|
||||
var rf_storage_controllers, err = rf_storage.Controllers()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to get storage controllers")
|
||||
} else {
|
||||
for _, controller := range rf_storage_controllers {
|
||||
controllers = append(controllers, StorageController{
|
||||
// Identifiers: controller.Identifiers,
|
||||
FirmwareVersion: controller.FirmwareVersion,
|
||||
Location: controller.Location.Info,
|
||||
Manufacturer: controller.Manufacturer,
|
||||
Model: controller.Model,
|
||||
PartNumber: controller.PartNumber,
|
||||
SerialNumber: controller.SerialNumber,
|
||||
SpeedGbps: controller.SpeedGbps,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
storage = append(storage, Storage{
|
||||
URI: baseURI + "/redfish/v1/Storage/" + rf_storage.ID,
|
||||
ID: rf_storage.ID,
|
||||
Name: rf_storage.Name,
|
||||
Description: rf_storage.Description,
|
||||
DrivesCount: rf_storage.DrivesCount,
|
||||
Controllers: controllers,
|
||||
})
|
||||
}
|
||||
return storage, nil
|
||||
}
|
||||
|
||||
func loadBMCCreds(config CrawlerConfig) (bmc.BMCCredentials, error) {
|
||||
// NOTE: it is possible for the SecretStore to be nil, so we need a check
|
||||
|
|
|
|||
100
pkg/list.go
Normal file
100
pkg/list.go
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
package magellan
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/davidallendj/magellan/internal/util"
|
||||
"github.com/davidallendj/magellan/pkg/crawler"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/stmcginnis/gofish"
|
||||
"github.com/stmcginnis/gofish/redfish"
|
||||
)
|
||||
|
||||
func PrintRemoteAssets(data []RemoteAsset, format string) {
|
||||
switch strings.ToLower(format) {
|
||||
case "json":
|
||||
util.PrintJSON(data)
|
||||
case "yaml":
|
||||
util.PrintYAML(data)
|
||||
case "none":
|
||||
for _, r := range data {
|
||||
fmt.Printf("%s:%d (%s) @%s\n", r.Host, r.Port, r.Protocol, r.Timestamp.Format(time.UnixDate))
|
||||
}
|
||||
default:
|
||||
log.Error().Msg("unrecognized format")
|
||||
}
|
||||
}
|
||||
|
||||
func PrintMapFormat(data map[string]any, format string) {
|
||||
switch strings.ToLower(format) {
|
||||
case "json":
|
||||
util.PrintJSON(data)
|
||||
case "yaml":
|
||||
util.PrintYAML(data)
|
||||
case "none":
|
||||
for k, v := range data {
|
||||
fmt.Printf("%s: %v\n", k, v)
|
||||
}
|
||||
default:
|
||||
log.Error().Msg("unrecognized format")
|
||||
}
|
||||
}
|
||||
|
||||
func ListDrives(cc *crawler.CrawlerConfig) ([]*redfish.Drive, error) {
|
||||
user, err := cc.GetUserPass()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get username and password: %v", err)
|
||||
}
|
||||
log.Info().Str("username", user.Username).Str("password", user.Password).Str("host", cc.URI).Msg("credentials used")
|
||||
// Create a new instance of gofish client, ignoring self-signed certs
|
||||
c, err := gofish.Connect(gofish.ClientConfig{
|
||||
Endpoint: cc.URI,
|
||||
Username: user.Username,
|
||||
Password: user.Password,
|
||||
Insecure: cc.Insecure,
|
||||
BasicAuth: true,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to connect to host: %v", util.TidyJSON(err.Error()))
|
||||
}
|
||||
defer c.Logout()
|
||||
|
||||
// Retrieve the service root
|
||||
systems, err := c.Service.Systems()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to retrieve systems: %v", err)
|
||||
}
|
||||
|
||||
// aggregate all of the drives together
|
||||
foundDrives := []*redfish.Drive{}
|
||||
for _, system := range systems {
|
||||
storage, err := system.Storage()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, ss := range storage {
|
||||
drives, err := ss.Drives()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
foundDrives = append(foundDrives, drives...)
|
||||
}
|
||||
}
|
||||
return foundDrives, nil
|
||||
}
|
||||
|
||||
func PrintDrives(drives []*redfish.Drive) {
|
||||
for i, drive := range drives {
|
||||
fmt.Printf("Drive %d\n", i)
|
||||
fmt.Printf("\tManufacturer: %s\n", drive.Manufacturer)
|
||||
fmt.Printf("\tModel: %s\n", drive.Model)
|
||||
fmt.Printf("\tSize: %d GiB\n", (drive.CapacityBytes / 1024 / 1024 / 1024))
|
||||
fmt.Printf("\tSerial number: %s\n", drive.SerialNumber)
|
||||
fmt.Printf("\tPart number: %s\n", drive.PartNumber)
|
||||
fmt.Printf("\tLocation: %s %d\n", drive.PhysicalLocation.PartLocation.LocationType, drive.PhysicalLocation.PartLocation.LocationOrdinalValue)
|
||||
}
|
||||
}
|
||||
|
|
@ -10,7 +10,7 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
urlx "github.com/davidallendj/magellan/internal/url"
|
||||
urlx "github.com/davidallendj/magellan/internal/urlx"
|
||||
"github.com/davidallendj/magellan/pkg/client"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue