Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure mc support callhome command #4372

Merged
merged 4 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions cmd/auto-complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,22 +448,19 @@ var completeCmds = map[string]complete.Predictor{
"/alias/remove": aliasCompleter,
"/alias/import": nil,

"/support/callhome": aliasCompleter,
"/support/logs/enable": aliasCompleter,
"/support/logs/disable": aliasCompleter,
"/support/logs/status": aliasCompleter,
"/support/logs/show": aliasCompleter,
"/support/register": aliasCompleter,
"/support/diag": aliasCompleter,
"/support/profile": aliasCompleter,
"/support/inspect": aliasCompleter,
"/support/perf": aliasCompleter,
"/support/metrics": aliasCompleter,
"/support/status": aliasCompleter,
"/support/top/locks": aliasCompleter,
"/support/top/api": aliasCompleter,
"/support/top/drive": aliasCompleter,
"/support/top/disk": aliasCompleter,
"/support/callhome": aliasCompleter,
"/support/logs": aliasCompleter,
"/support/register": aliasCompleter,
"/support/diag": aliasCompleter,
"/support/profile": aliasCompleter,
"/support/inspect": aliasCompleter,
"/support/perf": aliasCompleter,
"/support/metrics": aliasCompleter,
"/support/status": aliasCompleter,
"/support/top/locks": aliasCompleter,
"/support/top/api": aliasCompleter,
"/support/top/drive": aliasCompleter,
"/support/top/disk": aliasCompleter,

"/license/register": aliasCompleter,
"/license/info": aliasCompleter,
Expand Down
131 changes: 110 additions & 21 deletions cmd/support-callhome.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,42 @@
package cmd

import (
"strings"

"github.com/minio/cli"
json "github.com/minio/colorjson"
"github.com/minio/madmin-go"
"github.com/minio/mc/pkg/probe"
"github.com/minio/pkg/console"
)

var supportCallhomeFlags = append([]cli.Flag{
cli.BoolFlag{
Name: "logs",
Usage: "push logs to SUBNET in real-time",
},
cli.BoolFlag{
Name: "diag",
Usage: "push diagnostics info to SUBNET every 24hrs",
},
}, supportGlobalFlags...)

var supportCallhomeCmd = cli.Command{
Name: "callhome",
Usage: "configure callhome settings",
OnUsageError: onUsageError,
Action: mainCallhome,
Before: setGlobalsFromContext,
Flags: supportGlobalFlags,
Flags: supportCallhomeFlags,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}

USAGE:
{{.HelpName}} enable|disable|status ALIAS

OPTIONS:
enable - Enable pushing callhome info to SUBNET every 24hrs
disable - Disable pushing callhome info to SUBNET
enable - Enable callhome
disable - Disable callhome
status - Display callhome settings

FLAGS:
Expand All @@ -55,21 +68,45 @@ EXAMPLES:

3. Check callhome status for cluster with alias 'myminio'
{{.Prompt}} {{.HelpName}} status myminio

4. Enable diagnostics callhome for cluster with alias 'myminio'
{{.Prompt}} {{.HelpName}} enable myminio --diag

5. Disable logs callhome for cluster with alias 'myminio'
{{.Prompt}} {{.HelpName}} disable myminio --logs

6. Check logs callhome status for cluster with alias 'myminio'
{{.Prompt}} {{.HelpName}} status myminio --logs
`,
}

type supportCallhomeMessage struct {
Status string `json:"status"`
Callhome string `json:"callhome"`
MsgPfx string `json:"-"`
Status string `json:"status"`
Diag string `json:"diag,omitempty"`
Logs string `json:"logs,omitempty"`
Feature string `json:"-"`
Action string `json:"-"`
}

// String colorized service status message.
// String colorized callhome command output message.
func (s supportCallhomeMessage) String() string {
return console.Colorize(featureToggleMessageTag, s.MsgPfx+s.Callhome)
var msg string
if s.Action == "status" {
msgs := []string{}
if len(s.Diag) > 0 {
msgs = append(msgs, "Diagnostics is "+s.Diag)
}
if len(s.Logs) > 0 {
msgs = append(msgs, "Logs is "+s.Logs)
}
msg = strings.Join(msgs, "\n")
} else {
msg = s.Feature + " is now " + s.Action
}
return console.Colorize(featureToggleMessageTag, msg)
}

// JSON jsonified service status message.
// JSON jsonified callhome command output message.
func (s supportCallhomeMessage) JSON() string {
s.Status = "success"
jsonBytes, e := json.MarshalIndent(s, "", " ")
Expand All @@ -78,28 +115,85 @@ func (s supportCallhomeMessage) JSON() string {
return string(jsonBytes)
}

func isSupportCallhomeEnabled(alias string) bool {
func isDiagCallhomeEnabled(alias string) bool {
return isFeatureEnabled(alias, "callhome", madmin.Default)
}

func mainCallhome(ctx *cli.Context) error {
setToggleMessageColor()
alias, arg := checkToggleCmdSyntax(ctx, "callhome")

diag, logs := parseCallhomeFlags(ctx)

if arg == "status" {
printMsg(supportCallhomeMessage{
Callhome: featureStatusStr(isSupportCallhomeEnabled(alias)),
})
printCallhomeStatus(alias, diag, logs)
return nil
}

enable := arg == "enable"
toggleCallhome(alias, arg == "enable", diag, logs)

return nil
}

func parseCallhomeFlags(ctx *cli.Context) (diag bool, logs bool) {
diag = ctx.Bool("diag")
logs = ctx.Bool("logs")

if !diag && !logs {
// When both flags are not passed, apply the action to both
diag = true
logs = true
}

return diag, logs
}

func printCallhomeStatus(alias string, diag bool, logs bool) {
resultMsg := supportCallhomeMessage{Action: "status"}
if diag {
resultMsg.Diag = featureStatusStr(isDiagCallhomeEnabled(alias))
}

if logs {
resultMsg.Logs = featureStatusStr(isLogsCallhomeEnabled(alias))
}
printMsg(resultMsg)
}

func toggleCallhome(alias string, enable bool, diag bool, logs bool) {
newStatus := featureStatusStr(enable)
resultMsg := supportCallhomeMessage{
Action: newStatus,
Feature: getFeature(diag, logs),
}

if enable {
validateClusterRegistered(alias, true)
}
setCallhomeConfig(alias, enable)

return nil
if diag {
setCallhomeConfig(alias, enable)
resultMsg.Diag = newStatus
}

if logs {
configureSubnetWebhook(alias, enable)
resultMsg.Logs = newStatus
}

printMsg(resultMsg)
}

func getFeature(diag bool, logs bool) string {
if diag && logs {
return "Diagnostics and logs callhome"
}

if diag {
return "Diagnostics"
}

return "Logs"
}

func setCallhomeConfig(alias string, enableCallhome bool) {
Expand All @@ -118,9 +212,4 @@ func setCallhomeConfig(alias string, enableCallhome bool) {
configStr := "callhome enable=" + enableStr
_, e := client.SetConfigKV(globalContext, configStr)
fatalIf(probe.NewError(e), "Unable to set callhome config on minio")

printMsg(supportCallhomeMessage{
Callhome: featureStatusStr(enableCallhome),
MsgPfx: "Callhome is now ",
})
}
49 changes: 0 additions & 49 deletions cmd/support-logs-disable.go

This file was deleted.

50 changes: 0 additions & 50 deletions cmd/support-logs-enable.go

This file was deleted.

25 changes: 0 additions & 25 deletions cmd/support-logs-show.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,6 @@ var logsShowFlags = []cli.Flag{
},
}

var supportLogsShowCmd = cli.Command{
Name: "show",
Usage: "show MinIO logs",
Action: mainLogsShowConsole,
OnUsageError: onUsageError,
Before: setGlobalsFromContext,
Flags: append(logsShowFlags, globalFlags...),
HideHelpCommand: true,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} [FLAGS] TARGET [NODENAME]
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Show logs for a MinIO server with alias 'myminio'
{{.Prompt}} {{.HelpName}} myminio
2. Show last 5 log entries for node 'node1' for a MinIO server with alias 'myminio'
{{.Prompt}} {{.HelpName}} --last 5 myminio node1
3. Show application errors in logs for a MinIO server with alias 'myminio'
{{.Prompt}} {{.HelpName}} --type application myminio
`,
}

func checkLogsShowSyntax(ctx *cli.Context) {
if len(ctx.Args()) == 0 || len(ctx.Args()) > 3 {
showCommandHelpAndExit(ctx, 1) // last argument is exit code
Expand Down
Loading