From 3386690f171a2388ff25c71f725674f370828b46 Mon Sep 17 00:00:00 2001 From: Alex Lovell-Troy Date: Mon, 1 Jul 2024 10:59:42 -0400 Subject: [PATCH 1/5] Add uri positional argument and remove uri flag --- cmd/crawl.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/cmd/crawl.go b/cmd/crawl.go index 4707999..4d086ec 100644 --- a/cmd/crawl.go +++ b/cmd/crawl.go @@ -3,17 +3,29 @@ package cmd import ( "encoding/json" "fmt" + "net/url" "github.com/OpenCHAMI/magellan/pkg/crawler" "github.com/spf13/cobra" ) var crawlCmd = &cobra.Command{ - Use: "crawl", + Use: "crawl [uri]", Short: "Crawl a single BMC for inventory information", + Args: func(cmd *cobra.Command, args []string) error { + // Validate that the only argument is a valid URI + if err := cobra.ExactArgs(1)(cmd, args); err != nil { + return err + } + _, err := url.ParseRequestURI(args[0]) + if err != nil { + return fmt.Errorf("invalid URI specified: %s", args[0]) + } + return nil + }, Run: func(cmd *cobra.Command, args []string) { systems, err := crawler.CrawlBMC(crawler.CrawlerConfig{ - URI: cmd.Flag("uri").Value.String(), + URI: args[0], Username: cmd.Flag("username").Value.String(), Password: cmd.Flag("password").Value.String(), Insecure: cmd.Flag("insecure").Value.String() == "true", @@ -34,8 +46,7 @@ var crawlCmd = &cobra.Command{ } func init() { - crawlCmd.Flags().StringP("uri", "u", "", "URI of the BMC") - crawlCmd.Flags().StringP("username", "n", "", "Username for the BMC") + crawlCmd.Flags().StringP("username", "u", "", "Username for the BMC") crawlCmd.Flags().StringP("password", "p", "", "Password for the BMC") crawlCmd.Flags().BoolP("insecure", "i", false, "Ignore SSL errors") From fdc574f5f290edbb7ee1b27cfd8f359d3aaa5795 Mon Sep 17 00:00:00 2001 From: Alex Lovell-Troy Date: Mon, 1 Jul 2024 13:53:32 -0400 Subject: [PATCH 2/5] Improved Error handling without panics --- cmd/crawl.go | 3 ++- pkg/crawler/main.go | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/cmd/crawl.go b/cmd/crawl.go index 4d086ec..f61ae4d 100644 --- a/cmd/crawl.go +++ b/cmd/crawl.go @@ -3,6 +3,7 @@ package cmd import ( "encoding/json" "fmt" + "log" "net/url" "github.com/OpenCHAMI/magellan/pkg/crawler" @@ -31,7 +32,7 @@ var crawlCmd = &cobra.Command{ Insecure: cmd.Flag("insecure").Value.String() == "true", }) if err != nil { - panic(err) + log.Fatalf("Error crawling BMC: %v", err) } // Marshal the inventory details to JSON jsonData, err := json.MarshalIndent(systems, "", " ") diff --git a/pkg/crawler/main.go b/pkg/crawler/main.go index d2d5b64..d7869ed 100644 --- a/pkg/crawler/main.go +++ b/pkg/crawler/main.go @@ -2,6 +2,7 @@ package crawler import ( "fmt" + "strings" "github.com/rs/zerolog/log" "github.com/stmcginnis/gofish" @@ -56,6 +57,12 @@ func CrawlBMC(config CrawlerConfig) ([]InventoryDetail, error) { BasicAuth: true, }) if err != nil { + if strings.HasPrefix(err.Error(), "404:") { + err = fmt.Errorf("no ServiceRoot found. This is probably not a BMC: %s", config.URI) + } + if strings.HasPrefix(err.Error(), "401:") { + err = fmt.Errorf("authentication failed. Check your username and password: %s", config.URI) + } event := log.Error() event.Err(err) event.Msg("failed to connect to BMC") From eccd9adb41bd853c2c348b9e365e599c28af8122 Mon Sep 17 00:00:00 2001 From: Alex Lovell-Troy Date: Tue, 2 Jul 2024 16:25:28 -0400 Subject: [PATCH 3/5] Improve crawl instruction and log Redfish Version --- cmd/crawl.go | 2 +- pkg/crawler/main.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/crawl.go b/cmd/crawl.go index f61ae4d..87f093b 100644 --- a/cmd/crawl.go +++ b/cmd/crawl.go @@ -11,7 +11,7 @@ import ( ) var crawlCmd = &cobra.Command{ - Use: "crawl [uri]", + Use: "crawl [uri] e.g. magellan crawl https://bmc.example.com", Short: "Crawl a single BMC for inventory information", Args: func(cmd *cobra.Command, args []string) error { // Validate that the only argument is a valid URI diff --git a/pkg/crawler/main.go b/pkg/crawler/main.go index d7869ed..00ee07f 100644 --- a/pkg/crawler/main.go +++ b/pkg/crawler/main.go @@ -72,6 +72,7 @@ func CrawlBMC(config CrawlerConfig) ([]InventoryDetail, error) { // Obtain the ServiceRoot rf_service := client.GetService() + log.Info().Msgf("found ServiceRoot %s. Redfish Version %s", rf_service.ID, rf_service.RedfishVersion) var rf_systems []*redfish.ComputerSystem From afe84f6129348b493323d9fec429ad6c6c50584d Mon Sep 17 00:00:00 2001 From: Alex Lovell-Troy Date: Tue, 2 Jul 2024 16:37:39 -0400 Subject: [PATCH 4/5] chore: Clean up URI handling in crawl command --- cmd/crawl.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cmd/crawl.go b/cmd/crawl.go index 87f093b..84fb24e 100644 --- a/cmd/crawl.go +++ b/cmd/crawl.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "net/url" + "strings" "github.com/OpenCHAMI/magellan/pkg/crawler" "github.com/spf13/cobra" @@ -18,10 +19,16 @@ var crawlCmd = &cobra.Command{ if err := cobra.ExactArgs(1)(cmd, args); err != nil { return err } - _, err := url.ParseRequestURI(args[0]) + parsedURI, err := url.ParseRequestURI(args[0]) if err != nil { return fmt.Errorf("invalid URI specified: %s", args[0]) } + // Remove any trailing slashes + parsedURI.Path = strings.TrimSuffix(parsedURI.Path, "/") + // Collapse any doubled slashes + parsedURI.Path = strings.ReplaceAll(parsedURI.Path, "//", "/") + // Update the URI in the args slice + args[0] = parsedURI.String() return nil }, Run: func(cmd *cobra.Command, args []string) { From 09eb48a2413f14a5750dcd9d49e6ea3078eb0781 Mon Sep 17 00:00:00 2001 From: Alex Lovell-Troy Date: Tue, 2 Jul 2024 16:55:17 -0400 Subject: [PATCH 5/5] Update crawl command to improve URI handling and provide clearer instructions --- cmd/crawl.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cmd/crawl.go b/cmd/crawl.go index 84fb24e..cd573ed 100644 --- a/cmd/crawl.go +++ b/cmd/crawl.go @@ -12,7 +12,11 @@ import ( ) var crawlCmd = &cobra.Command{ - Use: "crawl [uri] e.g. magellan crawl https://bmc.example.com", + Use: "crawl [uri]", + Long: "Crawl a single BMC for inventory information\n" + + "\n" + + "Example:\n" + + " magellan crawl https://bmc.example.com", Short: "Crawl a single BMC for inventory information", Args: func(cmd *cobra.Command, args []string) error { // Validate that the only argument is a valid URI