Add commands to enable/disable callhome and logs (#4107)

- Remove the get/set options under `callhome`
- Move logs settings to a separate command (`mc support logs`)
- Provide toggle options (`enable, disable, status`) for both the
commands

In case of logs (`mc support logs enable|disable|status`),

`enable` configures minio to push logs to SUBNET in real-time by
configuring the webhook `logger_webhook:subnet`
`disable` disables the webhook
`status` displays whether it is enabled or disabled

In the case of call home (`mc support callhome enable|disable|status`),

`enable` configures minio to automatically push callhome info to SUBNET
every 24hrs by setting the config `callhome enable=on`
`disable` disables callhome by setting `enable=off`
`status` displays whether it is enabled or disabled
This commit is contained in:
Shireesh Anjal
2022-06-15 09:14:52 -07:00
committed by GitHub
parent 127d84854d
commit 80ae287b02
7 changed files with 325 additions and 340 deletions
+8 -8
View File
@@ -407,14 +407,14 @@ var completeCmds = map[string]complete.Predictor{
"/alias/remove": aliasCompleter,
"/alias/import": nil,
"/support/callhome/set": aliasCompleter,
"/support/callhome/get": aliasCompleter,
"/support/register": aliasCompleter,
"/support/diag": aliasCompleter,
"/support/profile": aliasCompleter,
"/support/inspect": aliasCompleter,
"/support/perf": aliasCompleter,
"/support/metrics": 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,
"/update": nil,
}
+10 -10
View File
@@ -218,12 +218,12 @@ func getSubSysKeyFromMinIOConfig(client *madmin.AdminClient, subSys string) (mad
return tgt.KVS, nil
}
func getSubnetKeyFromMinIOConfig(alias string, key string) (string, bool) {
func getKeyFromMinIOConfig(alias string, subSys string, key string) (string, bool) {
client, err := newAdminClient(alias)
fatalIf(err, "Unable to initialize admin connection.")
if minioConfigSupportsSubnet(client) {
kvs, e := getSubSysKeyFromMinIOConfig(client, "subnet")
if minioConfigSupportsSubSys(client, subSys) {
kvs, e := getSubSysKeyFromMinIOConfig(client, subSys)
fatalIf(probe.NewError(e), "Unable to get server config for subnet")
return kvs.Lookup(key)
}
@@ -233,7 +233,7 @@ func getSubnetKeyFromMinIOConfig(alias string, key string) (string, bool) {
func getSubnetAPIKeyFromConfig(alias string) string {
// get the subnet api_key config from MinIO if available
apiKey, supported := getSubnetKeyFromMinIOConfig(alias, "api_key")
apiKey, supported := getKeyFromMinIOConfig(alias, "subnet", "api_key")
if supported {
return apiKey
}
@@ -249,7 +249,7 @@ func setSubnetProxyFromConfig(alias string) error {
}
// get the subnet proxy config from MinIO if available
proxy, supported := getSubnetKeyFromMinIOConfig(alias, "proxy")
proxy, supported := getKeyFromMinIOConfig(alias, "subnet", "proxy")
if supported && len(proxy) > 0 {
proxyURL, e := url.Parse(proxy)
if e != nil {
@@ -262,7 +262,7 @@ func setSubnetProxyFromConfig(alias string) error {
func getSubnetLicenseFromConfig(alias string) string {
// get the subnet license config from MinIO if available
lic, supported := getSubnetKeyFromMinIOConfig(alias, "license")
lic, supported := getKeyFromMinIOConfig(alias, "subnet", "license")
if supported {
return lic
}
@@ -278,12 +278,12 @@ func mcConfig() *configV10 {
return config
}
func minioConfigSupportsSubnet(client *madmin.AdminClient) bool {
func minioConfigSupportsSubSys(client *madmin.AdminClient, subSys string) bool {
help, e := client.HelpConfigKV(globalContext, "", "", false)
fatalIf(probe.NewError(e), "Unable to get minio config keys")
for _, h := range help.KeysHelp {
if h.Key == "subnet" {
if h.Key == subSys {
return true
}
}
@@ -292,7 +292,7 @@ func minioConfigSupportsSubnet(client *madmin.AdminClient) bool {
}
func setSubnetAPIKeyConfig(alias string, apiKey string) {
_, supported := getSubnetKeyFromMinIOConfig(alias, "api_key")
_, supported := getKeyFromMinIOConfig(alias, "subnet", "api_key")
if supported {
// Create a new MinIO Admin Client
client, err := newAdminClient(alias)
@@ -300,7 +300,7 @@ func setSubnetAPIKeyConfig(alias string, apiKey string) {
configStr := "subnet license= api_key=" + apiKey
_, e := client.SetConfigKV(globalContext, configStr)
fatalIf(probe.NewError(e), "Unable to set SUBNET API key config on minio")
fatalIf(probe.NewError(e), "Unable to set SUBNET API key config on MinIO")
return
}
mcCfg := mcConfig()
-153
View File
@@ -1,153 +0,0 @@
// Copyright (c) 2015-2022 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"fmt"
"strings"
"github.com/minio/cli"
json "github.com/minio/colorjson"
"github.com/minio/mc/pkg/probe"
)
var callhomeGetCmd = cli.Command{
Name: "get",
Usage: "retrieve callhome settings",
OnUsageError: onUsageError,
Action: mainCallhomeGet,
Before: setGlobalsFromContext,
Flags: globalFlags,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} TARGET option
OPTIONS:
logs - push MinIO server logs to SUBNET
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Get all callhome settings for cluster with alias 'play'
{{.Prompt}} {{.HelpName}} play
2. Get callhome 'logs' setting for cluster with alias 'play'
{{.Prompt}} {{.HelpName}} play logs
`,
}
// checkCallhomeSyntax - validate arguments passed by a user
func checkCallhomeGetSyntax(ctx *cli.Context) {
nArgs := len(ctx.Args())
if nArgs < 1 || nArgs > 2 {
cli.ShowCommandHelpAndExit(ctx, "get", 1) // last argument is exit code
}
}
func validateCallhomeGetArgs(opt string) error {
so := supportedOptions()
if !so.Contains(opt) {
return fmt.Errorf("Invalid option %s. Valid options are %s", opt, so.String())
}
return nil
}
// callhomeGetMessage - container to hold callhome setting information
type callhomeGetMessage struct {
Opt string `json:"option"`
Value string `json:"value"`
}
// String colorized service status message.
func (m callhomeGetMessage) String() string {
return m.Opt + "=" + string(m.Value)
}
// JSON jsonified service status Message message.
func (m callhomeGetMessage) JSON() string {
statusJSONBytes, e := json.MarshalIndent(m, "", " ")
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
return string(statusJSONBytes)
}
func printCallhomeSetting(alias string, s string) {
switch s {
case "logs":
// Create a new MinIO Admin Client
client, err := newAdminClient(alias)
fatalIf(err, "Unable to initialize admin connection.")
val := "off"
key := "logger_webhook:subnet"
kvs, e := getSubSysKeyFromMinIOConfig(client, key)
if e != nil {
// Ignore error saying the subnet webhook is not configured
if e.Error() != fmt.Sprintf("sub-system target '%s' doesn't exist", key) {
fatalIf(probe.NewError(e), "Unable to get server config for subnet")
}
} else {
ep, found := kvs.Lookup("endpoint")
if found && len(ep) > 0 {
// subnet webhook is configured. check if it is enabled
enable, found := kvs.Lookup("enable")
if found {
val = enable
} else {
// if the 'enable' key is not found, it means that the webhook is enabled
val = "on"
}
}
}
printMsg(callhomeGetMessage{
Opt: s,
Value: val,
})
}
}
func mainCallhomeGet(ctx *cli.Context) error {
checkCallhomeGetSyntax(ctx)
args := ctx.Args()
aliasedURL := args.Get(0)
alias, _ := url2Alias(aliasedURL)
var opt string
if len(args) == 2 {
opt = args.Get(1)
fatalIf(probe.NewError(validateCallhomeGetArgs(opt)), fmt.Sprintf("Invalid arguments: %s", strings.Join(args, ",")))
}
if opt == "" {
// print all settings
for _, o := range supportedOptions().ToSlice() {
printCallhomeSetting(alias, o)
}
} else {
printCallhomeSetting(alias, opt)
}
return nil
}
-160
View File
@@ -1,160 +0,0 @@
// Copyright (c) 2015-2022 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"fmt"
"strings"
"github.com/minio/cli"
"github.com/minio/mc/pkg/probe"
"github.com/minio/minio-go/v7/pkg/set"
)
var callhomeFlags = append(globalFlags, cli.BoolFlag{
Name: "dev",
Usage: "development mode - talks to local SUBNET",
Hidden: true,
})
var callhomeSetCmd = cli.Command{
Name: "set",
Usage: "configure callhome settings",
OnUsageError: onUsageError,
Action: mainCallhomeSet,
Before: setGlobalsFromContext,
Flags: callhomeFlags,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} TARGET option=on|off
OPTIONS:
logs - push MinIO server logs to SUBNET
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Enable logs callhome for cluster with alias 'play'
{{.Prompt}} {{.HelpName}} play logs=on
2. Disable logs callhome for cluster with alias 'play'
{{.Prompt}} {{.HelpName}} play logs=off
`,
}
func supportedOptions() set.StringSet {
return set.CreateStringSet("logs")
}
func validOptionValues() set.StringSet {
return set.CreateStringSet("on", "off")
}
// checkCallhomeSyntax - validate arguments passed by a user
func checkCallhomeSetSyntax(ctx *cli.Context) {
if len(ctx.Args()) < 2 {
cli.ShowCommandHelpAndExit(ctx, "set", 1) // last argument is exit code
}
}
func validateCallhomeSetArgs(args []string) error {
for _, arg := range args {
_, _, e := getOptionKV(arg)
if e != nil {
return e
}
}
return nil
}
func mainCallhomeSet(ctx *cli.Context) error {
checkCallhomeSetSyntax(ctx)
aliasedURL := ctx.Args().Get(0)
args := ctx.Args().Tail()
fatalIf(probe.NewError(validateCallhomeSetArgs(args)), fmt.Sprintf("Invalid arguments: %s", strings.Join(args, ",")))
for _, option := range ctx.Args().Tail() {
// options have already been validated, so third (error) return value can be ignored
k, v, _ := getOptionKV(option)
if k == "logs" {
configureSubnetWebhook(aliasedURL, v == "on")
}
}
return nil
}
func configureSubnetWebhook(aliasedURL string, enable bool) {
// Get the alias parameter from cli
alias, _ := url2Alias(aliasedURL)
// Create a new MinIO Admin Client
client, err := newAdminClient(aliasedURL)
fatalIf(err, "Unable to initialize admin connection.")
apiKey := getSubnetAPIKeyFromConfig(alias)
if len(apiKey) == 0 {
e := fmt.Errorf("Please register the cluster first by running 'mc support register %s'", alias)
fatalIf(probe.NewError(e), "Cluster not registered.")
}
enableStr := "off"
if enable {
enableStr = "on"
}
input := fmt.Sprintf("logger_webhook:subnet endpoint=%s auth_token=%s enable=%s",
subnetLogWebhookURL(), apiKey, enableStr)
// Call set config API
restart, e := client.SetConfigKV(globalContext, input)
fatalIf(probe.NewError(e), "Unable to set '%s' to server", input)
// Print set config result
printMsg(configSetMessage{
targetAlias: aliasedURL,
restart: restart,
})
}
func getOptionKV(input string) (string, string, error) {
so := supportedOptions()
vv := validOptionValues()
parts := strings.Split(input, "=")
if len(parts) != 2 {
return "", "", fmt.Errorf("Invalid argument %s. Must be of the form '%s=%s'",
input, strings.Join(so.ToSlice(), "|"), strings.Join(vv.ToSlice(), "|"))
}
opt := parts[0]
if !so.Contains(opt) {
return "", "", fmt.Errorf("Invalid option %s. Valid options are %s", opt, so.String())
}
val := parts[1]
if !vv.Contains(val) {
return "", "", fmt.Errorf("Invalid value %s for option %s. Valid values are %s", val, opt, vv.String())
}
return opt, val, nil
}
+84 -9
View File
@@ -19,13 +19,11 @@ package cmd
import (
"github.com/minio/cli"
json "github.com/minio/colorjson"
"github.com/minio/mc/pkg/probe"
"github.com/minio/pkg/console"
)
var callhomeSubcommands = []cli.Command{
callhomeSetCmd,
callhomeGetCmd,
}
var supportCallhomeCmd = cli.Command{
Name: "callhome",
Usage: "configure callhome settings",
@@ -33,12 +31,89 @@ var supportCallhomeCmd = cli.Command{
Action: mainCallhome,
Before: setGlobalsFromContext,
Flags: globalFlags,
Hidden: true,
Subcommands: callhomeSubcommands,
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
status - Display callhome settings
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Enable callhome for cluster with alias 'play'
{{.Prompt}} {{.HelpName}} enable play
2. Disable callhome for cluster with alias 'play'
{{.Prompt}} {{.HelpName}} disable play
3. Check callhome status for cluster with alias 'play'
{{.Prompt}} {{.HelpName}} status play
`,
}
type supportCallhomeMessage struct {
Status string `json:"status"`
Callhome string `json:"callhome"`
MsgPfx string `json:"-"`
}
// String colorized service status message.
func (s supportCallhomeMessage) String() string {
return console.Colorize(featureToggleMessageTag, s.MsgPfx+s.Callhome)
}
// JSON jsonified service status message.
func (s supportCallhomeMessage) JSON() string {
s.Status = "success"
jsonBytes, e := json.MarshalIndent(s, "", " ")
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
return string(jsonBytes)
}
func mainCallhome(ctx *cli.Context) error {
commandNotFound(ctx, callhomeSubcommands)
setToggleMessageColor()
alias, arg := checkToggleCmdSyntax(ctx, "callhome")
if arg == "status" {
enabled := isFeatureEnabled(alias, "callhome", "callhome")
printMsg(supportCallhomeMessage{
Callhome: featureStatusStr(enabled),
})
return nil
}
setCallhomeConfig(alias, arg == "enable")
return nil
// Sub-commands like "set", "get" have their own main.
}
func setCallhomeConfig(alias string, enableCallhome bool) {
// Create a new MinIO Admin Client
client, err := newAdminClient(alias)
fatalIf(err, "Unable to initialize admin connection.")
if !minioConfigSupportsSubSys(client, "callhome") {
fatal(errDummy().Trace(), "Your version of MinIO doesn't support this configuration")
}
enableStr := "off"
if enableCallhome {
validateClusterRegistered(alias)
enableStr = "on"
}
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 ",
})
}
+128
View File
@@ -0,0 +1,128 @@
// Copyright (c) 2015-2022 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"fmt"
"github.com/minio/cli"
json "github.com/minio/colorjson"
"github.com/minio/mc/pkg/probe"
"github.com/minio/pkg/console"
)
var logsFlags = append(globalFlags, cli.BoolFlag{
Name: "dev",
Usage: "development mode - talks to local SUBNET",
Hidden: true,
})
var supportLogsCmd = cli.Command{
Name: "logs",
Usage: "configure logs settings",
OnUsageError: onUsageError,
Action: mainLogs,
Before: setGlobalsFromContext,
Flags: logsFlags,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} enable|disable|status ALIAS
OPTIONS:
enable - Enable pushing MinIO logs to SUBNET in real-time
disable - Disable pushing MinIO logs to SUBNET
status - Display logs settings
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Enable logs for cluster with alias 'play'
{{.Prompt}} {{.HelpName}} enable play
2. Disable logs for cluster with alias 'play'
{{.Prompt}} {{.HelpName}} disable play
3. Check logs status for cluster with alias 'play'
{{.Prompt}} {{.HelpName}} status play
`,
}
type supportLogsMessage struct {
Status string `json:"status"`
Logs string `json:"logs"`
MsgPfx string `json:"-"`
}
// String colorized service status message.
func (s supportLogsMessage) String() string {
return console.Colorize(featureToggleMessageTag, s.MsgPfx+s.Logs)
}
// JSON jsonified service status message.
func (s supportLogsMessage) JSON() string {
s.Status = "success"
jsonBytes, e := json.MarshalIndent(s, "", " ")
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
return string(jsonBytes)
}
func mainLogs(ctx *cli.Context) error {
setToggleMessageColor()
alias, arg := checkToggleCmdSyntax(ctx, "logs")
if arg == "status" {
enabled := isFeatureEnabled(alias, "logger_webhook", "logger_webhook:subnet")
printMsg(supportLogsMessage{
Logs: featureStatusStr(enabled),
})
return nil
}
configureSubnetWebhook(alias, arg == "enable")
return nil
}
func configureSubnetWebhook(alias string, enable bool) {
// Create a new MinIO Admin Client
client, err := newAdminClient(alias)
fatalIf(err, "Unable to initialize admin connection.")
apiKey := validateClusterRegistered(alias)
enableStr := "off"
if enable {
enableStr = "on"
}
input := fmt.Sprintf("logger_webhook:subnet endpoint=%s auth_token=%s enable=%s",
subnetLogWebhookURL(), apiKey, enableStr)
// Call set config API
_, e := client.SetConfigKV(globalContext, input)
fatalIf(probe.NewError(e), "Unable to set '%s' to server", input)
printMsg(supportLogsMessage{
Logs: featureStatusStr(enable),
MsgPfx: "Logging to support is now ",
})
}
+95
View File
@@ -18,12 +18,21 @@
package cmd
import (
"fmt"
"github.com/fatih/color"
"github.com/minio/cli"
"github.com/minio/mc/pkg/probe"
"github.com/minio/minio-go/v7/pkg/set"
"github.com/minio/pkg/console"
)
const featureToggleMessageTag = "FeatureToggleMessage"
var supportSubcommands = []cli.Command{
supportRegisterCmd,
supportCallhomeCmd,
supportLogsCmd,
supportDiagCmd,
supportPerfCmd,
supportInspectCmd,
@@ -40,6 +49,92 @@ var supportCmd = cli.Command{
HideHelpCommand: true,
}
func toggleCmdArgs() set.StringSet {
return set.CreateStringSet("enable", "disable", "status")
}
func validateToggleCmdArg(arg string) error {
valid := toggleCmdArgs()
if !valid.Contains(arg) {
return fmt.Errorf("Invalid argument '%s'. Must be one of %v", arg, valid)
}
return nil
}
func checkToggleCmdSyntax(ctx *cli.Context, cmdName string) (string, string) {
if len(ctx.Args()) != 2 {
cli.ShowCommandHelpAndExit(ctx, cmdName, 1) // last argument is exit code
}
arg := ctx.Args().Get(0)
aliasedURL := ctx.Args().Get(1)
fatalIf(probe.NewError(validateToggleCmdArg(arg)), "Invalid arguments.")
alias, _ := url2Alias(aliasedURL)
return alias, arg
}
func setToggleMessageColor() {
console.SetColor(featureToggleMessageTag, color.New(color.FgGreen, color.Bold))
}
func featureStatusStr(enabled bool) string {
if enabled {
return "enabled"
}
return "disabled"
}
func validateClusterRegistered(alias string) string {
apiKey := getSubnetAPIKeyFromConfig(alias)
if len(apiKey) == 0 {
e := fmt.Errorf("Please register the cluster first by running 'mc support register %s'", alias)
fatalIf(probe.NewError(e), "Cluster not registered.")
}
return apiKey
}
// isFeatureEnabled - checks if a feature is enabled in MinIO config
// To be used with configs that can be switched on/off using the `enable` key
// e.g. subSys = logger_webhook, target = logger_webhook:subnet
// Returns true if any of the following is true
// - `enable` is set to `on`
// - `enable` key is not found
// Returns false if any of the following is true
// - given subsystem is not supported by the version of MinIO
// - the given target doesn't exist in the config
// - `enable` is set to `off`
func isFeatureEnabled(alias string, subSys string, target string) bool {
client, err := newAdminClient(alias)
// Create a new MinIO Admin Client
fatalIf(err, "Unable to initialize admin connection.")
if !minioConfigSupportsSubSys(client, subSys) {
return false
}
kvs, e := getSubSysKeyFromMinIOConfig(client, target)
if e != nil {
// Ignore error if the given target doesn't exist
// e.g. logger_webhook:subnet doesn't exist when
// pushing logs to SUBNET has not been enabled
if e.Error() == fmt.Sprintf("sub-system target '%s' doesn't exist", target) {
return false
}
fatalIf(probe.NewError(e), fmt.Sprintf("Unable to get server config for '%s'", subSys))
}
enable, found := kvs.Lookup("enable")
if !found {
// if `enable` key is not found, it means that `enable=on`
return true
}
return enable == "on"
}
// mainSupport is the handle for "mc support" command.
func mainSupport(ctx *cli.Context) error {
commandNotFound(ctx, supportSubcommands)