mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 11:37:01 -07:00
feat: initial implementation of command split
This commit is contained in:
parent
522ddb985d
commit
835b678e75
8 changed files with 181 additions and 63 deletions
|
|
@ -74,6 +74,7 @@ var CollectCmd = &cobra.Command{
|
|||
SecretsFile: secretsFile,
|
||||
Username: username,
|
||||
Password: password,
|
||||
Format: format,
|
||||
}
|
||||
|
||||
// show all of the 'collect' parameters being set from CLI if verbose
|
||||
|
|
@ -122,15 +123,17 @@ var CollectCmd = &cobra.Command{
|
|||
|
||||
func init() {
|
||||
currentUser, _ = user.Current()
|
||||
CollectCmd.PersistentFlags().StringVar(&host, "host", "", "Set the URI to the SMD root endpoint")
|
||||
CollectCmd.PersistentFlags().StringVarP(&username, "username", "u", "", "Set the master BMC username")
|
||||
CollectCmd.PersistentFlags().StringVarP(&password, "password", "p", "", "Set the master BMC password")
|
||||
CollectCmd.PersistentFlags().StringVar(&secretsFile, "secrets-file", "", "Set path to the node secrets file")
|
||||
CollectCmd.PersistentFlags().StringVar(&scheme, "scheme", "https", "Set the default scheme used to query when not included in URI")
|
||||
CollectCmd.PersistentFlags().StringVar(&protocol, "protocol", "tcp", "Set the protocol used to query")
|
||||
CollectCmd.PersistentFlags().StringVarP(&outputPath, "output", "o", fmt.Sprintf("/tmp/%smagellan/inventory/", currentUser.Username+"/"), "Set the path to store collection data")
|
||||
CollectCmd.PersistentFlags().BoolVar(&forceUpdate, "force-update", false, "Set flag to force update data sent to SMD")
|
||||
CollectCmd.PersistentFlags().StringVar(&cacertPath, "cacert", "", "Set the path to CA cert file. (defaults to system CAs when blank)")
|
||||
CollectCmd.Flags().StringVar(&host, "host", "", "Set the URI to the SMD root endpoint")
|
||||
CollectCmd.Flags().StringVarP(&username, "username", "u", "", "Set the master BMC username")
|
||||
CollectCmd.Flags().StringVarP(&password, "password", "p", "", "Set the master BMC password")
|
||||
CollectCmd.Flags().StringVar(&secretsFile, "secrets-file", "", "Set path to the node secrets file")
|
||||
CollectCmd.Flags().StringVar(&scheme, "scheme", "https", "Set the default scheme used to query when not included in URI")
|
||||
CollectCmd.Flags().StringVar(&protocol, "protocol", "tcp", "Set the protocol used to query")
|
||||
CollectCmd.Flags().StringVarP(&outputPath, "output", "o", fmt.Sprintf("/tmp/%smagellan/inventory/", currentUser.Username+"/"), "Set the path to store collection data")
|
||||
CollectCmd.Flags().BoolVar(&forceUpdate, "force-update", false, "Set flag to force update data sent to SMD")
|
||||
CollectCmd.Flags().StringVar(&cacertPath, "cacert", "", "Set the path to CA cert file. (defaults to system CAs when blank)")
|
||||
CollectCmd.Flags().StringVarP(&format, "format", "F", "hive", "Set the output format (json|yaml)")
|
||||
CollectCmd.Flags().BoolVar(&useHive, "use-hive", true, "Set the output format")
|
||||
|
||||
// set flags to only be used together
|
||||
CollectCmd.MarkFlagsRequiredTogether("username", "password")
|
||||
|
|
@ -143,6 +146,7 @@ func init() {
|
|||
checkBindFlagError(viper.BindPFlag("collect.force-update", CollectCmd.Flags().Lookup("force-update")))
|
||||
checkBindFlagError(viper.BindPFlag("collect.cacert", CollectCmd.Flags().Lookup("cacert")))
|
||||
checkBindFlagError(viper.BindPFlags(CollectCmd.Flags()))
|
||||
checkBindFlagError(viper.BindPFlag("collect.use-hive", CollectCmd.Flags().Lookup("use-hive")))
|
||||
|
||||
rootCmd.AddCommand(CollectCmd)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,6 +45,7 @@ var (
|
|||
debug bool
|
||||
forceUpdate bool
|
||||
insecure bool
|
||||
useHive bool
|
||||
)
|
||||
|
||||
// The `root` command doesn't do anything on it's own except display
|
||||
|
|
@ -79,7 +80,7 @@ func init() {
|
|||
rootCmd.PersistentFlags().IntVarP(&timeout, "timeout", "t", 5, "Set the timeout for requests")
|
||||
rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "", "Set the config file path")
|
||||
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Set to enable/disable verbose output")
|
||||
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "Set to enable/disable debug messages")
|
||||
rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "Set to enable/disable debug messages")
|
||||
rootCmd.PersistentFlags().StringVar(&accessToken, "access-token", "", "Set the access token")
|
||||
rootCmd.PersistentFlags().StringVar(&cachePath, "cache", fmt.Sprintf("/tmp/%s/magellan/assets.db", currentUser.Username), "Set the scanning result cache path")
|
||||
|
||||
|
|
|
|||
98
cmd/send.go
Normal file
98
cmd/send.go
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
urlx "github.com/OpenCHAMI/magellan/internal/url"
|
||||
"github.com/OpenCHAMI/magellan/pkg/auth"
|
||||
"github.com/OpenCHAMI/magellan/pkg/client"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var sendData []string
|
||||
|
||||
var sendCmd = &cobra.Command{
|
||||
Use: "send [host]",
|
||||
Example: ` // send data from collect output
|
||||
magellan send -d @collected-1.json -d @collected-2.json https://smd.openchami.cluster
|
||||
magellan send -d '{...}' -d @collected-1.json https://api.exampe.com
|
||||
`,
|
||||
Short: "Send collected node information to specified host.",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// concatenate all of the data from `-d` flag to send
|
||||
var (
|
||||
smdClient = &client.SmdClient{Client: &http.Client{}}
|
||||
inputData = []byte(strings.Join(sendData, "\n"))
|
||||
)
|
||||
|
||||
// try to load access token either from env var, file, or config if var not set
|
||||
if accessToken == "" {
|
||||
var err error
|
||||
accessToken, err = auth.LoadAccessToken(tokenPath)
|
||||
if err != nil && verbose {
|
||||
log.Warn().Err(err).Msgf("could not load access token")
|
||||
}
|
||||
}
|
||||
|
||||
// try and load cert if argument is passed
|
||||
if cacertPath != "" {
|
||||
cacert, err := os.ReadFile(cacertPath)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("failed to read cert path")
|
||||
}
|
||||
certPool := x509.NewCertPool()
|
||||
certPool.AppendCertsFromPEM(cacert)
|
||||
// smdClient.WithCertPool(certPool)
|
||||
// client.WithCertPool(smdClient, certPool)
|
||||
}
|
||||
|
||||
// create and set headers for request
|
||||
headers := client.HTTPHeader{}
|
||||
headers.Authorization(accessToken)
|
||||
headers.ContentType("application/json")
|
||||
|
||||
// unmarshal into map
|
||||
data := map[string]any{}
|
||||
err := json.Unmarshal(inputData, &data)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to unmarshal data to make request")
|
||||
}
|
||||
|
||||
for _, host := range args {
|
||||
host, err := urlx.Sanitize(host)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to sanitize host")
|
||||
}
|
||||
|
||||
smdClient.URI = host
|
||||
err = smdClient.Add(inputData, headers)
|
||||
if err != nil {
|
||||
|
||||
// try updating instead
|
||||
if forceUpdate {
|
||||
smdClient.Xname = data["ID"].(string)
|
||||
err = smdClient.Update(inputData, headers)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("failed to forcibly update Redfish endpoint")
|
||||
}
|
||||
} else {
|
||||
log.Error().Err(err).Msgf("failed to add Redfish endpoint")
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
sendCmd.Flags().StringSliceVarP(&sendData, "data", "d", []string{}, "Set the data to send to specified host.")
|
||||
sendCmd.Flags().BoolVarP(&forceUpdate, "force-update", "f", false, "Set flag to force update data sent to SMD.")
|
||||
sendCmd.Flags().StringVar(&cacertPath, "cacert", "", "Set the path to CA cert file. (defaults to system CAs when blank)")
|
||||
|
||||
rootCmd.AddCommand(sendCmd)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue