From b504550d98f911e54668f2c35805d9a2a16f85ed Mon Sep 17 00:00:00 2001 From: "David J. Allen" Date: Tue, 19 Sep 2023 13:55:46 -0600 Subject: [PATCH] Refactored, renamed, and reorganized code --- README.md | 3 +- cmd/root.go | 3 +- internal/api/dora/dora.go | 4 +-- internal/api/smd/smd.go | 8 ++--- internal/api/util.go | 27 ---------------- internal/{ => log}/logger.go | 2 +- internal/util/util.go | 60 ++++++++++++++++++++++++++++++++++++ 7 files changed, 71 insertions(+), 36 deletions(-) delete mode 100644 internal/api/util.go rename internal/{ => log}/logger.go (94%) create mode 100644 internal/util/util.go diff --git a/README.md b/README.md index e892d48..0e45df0 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ List of things left to fix, do, or ideas... * [ ] Switch to internal scanner if `dora` fails * [ ] Set default port automatically depending on the driver used to scan * [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 * [ ] Clean up, remove unused, and tidy code diff --git a/cmd/root.go b/cmd/root.go index bb9ea50..3fa5217 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -20,6 +20,7 @@ var ( drivers []string preferredDriver string ipmitoolPath string + outputPath string verbose bool ) @@ -51,5 +52,5 @@ func init() { rootCmd.PersistentFlags().IntVar(&threads, "threads", -1, "set the number of threads") rootCmd.PersistentFlags().IntVar(&timeout, "timeout", 30, "set the timeout") 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") } diff --git a/internal/api/dora/dora.go b/internal/api/dora/dora.go index 0e87a92..e1a27f9 100644 --- a/internal/api/dora/dora.go +++ b/internal/api/dora/dora.go @@ -4,7 +4,7 @@ import ( "encoding/json" "fmt" - "github.com/bikeshack/magellan/internal/api" + "github.com/bikeshack/magellan/internal/util" "github.com/jmoiron/sqlx" ) @@ -43,7 +43,7 @@ func ScanForAssets() error { func QueryScannedPorts() error { // Perform scan and collect from dora server url := makeEndpointUrl("/scanned_ports") - _, body, err := api.MakeRequest(url, "GET", nil, nil) + _, body, err := util.MakeRequest(url, "GET", nil, nil) if err != nil { return fmt.Errorf("could not discover assets: %v", err) } diff --git a/internal/api/smd/smd.go b/internal/api/smd/smd.go index 726df8d..5e8e305 100644 --- a/internal/api/smd/smd.go +++ b/internal/api/smd/smd.go @@ -6,7 +6,7 @@ package smd import ( "fmt" - "github.com/bikeshack/magellan/internal/api" + "github.com/bikeshack/magellan/internal/util" // hms "github.com/alexlovelltroy/hms-smd" ) @@ -22,7 +22,7 @@ func makeEndpointUrl(endpoint string) string { func GetRedfishEndpoints() error { url := makeEndpointUrl("/Inventory/RedfishEndpoints") - _, body, err := api.MakeRequest(url, "GET", nil, nil) + _, body, err := util.MakeRequest(url, "GET", nil, nil) if err != nil { return fmt.Errorf("could not get endpoint: %v", err) } @@ -33,7 +33,7 @@ func GetRedfishEndpoints() error { func GetComponentEndpoint(xname string) error { 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 { return fmt.Errorf("could not get endpoint: %v", err) } @@ -51,7 +51,7 @@ func AddRedfishEndpoint(data []byte, headers map[string]string) error { // _ = ep // Add redfish endpoint via POST `/hsm/v2/Inventory/RedfishEndpoints` endpoint 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("res: ", res) fmt.Println("body: ", string(body)) diff --git a/internal/api/util.go b/internal/api/util.go deleted file mode 100644 index ef121c3..0000000 --- a/internal/api/util.go +++ /dev/null @@ -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 -} diff --git a/internal/logger.go b/internal/log/logger.go similarity index 94% rename from internal/logger.go rename to internal/log/logger.go index 31f68e1..43e2db2 100644 --- a/internal/logger.go +++ b/internal/log/logger.go @@ -1,4 +1,4 @@ -package magellan +package log import ( "github.com/sirupsen/logrus" diff --git a/internal/util/util.go b/internal/util/util.go new file mode 100644 index 0000000..8164e68 --- /dev/null +++ b/internal/util/util.go @@ -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 +} \ No newline at end of file