mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 03:27:03 -07:00
Changed the wording of error messages slightly
This commit is contained in:
parent
cc7bad8b94
commit
0403b2bcbc
11 changed files with 31 additions and 32 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
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
|
|
@ -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() {
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,6 @@ var scanCmd = &cobra.Command{
|
||||||
}
|
}
|
||||||
probeStates := magellan.ScanForAssets(hostsToScan, portsToScan, threads, timeout, disableProbing)
|
probeStates := magellan.ScanForAssets(hostsToScan, portsToScan, threads, timeout, disableProbing)
|
||||||
if verbose {
|
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)
|
||||||
}
|
}
|
||||||
|
|
@ -68,7 +67,7 @@ var scanCmd = &cobra.Command{
|
||||||
// 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)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -45,13 +45,13 @@ func QueryScannedPorts() error {
|
||||||
url := makeEndpointUrl("/scanned_ports")
|
url := makeEndpointUrl("/scanned_ports")
|
||||||
_, body, err := util.MakeRequest(nil, 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"]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,7 @@ func (c *Client) GetRedfishEndpoints(headers map[string]string, opts ...Option)
|
||||||
url := makeEndpointUrl("/Inventory/RedfishEndpoints")
|
url := makeEndpointUrl("/Inventory/RedfishEndpoints")
|
||||||
_, body, err := c.MakeRequest(url, "GET", nil, headers)
|
_, 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))
|
||||||
|
|
@ -90,7 +90,7 @@ func (c *Client) GetComponentEndpoint(xname string) error {
|
||||||
url := makeEndpointUrl("/Inventory/ComponentsEndpoints/" + xname)
|
url := makeEndpointUrl("/Inventory/ComponentsEndpoints/" + xname)
|
||||||
res, body, err := c.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))
|
||||||
|
|
@ -99,7 +99,7 @@ func (c *Client) GetComponentEndpoint(xname string) error {
|
||||||
|
|
||||||
func (c *Client) 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
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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,7 +137,7 @@ 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(nil, url, "POST", data, headers)
|
res, body, err := util.MakeRequest(nil, url, "POST", data, headers)
|
||||||
if err != nil {
|
if err != 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 {
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ func MakeRequest(client *http.Client, url string, httpMethod string, body []byte
|
||||||
}
|
}
|
||||||
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 tocreate 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 {
|
||||||
|
|
@ -57,12 +57,12 @@ func MakeRequest(client *http.Client, url string, httpMethod string, body []byte
|
||||||
}
|
}
|
||||||
res, err := client.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 tomake 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 toread response body: %v", err)
|
||||||
}
|
}
|
||||||
return res, b, err
|
return res, b, err
|
||||||
}
|
}
|
||||||
|
|
@ -76,7 +76,7 @@ func MakeOutputDirectory(path string) (string, error) {
|
||||||
// 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 tocheck 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
|
||||||
|
|
@ -86,7 +86,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 tomake directory: %v", err)
|
||||||
}
|
}
|
||||||
return final, nil
|
return final, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue