More minor changes

This commit is contained in:
David J. Allen 2024-05-08 13:29:07 -06:00
parent 2289209604
commit c041acafee
No known key found for this signature in database
GPG key ID: 717C593FF60A2ACC
2 changed files with 55 additions and 72 deletions

View file

@ -117,7 +117,7 @@ func (c *Client) AddRedfishEndpoint(data []byte, headers map[string]string) erro
func (c *Client) UpdateRedfishEndpoint(xname string, data []byte, headers map[string]string) error { func (c *Client) UpdateRedfishEndpoint(xname string, data []byte, headers map[string]string) error {
if data == nil { if data == nil {
return fmt.Errorf("could not add redfish endpoint: no data found") return fmt.Errorf("failed to add redfish endpoint: no data found")
} }
// Update redfish endpoint via PUT `/hsm/v2/Inventory/RedfishEndpoints` endpoint // Update redfish endpoint via PUT `/hsm/v2/Inventory/RedfishEndpoints` endpoint
url := makeEndpointUrl("/Inventory/RedfishEndpoints/" + xname) url := makeEndpointUrl("/Inventory/RedfishEndpoints/" + xname)
@ -126,7 +126,7 @@ func (c *Client) UpdateRedfishEndpoint(xname string, data []byte, headers map[st
if res != nil { if res != nil {
statusOk := res.StatusCode >= 200 && res.StatusCode < 300 statusOk := res.StatusCode >= 200 && res.StatusCode < 300
if !statusOk { if !statusOk {
return fmt.Errorf("could not update redfish endpoint") return fmt.Errorf("failed to update redfish endpoint (returned %s)", res.Status)
} }
} }
return err return err

View file

@ -2,8 +2,10 @@ package magellan
import ( import (
"context" "context"
"crypto/tls"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net"
"net/http" "net/http"
"os" "os"
"path" "path"
@ -62,7 +64,7 @@ func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) err
outputPath := path.Clean(q.OutputPath) outputPath := path.Clean(q.OutputPath)
outputPath, err := util.MakeOutputDirectory(outputPath) outputPath, err := util.MakeOutputDirectory(outputPath)
if err != nil { if err != nil {
l.Log.Errorf("could not make output directory: %v", err) l.Log.Errorf("failed to make output directory: %v", err)
} }
// collect bmc information asynchronously // collect bmc information asynchronously
@ -97,14 +99,9 @@ func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) err
} }
offset += 1 offset += 1
// bmclibClient, err := NewClient(l, q)
// if err != nil {
// l.Log.Errorf("could not make client: %v", err)
// }
gofishClient, err := connectGofish(q) gofishClient, err := connectGofish(q)
if err != nil { if err != nil {
l.Log.Errorf("could not connect to bmc (%v:%v): %v", q.Host, q.Port, err) l.Log.Errorf("failed to connect to bmc (%v:%v): %v", q.Host, q.Port, err)
} }
// data to be sent to smd // data to be sent to smd
@ -122,21 +119,11 @@ func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) err
// unmarshal json to send in correct format // unmarshal json to send in correct format
var rm map[string]json.RawMessage var rm map[string]json.RawMessage
// inventories
// if bmclibClient != nil {
// inventory, err := CollectInventory(bmclibClient, q)
// if err != nil {
// l.Log.Errorf("could not query inventory (%v:%v): %v", q.Host, q.Port, err)
// }
// json.Unmarshal(inventory, &rm)
// data["Inventory"] = rm["Inventory"]
// }
// chassis // chassis
if gofishClient != nil { if gofishClient != nil {
chassis, err := CollectChassis(gofishClient, q) chassis, err := CollectChassis(gofishClient, q)
if err != nil { if err != nil {
l.Log.Errorf("could not query chassis: %v", err) l.Log.Errorf("failed to query chassis: %v", err)
continue continue
} }
json.Unmarshal(chassis, &rm) json.Unmarshal(chassis, &rm)
@ -145,7 +132,7 @@ func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) err
// systems // systems
systems, err := CollectSystems(gofishClient, q) systems, err := CollectSystems(gofishClient, q)
if err != nil { if err != nil {
l.Log.Errorf("could not query systems: %v", err) l.Log.Errorf("failed to query systems: %v", err)
} }
json.Unmarshal(systems, &rm) json.Unmarshal(systems, &rm)
data["Systems"] = rm["Systems"] data["Systems"] = rm["Systems"]
@ -168,7 +155,7 @@ func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) err
body, err := json.MarshalIndent(data, "", " ") body, err := json.MarshalIndent(data, "", " ")
if err != nil { if err != nil {
l.Log.Errorf("could not marshal JSON: %v", err) l.Log.Errorf("failed to marshal JSON: %v", err)
} }
if q.Verbose { if q.Verbose {
@ -179,7 +166,7 @@ func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) err
if outputPath != "" { if outputPath != "" {
err = os.WriteFile(path.Clean(outputPath+"/"+q.Host+".json"), body, os.ModePerm) err = os.WriteFile(path.Clean(outputPath+"/"+q.Host+".json"), body, os.ModePerm)
if err != nil { if err != nil {
l.Log.Errorf("could not write data to file: %v", err) l.Log.Errorf("failed to write data to file: %v", err)
} }
} }
@ -232,15 +219,13 @@ func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) err
} }
func CollectMetadata(client *bmclib.Client, q *QueryParams) ([]byte, error) { func CollectMetadata(client *bmclib.Client, q *QueryParams) ([]byte, error) {
// client, err := NewClient(l, q)
// open BMC session and update driver registry // open BMC session and update driver registry
ctx, ctxCancel := context.WithTimeout(context.Background(), time.Second*time.Duration(q.Timeout)) ctx, ctxCancel := context.WithTimeout(context.Background(), time.Second*time.Duration(q.Timeout))
client.Registry.FilterForCompatible(ctx) client.Registry.FilterForCompatible(ctx)
err := client.Open(ctx) err := client.Open(ctx)
if err != nil { if err != nil {
ctxCancel() ctxCancel()
return nil, fmt.Errorf("could not connect to bmc: %v", err) return nil, fmt.Errorf("failed to connect to bmc: %v", err)
} }
defer client.Close(ctx) defer client.Close(ctx)
@ -248,14 +233,14 @@ func CollectMetadata(client *bmclib.Client, q *QueryParams) ([]byte, error) {
metadata := client.GetMetadata() metadata := client.GetMetadata()
if err != nil { if err != nil {
ctxCancel() ctxCancel()
return nil, fmt.Errorf("could not get metadata: %v", err) return nil, fmt.Errorf("failed to get metadata: %v", err)
} }
// retrieve inventory data // retrieve inventory data
b, err := json.MarshalIndent(metadata, "", " ") b, err := json.MarshalIndent(metadata, "", " ")
if err != nil { if err != nil {
ctxCancel() ctxCancel()
return nil, fmt.Errorf("could not marshal JSON: %v", err) return nil, fmt.Errorf("failed to marshal JSON: %v", err)
} }
ctxCancel() ctxCancel()
@ -269,13 +254,13 @@ func CollectInventory(client *bmclib.Client, q *QueryParams) ([]byte, error) {
err := client.PreferProvider(q.Preferred).Open(ctx) err := client.PreferProvider(q.Preferred).Open(ctx)
if err != nil { if err != nil {
ctxCancel() ctxCancel()
return nil, fmt.Errorf("could not open client: %v", err) return nil, fmt.Errorf("failed to open client: %v", err)
} }
inventory, err := client.Inventory(ctx) inventory, err := client.Inventory(ctx)
if err != nil { if err != nil {
ctxCancel() ctxCancel()
return nil, fmt.Errorf("could not get inventory: %v", err) return nil, fmt.Errorf("failed to get inventory: %v", err)
} }
// retrieve inventory data // retrieve inventory data
@ -283,7 +268,7 @@ func CollectInventory(client *bmclib.Client, q *QueryParams) ([]byte, error) {
b, err := json.MarshalIndent(data, "", " ") b, err := json.MarshalIndent(data, "", " ")
if err != nil { if err != nil {
ctxCancel() ctxCancel()
return nil, fmt.Errorf("could not marshal JSON: %v", err) return nil, fmt.Errorf("failed to marshal JSON: %v", err)
} }
ctxCancel() ctxCancel()
@ -296,13 +281,13 @@ func CollectPowerState(client *bmclib.Client, q *QueryParams) ([]byte, error) {
err := client.PreferProvider(q.Preferred).Open(ctx) err := client.PreferProvider(q.Preferred).Open(ctx)
if err != nil { if err != nil {
ctxCancel() ctxCancel()
return nil, fmt.Errorf("could not open client: %v", err) return nil, fmt.Errorf("failed to open client: %v", err)
} }
powerState, err := client.GetPowerState(ctx) powerState, err := client.GetPowerState(ctx)
if err != nil { if err != nil {
ctxCancel() ctxCancel()
return nil, fmt.Errorf("could not get inventory: %v", err) return nil, fmt.Errorf("failed to get inventory: %v", err)
} }
// retrieve inventory data // retrieve inventory data
@ -310,7 +295,7 @@ func CollectPowerState(client *bmclib.Client, q *QueryParams) ([]byte, error) {
b, err := json.MarshalIndent(data, "", " ") b, err := json.MarshalIndent(data, "", " ")
if err != nil { if err != nil {
ctxCancel() ctxCancel()
return nil, fmt.Errorf("could not marshal JSON: %v", err) return nil, fmt.Errorf("failed to marshal JSON: %v", err)
} }
ctxCancel() ctxCancel()
@ -325,7 +310,7 @@ func CollectUsers(client *bmclib.Client, q *QueryParams) ([]byte, error) {
err := client.Open(ctx) err := client.Open(ctx)
if err != nil { if err != nil {
ctxCancel() ctxCancel()
return nil, fmt.Errorf("could not connect to bmc: %v", err) return nil, fmt.Errorf("failed to connect to bmc: %v", err)
} }
defer client.Close(ctx) defer client.Close(ctx)
@ -333,7 +318,7 @@ func CollectUsers(client *bmclib.Client, q *QueryParams) ([]byte, error) {
users, err := client.ReadUsers(ctx) users, err := client.ReadUsers(ctx)
if err != nil { if err != nil {
ctxCancel() ctxCancel()
return nil, fmt.Errorf("could not get users: %v", err) return nil, fmt.Errorf("failed to get users: %v", err)
} }
// retrieve inventory data // retrieve inventory data
@ -341,7 +326,7 @@ func CollectUsers(client *bmclib.Client, q *QueryParams) ([]byte, error) {
b, err := json.MarshalIndent(data, "", " ") b, err := json.MarshalIndent(data, "", " ")
if err != nil { if err != nil {
ctxCancel() ctxCancel()
return nil, fmt.Errorf("could not marshal JSON: %v", err) return nil, fmt.Errorf("failed to marshal JSON: %v", err)
} }
ctxCancel() ctxCancel()
@ -351,7 +336,7 @@ func CollectUsers(client *bmclib.Client, q *QueryParams) ([]byte, error) {
func CollectBios(client *bmclib.Client, q *QueryParams) ([]byte, error) { func CollectBios(client *bmclib.Client, q *QueryParams) ([]byte, error) {
// client, err := NewClient(l, q) // client, err := NewClient(l, q)
// if err != nil { // if err != nil {
// return nil, fmt.Errorf("could not make query: %v", err) // return nil, fmt.Errorf("failed to make query: %v", err)
// } // }
b, err := makeRequest(client, client.GetBiosConfiguration, q.Timeout) b, err := makeRequest(client, client.GetBiosConfiguration, q.Timeout)
return b, err return b, err
@ -360,7 +345,7 @@ func CollectBios(client *bmclib.Client, q *QueryParams) ([]byte, error) {
func CollectEthernetInterfaces(c *gofish.APIClient, q *QueryParams, systemID string) ([]byte, error) { func CollectEthernetInterfaces(c *gofish.APIClient, q *QueryParams, systemID string) ([]byte, error) {
systems, err := c.Service.Systems() systems, err := c.Service.Systems()
if err != nil { if err != nil {
return nil, fmt.Errorf("could not query storage systems (%v:%v): %v", q.Host, q.Port, err) return nil, fmt.Errorf("failed to query storage systems (%v:%v): %v", q.Host, q.Port, err)
} }
var interfaces []*redfish.EthernetInterface var interfaces []*redfish.EthernetInterface
@ -373,13 +358,13 @@ func CollectEthernetInterfaces(c *gofish.APIClient, q *QueryParams, systemID str
} }
if len(interfaces) <= 0 { if len(interfaces) <= 0 {
return nil, fmt.Errorf("could not get ethernet interfaces: %v", err) return nil, fmt.Errorf("failed to get ethernet interfaces: %v", err)
} }
data := map[string]any{"EthernetInterfaces": interfaces} data := map[string]any{"EthernetInterfaces": interfaces}
b, err := json.MarshalIndent(data, "", " ") b, err := json.MarshalIndent(data, "", " ")
if err != nil { if err != nil {
return nil, fmt.Errorf("could not marshal JSON: %v", err) return nil, fmt.Errorf("failed to marshal JSON: %v", err)
} }
return b, nil return b, nil
@ -388,13 +373,13 @@ func CollectEthernetInterfaces(c *gofish.APIClient, q *QueryParams, systemID str
func CollectChassis(c *gofish.APIClient, q *QueryParams) ([]byte, error) { func CollectChassis(c *gofish.APIClient, q *QueryParams) ([]byte, error) {
chassis, err := c.Service.Chassis() chassis, err := c.Service.Chassis()
if err != nil { if err != nil {
return nil, fmt.Errorf("could not query chassis (%v:%v): %v", q.Host, q.Port, err) return nil, fmt.Errorf("failed to query chassis (%v:%v): %v", q.Host, q.Port, err)
} }
data := map[string]any{"Chassis": chassis} data := map[string]any{"Chassis": chassis}
b, err := json.MarshalIndent(data, "", " ") b, err := json.MarshalIndent(data, "", " ")
if err != nil { if err != nil {
return nil, fmt.Errorf("could not marshal JSON: %v", err) return nil, fmt.Errorf("failed to marshal JSON: %v", err)
} }
return b, nil return b, nil
@ -403,12 +388,12 @@ func CollectChassis(c *gofish.APIClient, q *QueryParams) ([]byte, error) {
func CollectStorage(c *gofish.APIClient, q *QueryParams) ([]byte, error) { func CollectStorage(c *gofish.APIClient, q *QueryParams) ([]byte, error) {
systems, err := c.Service.StorageSystems() systems, err := c.Service.StorageSystems()
if err != nil { if err != nil {
return nil, fmt.Errorf("could not query storage systems (%v:%v): %v", q.Host, q.Port, err) return nil, fmt.Errorf("failed to query storage systems (%v:%v): %v", q.Host, q.Port, err)
} }
services, err := c.Service.StorageServices() services, err := c.Service.StorageServices()
if err != nil { if err != nil {
return nil, fmt.Errorf("could not query storage services (%v:%v): %v", q.Host, q.Port, err) return nil, fmt.Errorf("failed to query storage services (%v:%v): %v", q.Host, q.Port, err)
} }
data := map[string]any{ data := map[string]any{
@ -419,7 +404,7 @@ func CollectStorage(c *gofish.APIClient, q *QueryParams) ([]byte, error) {
} }
b, err := json.MarshalIndent(data, "", " ") b, err := json.MarshalIndent(data, "", " ")
if err != nil { if err != nil {
return nil, fmt.Errorf("could not marshal JSON: %v", err) return nil, fmt.Errorf("failed to marshal JSON: %v", err)
} }
return b, nil return b, nil
@ -428,7 +413,7 @@ func CollectStorage(c *gofish.APIClient, q *QueryParams) ([]byte, error) {
func CollectSystems(c *gofish.APIClient, q *QueryParams) ([]byte, error) { func CollectSystems(c *gofish.APIClient, q *QueryParams) ([]byte, error) {
systems, err := c.Service.Systems() systems, err := c.Service.Systems()
if err != nil { if err != nil {
return nil, fmt.Errorf("could not query systems (%v:%v): %v", q.Host, q.Port, err) return nil, fmt.Errorf("failed to query systems (%v:%v): %v", q.Host, q.Port, err)
} }
// query the system's ethernet interfaces // query the system's ethernet interfaces
@ -449,7 +434,7 @@ func CollectSystems(c *gofish.APIClient, q *QueryParams) ([]byte, error) {
data := map[string]any{"Systems": temp} data := map[string]any{"Systems": temp}
b, err := json.MarshalIndent(data, "", " ") b, err := json.MarshalIndent(data, "", " ")
if err != nil { if err != nil {
return nil, fmt.Errorf("could not marshal JSON: %v", err) return nil, fmt.Errorf("failed to marshal JSON: %v", err)
} }
return b, nil return b, nil
@ -458,13 +443,13 @@ func CollectSystems(c *gofish.APIClient, q *QueryParams) ([]byte, error) {
func CollectRegisteries(c *gofish.APIClient, q *QueryParams) ([]byte, error) { func CollectRegisteries(c *gofish.APIClient, q *QueryParams) ([]byte, error) {
registries, err := c.Service.Registries() registries, err := c.Service.Registries()
if err != nil { if err != nil {
return nil, fmt.Errorf("could not query storage systems (%v:%v): %v", q.Host, q.Port, err) return nil, fmt.Errorf("failed to query storage systems (%v:%v): %v", q.Host, q.Port, err)
} }
data := map[string]any{"Registries": registries} data := map[string]any{"Registries": registries}
b, err := json.MarshalIndent(data, "", " ") b, err := json.MarshalIndent(data, "", " ")
if err != nil { if err != nil {
return nil, fmt.Errorf("could not marshal JSON: %v", err) return nil, fmt.Errorf("failed to marshal JSON: %v", err)
} }
return b, nil return b, nil
@ -504,7 +489,7 @@ func CollectProcessors(q *QueryParams) ([]byte, error) {
data := map[string]any{"Processors": procs} data := map[string]any{"Processors": procs}
b, err := json.MarshalIndent(data, "", " ") b, err := json.MarshalIndent(data, "", " ")
if err != nil { if err != nil {
return nil, fmt.Errorf("could not marshal JSON: %v", err) return nil, fmt.Errorf("failed to marshal JSON: %v", err)
} }
return b, nil return b, nil
@ -517,7 +502,7 @@ func connectGofish(q *QueryParams) (*gofish.APIClient, error) {
} }
c, err := gofish.Connect(config) c, err := gofish.Connect(config)
if err != nil { if err != nil {
return nil, fmt.Errorf("could not connect to redfish endpoint: %v", err) return nil, fmt.Errorf("failed to connect to redfish endpoint: %v", err)
} }
if c != nil { if c != nil {
c.Service.ProtocolFeaturesSupported = gofish.ProtocolFeaturesSupported{ c.Service.ProtocolFeaturesSupported = gofish.ProtocolFeaturesSupported{
@ -532,24 +517,22 @@ func connectGofish(q *QueryParams) (*gofish.APIClient, error) {
func makeGofishConfig(q *QueryParams) (gofish.ClientConfig, error) { func makeGofishConfig(q *QueryParams) (gofish.ClientConfig, error) {
var ( var (
client = &http.Client{} client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
DisableKeepAlives: true,
Dial: (&net.Dialer{
Timeout: 120 * time.Second,
KeepAlive: 120 * time.Second,
}).Dial,
TLSHandshakeTimeout: 120 * time.Second,
ResponseHeaderTimeout: 120 * time.Second,
},
}
url = baseRedfishUrl(q) url = baseRedfishUrl(q)
) )
// NOTE: CA certs are not used for BMCs as far as I know
// if q.CaCertPath != "" {
// cacert, err := os.ReadFile(q.CaCertPath)
// if err != nil {
// return config, fmt.Errorf("failed to read CA cert file: %v", err)
// }
// certPool := x509.NewCertPool()
// certPool.AppendCertsFromPEM(cacert)
// client.Transport = &http.Transport{
// TLSClientConfig: &tls.Config{
// RootCAs: certPool,
// InsecureSkipVerify: false,
// },
// }
// }
return gofish.ClientConfig{ return gofish.ClientConfig{
Endpoint: url, Endpoint: url,
Username: q.User, Username: q.User,
@ -567,7 +550,7 @@ func makeRequest[T any](client *bmclib.Client, fn func(context.Context) (T, erro
err := client.Open(ctx) err := client.Open(ctx)
if err != nil { if err != nil {
ctxCancel() ctxCancel()
return nil, fmt.Errorf("could not open client: %v", err) return nil, fmt.Errorf("failed to open client: %v", err)
} }
defer client.Close(ctx) defer client.Close(ctx)
@ -575,7 +558,7 @@ func makeRequest[T any](client *bmclib.Client, fn func(context.Context) (T, erro
response, err := fn(ctx) response, err := fn(ctx)
if err != nil { if err != nil {
ctxCancel() ctxCancel()
return nil, fmt.Errorf("could not get response: %v", err) return nil, fmt.Errorf("failed to get response: %v", err)
} }
ctxCancel() ctxCancel()
@ -585,7 +568,7 @@ func makeRequest[T any](client *bmclib.Client, fn func(context.Context) (T, erro
func makeJson(object any) ([]byte, error) { func makeJson(object any) ([]byte, error) {
b, err := json.MarshalIndent(object, "", " ") b, err := json.MarshalIndent(object, "", " ")
if err != nil { if err != nil {
return nil, fmt.Errorf("could not marshal JSON: %v", err) return nil, fmt.Errorf("failed to marshal JSON: %v", err)
} }
return []byte(b), nil return []byte(b), nil
} }