mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 22:00:04 -07:00
Merge pull request #26 from OpenCHAMI/cleanup
Clean up and remove some old code paths and fix cert issues
This commit is contained in:
commit
8ba2b1c1db
13 changed files with 205 additions and 211 deletions
|
|
@ -27,7 +27,7 @@ var collectCmd = &cobra.Command{
|
||||||
// get probe states stored in db from scan
|
// get probe states stored in db from scan
|
||||||
probeStates, err := sqlite.GetProbeResults(dbpath)
|
probeStates, err := sqlite.GetProbeResults(dbpath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Log.Errorf("could not get states: %v", err)
|
l.Log.Errorf("failed toget states: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// try to load access token either from env var, file, or config if var not set
|
// try to load access token either from env var, file, or config if var not set
|
||||||
|
|
@ -63,10 +63,10 @@ var collectCmd = &cobra.Command{
|
||||||
}
|
}
|
||||||
magellan.CollectAll(&probeStates, l, q)
|
magellan.CollectAll(&probeStates, l, q)
|
||||||
|
|
||||||
// confirm the inventories were added
|
// add necessary headers for final request (like token)
|
||||||
err = smd.GetRedfishEndpoints()
|
headers := make(map[string]string)
|
||||||
if err != nil {
|
if q.AccessToken != "" {
|
||||||
l.Log.Errorf("could not get redfish endpoints: %v", err)
|
headers["Authorization"] = "Bearer " + q.AccessToken
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
@ -77,7 +77,7 @@ func init() {
|
||||||
collectCmd.PersistentFlags().IntVarP(&smd.Port, "port", "p", smd.Port, "set the port to the smd API")
|
collectCmd.PersistentFlags().IntVarP(&smd.Port, "port", "p", smd.Port, "set the port to the smd API")
|
||||||
collectCmd.PersistentFlags().StringVar(&user, "user", "", "set the BMC user")
|
collectCmd.PersistentFlags().StringVar(&user, "user", "", "set the BMC user")
|
||||||
collectCmd.PersistentFlags().StringVar(&pass, "pass", "", "set the BMC password")
|
collectCmd.PersistentFlags().StringVar(&pass, "pass", "", "set the BMC password")
|
||||||
collectCmd.PersistentFlags().StringVar(&protocol, "protocol", "https", "set the Redfish protocol")
|
collectCmd.PersistentFlags().StringVar(&protocol, "protocol", "https", "set the protocol used to query")
|
||||||
collectCmd.PersistentFlags().StringVarP(&outputPath, "output", "o", "/tmp/magellan/data/", "set the path to store collection data")
|
collectCmd.PersistentFlags().StringVarP(&outputPath, "output", "o", "/tmp/magellan/data/", "set the path to store collection data")
|
||||||
collectCmd.PersistentFlags().BoolVar(&forceUpdate, "force-update", false, "set flag to force update data sent to SMD ")
|
collectCmd.PersistentFlags().BoolVar(&forceUpdate, "force-update", false, "set flag to force update data sent to SMD ")
|
||||||
collectCmd.PersistentFlags().StringVar(&preferredDriver, "preferred-driver", "ipmi", "set the preferred driver to use")
|
collectCmd.PersistentFlags().StringVar(&preferredDriver, "preferred-driver", "ipmi", "set the preferred driver to use")
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ var listCmd = &cobra.Command{
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
probeResults, err := sqlite.GetProbeResults(dbpath)
|
probeResults, err := sqlite.GetProbeResults(dbpath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("could not get probe results: %v\n", err)
|
logrus.Errorf("failed toget probe results: %v\n", err)
|
||||||
}
|
}
|
||||||
for _, r := range probeResults {
|
for _, r := range probeResults {
|
||||||
fmt.Printf("%s:%d (%s)\n", r.Host, r.Port, r.Protocol)
|
fmt.Printf("%s:%d (%s)\n", r.Host, r.Port, r.Protocol)
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ func Execute() {
|
||||||
|
|
||||||
func LoadAccessToken() (string, error) {
|
func LoadAccessToken() (string, error) {
|
||||||
// try to load token from env var
|
// try to load token from env var
|
||||||
testToken := os.Getenv("OCHAMI_ACCESS_TOKEN")
|
testToken := os.Getenv("MAGELLAN_ACCESS_TOKEN")
|
||||||
if testToken != "" {
|
if testToken != "" {
|
||||||
return testToken, nil
|
return testToken, nil
|
||||||
}
|
}
|
||||||
|
|
@ -72,7 +72,7 @@ func LoadAccessToken() (string, error) {
|
||||||
if testToken != "" {
|
if testToken != "" {
|
||||||
return testToken, nil
|
return testToken, nil
|
||||||
}
|
}
|
||||||
return "", fmt.Errorf("could not load token from environment variable, file, or config")
|
return "", fmt.Errorf("failed toload token from environment variable, file, or config")
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
@ -80,7 +80,7 @@ func init() {
|
||||||
rootCmd.PersistentFlags().IntVar(&threads, "threads", -1, "set the number of threads")
|
rootCmd.PersistentFlags().IntVar(&threads, "threads", -1, "set the number of threads")
|
||||||
rootCmd.PersistentFlags().IntVar(&timeout, "timeout", 30, "set the timeout")
|
rootCmd.PersistentFlags().IntVar(&timeout, "timeout", 30, "set the timeout")
|
||||||
rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "", "set the config file path")
|
rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "", "set the config file path")
|
||||||
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", true, "set verbose flag")
|
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "set verbose flag")
|
||||||
rootCmd.PersistentFlags().StringVar(&accessToken, "access-token", "", "set the access token")
|
rootCmd.PersistentFlags().StringVar(&accessToken, "access-token", "", "set the access token")
|
||||||
rootCmd.PersistentFlags().StringVar(&dbpath, "db.path", "/tmp/magellan/magellan.db", "set the probe storage path")
|
rootCmd.PersistentFlags().StringVar(&dbpath, "db.path", "/tmp/magellan/magellan.db", "set the probe storage path")
|
||||||
|
|
||||||
|
|
@ -103,7 +103,7 @@ func SetDefaults() {
|
||||||
viper.SetDefault("threads", 1)
|
viper.SetDefault("threads", 1)
|
||||||
viper.SetDefault("timeout", 30)
|
viper.SetDefault("timeout", 30)
|
||||||
viper.SetDefault("config", "")
|
viper.SetDefault("config", "")
|
||||||
viper.SetDefault("verbose", true)
|
viper.SetDefault("verbose", false)
|
||||||
viper.SetDefault("db.path", "/tmp/magellan/magellan.db")
|
viper.SetDefault("db.path", "/tmp/magellan/magellan.db")
|
||||||
viper.SetDefault("scan.hosts", []string{})
|
viper.SetDefault("scan.hosts", []string{})
|
||||||
viper.SetDefault("scan.ports", []int{})
|
viper.SetDefault("scan.ports", []int{})
|
||||||
|
|
|
||||||
|
|
@ -58,14 +58,16 @@ var scanCmd = &cobra.Command{
|
||||||
threads = mathutil.Clamp(len(hostsToScan), 1, 255)
|
threads = mathutil.Clamp(len(hostsToScan), 1, 255)
|
||||||
}
|
}
|
||||||
probeStates := magellan.ScanForAssets(hostsToScan, portsToScan, threads, timeout, disableProbing)
|
probeStates := magellan.ScanForAssets(hostsToScan, portsToScan, threads, timeout, disableProbing)
|
||||||
|
if verbose {
|
||||||
for _, r := range probeStates {
|
for _, r := range probeStates {
|
||||||
fmt.Printf("%s:%d (%s)\n", r.Host, r.Port, r.Protocol)
|
fmt.Printf("%s:%d (%s)\n", r.Host, r.Port, r.Protocol)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// make the dbpath dir if needed
|
// make the dbpath dir if needed
|
||||||
err := os.MkdirAll(path.Dir(dbpath), 0766)
|
err := os.MkdirAll(path.Dir(dbpath), 0766)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("could not make database directory: %v", err)
|
fmt.Printf("failed tomake database directory: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
sqlite.InsertProbeResults(dbpath, &probeStates)
|
sqlite.InsertProbeResults(dbpath, &probeStates)
|
||||||
|
|
|
||||||
|
|
@ -49,19 +49,19 @@ var updateCmd = &cobra.Command{
|
||||||
if status {
|
if status {
|
||||||
err := magellan.GetUpdateStatus(q)
|
err := magellan.GetUpdateStatus(q)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Log.Errorf("could not get update status: %v", err)
|
l.Log.Errorf("failed toget update status: %v", err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// client, err := magellan.NewClient(l, &q.QueryParams)
|
// client, err := magellan.NewClient(l, &q.QueryParams)
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
// l.Log.Errorf("could not make client: %v", err)
|
// l.Log.Errorf("failed tomake client: %v", err)
|
||||||
// }
|
// }
|
||||||
// err = magellan.UpdateFirmware(client, l, q)
|
// err = magellan.UpdateFirmware(client, l, q)
|
||||||
err := magellan.UpdateFirmwareRemote(q)
|
err := magellan.UpdateFirmwareRemote(q)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Log.Errorf("could not update firmware: %v", err)
|
l.Log.Errorf("failed toupdate firmware: %v", err)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,15 +43,15 @@ func ScanForAssets() error {
|
||||||
func QueryScannedPorts() error {
|
func QueryScannedPorts() error {
|
||||||
// Perform scan and collect from dora server
|
// Perform scan and collect from dora server
|
||||||
url := makeEndpointUrl("/scanned_ports")
|
url := makeEndpointUrl("/scanned_ports")
|
||||||
_, body, err := util.MakeRequest(url, "GET", nil, nil)
|
_, body, err := util.MakeRequest(nil, url, "GET", nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not discover assets: %v", err)
|
return fmt.Errorf("failed todiscover assets: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// get data from JSON
|
// get data from JSON
|
||||||
var res map[string]any
|
var res map[string]any
|
||||||
if err := json.Unmarshal(body, &res); err != nil {
|
if err := json.Unmarshal(body, &res); err != nil {
|
||||||
return fmt.Errorf("could not unmarshal response body: %v", err)
|
return fmt.Errorf("failed tounmarshal response body: %v", err)
|
||||||
}
|
}
|
||||||
data := res["data"]
|
data := res["data"]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,18 @@
|
||||||
package smd
|
package smd
|
||||||
|
|
||||||
// See ref for API docs:
|
// See ref for API docs:
|
||||||
// https://github.com/Cray-HPE/hms-smd/blob/master/docs/examples.adoc
|
// https://github.com/OpenCHAMI/hms-smd/blob/master/docs/examples.adoc
|
||||||
// https://github.com/alexlovelltroy/hms-smd
|
// https://github.com/OpenCHAMI/hms-smd
|
||||||
import (
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"crypto/x509"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/OpenCHAMI/magellan/internal/util"
|
"github.com/OpenCHAMI/magellan/internal/util"
|
||||||
// hms "github.com/alexlovelltroy/hms-smd"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -16,40 +21,90 @@ var (
|
||||||
Port = 27779
|
Port = 27779
|
||||||
)
|
)
|
||||||
|
|
||||||
func makeEndpointUrl(endpoint string) string {
|
type Option func(*Client)
|
||||||
return Host + ":" + fmt.Sprint(Port) + BaseEndpoint + endpoint
|
|
||||||
|
type Client struct {
|
||||||
|
*http.Client
|
||||||
|
CACertPool *x509.CertPool
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRedfishEndpoints() error {
|
func NewClient(opts ...Option) *Client {
|
||||||
|
client := &Client{
|
||||||
|
Client: http.DefaultClient,
|
||||||
|
}
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(client)
|
||||||
|
}
|
||||||
|
return client
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithHttpClient(httpClient *http.Client) Option {
|
||||||
|
return func(c *Client) {
|
||||||
|
c.Client = httpClient
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This MakeRequest function is a wrapper around the util.MakeRequest function
|
||||||
|
// with a couple of niceties with using a smd.Client
|
||||||
|
func (c *Client) MakeRequest(url string, method string, body []byte, headers map[string]string) (*http.Response, []byte, error) {
|
||||||
|
return util.MakeRequest(c.Client, url, method, body, headers)
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithCertPool(certPool *x509.CertPool) Option {
|
||||||
|
return func(c *Client) {
|
||||||
|
c.Client.Transport = &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{
|
||||||
|
RootCAs: certPool,
|
||||||
|
InsecureSkipVerify: true,
|
||||||
|
},
|
||||||
|
DisableKeepAlives: true,
|
||||||
|
Dial: (&net.Dialer{
|
||||||
|
Timeout: 120 * time.Second,
|
||||||
|
KeepAlive: 120 * time.Second,
|
||||||
|
}).Dial,
|
||||||
|
TLSHandshakeTimeout: 120 * time.Second,
|
||||||
|
ResponseHeaderTimeout: 120 * time.Second,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithSecureTLS(certPath string) Option {
|
||||||
|
cacert, _ := os.ReadFile(certPath)
|
||||||
|
certPool := x509.NewCertPool()
|
||||||
|
certPool.AppendCertsFromPEM(cacert)
|
||||||
|
return WithCertPool(certPool)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) GetRedfishEndpoints(headers map[string]string, opts ...Option) error {
|
||||||
url := makeEndpointUrl("/Inventory/RedfishEndpoints")
|
url := makeEndpointUrl("/Inventory/RedfishEndpoints")
|
||||||
_, body, err := util.MakeRequest(url, "GET", nil, nil)
|
_, body, err := c.MakeRequest(url, "GET", nil, headers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not get endpoint: %v", err)
|
return fmt.Errorf("failed toget endpoint: %v", err)
|
||||||
}
|
}
|
||||||
// fmt.Println(res)
|
// fmt.Println(res)
|
||||||
fmt.Println(string(body))
|
fmt.Println(string(body))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetComponentEndpoint(xname string) error {
|
func (c *Client) GetComponentEndpoint(xname string) error {
|
||||||
url := makeEndpointUrl("/Inventory/ComponentsEndpoints/" + xname)
|
url := makeEndpointUrl("/Inventory/ComponentsEndpoints/" + xname)
|
||||||
res, body, err := util.MakeRequest(url, "GET", nil, nil)
|
res, body, err := c.MakeRequest(url, "GET", nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not get endpoint: %v", err)
|
return fmt.Errorf("failed toget endpoint: %v", err)
|
||||||
}
|
}
|
||||||
fmt.Println(res)
|
fmt.Println(res)
|
||||||
fmt.Println(string(body))
|
fmt.Println(string(body))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddRedfishEndpoint(data []byte, headers map[string]string) error {
|
func (c *Client) AddRedfishEndpoint(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 toadd redfish endpoint: no data found")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add redfish endpoint via POST `/hsm/v2/Inventory/RedfishEndpoints` endpoint
|
// Add redfish endpoint via POST `/hsm/v2/Inventory/RedfishEndpoints` endpoint
|
||||||
url := makeEndpointUrl("/Inventory/RedfishEndpoints")
|
url := makeEndpointUrl("/Inventory/RedfishEndpoints")
|
||||||
res, body, err := util.MakeRequest(url, "POST", data, headers)
|
res, body, err := c.MakeRequest(url, "POST", data, headers)
|
||||||
if res != nil {
|
if res != nil {
|
||||||
statusOk := res.StatusCode >= 200 && res.StatusCode < 300
|
statusOk := res.StatusCode >= 200 && res.StatusCode < 300
|
||||||
if !statusOk {
|
if !statusOk {
|
||||||
|
|
@ -60,19 +115,23 @@ func AddRedfishEndpoint(data []byte, headers map[string]string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func 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)
|
||||||
res, body, err := util.MakeRequest(url, "PUT", data, headers)
|
res, body, err := c.MakeRequest(url, "PUT", data, headers)
|
||||||
fmt.Printf("%v (%v)\n%s\n", url, res.Status, string(body))
|
fmt.Printf("%v (%v)\n%s\n", url, res.Status, string(body))
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func makeEndpointUrl(endpoint string) string {
|
||||||
|
return Host + ":" + fmt.Sprint(Port) + BaseEndpoint + endpoint
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ package magellan
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"crypto/x509"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
@ -19,7 +18,6 @@ import (
|
||||||
|
|
||||||
"github.com/Cray-HPE/hms-xname/xnames"
|
"github.com/Cray-HPE/hms-xname/xnames"
|
||||||
bmclib "github.com/bmc-toolbox/bmclib/v2"
|
bmclib "github.com/bmc-toolbox/bmclib/v2"
|
||||||
"github.com/jacobweinstock/registrar"
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
"github.com/stmcginnis/gofish"
|
"github.com/stmcginnis/gofish"
|
||||||
_ "github.com/stmcginnis/gofish"
|
_ "github.com/stmcginnis/gofish"
|
||||||
|
|
@ -52,56 +50,6 @@ type QueryParams struct {
|
||||||
AccessToken string
|
AccessToken string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(l *log.Logger, q *QueryParams) (*bmclib.Client, error) {
|
|
||||||
|
|
||||||
tr := &http.Transport{
|
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
||||||
}
|
|
||||||
httpClient := http.Client{
|
|
||||||
Transport: tr,
|
|
||||||
}
|
|
||||||
|
|
||||||
// init client
|
|
||||||
clientOpts := []bmclib.Option{
|
|
||||||
// bmclib.WithSecureTLS(nil),
|
|
||||||
bmclib.WithHTTPClient(&httpClient),
|
|
||||||
// bmclib.WithLogger(),
|
|
||||||
bmclib.WithRedfishHTTPClient(&httpClient),
|
|
||||||
// bmclib.WithDellRedfishUseBasicAuth(true),
|
|
||||||
bmclib.WithRedfishPort(fmt.Sprint(q.Port)),
|
|
||||||
bmclib.WithRedfishUseBasicAuth(true),
|
|
||||||
bmclib.WithIpmitoolPort(fmt.Sprint(IPMI_PORT)),
|
|
||||||
bmclib.WithIpmitoolPath(q.IpmitoolPath),
|
|
||||||
}
|
|
||||||
|
|
||||||
// only work if valid cert is provided
|
|
||||||
if q.CaCertPath != "" {
|
|
||||||
pool := x509.NewCertPool()
|
|
||||||
data, err := os.ReadFile(q.CaCertPath)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("could not read cert pool file: %v", err)
|
|
||||||
}
|
|
||||||
pool.AppendCertsFromPEM(data)
|
|
||||||
// a nil pool uses the system certs
|
|
||||||
clientOpts = append(clientOpts, bmclib.WithSecureTLS(pool))
|
|
||||||
}
|
|
||||||
url := ""
|
|
||||||
fmt.Println(url)
|
|
||||||
if q.User != "" && q.Pass != "" {
|
|
||||||
url += fmt.Sprintf("%s://%s:%s@%s", q.Protocol, q.User, q.Pass, q.Host)
|
|
||||||
} else {
|
|
||||||
url += q.Host
|
|
||||||
}
|
|
||||||
client := bmclib.NewClient(url, q.User, q.Pass, clientOpts...)
|
|
||||||
ds := registrar.Drivers{}
|
|
||||||
for _, driver := range q.Drivers {
|
|
||||||
ds = append(ds, client.Registry.Using(driver)...) // ipmi, gofish, redfish
|
|
||||||
}
|
|
||||||
client.Registry.Drivers = ds
|
|
||||||
|
|
||||||
return client, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) error {
|
func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) error {
|
||||||
// check for available probe states
|
// check for available probe states
|
||||||
if probeStates == nil {
|
if probeStates == nil {
|
||||||
|
|
@ -115,16 +63,20 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
found := make([]string, 0, len(*probeStates))
|
|
||||||
done := make(chan struct{}, q.Threads+1)
|
|
||||||
chanProbeState := make(chan ScannedResult, q.Threads+1)
|
|
||||||
|
|
||||||
// collect bmc information asynchronously
|
// collect bmc information asynchronously
|
||||||
var offset = 0
|
var (
|
||||||
var wg sync.WaitGroup
|
offset = 0
|
||||||
|
wg sync.WaitGroup
|
||||||
|
found = make([]string, 0, len(*probeStates))
|
||||||
|
done = make(chan struct{}, q.Threads+1)
|
||||||
|
chanProbeState = make(chan ScannedResult, q.Threads+1)
|
||||||
|
client = smd.NewClient(
|
||||||
|
smd.WithSecureTLS(q.CaCertPath),
|
||||||
|
)
|
||||||
|
)
|
||||||
wg.Add(q.Threads)
|
wg.Add(q.Threads)
|
||||||
for i := 0; i < q.Threads; i++ {
|
for i := 0; i < q.Threads; i++ {
|
||||||
go func() {
|
go func() {
|
||||||
|
|
@ -146,14 +98,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
|
||||||
|
|
@ -171,21 +118,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)
|
||||||
|
|
@ -194,7 +131,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"]
|
||||||
|
|
@ -217,27 +154,29 @@ 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 {
|
||||||
fmt.Printf("%v\n", string(body))
|
fmt.Printf("%v\n", string(body))
|
||||||
}
|
}
|
||||||
|
|
||||||
// write JSON data to file
|
// write JSON data to file if output path is set
|
||||||
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// add all endpoints to smd
|
// add all endpoints to smd
|
||||||
err = smd.AddRedfishEndpoint(body, headers)
|
err = client.AddRedfishEndpoint(body, headers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Log.Error(err)
|
l.Log.Error(err)
|
||||||
|
|
||||||
// try updating instead
|
// try updating instead
|
||||||
if q.ForceUpdate {
|
if q.ForceUpdate {
|
||||||
err = smd.UpdateRedfishEndpoint(data["ID"].(string), body, headers)
|
err = client.UpdateRedfishEndpoint(data["ID"].(string), body, headers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Log.Error(err)
|
l.Log.Error(err)
|
||||||
}
|
}
|
||||||
|
|
@ -274,19 +213,18 @@ func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) err
|
||||||
close(chanProbeState)
|
close(chanProbeState)
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
close(done)
|
close(done)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
|
|
@ -294,14 +232,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()
|
||||||
|
|
@ -315,13 +253,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
|
||||||
|
|
@ -329,7 +267,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()
|
||||||
|
|
@ -342,13 +280,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
|
||||||
|
|
@ -356,7 +294,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()
|
||||||
|
|
@ -371,7 +309,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)
|
||||||
|
|
@ -379,7 +317,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
|
||||||
|
|
@ -387,7 +325,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()
|
||||||
|
|
@ -397,7 +335,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
|
||||||
|
|
@ -406,7 +344,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
|
||||||
|
|
@ -419,13 +357,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
|
||||||
|
|
@ -434,13 +372,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
|
||||||
|
|
@ -449,12 +387,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{
|
||||||
|
|
@ -465,7 +403,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
|
||||||
|
|
@ -474,7 +412,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
|
||||||
|
|
@ -495,7 +433,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
|
||||||
|
|
@ -504,13 +442,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
|
||||||
|
|
@ -518,7 +456,7 @@ func CollectRegisteries(c *gofish.APIClient, q *QueryParams) ([]byte, error) {
|
||||||
|
|
||||||
func CollectProcessors(q *QueryParams) ([]byte, error) {
|
func CollectProcessors(q *QueryParams) ([]byte, error) {
|
||||||
url := baseRedfishUrl(q) + "/Systems"
|
url := baseRedfishUrl(q) + "/Systems"
|
||||||
res, body, err := util.MakeRequest(url, "GET", nil, nil)
|
res, body, err := util.MakeRequest(nil, url, "GET", nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("something went wrong: %v", err)
|
return nil, fmt.Errorf("something went wrong: %v", err)
|
||||||
} else if res == nil {
|
} else if res == nil {
|
||||||
|
|
@ -537,7 +475,7 @@ func CollectProcessors(q *QueryParams) ([]byte, error) {
|
||||||
for _, member := range members {
|
for _, member := range members {
|
||||||
var oid = member["@odata.id"].(string)
|
var oid = member["@odata.id"].(string)
|
||||||
var infoUrl = url + oid
|
var infoUrl = url + oid
|
||||||
res, _, err := util.MakeRequest(infoUrl, "GET", nil, nil)
|
res, _, err := util.MakeRequest(nil, infoUrl, "GET", nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("something went wrong: %v", err)
|
return nil, fmt.Errorf("something went wrong: %v", err)
|
||||||
} else if res == nil {
|
} else if res == nil {
|
||||||
|
|
@ -550,7 +488,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
|
||||||
|
|
@ -563,7 +501,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{
|
||||||
|
|
@ -578,39 +516,23 @@ 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{
|
||||||
url = baseRedfishUrl(q)
|
Transport: &http.Transport{
|
||||||
config = gofish.ClientConfig{
|
|
||||||
Endpoint: url,
|
|
||||||
Username: q.User,
|
|
||||||
Password: q.Pass,
|
|
||||||
Insecure: q.CaCertPath == "",
|
|
||||||
TLSHandshakeTimeout: q.Timeout,
|
|
||||||
HTTPClient: client,
|
|
||||||
// MaxConcurrentRequests: int64(q.Threads), // NOTE: this was added in latest gofish
|
|
||||||
}
|
|
||||||
)
|
|
||||||
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{
|
TLSClientConfig: &tls.Config{
|
||||||
RootCAs: certPool,
|
InsecureSkipVerify: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
url = baseRedfishUrl(q)
|
||||||
|
)
|
||||||
return gofish.ClientConfig{
|
return gofish.ClientConfig{
|
||||||
Endpoint: url,
|
Endpoint: url,
|
||||||
Username: q.User,
|
Username: q.User,
|
||||||
Password: q.Pass,
|
Password: q.Pass,
|
||||||
Insecure: q.CaCertPath == "",
|
Insecure: true,
|
||||||
TLSHandshakeTimeout: q.Timeout,
|
TLSHandshakeTimeout: q.Timeout,
|
||||||
HTTPClient: client,
|
HTTPClient: client,
|
||||||
// MaxConcurrentRequests: int64(q.Threads), // NOTE: this was added in latest gofish
|
// MaxConcurrentRequests: int64(q.Threads), // NOTE: this was added in latest version of gofish
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -620,7 +542,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)
|
||||||
|
|
@ -628,7 +550,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()
|
||||||
|
|
@ -638,7 +560,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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ func LoadConfig(path string) error {
|
||||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||||
return fmt.Errorf("config file not found: %w", err)
|
return fmt.Errorf("config file not found: %w", err)
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("could not load config file: %w", err)
|
return fmt.Errorf("failed toload config file: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ func CreateProbeResultsIfNotExists(path string) (*sqlx.DB, error) {
|
||||||
// TODO: it may help with debugging to check for file permissions here first
|
// TODO: it may help with debugging to check for file permissions here first
|
||||||
db, err := sqlx.Open("sqlite3", path)
|
db, err := sqlx.Open("sqlite3", path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not open database: %v", err)
|
return nil, fmt.Errorf("failed toopen database: %v", err)
|
||||||
}
|
}
|
||||||
db.MustExec(schema)
|
db.MustExec(schema)
|
||||||
return db, nil
|
return db, nil
|
||||||
|
|
@ -45,12 +45,12 @@ func InsertProbeResults(path string, states *[]magellan.ScannedResult) error {
|
||||||
VALUES (:host, :port, :protocol, :state);`
|
VALUES (:host, :port, :protocol, :state);`
|
||||||
_, err := tx.NamedExec(sql, &state)
|
_, err := tx.NamedExec(sql, &state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("could not execute transaction: %v\n", err)
|
fmt.Printf("failed toexecute transaction: %v\n", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = tx.Commit()
|
err = tx.Commit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not commit transaction: %v", err)
|
return fmt.Errorf("failed tocommit transaction: %v", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -61,20 +61,20 @@ func DeleteProbeResults(path string, results *[]magellan.ScannedResult) error {
|
||||||
}
|
}
|
||||||
db, err := sqlx.Open("sqlite3", path)
|
db, err := sqlx.Open("sqlite3", path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not open database: %v", err)
|
return fmt.Errorf("failed toopen database: %v", err)
|
||||||
}
|
}
|
||||||
tx := db.MustBegin()
|
tx := db.MustBegin()
|
||||||
for _, state := range *results {
|
for _, state := range *results {
|
||||||
sql := `DELETE FROM magellan_scanned_ports WHERE host = :host, port = :port;`
|
sql := `DELETE FROM magellan_scanned_ports WHERE host = :host, port = :port;`
|
||||||
_, err := tx.NamedExec(sql, &state)
|
_, err := tx.NamedExec(sql, &state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("could not execute transaction: %v\n", err)
|
fmt.Printf("failed toexecute transaction: %v\n", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = tx.Commit()
|
err = tx.Commit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not commit transaction: %v", err)
|
return fmt.Errorf("failed tocommit transaction: %v", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -82,13 +82,13 @@ func DeleteProbeResults(path string, results *[]magellan.ScannedResult) error {
|
||||||
func GetProbeResults(path string) ([]magellan.ScannedResult, error) {
|
func GetProbeResults(path string) ([]magellan.ScannedResult, error) {
|
||||||
db, err := sqlx.Open("sqlite3", path)
|
db, err := sqlx.Open("sqlite3", path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not open database: %v", err)
|
return nil, fmt.Errorf("failed toopen database: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
results := []magellan.ScannedResult{}
|
results := []magellan.ScannedResult{}
|
||||||
err = db.Select(&results, "SELECT * FROM magellan_scanned_ports ORDER BY host ASC, port ASC;")
|
err = db.Select(&results, "SELECT * FROM magellan_scanned_ports ORDER BY host ASC, port ASC;")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not retrieve probes: %v", err)
|
return nil, fmt.Errorf("failed toretrieve probes: %v", err)
|
||||||
}
|
}
|
||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -116,7 +116,7 @@ func ScanForAssets(hosts []string, ports []int, threads int, timeout int, disabl
|
||||||
probeResults := []ScannedResult{}
|
probeResults := []ScannedResult{}
|
||||||
for _, result := range scannedResults {
|
for _, result := range scannedResults {
|
||||||
url := fmt.Sprintf("https://%s:%d/redfish/v1/", result.Host, result.Port)
|
url := fmt.Sprintf("https://%s:%d/redfish/v1/", result.Host, result.Port)
|
||||||
res, _, err := util.MakeRequest(url, "GET", nil, nil)
|
res, _, err := util.MakeRequest(nil, url, "GET", nil, nil)
|
||||||
if err != nil || res == nil {
|
if err != nil || res == nil {
|
||||||
continue
|
continue
|
||||||
} else if res.StatusCode != http.StatusOK {
|
} else if res.StatusCode != http.StatusOK {
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ func UpdateFirmware(client *bmclib.Client, l *log.Logger, q *UpdateParams) error
|
||||||
err := client.Open(ctx)
|
err := client.Open(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctxCancel()
|
ctxCancel()
|
||||||
return fmt.Errorf("could not connect to bmc: %v", err)
|
return fmt.Errorf("failed toconnect to bmc: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer client.Close(ctx)
|
defer client.Close(ctx)
|
||||||
|
|
@ -47,7 +47,7 @@ func UpdateFirmware(client *bmclib.Client, l *log.Logger, q *UpdateParams) error
|
||||||
file, err := os.Open(q.FirmwarePath)
|
file, err := os.Open(q.FirmwarePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctxCancel()
|
ctxCancel()
|
||||||
return fmt.Errorf("could not open firmware path: %v", err)
|
return fmt.Errorf("failed toopen firmware path: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
@ -55,7 +55,7 @@ func UpdateFirmware(client *bmclib.Client, l *log.Logger, q *UpdateParams) error
|
||||||
taskId, err := client.FirmwareInstall(ctx, q.Component, constants.FirmwareApplyOnReset, true, file)
|
taskId, err := client.FirmwareInstall(ctx, q.Component, constants.FirmwareApplyOnReset, true, file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctxCancel()
|
ctxCancel()
|
||||||
return fmt.Errorf("could not install firmware: %v", err)
|
return fmt.Errorf("failed toinstall firmware: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|
@ -137,9 +137,9 @@ func UpdateFirmwareRemote(q *UpdateParams) error {
|
||||||
}
|
}
|
||||||
data, err := json.Marshal(b)
|
data, err := json.Marshal(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not marshal data: %v", err)
|
return fmt.Errorf("failed tomarshal data: %v", err)
|
||||||
}
|
}
|
||||||
res, body, err := util.MakeRequest(url, "POST", data, headers)
|
res, body, err := util.MakeRequest(nil, url, "POST", data, headers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("something went wrong: %v", err)
|
return fmt.Errorf("something went wrong: %v", err)
|
||||||
} else if res == nil {
|
} else if res == nil {
|
||||||
|
|
@ -153,7 +153,7 @@ func UpdateFirmwareRemote(q *UpdateParams) error {
|
||||||
|
|
||||||
func GetUpdateStatus(q *UpdateParams) error {
|
func GetUpdateStatus(q *UpdateParams) error {
|
||||||
url := baseRedfishUrl(&q.QueryParams) + "/redfish/v1/UpdateService"
|
url := baseRedfishUrl(&q.QueryParams) + "/redfish/v1/UpdateService"
|
||||||
res, body, err := util.MakeRequest(url, "GET", nil, nil)
|
res, body, err := util.MakeRequest(nil, url, "GET", nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("something went wrong: %v", err)
|
return fmt.Errorf("something went wrong: %v", err)
|
||||||
} else if res == nil {
|
} else if res == nil {
|
||||||
|
|
@ -180,7 +180,7 @@ func GetUpdateStatus(q *UpdateParams) error {
|
||||||
// // load file from disk
|
// // load file from disk
|
||||||
// file, err := os.ReadFile(q.FirmwarePath)
|
// file, err := os.ReadFile(q.FirmwarePath)
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
// return fmt.Errorf("could not read file: %v", err)
|
// return fmt.Errorf("failed toread file: %v", err)
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// switch q.TransferProtocol {
|
// switch q.TransferProtocol {
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,12 @@ import (
|
||||||
|
|
||||||
func PathExists(path string) (bool, error) {
|
func PathExists(path string) (bool, error) {
|
||||||
_, err := os.Stat(path)
|
_, err := os.Stat(path)
|
||||||
if err == nil { return true, nil }
|
if err == nil {
|
||||||
if os.IsNotExist(err) { return false, nil }
|
return true, nil
|
||||||
|
}
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -36,24 +40,31 @@ func GetNextIP(ip *net.IP, inc uint) *net.IP {
|
||||||
return &r
|
return &r
|
||||||
}
|
}
|
||||||
|
|
||||||
func MakeRequest(url string, httpMethod string, body []byte, headers map[string]string) (*http.Response, []byte, error) {
|
// Generic convenience function used to make HTTP requests.
|
||||||
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
func MakeRequest(client *http.Client, url string, httpMethod string, body []byte, headers map[string]string) (*http.Response, []byte, error) {
|
||||||
|
// use defaults if no client provided
|
||||||
|
if client == nil {
|
||||||
|
client = http.DefaultClient
|
||||||
|
client.Transport = &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
|
}
|
||||||
|
}
|
||||||
req, err := http.NewRequest(httpMethod, url, bytes.NewBuffer(body))
|
req, err := http.NewRequest(httpMethod, url, bytes.NewBuffer(body))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("could not create new HTTP request: %v", err)
|
return nil, nil, fmt.Errorf("failed to create new HTTP request: %v", err)
|
||||||
}
|
}
|
||||||
req.Header.Add("User-Agent", "magellan")
|
req.Header.Add("User-Agent", "magellan")
|
||||||
for k, v := range headers {
|
for k, v := range headers {
|
||||||
req.Header.Add(k, v)
|
req.Header.Add(k, v)
|
||||||
}
|
}
|
||||||
res, err := http.DefaultClient.Do(req)
|
res, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("could not make request: %v", err)
|
return nil, nil, fmt.Errorf("failed to make request: %v", err)
|
||||||
}
|
}
|
||||||
b, err := io.ReadAll(res.Body)
|
b, err := io.ReadAll(res.Body)
|
||||||
res.Body.Close()
|
res.Body.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("could not read response body: %v", err)
|
return nil, nil, fmt.Errorf("failed to read response body: %v", err)
|
||||||
}
|
}
|
||||||
return res, b, err
|
return res, b, err
|
||||||
}
|
}
|
||||||
|
|
@ -65,9 +76,9 @@ func MakeOutputDirectory(path string) (string, error) {
|
||||||
final := path + "/" + dirname
|
final := path + "/" + dirname
|
||||||
|
|
||||||
// check if path is valid and directory
|
// check if path is valid and directory
|
||||||
pathExists, err := PathExists(final);
|
pathExists, err := PathExists(final)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return final, fmt.Errorf("could not check for existing path: %v", err)
|
return final, fmt.Errorf("failed to check for existing path: %v", err)
|
||||||
}
|
}
|
||||||
if pathExists {
|
if pathExists {
|
||||||
// make sure it is directory with 0o644 permissions
|
// make sure it is directory with 0o644 permissions
|
||||||
|
|
@ -77,7 +88,7 @@ func MakeOutputDirectory(path string) (string, error) {
|
||||||
// create directory with data + time
|
// create directory with data + time
|
||||||
err = os.MkdirAll(final, 0766)
|
err = os.MkdirAll(final, 0766)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return final, fmt.Errorf("could not make directory: %v", err)
|
return final, fmt.Errorf("failed to make directory: %v", err)
|
||||||
}
|
}
|
||||||
return final, nil
|
return final, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue