Refactored, renamed, and reorganized code

This commit is contained in:
David J. Allen 2023-09-19 13:55:46 -06:00
parent 589cc12962
commit b504550d98
7 changed files with 71 additions and 36 deletions

View file

@ -83,6 +83,7 @@ List of things left to fix, do, or ideas...
* [ ] Switch to internal scanner if `dora` fails * [ ] Switch to internal scanner if `dora` fails
* [ ] Set default port automatically depending on the driver used to scan * [ ] Set default port automatically depending on the driver used to scan
* [X] Test using different `bmclib` supported drivers (mainly 'redfish') * [X] Test using different `bmclib` supported drivers (mainly 'redfish')
* [ ] Confirm loading different components into `hms-smd` * [X] Confirm loading different components into `hms-smd`
* [ ] Add ability to set subnet mask for scanning
* [ ] Add unit tests for `scan`, `list`, and `collect` commands * [ ] Add unit tests for `scan`, `list`, and `collect` commands
* [ ] Clean up, remove unused, and tidy code * [ ] Clean up, remove unused, and tidy code

View file

@ -20,6 +20,7 @@ var (
drivers []string drivers []string
preferredDriver string preferredDriver string
ipmitoolPath string ipmitoolPath string
outputPath string
verbose bool verbose bool
) )
@ -51,5 +52,5 @@ 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().BoolVarP(&verbose, "verbose", "v", true, "set verbose flag") rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", true, "set verbose flag")
rootCmd.PersistentFlags().StringVar(&dbpath, "db.path", "/tmp/magellan.db", "set the probe storage path") rootCmd.PersistentFlags().StringVar(&dbpath, "db.path", "/tmp/magellan/magellan.db", "set the probe storage path")
} }

View file

@ -4,7 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/bikeshack/magellan/internal/api" "github.com/bikeshack/magellan/internal/util"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
) )
@ -43,7 +43,7 @@ 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 := api.MakeRequest(url, "GET", nil, nil) _, body, err := util.MakeRequest(url, "GET", nil, nil)
if err != nil { if err != nil {
return fmt.Errorf("could not discover assets: %v", err) return fmt.Errorf("could not discover assets: %v", err)
} }

View file

@ -6,7 +6,7 @@ package smd
import ( import (
"fmt" "fmt"
"github.com/bikeshack/magellan/internal/api" "github.com/bikeshack/magellan/internal/util"
// hms "github.com/alexlovelltroy/hms-smd" // hms "github.com/alexlovelltroy/hms-smd"
) )
@ -22,7 +22,7 @@ func makeEndpointUrl(endpoint string) string {
func GetRedfishEndpoints() error { func GetRedfishEndpoints() error {
url := makeEndpointUrl("/Inventory/RedfishEndpoints") url := makeEndpointUrl("/Inventory/RedfishEndpoints")
_, body, err := api.MakeRequest(url, "GET", nil, nil) _, body, err := util.MakeRequest(url, "GET", nil, nil)
if err != nil { if err != nil {
return fmt.Errorf("could not get endpoint: %v", err) return fmt.Errorf("could not get endpoint: %v", err)
} }
@ -33,7 +33,7 @@ func GetRedfishEndpoints() error {
func GetComponentEndpoint(xname string) error { func GetComponentEndpoint(xname string) error {
url := makeEndpointUrl("/Inventory/ComponentsEndpoints/" + xname) url := makeEndpointUrl("/Inventory/ComponentsEndpoints/" + xname)
res, body, err := api.MakeRequest(url, "GET", nil, nil) res, body, err := util.MakeRequest(url, "GET", nil, nil)
if err != nil { if err != nil {
return fmt.Errorf("could not get endpoint: %v", err) return fmt.Errorf("could not get endpoint: %v", err)
} }
@ -51,7 +51,7 @@ func AddRedfishEndpoint(data []byte, headers map[string]string) error {
// _ = ep // _ = ep
// 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, _ := api.MakeRequest(url, "POST", data, headers) res, body, _ := util.MakeRequest(url, "POST", data, headers)
fmt.Println("smd url: ", url) fmt.Println("smd url: ", url)
fmt.Println("res: ", res) fmt.Println("res: ", res)
fmt.Println("body: ", string(body)) fmt.Println("body: ", string(body))

View file

@ -1,27 +0,0 @@
package api
import (
"bytes"
"fmt"
"io"
"net/http"
)
func MakeRequest(url string, httpMethod string, body []byte, headers map[string]string) (*http.Response, []byte, error) {
// url := getSmdEndpointUrl(endpoint)
req, _ := http.NewRequest(httpMethod, url, bytes.NewBuffer(body))
req.Header.Add("User-Agent", "magellan")
for k, v := range headers {
req.Header.Add(k, v)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("could not make request: %v", err)
}
b, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return nil, nil, fmt.Errorf("could not read response body: %v", err)
}
return res, b, err
}

View file

@ -1,4 +1,4 @@
package magellan package log
import ( import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"

60
internal/util/util.go Normal file
View file

@ -0,0 +1,60 @@
package util
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"time"
)
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil { return true, nil }
if os.IsNotExist(err) { return false, nil }
return false, err
}
func MakeRequest(url string, httpMethod string, body []byte, headers map[string]string) (*http.Response, []byte, error) {
// url := getSmdEndpointUrl(endpoint)
req, _ := http.NewRequest(httpMethod, url, bytes.NewBuffer(body))
req.Header.Add("User-Agent", "magellan")
for k, v := range headers {
req.Header.Add(k, v)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("could not make request: %v", err)
}
b, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return nil, nil, fmt.Errorf("could not read response body: %v", err)
}
return res, b, err
}
func MakeOutputDirectory(path string) (string, error) {
// get the current data + time using Go's stupid formatting
t := time.Now()
dirname := t.Format("2006-01-01 15:04:05")
final := path + "/" + dirname
// check if path is valid and directory
pathExists, err := PathExists(final);
if err != nil {
return final, fmt.Errorf("could not check for existing path: %v", err)
}
if pathExists {
// make sure it is directory with 0o644 permissions
return final, fmt.Errorf("found existing path: %v", final)
}
// create directory with data + time
err = os.MkdirAll(final, 0766)
if err != nil {
return final, fmt.Errorf("could not make directory: %v", err)
}
return final, nil
}