From 66daa906eb988b73ba1fe8e9032ef6c4913f4d9e Mon Sep 17 00:00:00 2001 From: Shireesh Anjal <355479+anjalshireesh@users.noreply.github.com> Date: Sun, 3 Mar 2024 01:25:14 +0530 Subject: [PATCH] mc support commands: allow --json in online mode (#4859) There was a validation that said `--json` is supported only when `--airgap` is also passed for SUBNET related `mc support` commands. Remove this validation to allow `--json` to be passed even in online mode. In this scenario, print a status JSON after upload to SUBNET. --- cmd/license-info.go | 2 +- cmd/license-register.go | 2 +- cmd/license-unregister.go | 2 +- cmd/subnet-utils.go | 21 +++----------- cmd/support-diag.go | 31 ++++++++++++-------- cmd/support-inspect.go | 37 ++++++++++++++---------- cmd/support-perf.go | 2 +- cmd/support-profile.go | 56 +++++++++++++++++++++++++++++++++---- cmd/support-proxy-remove.go | 2 +- cmd/support-proxy-set.go | 2 +- cmd/support-proxy-show.go | 2 +- cmd/support.go | 9 +++++- 12 files changed, 112 insertions(+), 56 deletions(-) diff --git a/cmd/license-info.go b/cmd/license-info.go index 5c58a2ab..043b30ef 100644 --- a/cmd/license-info.go +++ b/cmd/license-info.go @@ -179,7 +179,7 @@ func mainLicenseInfo(ctx *cli.Context) error { initLicInfoColors() aliasedURL := ctx.Args().Get(0) - alias, _ := initSubnetConnectivity(ctx, aliasedURL, false, true) + alias, _ := initSubnetConnectivity(ctx, aliasedURL, true) apiKey, lic, e := getSubnetCreds(alias) fatalIf(probe.NewError(e), "Error in checking cluster registration status") diff --git a/cmd/license-register.go b/cmd/license-register.go index e7566444..c5e547a5 100644 --- a/cmd/license-register.go +++ b/cmd/license-register.go @@ -220,7 +220,7 @@ func mainLicenseRegister(ctx *cli.Context) error { alias, _ = url2Alias(aliasedURL) accAPIKey = validateAndSaveLic(string(licBytes), alias, true) } else { - alias, accAPIKey = initSubnetConnectivity(ctx, aliasedURL, true, false) + alias, accAPIKey = initSubnetConnectivity(ctx, aliasedURL, false) } clusterName := ctx.String("name") diff --git a/cmd/license-unregister.go b/cmd/license-unregister.go index c316b8c6..6f07eab1 100644 --- a/cmd/license-unregister.go +++ b/cmd/license-unregister.go @@ -83,7 +83,7 @@ func mainLicenseUnregister(ctx *cli.Context) error { checkLicenseUnregisterSyntax(ctx) aliasedURL := ctx.Args().Get(0) - alias, apiKey := initSubnetConnectivity(ctx, aliasedURL, false, true) + alias, apiKey := initSubnetConnectivity(ctx, aliasedURL, true) if len(apiKey) == 0 { // api key not passed as flag. Check that the cluster is registered. apiKey = validateClusterRegistered(alias, true) diff --git a/cmd/subnet-utils.go b/cmd/subnet-utils.go index ee280c0a..a955bee4 100644 --- a/cmd/subnet-utils.go +++ b/cmd/subnet-utils.go @@ -782,9 +782,10 @@ func getAPIKeyFlag(ctx *cli.Context) (string, error) { return apiKey, nil } -func initSubnetConnectivity(ctx *cli.Context, aliasedURL string, forUpload bool, failOnConnErr bool) (string, string) { - e := validateSubnetFlags(ctx, forUpload) - fatalIf(probe.NewError(e), "Invalid flags:") +func initSubnetConnectivity(ctx *cli.Context, aliasedURL string, failOnConnErr bool) (string, string) { + if ctx.Bool("airgap") && len(ctx.String("api-key")) > 0 { + fatal(errDummy().Trace(), "--api-key is not applicable in airgap mode") + } alias, _ := url2Alias(aliasedURL) @@ -805,17 +806,3 @@ func initSubnetConnectivity(ctx *cli.Context, aliasedURL string, forUpload bool, return alias, apiKey } - -func validateSubnetFlags(ctx *cli.Context, forUpload bool) error { - if !globalAirgapped { - if globalJSON && forUpload { - return errors.New("--json is applicable only when --airgap is also passed") - } - return nil - } - - if len(ctx.String("api-key")) > 0 { - return errors.New("--api-key is not applicable in airgap mode") - } - return nil -} diff --git a/cmd/support-diag.go b/cmd/support-diag.go index c4a8813c..47a5c742 100644 --- a/cmd/support-diag.go +++ b/cmd/support-diag.go @@ -24,7 +24,6 @@ import ( "flag" "fmt" "io" - "net/url" "os" "path/filepath" "strings" @@ -38,7 +37,6 @@ import ( "github.com/minio/madmin-go/v3" "github.com/minio/mc/pkg/probe" "github.com/minio/pkg/v2/console" - "github.com/tidwall/gjson" ) const ( @@ -96,6 +94,21 @@ EXAMPLES: `, } +type supportDiagMessage struct { + Status string `json:"status"` +} + +// String colorized status message +func (s supportDiagMessage) String() string { + return console.Colorize(supportSuccessMsgTag, "MinIO diagnostics report was successfully uploaded to SUBNET.") +} + +// JSON jsonified status message +func (s supportDiagMessage) JSON() string { + s.Status = "success" + return toJSON(s) +} + // checkSupportDiagSyntax - validate arguments passed by a user func checkSupportDiagSyntax(ctx *cli.Context) { if len(ctx.Args()) == 0 || len(ctx.Args()) > 1 { @@ -168,7 +181,7 @@ func mainSupportDiag(ctx *cli.Context) error { // Get the alias parameter from cli aliasedURL := ctx.Args().Get(0) - alias, apiKey := initSubnetConnectivity(ctx, aliasedURL, true, true) + alias, apiKey := initSubnetConnectivity(ctx, aliasedURL, true) if len(apiKey) == 0 { // api key not passed as flag. Check that the cluster is registered. apiKey = validateClusterRegistered(alias, true) @@ -186,6 +199,7 @@ func mainSupportDiag(ctx *cli.Context) error { func execSupportDiag(ctx *cli.Context, client *madmin.AdminClient, alias, apiKey string) { var reqURL string var headers map[string]string + setSuccessMessageColor() filename := fmt.Sprintf("%s-health_%s.json.gz", filepath.Clean(alias), UTCNow().Format("20060102150405")) if !globalAirgapped { @@ -198,7 +212,7 @@ func execSupportDiag(ctx *cli.Context, client *madmin.AdminClient, alias, apiKey healthInfo, version, e := fetchServerDiagInfo(ctx, client) fatalIf(probe.NewError(e), "Unable to fetch health information.") - if globalJSON { + if globalJSON && globalAirgapped { switch version { case madmin.HealthInfoVersion0: printMsg(healthInfo.(madmin.HealthInfoV0)) @@ -214,15 +228,10 @@ func execSupportDiag(ctx *cli.Context, client *madmin.AdminClient, alias, apiKey fatalIf(probe.NewError(e), "Unable to save MinIO diagnostics report") if !globalAirgapped { - resp, e := uploadFileToSubnet(alias, filename, reqURL, headers) + _, e := uploadFileToSubnet(alias, filename, reqURL, headers) fatalIf(probe.NewError(e), "Unable to upload MinIO diagnostics report to SUBNET portal") - msg := "MinIO diagnostics report was successfully uploaded to SUBNET." - clusterURL, _ := url.PathUnescape(gjson.Get(resp, "cluster_url").String()) - if len(clusterURL) > 0 { - msg += fmt.Sprintf(" Please click here to view our analysis: %s", clusterURL) - } - console.Infoln(msg) + printMsg(supportDiagMessage{}) } } diff --git a/cmd/support-inspect.go b/cmd/support-inspect.go index 99bc861b..c0d609ae 100644 --- a/cmd/support-inspect.go +++ b/cmd/support-inspect.go @@ -81,27 +81,34 @@ EXAMPLES: } type inspectMessage struct { - File string `json:"file"` - Key string `json:"key,omitempty"` + Status string `json:"status"` + AliasedURL string `json:"aliasedURL,omitempty"` + File string `json:"file,omitempty"` + Key string `json:"key,omitempty"` } // Colorized message for console printing. func (t inspectMessage) String() string { - msg := "" - if t.Key == "" { - msg += fmt.Sprintf("File data successfully downloaded as %s\n", console.Colorize("File", t.File)) - } else { - msg += fmt.Sprintf("Encrypted file data successfully downloaded as %s\n", console.Colorize("File", t.File)) - msg += fmt.Sprintf("Decryption key: %s\n\n", console.Colorize("Key", t.Key)) + var msg string + if globalAirgapped { + if t.Key == "" { + msg = fmt.Sprintf("File data successfully downloaded as %s", console.Colorize("File", t.File)) + } else { + msg = fmt.Sprintf("Encrypted file data successfully downloaded as %s\n", console.Colorize("File", t.File)) + msg += fmt.Sprintf("Decryption key: %s\n\n", console.Colorize("Key", t.Key)) - msg += "The decryption key will ONLY be shown here. It cannot be recovered.\n" - msg += "The encrypted file can safely be shared without the decryption key.\n" - msg += "Even with the decryption key, data stored with encryption cannot be accessed.\n" + msg += "The decryption key will ONLY be shown here. It cannot be recovered.\n" + msg += "The encrypted file can safely be shared without the decryption key.\n" + msg += "Even with the decryption key, data stored with encryption cannot be accessed." + } + } else { + msg = fmt.Sprintf("Object inspection data for '%s' uploaded to SUBNET successfully", t.AliasedURL) } - return msg + return console.Colorize(supportSuccessMsgTag, msg) } func (t inspectMessage) JSON() string { + t.Status = "success" jsonMessageBytes, e := json.MarshalIndent(t, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(jsonMessageBytes) @@ -118,11 +125,13 @@ func mainSupportInspect(ctx *cli.Context) error { // Check for command syntax checkSupportInspectSyntax(ctx) + setSuccessMessageColor() + // Get the alias parameter from cli args := ctx.Args() aliasedURL := args.Get(0) - alias, apiKey := initSubnetConnectivity(ctx, aliasedURL, true, true) + alias, apiKey := initSubnetConnectivity(ctx, aliasedURL, true) if len(apiKey) == 0 { // api key not passed as flag. Check that the cluster is registered. apiKey = validateClusterRegistered(alias, true) @@ -197,7 +206,7 @@ func mainSupportInspect(ctx *cli.Context) error { return nil } - console.Infof("Object inspection data for '%s' uploaded to SUBNET successfully\n", aliasedURL) + printMsg(inspectMessage{AliasedURL: aliasedURL}) return nil } diff --git a/cmd/support-perf.go b/cmd/support-perf.go index d2fca06d..ff870462 100644 --- a/cmd/support-perf.go +++ b/cmd/support-perf.go @@ -461,7 +461,7 @@ func convertPerfResults(results []PerfTestResult) PerfTestOutput { } func execSupportPerf(ctx *cli.Context, aliasedURL, perfType string) { - alias, apiKey := initSubnetConnectivity(ctx, aliasedURL, true, true) + alias, apiKey := initSubnetConnectivity(ctx, aliasedURL, true) if len(apiKey) == 0 { // api key not passed as flag. Check that the cluster is registered. apiKey = validateClusterRegistered(alias, true) diff --git a/cmd/support-profile.go b/cmd/support-profile.go index 1237a857..d8e39fa3 100644 --- a/cmd/support-profile.go +++ b/cmd/support-profile.go @@ -18,6 +18,7 @@ package cmd import ( + "fmt" "io" "os" "strings" @@ -48,6 +49,36 @@ var ( const profileFile = "profile.zip" +type supportProfileMessage struct { + Status string `json:"status"` + File string `json:"file,omitempty"` + Error string `json:"error,omitempty"` +} + +// Colorized message for console printing. +func (s supportProfileMessage) String() string { + var msg string + if s.Error != "" { + errMsg := fmt.Sprintln("Unable to upload profile file to SUBNET: ", s.Error) + msg := console.Colorize(supportErrorMsgTag, errMsg) + infoMsg := fmt.Sprintf("Profiling data saved locally at '%s'", profileFile) + msg += console.Colorize(supportSuccessMsgTag, infoMsg) + return msg + } + + if globalAirgapped { + msg = fmt.Sprintf("Profiling data saved successfully at %s", s.File) + } else { + msg = "Profiling data uploaded to SUBNET successfully" + } + return console.Colorize(supportSuccessMsgTag, msg) +} + +// JSON jsonified proxy remove message +func (s supportProfileMessage) JSON() string { + return toJSON(s) +} + var supportProfileCmd = cli.Command{ Name: "profile", Usage: "upload profile data for debugging", @@ -167,9 +198,12 @@ func mainSupportProfile(ctx *cli.Context) error { // Check for command syntax checkAdminProfileSyntax(ctx) + setSuccessMessageColor() + setErrorMessageColor() + // Get the alias parameter from cli aliasedURL := ctx.Args().Get(0) - alias, apiKey := initSubnetConnectivity(ctx, aliasedURL, true, true) + alias, apiKey := initSubnetConnectivity(ctx, aliasedURL, true) if len(apiKey) == 0 { // api key not passed as flag. Check that the cluster is registered. apiKey = validateClusterRegistered(alias, true) @@ -196,7 +230,9 @@ func execSupportProfile(ctx *cli.Context, client *madmin.AdminClient, alias, api reqURL, headers = prepareSubnetUploadURL(uploadURL, alias, apiKey) } - console.Infof("Profiling '%s' for %d seconds... \n", alias, duration) + if !globalJSON { + console.Infof("Profiling '%s' for %d seconds... \n", alias, duration) + } data, e := client.Profile(globalContext, madmin.ProfilerType(profilers), time.Second*time.Duration(duration)) fatalIf(probe.NewError(e), "Unable to save profile data") @@ -205,12 +241,20 @@ func execSupportProfile(ctx *cli.Context, client *madmin.AdminClient, alias, api if !globalAirgapped { _, e = uploadFileToSubnet(alias, profileFile, reqURL, headers) if e != nil { - errorIf(probe.NewError(e), "Unable to upload profile file to SUBNET") - console.Infof("Profiling data saved locally at '%s'\n", profileFile) + printMsg(supportProfileMessage{ + Status: "error", + Error: e.Error(), + File: profileFile, + }) return } - console.Infoln("Profiling data uploaded to SUBNET successfully") + printMsg(supportProfileMessage{ + Status: "success", + }) } else { - console.Infoln("Profiling data saved successfully at ", profileFile) + printMsg(supportProfileMessage{ + Status: "success", + File: profileFile, + }) } } diff --git a/cmd/support-proxy-remove.go b/cmd/support-proxy-remove.go index 6d4ae961..94a37797 100644 --- a/cmd/support-proxy-remove.go +++ b/cmd/support-proxy-remove.go @@ -44,7 +44,7 @@ var supportProxyRemoveCmd = cli.Command{ Action: mainSupportProxyRemove, OnUsageError: onUsageError, Before: setGlobalsFromContext, - Flags: supportGlobalFlags, + Flags: globalFlags, HideHelpCommand: true, CustomHelpTemplate: `NAME: {{.HelpName}} - {{.Usage}} diff --git a/cmd/support-proxy-set.go b/cmd/support-proxy-set.go index d90cb66e..fc9f1375 100644 --- a/cmd/support-proxy-set.go +++ b/cmd/support-proxy-set.go @@ -47,7 +47,7 @@ var supportProxySetCmd = cli.Command{ Action: mainSupportProxySet, OnUsageError: onUsageError, Before: setGlobalsFromContext, - Flags: supportGlobalFlags, + Flags: globalFlags, HideHelpCommand: true, CustomHelpTemplate: `NAME: {{.HelpName}} - {{.Usage}} diff --git a/cmd/support-proxy-show.go b/cmd/support-proxy-show.go index 87b600df..57d9a364 100644 --- a/cmd/support-proxy-show.go +++ b/cmd/support-proxy-show.go @@ -28,7 +28,7 @@ var supportProxyShowCmd = cli.Command{ Action: mainSupportProxyShow, OnUsageError: onUsageError, Before: setGlobalsFromContext, - Flags: supportGlobalFlags, + Flags: globalFlags, HideHelpCommand: true, CustomHelpTemplate: `NAME: {{.HelpName}} - {{.Usage}} diff --git a/cmd/support.go b/cmd/support.go index 100b4921..9cba3868 100644 --- a/cmd/support.go +++ b/cmd/support.go @@ -29,7 +29,10 @@ import ( "github.com/minio/pkg/v2/console" ) -const supportSuccessMsgTag = "SupportSuccessMessage" +const ( + supportSuccessMsgTag = "SupportSuccessMessage" + supportErrorMsgTag = "SupportErrorMessage" +) var supportGlobalFlags = append(globalFlags, cli.BoolFlag{ @@ -94,6 +97,10 @@ func setSuccessMessageColor() { console.SetColor(supportSuccessMsgTag, color.New(color.FgGreen, color.Bold)) } +func setErrorMessageColor() { + console.SetColor(supportErrorMsgTag, color.New(color.FgYellow, color.Italic)) +} + func featureStatusStr(enabled bool) string { if enabled { return "enabled"