Split the collect Command For Customization (#93)

* feat: initial implementation of command split

* feat: update collect and new send cmd

* chore: cleanup unused code

* chore: refactored getting username

* chore: more refactoring and cleanup

* feat: update send cmd implementation

* chore: changed/updated example config

* chore: made cmd more consistent and added formatting

* refactor: removed --host flag from scan

* chore: cleaned up and fixed issue with client

* chore: cleaned up CLI flags in collect cmd

* feat: updated crawl to include managers and output YAML optionally

* refactor: updated and improved send implementation

* refactor: minor improvements

* refactor: added util func to check for empty slices

* fix: issue with reading from stdin

* refactor: added scheme trimming function for URIs

* refactor: changed host arg back to positional

* refactor: removed unused vars and added --output-dir flag

* fix: make -f for secrets persistent

* refactor: removed --host flag and request in collect

* refactor: changed --output flag to --output-file

* fix: updated flags for collect

* fix: typo in crawler error

* fix: dir being created when outputDir not set

* fix: reading stdin and data args

* fix: made output using -v and -o consistent

* readme: added info about command split

* updated changelog adding missing version entries

* chore: updated example to use host as positional arg

* fix: issue with reading --data arg

* fix: remove unused import from collect pkg

Signed-off-by: Devon Bautista <devonb@lanl.gov>

---------

Signed-off-by: David Allen <16520934+davidallendj@users.noreply.github.com>
Signed-off-by: Devon Bautista <devonb@lanl.gov>
Co-authored-by: Devon Bautista <devonb@lanl.gov>
This commit is contained in:
David Allen 2025-05-29 13:15:46 -06:00 committed by GitHub
parent fba4a89a0e
commit 04e1fb26c9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 736 additions and 223 deletions

View file

@ -3,8 +3,10 @@ package cmd
import (
"encoding/json"
"fmt"
"os"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v3"
urlx "github.com/OpenCHAMI/magellan/internal/url"
"github.com/OpenCHAMI/magellan/pkg/bmc"
@ -14,6 +16,8 @@ import (
"github.com/spf13/viper"
)
var crawlOutputFormat string
// The `crawl` command walks a collection of Redfish endpoints to collect
// specfic inventory detail. This command only expects host names and does
// not require a scan to be performed beforehand.
@ -37,9 +41,10 @@ var CrawlCmd = &cobra.Command{
},
Run: func(cmd *cobra.Command, args []string) {
var (
uri = args[0]
store secrets.SecretStore
err error
uri = args[0]
store secrets.SecretStore
output []byte
err error
)
if username != "" && password != "" {
@ -76,24 +81,53 @@ var CrawlCmd = &cobra.Command{
store = &nodeCreds
}
systems, err := crawler.CrawlBMCForSystems(crawler.CrawlerConfig{
URI: uri,
CredentialStore: store,
Insecure: insecure,
UseDefault: true,
})
var (
systems []crawler.InventoryDetail
managers []crawler.Manager
config = crawler.CrawlerConfig{
URI: uri,
CredentialStore: store,
Insecure: insecure,
UseDefault: true,
}
)
systems, err = crawler.CrawlBMCForSystems(config)
if err != nil {
log.Error().Err(err).Msg("failed to crawl BMC")
log.Error().Err(err).Msg("failed to crawl BMC for systems")
}
// Marshal the inventory details to JSON
jsonData, err := json.MarshalIndent(systems, "", " ")
managers, err = crawler.CrawlBMCForManagers(config)
if err != nil {
log.Error().Err(err).Msg("failed to marshal JSON")
return
log.Error().Err(err).Msg("failed to crawl BMC for managers")
}
// Print the pretty JSON
fmt.Println(string(jsonData))
data := map[string]any{
"Systems": systems,
"Managers": managers,
}
switch crawlOutputFormat {
case FORMAT_JSON:
// Marshal the inventory details to JSON
output, err = json.MarshalIndent(data, "", " ")
if err != nil {
log.Error().Err(err).Msg("failed to marshal JSON")
return
}
case FORMAT_YAML:
// Marshal the inventory details to JSON
output, err = yaml.Marshal(data)
if err != nil {
log.Error().Err(err).Msg("failed to marshal JSON")
return
}
default:
log.Error().Str("hint", "Try setting --format/-F to 'json' or 'yaml'").Msg("unrecognized format")
os.Exit(1)
}
// Print the pretty JSON or YAML
fmt.Println(string(output))
},
}
@ -101,8 +135,11 @@ func init() {
CrawlCmd.Flags().StringVarP(&username, "username", "u", "", "Set the username for the BMC")
CrawlCmd.Flags().StringVarP(&password, "password", "p", "", "Set the password for the BMC")
CrawlCmd.Flags().BoolVarP(&insecure, "insecure", "i", false, "Ignore SSL errors")
CrawlCmd.Flags().StringVarP(&secretsFile, "file", "f", "nodes.json", "set the secrets file with BMC credentials")
CrawlCmd.Flags().StringVarP(&secretsFile, "secrets-file", "f", "secrets.json", "Set path to the node secrets file")
CrawlCmd.Flags().StringVarP(&crawlOutputFormat, "format", "F", FORMAT_JSON, "Set the output format (json|yaml)")
checkBindFlagError(viper.BindPFlag("crawl.insecure", CrawlCmd.Flags().Lookup("insecure")))
checkBindFlagError(viper.BindPFlag("crawl.insecure", CrawlCmd.Flags().Lookup("insecure")))
checkBindFlagError(viper.BindPFlag("crawl.insecure", CrawlCmd.Flags().Lookup("insecure")))
rootCmd.AddCommand(CrawlCmd)