mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 11:37:01 -07:00
chore: made changes to build and fix issues
This commit is contained in:
parent
9f80d746c7
commit
1410e53849
6 changed files with 91 additions and 44 deletions
11
cmd/list.go
11
cmd/list.go
|
|
@ -1,6 +1,7 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
|
@ -19,7 +20,10 @@ import (
|
|||
|
||||
var (
|
||||
showCache bool
|
||||
showCacheInfo bool
|
||||
listOutputFormat string
|
||||
listUsername string
|
||||
listPassword string
|
||||
)
|
||||
|
||||
// The `list` command provides an easy way to show what was found
|
||||
|
|
@ -40,7 +44,7 @@ var ListCmd = &cobra.Command{
|
|||
if showCacheInfo {
|
||||
magellan.PrintMapWithFormat(map[string]any{
|
||||
"path": cachePath,
|
||||
}, format)
|
||||
}, listOutputFormat)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
|
|
@ -69,9 +73,10 @@ var ListCmd = &cobra.Command{
|
|||
default:
|
||||
log.Error().Msg("unrecognized format")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// print cache data in specified format
|
||||
magellan.PrintRemoteAssets(scannedResults, format)
|
||||
magellan.PrintRemoteAssets(scannedResults, listOutputFormat)
|
||||
},
|
||||
}
|
||||
|
||||
|
|
@ -113,7 +118,7 @@ var listDrivesCmd = &cobra.Command{
|
|||
}
|
||||
|
||||
func init() {
|
||||
ListCmd.Flags().StringVarP(&format, "format", "F", "none", "Set the output format (list|json|yaml)")
|
||||
ListCmd.Flags().StringVarP(&listOutputFormat, "format", "F", "none", "Set the output format (list|json|yaml)")
|
||||
ListCmd.Flags().BoolVar(&showCacheInfo, "cache-info", false, "Alias for 'magellan cache info'")
|
||||
|
||||
listDrivesCmd.Flags().StringVarP(&listUsername, "username", "u", "", "Set the username for BMC login")
|
||||
|
|
|
|||
36
cmd/root.go
36
cmd/root.go
|
|
@ -37,23 +37,24 @@ const (
|
|||
|
||||
// CLI arguments as variables to not fiddle with error-prone strings
|
||||
var (
|
||||
currentUser *user.User
|
||||
accessToken string
|
||||
timeout int
|
||||
concurrency int
|
||||
ports []int
|
||||
protocol string
|
||||
cacertPath string
|
||||
username string
|
||||
password string
|
||||
cachePath string
|
||||
outputPath string
|
||||
outputDir string
|
||||
configPath string
|
||||
verbose bool
|
||||
debug bool
|
||||
forceUpdate bool
|
||||
insecure bool
|
||||
currentUser *user.User
|
||||
accessToken string
|
||||
accessTokenPath string
|
||||
timeout int
|
||||
concurrency int
|
||||
ports []int
|
||||
protocol string
|
||||
cacertPath string
|
||||
username string
|
||||
password string
|
||||
cachePath string
|
||||
outputPath string
|
||||
outputDir string
|
||||
configPath string
|
||||
verbose bool
|
||||
debug bool
|
||||
forceUpdate bool
|
||||
insecure bool
|
||||
)
|
||||
|
||||
// The `root` command doesn't do anything on it's own except display
|
||||
|
|
@ -137,7 +138,6 @@ func SetDefaults() {
|
|||
viper.SetDefault("scan.subnet-masks", []net.IP{})
|
||||
viper.SetDefault("scan.disable-probing", false)
|
||||
viper.SetDefault("scan.disable-cache", false)
|
||||
viper.SetDefault("collect.host", host)
|
||||
viper.SetDefault("collect.username", "")
|
||||
viper.SetDefault("collect.password", "")
|
||||
viper.SetDefault("collect.protocol", "tcp")
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import (
|
|||
"os"
|
||||
"strings"
|
||||
|
||||
urlx "github.com/davidallendj/magellan/internal/url"
|
||||
urlx "github.com/davidallendj/magellan/internal/urlx"
|
||||
"github.com/davidallendj/magellan/pkg/auth"
|
||||
"github.com/davidallendj/magellan/pkg/client"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
|
@ -40,7 +40,7 @@ var sendCmd = &cobra.Command{
|
|||
// 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)
|
||||
accessToken, err = auth.LoadAccessToken(accessTokenPath)
|
||||
if err != nil && verbose {
|
||||
log.Warn().Err(err).Msgf("could not load access token")
|
||||
} else if debug && accessToken != "" {
|
||||
|
|
|
|||
|
|
@ -15,10 +15,11 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
sessionID string
|
||||
sessionToken string
|
||||
sessionTokenPath string
|
||||
storeToken bool
|
||||
sessionID string
|
||||
sessionToken string
|
||||
sessionTokenPath string
|
||||
sessionOutputFormat string
|
||||
storeToken bool
|
||||
)
|
||||
|
||||
var sessionCmd = &cobra.Command{
|
||||
|
|
@ -128,7 +129,7 @@ var sessionLoginCmd = &cobra.Command{
|
|||
}
|
||||
|
||||
// print the session IDs for created sessions
|
||||
switch format {
|
||||
switch sessionOutputFormat {
|
||||
case "list":
|
||||
for _, session := range newSessions {
|
||||
fmt.Println(session.ID)
|
||||
|
|
@ -138,7 +139,7 @@ var sessionLoginCmd = &cobra.Command{
|
|||
case "yaml":
|
||||
util.PrintYAML(newSessions)
|
||||
default:
|
||||
log.Error().Msg("unrecognized output format")
|
||||
log.Error().Msg("unrecognized output sessionOutputFormat")
|
||||
os.Exit(1)
|
||||
}
|
||||
},
|
||||
|
|
@ -225,7 +226,7 @@ var sessionLogoutCmd = &cobra.Command{
|
|||
|
||||
var sessionStatusCmd = &cobra.Command{
|
||||
Use: "status",
|
||||
Example: ` // show the host's session service status in YAML format
|
||||
Example: ` // show the host's session service status in YAML sessionOutputFormat
|
||||
magellan sessions status https://172.21.0.2:5000 -u $bmc_username -p $bmc_password -i -F yaml`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Short: "Show the status of the session service",
|
||||
|
|
@ -239,8 +240,8 @@ var sessionStatusCmd = &cobra.Command{
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
// show the service status in given format
|
||||
switch format {
|
||||
// show the service status in given sessionOutputFormat
|
||||
switch sessionOutputFormat {
|
||||
case FORMAT_LIST:
|
||||
fmt.Printf("%s: %v (%d secs)", host, status.Enabled, status.Timeout)
|
||||
case FORMAT_JSON:
|
||||
|
|
@ -274,7 +275,7 @@ var sessionListCmd = &cobra.Command{
|
|||
continue
|
||||
}
|
||||
|
||||
switch format {
|
||||
switch sessionOutputFormat {
|
||||
case FORMAT_LIST:
|
||||
fallthrough
|
||||
case FORMAT_JSON:
|
||||
|
|
@ -282,7 +283,7 @@ var sessionListCmd = &cobra.Command{
|
|||
case FORMAT_YAML:
|
||||
util.PrintYAML(session)
|
||||
default:
|
||||
log.Error().Msg("unrecognized output format")
|
||||
log.Error().Msg("unrecognized output sessionOutputFormat")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
|
|
@ -302,7 +303,7 @@ var sessionListCmd = &cobra.Command{
|
|||
continue
|
||||
}
|
||||
|
||||
switch format {
|
||||
switch sessionOutputFormat {
|
||||
case FORMAT_LIST:
|
||||
fmt.Println(strings.Join(sessionIDs, "\n"))
|
||||
case FORMAT_JSON:
|
||||
|
|
@ -310,7 +311,7 @@ var sessionListCmd = &cobra.Command{
|
|||
case FORMAT_YAML:
|
||||
util.PrintYAML(sessionIDs)
|
||||
default:
|
||||
log.Error().Msg("unrecognized output format")
|
||||
log.Error().Msg("unrecognized output sessionOutputFormat")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
|
@ -338,7 +339,7 @@ func init() {
|
|||
sessionCmd.PersistentFlags().StringVarP(&password, "password", "p", "", "Set the password for BMC login")
|
||||
sessionCmd.PersistentFlags().StringVar(&sessionTokenPath, "session-token-file", "", "Set the session token from a file")
|
||||
sessionCmd.PersistentFlags().StringVar(&sessionToken, "session-token", "", "Set the session token")
|
||||
sessionCmd.PersistentFlags().StringVarP(&format, "format", "F", "list", "Set the output format (list|json|yaml)")
|
||||
sessionCmd.PersistentFlags().StringVarP(&sessionOutputFormat, "sessionOutputFormat", "F", "list", "Set the output sessionOutputFormat (list|json|yaml)")
|
||||
sessionCmd.PersistentFlags().StringVarP(&secretsFile, "secrets-file", "f", "secrets.json", "Set the path to secrets store file to store credentials")
|
||||
|
||||
sessionCmd.MarkFlagRequired("username")
|
||||
|
|
|
|||
47
internal/util/bmc.go
Normal file
47
internal/util/bmc.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"github.com/davidallendj/magellan/pkg/bmc"
|
||||
"github.com/davidallendj/magellan/pkg/secrets"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func GetBMCCredentials(store secrets.SecretStore, id string) bmc.BMCCredentials {
|
||||
var (
|
||||
creds bmc.BMCCredentials
|
||||
err error
|
||||
)
|
||||
|
||||
if id == "" {
|
||||
log.Error().Msg("failed to get BMC credentials: id was empty")
|
||||
return creds
|
||||
}
|
||||
|
||||
if id == secrets.DEFAULT_KEY {
|
||||
log.Info().Msg("fetching default credentials")
|
||||
if creds, err = bmc.GetBMCCredentialsDefault(store); err != nil {
|
||||
log.Warn().Err(err).Msg("failed to get default credentials")
|
||||
} else {
|
||||
log.Info().Msg("default credentials found, using")
|
||||
}
|
||||
return creds
|
||||
}
|
||||
|
||||
if creds, err = bmc.GetBMCCredentials(store, id); err != nil {
|
||||
// Specific credentials for URI not found, fetch default.
|
||||
log.Warn().Str("id", id).Msg("specific credentials not found, falling back to default")
|
||||
if defaultSecret, err := bmc.GetBMCCredentialsDefault(store); err != nil {
|
||||
// We've exhausted all options, the credentials will be blank unless
|
||||
// overridden by a CLI flag.
|
||||
log.Warn().Str("id", id).Err(err).Msg("no default credentials were set, they will be blank unless overridden by CLI flags")
|
||||
} else {
|
||||
// Default credentials found, use them.
|
||||
log.Info().Str("id", id).Msg("default credentials found, using")
|
||||
creds = defaultSecret
|
||||
}
|
||||
} else {
|
||||
log.Info().Str("id", id).Msg("specific credentials found, using")
|
||||
}
|
||||
|
||||
return creds
|
||||
}
|
||||
|
|
@ -51,7 +51,7 @@ type CollectParams struct {
|
|||
//
|
||||
// Requests can be made to several of the nodes using a goroutine by setting the q.Concurrency
|
||||
// property value between 1 and 10000.
|
||||
func CollectInventory(assets *[]RemoteAsset, params *CollectParams, store secrets.SecretStore) ([]map[string]any, error) {
|
||||
func CollectInventory(assets *[]RemoteAsset, params *CollectParams) ([]map[string]any, error) {
|
||||
// check for available remote assets found from scan
|
||||
if assets == nil {
|
||||
return nil, fmt.Errorf("no assets found")
|
||||
|
|
@ -134,12 +134,6 @@ func CollectInventory(assets *[]RemoteAsset, params *CollectParams, store secret
|
|||
log.Warn().Str("id", config.URI).Msg("username will be blank")
|
||||
}
|
||||
|
||||
// get BMC username to send
|
||||
bmcCreds, err := bmc.GetBMCCredentials(params.SecretStore, config.URI)
|
||||
if err != nil {
|
||||
log.Error().Str("id", config.URI).Msg("username will be blank")
|
||||
}
|
||||
|
||||
// data to be sent to smd
|
||||
data := map[string]any{
|
||||
"ID": fmt.Sprintf("%v", node.String()[:len(node.String())-2]),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue