diff --git a/cmd/auto-complete.go b/cmd/auto-complete.go index 3f1f461c..c85a47e3 100644 --- a/cmd/auto-complete.go +++ b/cmd/auto-complete.go @@ -448,19 +448,22 @@ var completeCmds = map[string]complete.Predictor{ "/alias/remove": aliasCompleter, "/alias/import": nil, - "/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, + "/support/callhome": aliasCompleter, + "/support/logs": aliasCompleter, + "/support/register": aliasCompleter, + "/support/diag": aliasCompleter, + "/support/profile": aliasCompleter, + "/support/proxy/set": aliasCompleter, + "/support/proxy/show": aliasCompleter, + "/support/proxy/remove": 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, diff --git a/cmd/support-callhome.go b/cmd/support-callhome.go index 52234eec..1556d476 100644 --- a/cmd/support-callhome.go +++ b/cmd/support-callhome.go @@ -103,7 +103,7 @@ func (s supportCallhomeMessage) String() string { } else { msg = s.Feature + " is now " + s.Action } - return console.Colorize(featureToggleMessageTag, msg) + return console.Colorize(supportSuccessMsgTag, msg) } // JSON jsonified callhome command output message. @@ -120,7 +120,7 @@ func isDiagCallhomeEnabled(alias string) bool { } func mainCallhome(ctx *cli.Context) error { - setToggleMessageColor() + setSuccessMessageColor() alias, arg := checkToggleCmdSyntax(ctx, "callhome") diag, logs := parseCallhomeFlags(ctx) diff --git a/cmd/support-proxy-remove.go b/cmd/support-proxy-remove.go new file mode 100644 index 00000000..555778da --- /dev/null +++ b/cmd/support-proxy-remove.go @@ -0,0 +1,91 @@ +// 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 ( + "github.com/minio/cli" + "github.com/minio/mc/pkg/probe" + "github.com/minio/pkg/console" +) + +type supportProxyRemoveMessage struct { + Status string `json:"status"` +} + +// String colorized proxy remove message +func (s supportProxyRemoveMessage) String() string { + return console.Colorize(supportSuccessMsgTag, "Proxy has been removed") +} + +// JSON jsonified proxy remove message +func (s supportProxyRemoveMessage) JSON() string { + s.Status = "success" + return toJSON(s) +} + +var supportProxyRemoveCmd = cli.Command{ + Name: "remove", + Usage: "Remove proxy configuration", + Action: mainSupportProxyRemove, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: supportGlobalFlags, + HideHelpCommand: true, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} [FLAGS] TARGET + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. Remove the proxy configured for cluster with alias 'myminio' + {{.Prompt}} {{.HelpName}} myminio +`, +} + +func checkSupportProxyRemoveSyntax(ctx *cli.Context) { + if len(ctx.Args()) != 1 { + showCommandHelpAndExit(ctx, 1) // last argument is exit code + } +} + +// mainSupportProxyRemove is the handler for "mc support proxy remove" command. +func mainSupportProxyRemove(ctx *cli.Context) error { + // Check for command syntax + checkSupportProxyRemoveSyntax(ctx) + setSuccessMessageColor() + + // Get the alias parameter from cli + args := ctx.Args() + aliasedURL := args.Get(0) + alias, _ := url2Alias(aliasedURL) + validateClusterRegistered(alias, true) + + // Create a new MinIO Admin Client + client := getClient(aliasedURL) + + // Main execution + _, e := client.DelConfigKV(globalContext, "subnet proxy") + fatalIf(probe.NewError(e), "Unable to remove proxy:") + + printMsg(supportProxyRemoveMessage{}) + return nil +} diff --git a/cmd/support-proxy-set.go b/cmd/support-proxy-set.go new file mode 100644 index 00000000..db338f81 --- /dev/null +++ b/cmd/support-proxy-set.go @@ -0,0 +1,103 @@ +// 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 ( + "net/url" + + "github.com/minio/cli" + "github.com/minio/mc/pkg/probe" + "github.com/minio/pkg/console" +) + +type supportProxySetMessage struct { + Status string `json:"status"` + Proxy string `json:"proxy"` +} + +// String colorized proxy set message +func (s supportProxySetMessage) String() string { + return console.Colorize(supportSuccessMsgTag, "Proxy is now set to "+s.Proxy) +} + +// JSON jsonified proxy set message +func (s supportProxySetMessage) JSON() string { + s.Status = "success" + return toJSON(s) +} + +var supportProxySetCmd = cli.Command{ + Name: "set", + Usage: "configure proxy to given URL", + Action: mainSupportProxySet, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: supportGlobalFlags, + HideHelpCommand: true, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} [FLAGS] TARGET PROXY_URL + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. Set the proxy to http://my.proxy for cluster with alias 'myminio' + {{.Prompt}} {{.HelpName}} myminio http://my.proxy +`, +} + +func checkSupportProxySetSyntax(ctx *cli.Context) { + if len(ctx.Args()) != 2 { + showCommandHelpAndExit(ctx, 1) // last argument is exit code + } +} + +// mainSupportProxySet is the handle for "mc support proxy set" command. +func mainSupportProxySet(ctx *cli.Context) error { + // Check for command syntax + checkSupportProxySetSyntax(ctx) + setSuccessMessageColor() + + // Get the alias parameter from cli + args := ctx.Args() + aliasedURL := args.Get(0) + alias, _ := url2Alias(aliasedURL) + validateClusterRegistered(alias, true) + + // Create a new MinIO Admin Client + client := getClient(aliasedURL) + + // Call set config API + proxy := args.Get(1) + if len(proxy) == 0 { + fatal(errDummy().Trace(), "Proxy must not be empty") + } + + _, e := url.Parse(proxy) + fatalIf(probe.NewError(e), "Invalid proxy:") + + // Main execution + _, e = client.SetConfigKV(globalContext, "subnet proxy="+proxy) + fatalIf(probe.NewError(e), "Unable to set proxy '%s':", proxy) + + printMsg(supportProxySetMessage{Proxy: proxy}) + return nil +} diff --git a/cmd/support-proxy-show.go b/cmd/support-proxy-show.go new file mode 100644 index 00000000..4f1a9cec --- /dev/null +++ b/cmd/support-proxy-show.go @@ -0,0 +1,94 @@ +// 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 ( + "github.com/minio/cli" + "github.com/minio/pkg/console" +) + +var supportProxyShowCmd = cli.Command{ + Name: "show", + Usage: "Show the configured proxy", + Action: mainSupportProxyShow, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: supportGlobalFlags, + HideHelpCommand: true, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} [FLAGS] TARGET + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. Show the proxy configured for cluster with alias 'myminio' + {{.Prompt}} {{.HelpName}} myminio +`, +} + +type supportProxyShowMessage struct { + Status string `json:"status"` + Proxy string `json:"proxy"` +} + +// String colorized proxy show message +func (s supportProxyShowMessage) String() string { + msg := s.Proxy + if len(msg) == 0 { + msg = "Proxy is not configured" + } + return console.Colorize(supportSuccessMsgTag, msg) +} + +// JSON jsonified proxy show message +func (s supportProxyShowMessage) JSON() string { + s.Status = "success" + return toJSON(s) +} + +func checkSupportProxyShowSyntax(ctx *cli.Context) { + if len(ctx.Args()) != 1 { + showCommandHelpAndExit(ctx, 1) // last argument is exit code + } +} + +// mainSupportProxyShow is the handler for "mc support proxy show" command. +func mainSupportProxyShow(ctx *cli.Context) error { + // Check for command syntax + checkSupportProxyShowSyntax(ctx) + setSuccessMessageColor() + + // Get the alias parameter from cli + args := ctx.Args() + aliasedURL := args.Get(0) + alias, _ := url2Alias(aliasedURL) + + // Main execution + // get the subnet proxy config from MinIO if available + proxy, supported := getKeyFromSubnetConfig(alias, "proxy") + if !supported { + fatal(errDummy().Trace(), "Proxy configuration not supported in this version of MinIO.") + } + + printMsg(supportProxyShowMessage{Proxy: proxy}) + return nil +} diff --git a/cmd/support-proxy.go b/cmd/support-proxy.go new file mode 100644 index 00000000..13c25341 --- /dev/null +++ b/cmd/support-proxy.go @@ -0,0 +1,47 @@ +// 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 ( + "github.com/minio/cli" +) + +var supportProxySubcommands = []cli.Command{ + supportProxySetCmd, + supportProxyRemoveCmd, + supportProxyShowCmd, +} + +var supportProxyCmd = cli.Command{ + Name: "proxy", + Usage: "configure proxy", + Action: mainSupportProxy, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: supportGlobalFlags, + Subcommands: supportProxySubcommands, + HideHelpCommand: true, +} + +// mainSupportProxy is the handler for "mc support proxy" command. +func mainSupportProxy(ctx *cli.Context) error { + commandNotFound(ctx, supportProxySubcommands) + return nil + // Sub-commands like "set", "remove", "show" have their own main. + // Check for command syntax +} diff --git a/cmd/support.go b/cmd/support.go index ec7edfc0..e2653f5f 100644 --- a/cmd/support.go +++ b/cmd/support.go @@ -22,13 +22,14 @@ import ( "github.com/fatih/color" "github.com/minio/cli" + json "github.com/minio/colorjson" "github.com/minio/madmin-go" "github.com/minio/mc/pkg/probe" "github.com/minio/minio-go/v7/pkg/set" "github.com/minio/pkg/console" ) -const featureToggleMessageTag = "FeatureToggleMessage" +const supportSuccessMsgTag = "SupportSuccessMessage" var supportGlobalFlags = append(globalFlags, cli.BoolFlag{ Name: "dev", @@ -45,6 +46,7 @@ var supportSubcommands = []cli.Command{ supportInspectCmd, supportProfileCmd, supportTopCmd, + supportProxyCmd, } var supportCmd = cli.Command{ @@ -83,8 +85,8 @@ func checkToggleCmdSyntax(ctx *cli.Context, cmdName string) (string, string) { return alias, arg } -func setToggleMessageColor() { - console.SetColor(featureToggleMessageTag, color.New(color.FgGreen, color.Bold)) +func setSuccessMessageColor() { + console.SetColor(supportSuccessMsgTag, color.New(color.FgGreen, color.Bold)) } func featureStatusStr(enabled bool) string { @@ -158,6 +160,13 @@ func isFeatureEnabled(alias string, subSys string, target string) bool { return false } +func toJSON(obj interface{}) string { + jsonBytes, e := json.MarshalIndent(obj, "", " ") + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") + + return string(jsonBytes) +} + // mainSupport is the handle for "mc support" command. func mainSupport(ctx *cli.Context) error { commandNotFound(ctx, supportSubcommands)