mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 11:37:01 -07:00
Removed bmclib ctor and separated BMC and SMD clients
This commit is contained in:
parent
60770dadc0
commit
efda3d8aa7
1 changed files with 37 additions and 90 deletions
|
|
@ -2,8 +2,6 @@ package magellan
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
|
||||||
"crypto/x509"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
@ -19,7 +17,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 +49,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 {
|
||||||
|
|
@ -118,13 +65,17 @@ func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) err
|
||||||
l.Log.Errorf("could not make output directory: %v", err)
|
l.Log.Errorf("could not 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() {
|
||||||
|
|
@ -224,20 +175,22 @@ func CollectAll(probeStates *[]ScannedResult, l *log.Logger, q *QueryParams) err
|
||||||
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
|
||||||
err = os.WriteFile(path.Clean(outputPath+"/"+q.Host+".json"), body, os.ModePerm)
|
if outputPath != "" {
|
||||||
if err != nil {
|
err = os.WriteFile(path.Clean(outputPath+"/"+q.Host+".json"), body, os.ModePerm)
|
||||||
l.Log.Errorf("could not write data to file: %v", err)
|
if err != nil {
|
||||||
|
l.Log.Errorf("could not 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,6 +227,7 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -518,7 +472,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 +491,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 {
|
||||||
|
|
@ -580,29 +534,22 @@ func makeGofishConfig(q *QueryParams) (gofish.ClientConfig, error) {
|
||||||
var (
|
var (
|
||||||
client = &http.Client{}
|
client = &http.Client{}
|
||||||
url = baseRedfishUrl(q)
|
url = baseRedfishUrl(q)
|
||||||
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 != "" {
|
// NOTE: CA certs are not used for BMCs as far as I know
|
||||||
cacert, err := os.ReadFile(q.CaCertPath)
|
// if q.CaCertPath != "" {
|
||||||
if err != nil {
|
// cacert, err := os.ReadFile(q.CaCertPath)
|
||||||
return config, fmt.Errorf("failed to read CA cert file: %v", err)
|
// if err != nil {
|
||||||
}
|
// return config, fmt.Errorf("failed to read CA cert file: %v", err)
|
||||||
certPool := x509.NewCertPool()
|
// }
|
||||||
certPool.AppendCertsFromPEM(cacert)
|
// certPool := x509.NewCertPool()
|
||||||
client.Transport = &http.Transport{
|
// certPool.AppendCertsFromPEM(cacert)
|
||||||
TLSClientConfig: &tls.Config{
|
// client.Transport = &http.Transport{
|
||||||
RootCAs: certPool,
|
// TLSClientConfig: &tls.Config{
|
||||||
},
|
// RootCAs: certPool,
|
||||||
}
|
// InsecureSkipVerify: false,
|
||||||
}
|
// },
|
||||||
|
// }
|
||||||
|
// }
|
||||||
return gofish.ClientConfig{
|
return gofish.ClientConfig{
|
||||||
Endpoint: url,
|
Endpoint: url,
|
||||||
Username: q.User,
|
Username: q.User,
|
||||||
|
|
@ -610,7 +557,7 @@ func makeGofishConfig(q *QueryParams) (gofish.ClientConfig, error) {
|
||||||
Insecure: q.CaCertPath == "",
|
Insecure: q.CaCertPath == "",
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue