Add support for storage command and crawler output

Partially addresses issue #3 by adding a simple `magellan list devices` command to list storage devices. To close the issue, this PR still requires including storage device information in the `crawler`'s output.

Reviewed-on: towk/magellan-ng#5
This commit is contained in:
David Allen 2025-05-03 16:58:17 -06:00
parent f02c29917a
commit a6c445b86f
Signed by: towk
GPG key ID: 0430CDBE22619155
20 changed files with 449 additions and 18 deletions

View file

@ -5,7 +5,7 @@ import (
"github.com/cznic/mathutil"
"github.com/davidallendj/magellan/internal/cache/sqlite"
urlx "github.com/davidallendj/magellan/internal/url"
urlx "github.com/davidallendj/magellan/internal/urlx"
magellan "github.com/davidallendj/magellan/pkg"
"github.com/davidallendj/magellan/pkg/auth"
"github.com/davidallendj/magellan/pkg/bmc"

View file

@ -8,7 +8,7 @@ import (
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v3"
urlx "github.com/davidallendj/magellan/internal/url"
urlx "github.com/davidallendj/magellan/internal/urlx"
"github.com/davidallendj/magellan/pkg/bmc"
"github.com/davidallendj/magellan/pkg/crawler"
"github.com/davidallendj/magellan/pkg/secrets"

View file

@ -1,13 +1,16 @@
package cmd
import (
"encoding/json"
"fmt"
"os"
"strings"
"time"
"github.com/davidallendj/magellan/internal/cache/sqlite"
urlx "github.com/davidallendj/magellan/internal/urlx"
magellan "github.com/davidallendj/magellan/pkg"
"github.com/davidallendj/magellan/pkg/crawler"
"github.com/davidallendj/magellan/pkg/secrets"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v3"
@ -34,15 +37,17 @@ var ListCmd = &cobra.Command{
"See the 'scan' command on how to perform a scan.",
Run: func(cmd *cobra.Command, args []string) {
// check if we just want to show cache-related info and exit
if showCache {
fmt.Printf("cache: %s\n", cachePath)
return
if showCacheInfo {
magellan.PrintMapFormat(map[string]any{
"path": cachePath,
}, format)
os.Exit(0)
}
// load the assets found from scan
scannedResults, err := sqlite.GetScannedAssets(cachePath)
if err != nil {
log.Error().Err(err).Msg("failed to get scanned assets")
log.Error().Err(err).Str("path", cachePath).Msg("failed to get scanned assets from cache")
}
switch strings.ToLower(listOutputFormat) {
case FORMAT_JSON:
@ -65,6 +70,25 @@ var ListCmd = &cobra.Command{
log.Error().Msg("unrecognized format")
os.Exit(1)
}
args[0], err = urlx.Sanitize(args[0])
if err != nil {
return fmt.Errorf("failed to sanitize URI: %w", err)
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
store := secrets.NewStaticStore(listUsername, listPassword)
drives, err := magellan.ListDrives(&crawler.CrawlerConfig{
URI: args[0],
CredentialStore: store, //initSecretsStore(args[0]),
Insecure: insecure,
UseDefault: false,
})
if err != nil {
log.Error().Err(err).Msg("failed to get drives")
os.Exit(1)
}
magellan.PrintDrives(drives)
},
}

View file

@ -21,6 +21,8 @@ import (
magellan "github.com/davidallendj/magellan/internal"
"github.com/davidallendj/magellan/internal/util"
"github.com/davidallendj/magellan/pkg/bmc"
"github.com/davidallendj/magellan/pkg/secrets"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -150,3 +152,44 @@ func SetDefaults() {
viper.SetDefault("update.status", false)
}
func initSecretsStore(uri string) secrets.SecretStore {
var (
store secrets.SecretStore
err error
)
if username != "" && password != "" {
// First, try and load credentials from --username and --password if both are set.
log.Debug().Str("id", uri).Msgf("--username and --password specified, using them for BMC credentials")
store = secrets.NewStaticStore(username, password)
} else {
// Alternatively, locate specific credentials (falling back to default) and override those
// with --username or --password if either are passed.
log.Debug().Str("id", uri).Msgf("one or both of --username and --password NOT passed, attempting to obtain missing credentials from secret store at %s", secretsFile)
if store, err = secrets.OpenStore(secretsFile); err != nil {
log.Error().Str("id", uri).Err(err).Msg("failed to open local secrets store")
}
// Either none of the flags were passed or only one of them were; get
// credentials from secrets store to fill in the gaps.
bmcCreds, _ := bmc.GetBMCCredentials(store, uri)
nodeCreds := secrets.StaticStore{
Username: bmcCreds.Username,
Password: bmcCreds.Password,
}
// If either of the flags were passed, override the fetched
// credentials with them.
if username != "" {
log.Info().Str("id", uri).Msg("--username was set, overriding username for this BMC")
nodeCreds.Username = username
}
if password != "" {
log.Info().Str("id", uri).Msg("--password was set, overriding password for this BMC")
nodeCreds.Password = password
}
store = &nodeCreds
}
return store
}

View file

@ -12,7 +12,7 @@ import (
"github.com/rs/zerolog/log"
"github.com/cznic/mathutil"
urlx "github.com/davidallendj/magellan/internal/url"
urlx "github.com/davidallendj/magellan/internal/urlx"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

View file

@ -128,9 +128,7 @@ func init() {
checkBindFlagError(viper.BindPFlag("update.username", UpdateCmd.Flags().Lookup("username")))
checkBindFlagError(viper.BindPFlag("update.password", UpdateCmd.Flags().Lookup("password")))
checkBindFlagError(viper.BindPFlag("update.scheme", UpdateCmd.Flags().Lookup("scheme")))
checkBindFlagError(viper.BindPFlag("update.firmware-url", UpdateCmd.Flags().Lookup("firmware-url")))
checkBindFlagError(viper.BindPFlag("update.firmware-version", UpdateCmd.Flags().Lookup("firmware-version")))
checkBindFlagError(viper.BindPFlag("update.component", UpdateCmd.Flags().Lookup("component")))
checkBindFlagError(viper.BindPFlag("update.firmware-uri", UpdateCmd.Flags().Lookup("firmware-uri")))
checkBindFlagError(viper.BindPFlag("update.status", UpdateCmd.Flags().Lookup("status")))
rootCmd.AddCommand(UpdateCmd)