From f4f5932d3fbf66e72d1e2186d6c0b01adfc43c0f Mon Sep 17 00:00:00 2001 From: Shireesh Anjal <355479+anjalshireesh@users.noreply.github.com> Date: Tue, 22 Feb 2022 05:17:31 +0530 Subject: [PATCH] Add `mc support callhome get` command (#3986) * Add `mc support callhome get` command Will print all the callhome settings (currently just `logs=on|off`) * Rename retrieveCallhomeSetting to printCallhomeSetting As it doesn't need to return anything, and only prints the setting. --- cmd/auto-complete.go | 1 + cmd/subnet-utils.go | 41 +++++----- cmd/support-callhome-get.go | 145 ++++++++++++++++++++++++++++++++++++ cmd/support-callhome-set.go | 4 +- cmd/support-callhome.go | 1 + 5 files changed, 170 insertions(+), 22 deletions(-) create mode 100644 cmd/support-callhome-get.go diff --git a/cmd/auto-complete.go b/cmd/auto-complete.go index 16432896..a6377f84 100644 --- a/cmd/auto-complete.go +++ b/cmd/auto-complete.go @@ -401,6 +401,7 @@ var completeCmds = map[string]complete.Predictor{ "/alias/import": nil, "/support/callhome/set": aliasCompleter, + "/support/callhome/get": aliasCompleter, "/support/register": aliasCompleter, "/support/diag": aliasCompleter, "/support/profile/start": aliasCompleter, diff --git a/cmd/subnet-utils.go b/cmd/subnet-utils.go index 7b7cdf88..538bd82c 100644 --- a/cmd/subnet-utils.go +++ b/cmd/subnet-utils.go @@ -203,32 +203,33 @@ func subnetPostReq(reqURL string, payload interface{}, headers map[string]string return subnetReqDo(r, headers) } -func getSubnetKeyFromMinIOConfig(alias string, key string) (bool, string) { +func getSubSysKeyFromMinIOConfig(client *madmin.AdminClient, subSys string) madmin.KVS { + sh, pe := client.HelpConfigKV(globalContext, subSys, "", false) + fatalIf(probe.NewError(pe), fmt.Sprintf("Unable to get config keys for %s", subSys)) + + buf, e := client.GetConfigKV(globalContext, subSys) + fatalIf(probe.NewError(e), fmt.Sprintf("Unable to get server config for %s", subSys)) + + tgt, e := madmin.ParseSubSysTarget(buf, sh) + fatalIf(probe.NewError(e), fmt.Sprintf("Unable to parse sub-system target '%s'", subSys)) + + return tgt.KVS +} + +func getSubnetKeyFromMinIOConfig(alias string, key string) (string, bool) { client, err := newAdminClient(alias) fatalIf(err, "Unable to initialize admin connection.") if minioConfigSupportsSubnet(client) { - sh, pe := client.HelpConfigKV(globalContext, "subnet", "", false) - fatalIf(probe.NewError(pe), "Unable to get config keys for SUBNET") - - buf, e := client.GetConfigKV(globalContext, "subnet") - fatalIf(probe.NewError(e), "Unable to get server SUBNET config") - - tgt, e := madmin.ParseSubSysTarget(buf, sh) - fatalIf(probe.NewError(e), "Unable to parse sub-system target 'subnet'") - - for _, kv := range tgt.KVS { - if kv.Key == key { - return true, kv.Value - } - } + return getSubSysKeyFromMinIOConfig(client, "subnet").Lookup(key) } - return false, "" + + return "", false } func getSubnetAPIKeyFromConfig(alias string) string { // get the subnet api_key config from MinIO if available - supported, apiKey := getSubnetKeyFromMinIOConfig(alias, "api_key") + apiKey, supported := getSubnetKeyFromMinIOConfig(alias, "api_key") if supported { return apiKey } @@ -244,7 +245,7 @@ func setSubnetProxyFromConfig(alias string) error { } // get the subnet proxy config from MinIO if available - supported, proxy := getSubnetKeyFromMinIOConfig(alias, "proxy") + proxy, supported := getSubnetKeyFromMinIOConfig(alias, "proxy") if supported && len(proxy) > 0 { proxyURL, e := url.Parse(proxy) if e != nil { @@ -257,7 +258,7 @@ func setSubnetProxyFromConfig(alias string) error { func getSubnetLicenseFromConfig(alias string) string { // get the subnet license config from MinIO if available - supported, lic := getSubnetKeyFromMinIOConfig(alias, "license") + lic, supported := getSubnetKeyFromMinIOConfig(alias, "license") if supported { return lic } @@ -287,7 +288,7 @@ func minioConfigSupportsSubnet(client *madmin.AdminClient) bool { } func setSubnetAPIKeyConfig(alias string, apiKey string) { - supported, _ := getSubnetKeyFromMinIOConfig(alias, "api_key") + _, supported := getSubnetKeyFromMinIOConfig(alias, "api_key") if supported { // Create a new MinIO Admin Client client, err := newAdminClient(alias) diff --git a/cmd/support-callhome-get.go b/cmd/support-callhome-get.go new file mode 100644 index 00000000..a9a32bc2 --- /dev/null +++ b/cmd/support-callhome-get.go @@ -0,0 +1,145 @@ +// 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 . + +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" + + kvs := getSubSysKeyFromMinIOConfig(client, "logger_webhook:subnet") + 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 +} diff --git a/cmd/support-callhome-set.go b/cmd/support-callhome-set.go index 3513ad98..10d9c84f 100644 --- a/cmd/support-callhome-set.go +++ b/cmd/support-callhome-set.go @@ -75,7 +75,7 @@ func checkCallhomeSetSyntax(ctx *cli.Context) { } } -func validateArgs(args []string) error { +func validateCallhomeSetArgs(args []string) error { for _, arg := range args { _, _, e := getOptionKV(arg) if e != nil { @@ -90,7 +90,7 @@ func mainCallhomeSet(ctx *cli.Context) error { aliasedURL := ctx.Args().Get(0) args := ctx.Args().Tail() - fatalIf(probe.NewError(validateArgs(args)), fmt.Sprintf("Invalid arguments: %s", strings.Join(args, ","))) + 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 diff --git a/cmd/support-callhome.go b/cmd/support-callhome.go index 8309885d..775dd042 100644 --- a/cmd/support-callhome.go +++ b/cmd/support-callhome.go @@ -23,6 +23,7 @@ import ( var callhomeSubcommands = []cli.Command{ callhomeSetCmd, + callhomeGetCmd, } var supportCallhomeCmd = cli.Command{