mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 03:27:03 -07:00
Renamed vars and switched to use zerolog
This commit is contained in:
parent
606a7b47cc
commit
b27e6b6a73
7 changed files with 68 additions and 103 deletions
|
|
@ -6,10 +6,10 @@ import (
|
|||
|
||||
magellan "github.com/OpenCHAMI/magellan/internal"
|
||||
"github.com/OpenCHAMI/magellan/internal/db/sqlite"
|
||||
"github.com/OpenCHAMI/magellan/internal/log"
|
||||
"github.com/OpenCHAMI/magellan/pkg/smd"
|
||||
"github.com/OpenCHAMI/magellan/internal/util"
|
||||
"github.com/OpenCHAMI/magellan/pkg/client"
|
||||
"github.com/cznic/mathutil"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
|
@ -30,36 +30,32 @@ var collectCmd = &cobra.Command{
|
|||
" magellan collect --cache ./assets.db --output ./logs --timeout 30 --cacert cecert.pem\n" +
|
||||
" magellan collect --host smd.example.com --port 27779 --username username --password password",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// make application logger
|
||||
l := log.NewLogger(logrus.New(), logrus.DebugLevel)
|
||||
|
||||
// get probe states stored in db from scan
|
||||
probeStates, err := sqlite.GetProbeResults(cachePath)
|
||||
scannedResults, err := sqlite.GetScannedResults(cachePath)
|
||||
if err != nil {
|
||||
l.Log.Errorf("failed toget states: %v", err)
|
||||
log.Error().Err(err).Msgf("failed to get scanned results from cache")
|
||||
}
|
||||
|
||||
// try to load access token either from env var, file, or config if var not set
|
||||
if accessToken == "" {
|
||||
var err error
|
||||
accessToken, err = LoadAccessToken()
|
||||
accessToken, err = util.LoadAccessToken(tokenPath)
|
||||
if err != nil {
|
||||
l.Log.Errorf("failed to load access token: %v", err)
|
||||
log.Error().Err(err).Msgf("failed to load access token")
|
||||
}
|
||||
}
|
||||
|
||||
if verbose {
|
||||
fmt.Printf("access token: %v\n", accessToken)
|
||||
log.Debug().Str("Access Token", accessToken)
|
||||
}
|
||||
|
||||
//
|
||||
if concurrency <= 0 {
|
||||
concurrency = mathutil.Clamp(len(probeStates), 1, 255)
|
||||
concurrency = mathutil.Clamp(len(scannedResults), 1, 255)
|
||||
}
|
||||
q := &magellan.QueryParams{
|
||||
Username: username,
|
||||
Password: password,
|
||||
Protocol: protocol,
|
||||
Timeout: timeout,
|
||||
Concurrency: concurrency,
|
||||
Verbose: verbose,
|
||||
|
|
@ -68,23 +64,21 @@ var collectCmd = &cobra.Command{
|
|||
ForceUpdate: forceUpdate,
|
||||
AccessToken: accessToken,
|
||||
}
|
||||
err = magellan.CollectAll(&probeStates, l, q)
|
||||
err = magellan.CollectInventory(&scannedResults, q)
|
||||
if err != nil {
|
||||
l.Log.Errorf("failed to collect data: %v", err)
|
||||
log.Error().Err(err).Msgf("failed to collect data")
|
||||
}
|
||||
|
||||
// add necessary headers for final request (like token)
|
||||
headers := make(map[string]string)
|
||||
if q.AccessToken != "" {
|
||||
headers["Authorization"] = "Bearer " + q.AccessToken
|
||||
}
|
||||
header := util.HTTPHeader{}
|
||||
header.Authorization(q.AccessToken)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
currentUser, _ = user.Current()
|
||||
collectCmd.PersistentFlags().StringVar(&smd.Host, "host", smd.Host, "set the host to the SMD API")
|
||||
collectCmd.PersistentFlags().IntVarP(&smd.Port, "port", "p", smd.Port, "set the port to the SMD API")
|
||||
collectCmd.PersistentFlags().StringVar(&client.Host, "host", client.Host, "set the host to the SMD API")
|
||||
collectCmd.PersistentFlags().IntVarP(&client.Port, "port", "p", client.Port, "set the port to the SMD API")
|
||||
collectCmd.PersistentFlags().StringVar(&username, "username", "", "set the BMC user")
|
||||
collectCmd.PersistentFlags().StringVar(&password, "password", "", "set the BMC password")
|
||||
collectCmd.PersistentFlags().StringVar(&protocol, "protocol", "https", "set the protocol used to query")
|
||||
|
|
|
|||
|
|
@ -19,8 +19,9 @@ var crawlCmd = &cobra.Command{
|
|||
Short: "Crawl a single BMC for inventory information",
|
||||
Long: "Crawl a single BMC for inventory information\n" +
|
||||
"\n" +
|
||||
"Example:\n" +
|
||||
" magellan crawl https://bmc.example.com",
|
||||
"Examples:\n" +
|
||||
" magellan crawl https://bmc.example.com\n" +
|
||||
" magellan crawl https://bmc.example.com -i -u username -p password",
|
||||
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 {
|
||||
|
|
|
|||
10
cmd/list.go
10
cmd/list.go
|
|
@ -7,6 +7,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/OpenCHAMI/magellan/internal/db/sqlite"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
|
|
@ -24,16 +25,19 @@ var listCmd = &cobra.Command{
|
|||
" magellan list\n" +
|
||||
" magellan list --cache ./assets.db",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
probeResults, err := sqlite.GetProbeResults(cachePath)
|
||||
scannedResults, err := sqlite.GetScannedResults(cachePath)
|
||||
if err != nil {
|
||||
logrus.Errorf("failed toget probe results: %v\n", err)
|
||||
}
|
||||
format = strings.ToLower(format)
|
||||
if format == "json" {
|
||||
b, _ := json.Marshal(probeResults)
|
||||
b, err := json.Marshal(scannedResults)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("failed to unmarshal scanned results")
|
||||
}
|
||||
fmt.Printf("%s\n", string(b))
|
||||
} else {
|
||||
for _, r := range probeResults {
|
||||
for _, r := range scannedResults {
|
||||
fmt.Printf("%s:%d (%s) @ %s\n", r.Host, r.Port, r.Protocol, r.Timestamp.Format(time.UnixDate))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
27
cmd/login.go
27
cmd/login.go
|
|
@ -7,9 +7,9 @@ import (
|
|||
"os"
|
||||
|
||||
magellan "github.com/OpenCHAMI/magellan/internal"
|
||||
"github.com/OpenCHAMI/magellan/internal/log"
|
||||
"github.com/OpenCHAMI/magellan/internal/util"
|
||||
"github.com/lestrrat-go/jwx/jwt"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
|
@ -27,47 +27,50 @@ var loginCmd = &cobra.Command{
|
|||
Short: "Log in with identity provider for access token",
|
||||
Long: "",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// make application logger
|
||||
l := log.NewLogger(logrus.New(), logrus.DebugLevel)
|
||||
|
||||
// check if we have a valid JWT before starting login
|
||||
if !forceLogin {
|
||||
// try getting the access token from env var
|
||||
testToken, err := LoadAccessToken()
|
||||
testToken, err := util.LoadAccessToken(tokenPath)
|
||||
if err != nil {
|
||||
l.Log.Errorf("failed to load access token: %v", err)
|
||||
log.Error().Err(err).Msgf("failed to load access token")
|
||||
}
|
||||
|
||||
// parse into jwt.Token to validate
|
||||
token, err := jwt.Parse([]byte(testToken))
|
||||
if err != nil {
|
||||
fmt.Printf("failed to parse access token contents: %v\n", err)
|
||||
log.Error().Err(err).Msgf("failed to parse access token contents")
|
||||
return
|
||||
}
|
||||
// check if the token is invalid and we need a new one
|
||||
err = jwt.Validate(token)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to validate access token...fetching a new one")
|
||||
log.Error().Err(err).Msgf("failed to validate access token...fetching a new one")
|
||||
} else {
|
||||
fmt.Printf("found a valid token...skipping login (use the '-f/--force' flag to login anyway)")
|
||||
log.Printf("found a valid token...skipping login (use the '-f/--force' flag to login anyway)")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if verbose {
|
||||
log.Printf("Listening for token on %s:%d", targetHost, targetPort)
|
||||
}
|
||||
|
||||
// start the login flow
|
||||
var err error
|
||||
accessToken, err = magellan.Login(loginUrl, targetHost, targetPort)
|
||||
if errors.Is(err, http.ErrServerClosed) {
|
||||
if verbose {
|
||||
fmt.Printf("\n=========================================\nServer closed.\n=========================================\n\n")
|
||||
}
|
||||
} else if err != nil {
|
||||
fmt.Printf("failed to start server: %v\n", err)
|
||||
log.Error().Err(err).Msgf("failed to start server")
|
||||
}
|
||||
|
||||
// if we got a new token successfully, save it to the token path
|
||||
if accessToken != "" && tokenPath != "" {
|
||||
err := os.WriteFile(tokenPath, []byte(accessToken), os.ModePerm)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to write access token to file: %v\n", err)
|
||||
log.Error().Err(err).Msgf("failed to write access token to file")
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
|||
35
cmd/root.go
35
cmd/root.go
|
|
@ -21,7 +21,7 @@ import (
|
|||
"os/user"
|
||||
|
||||
magellan "github.com/OpenCHAMI/magellan/internal"
|
||||
"github.com/OpenCHAMI/magellan/pkg/smd"
|
||||
"github.com/OpenCHAMI/magellan/pkg/client"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
|
@ -66,35 +66,6 @@ func Execute() {
|
|||
}
|
||||
}
|
||||
|
||||
// LoadAccessToken() tries to load a JWT string from an environment
|
||||
// variable, file, or config in that order. If loading the token
|
||||
// fails with one options, it will fallback to the next option until
|
||||
// all options are exhausted.
|
||||
//
|
||||
// Returns a token as a string with no error if successful.
|
||||
// Alternatively, returns an empty string with an error if a token is
|
||||
// not able to be loaded.
|
||||
func LoadAccessToken() (string, error) {
|
||||
// try to load token from env var
|
||||
testToken := os.Getenv("ACCESS_TOKEN")
|
||||
if testToken != "" {
|
||||
return testToken, nil
|
||||
}
|
||||
|
||||
// try reading access token from a file
|
||||
b, err := os.ReadFile(tokenPath)
|
||||
if err == nil {
|
||||
return string(b), nil
|
||||
}
|
||||
|
||||
// TODO: try to load token from config
|
||||
testToken = viper.GetString("access_token")
|
||||
if testToken != "" {
|
||||
return testToken, nil
|
||||
}
|
||||
return "", fmt.Errorf("failed toload token from environment variable, file, or config")
|
||||
}
|
||||
|
||||
func init() {
|
||||
currentUser, _ = user.Current()
|
||||
cobra.OnInitialize(InitializeConfig)
|
||||
|
|
@ -140,8 +111,8 @@ func SetDefaults() {
|
|||
viper.SetDefault("scan.subnet-masks", []net.IP{})
|
||||
viper.SetDefault("scan.disable-probing", false)
|
||||
viper.SetDefault("collect.driver", []string{"redfish"})
|
||||
viper.SetDefault("collect.host", smd.Host)
|
||||
viper.SetDefault("collect.port", smd.Port)
|
||||
viper.SetDefault("collect.host", client.Host)
|
||||
viper.SetDefault("collect.port", client.Port)
|
||||
viper.SetDefault("collect.user", "")
|
||||
viper.SetDefault("collect.pass", "")
|
||||
viper.SetDefault("collect.protocol", "https")
|
||||
|
|
|
|||
|
|
@ -18,8 +18,6 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
begin uint8
|
||||
end uint8
|
||||
subnets []string
|
||||
subnetMasks []net.IP
|
||||
disableProbing bool
|
||||
|
|
@ -38,7 +36,7 @@ var scanCmd = &cobra.Command{
|
|||
"If the '--disable-probe` flag is used, the tool will not send another request to probe for available " +
|
||||
"Redfish services.\n\n" +
|
||||
"Example:\n" +
|
||||
" magellan scan --subnet 172.16.0.0/24 --add-host 10.0.0.101\n" +
|
||||
" magellan scan --subnet 172.16.0.0/24 --host 10.0.0.101\n" +
|
||||
" magellan scan --subnet 172.16.0.0 --subnet-mask 255.255.255.0 --cache ./assets.db",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
var (
|
||||
|
|
|
|||
|
|
@ -2,8 +2,7 @@ package cmd
|
|||
|
||||
import (
|
||||
magellan "github.com/OpenCHAMI/magellan/internal"
|
||||
"github.com/OpenCHAMI/magellan/internal/log"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
|
@ -26,17 +25,16 @@ var updateCmd = &cobra.Command{
|
|||
Short: "Update BMC node firmware",
|
||||
Long: "Perform an firmware update using Redfish by providing a remote firmware URL and component.\n" +
|
||||
"Examples:\n" +
|
||||
" magellan update --host 172.16.0.108 --port 443 --username bmc_username --password bmc_password --firmware-url http://172.16.0.200:8005/firmware/bios/image.RBU --component BIOS\n" +
|
||||
" magellan update --status --host 172.16.0.108 --port 443 --username bmc_username --password bmc_password",
|
||||
" magellan update --bmc.host 172.16.0.108 --bmc.port 443 --username bmc_username --password bmc_password --firmware-url http://172.16.0.200:8005/firmware/bios/image.RBU --component BIOS\n" +
|
||||
" magellan update --status --bmc.host 172.16.0.108 --bmc.port 443 --username bmc_username --password bmc_password",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
l := log.NewLogger(logrus.New(), logrus.DebugLevel)
|
||||
// set up update parameters
|
||||
q := &magellan.UpdateParams{
|
||||
FirmwarePath: firmwareUrl,
|
||||
FirmwareVersion: firmwareVersion,
|
||||
Component: component,
|
||||
TransferProtocol: transferProtocol,
|
||||
QueryParams: magellan.QueryParams{
|
||||
Protocol: protocol,
|
||||
Host: host,
|
||||
Username: username,
|
||||
Password: password,
|
||||
|
|
@ -47,53 +45,49 @@ var updateCmd = &cobra.Command{
|
|||
|
||||
// check if required params are set
|
||||
if host == "" || username == "" || password == "" {
|
||||
l.Log.Fatal("requires host, user, and pass to be set")
|
||||
log.Error().Msg("requires host, user, and pass to be set")
|
||||
}
|
||||
|
||||
// get status if flag is set and exit
|
||||
if status {
|
||||
err := magellan.GetUpdateStatus(q)
|
||||
if err != nil {
|
||||
l.Log.Errorf("failed toget update status: %v", err)
|
||||
log.Error().Err(err).Msgf("failed to get update status")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// client, err := magellan.NewClient(l, &q.QueryParams)
|
||||
// if err != nil {
|
||||
// l.Log.Errorf("failed tomake client: %v", err)
|
||||
// }
|
||||
// err = magellan.UpdateFirmware(client, l, q)
|
||||
// initiate a remote update
|
||||
err := magellan.UpdateFirmwareRemote(q)
|
||||
if err != nil {
|
||||
l.Log.Errorf("failed toupdate firmware: %v", err)
|
||||
log.Error().Err(err).Msgf("failed to update firmware")
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
updateCmd.Flags().StringVar(&host, "bmc-host", "", "set the BMC host")
|
||||
updateCmd.Flags().IntVar(&port, "bmc-port", 443, "set the BMC port")
|
||||
updateCmd.Flags().StringVar(&host, "bmc.host", "", "set the BMC host")
|
||||
updateCmd.Flags().IntVar(&port, "bmc.port", 443, "set the BMC port")
|
||||
updateCmd.Flags().StringVar(&username, "username", "", "set the BMC user")
|
||||
updateCmd.Flags().StringVar(&password, "password", "", "set the BMC password")
|
||||
updateCmd.Flags().StringVar(&transferProtocol, "transfer-protocol", "HTTP", "set the transfer protocol")
|
||||
updateCmd.Flags().StringVar(&protocol, "protocol", "https", "set the Redfish protocol")
|
||||
updateCmd.Flags().StringVar(&firmwareUrl, "firmware-url", "", "set the path to the firmware")
|
||||
updateCmd.Flags().StringVar(&firmwareVersion, "firmware-version", "", "set the version of firmware to be installed")
|
||||
updateCmd.Flags().StringVar(&firmwareUrl, "firmware.url", "", "set the path to the firmware")
|
||||
updateCmd.Flags().StringVar(&firmwareVersion, "firmware.version", "", "set the version of firmware to be installed")
|
||||
updateCmd.Flags().StringVar(&component, "component", "", "set the component to upgrade")
|
||||
updateCmd.Flags().BoolVar(&status, "status", false, "get the status of the update")
|
||||
|
||||
viper.BindPFlag("host", updateCmd.Flags().Lookup("host"))
|
||||
viper.BindPFlag("port", updateCmd.Flags().Lookup("port"))
|
||||
viper.BindPFlag("username", updateCmd.Flags().Lookup("username"))
|
||||
viper.BindPFlag("password", updateCmd.Flags().Lookup("password"))
|
||||
viper.BindPFlag("transfer-protocol", updateCmd.Flags().Lookup("transfer-protocol"))
|
||||
viper.BindPFlag("protocol", updateCmd.Flags().Lookup("protocol"))
|
||||
viper.BindPFlag("firmware.url", updateCmd.Flags().Lookup("firmware.url"))
|
||||
viper.BindPFlag("firmware.version", updateCmd.Flags().Lookup("firmware.version"))
|
||||
viper.BindPFlag("component", updateCmd.Flags().Lookup("component"))
|
||||
viper.BindPFlag("secure-tls", updateCmd.Flags().Lookup("secure-tls"))
|
||||
viper.BindPFlag("status", updateCmd.Flags().Lookup("status"))
|
||||
viper.BindPFlag("update.bmc.host", updateCmd.Flags().Lookup("bmc.host"))
|
||||
viper.BindPFlag("update.bmc.port", updateCmd.Flags().Lookup("bmc.port"))
|
||||
viper.BindPFlag("update.username", updateCmd.Flags().Lookup("username"))
|
||||
viper.BindPFlag("update.password", updateCmd.Flags().Lookup("password"))
|
||||
viper.BindPFlag("update.transfer-protocol", updateCmd.Flags().Lookup("transfer-protocol"))
|
||||
viper.BindPFlag("update.protocol", updateCmd.Flags().Lookup("protocol"))
|
||||
viper.BindPFlag("update.firmware.url", updateCmd.Flags().Lookup("firmware.url"))
|
||||
viper.BindPFlag("update.firmware.version", updateCmd.Flags().Lookup("firmware.version"))
|
||||
viper.BindPFlag("update.component", updateCmd.Flags().Lookup("component"))
|
||||
viper.BindPFlag("update.secure-tls", updateCmd.Flags().Lookup("secure-tls"))
|
||||
viper.BindPFlag("update.status", updateCmd.Flags().Lookup("status"))
|
||||
|
||||
rootCmd.AddCommand(updateCmd)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue