1
0
mirror of https://github.com/minio/mc.git synced 2026-01-04 02:44:40 +03:00

Add command to unregister a cluster (#4314)

This commit is contained in:
Shireesh Anjal
2022-10-21 00:06:25 +05:30
committed by GitHub
parent b9b729a3c9
commit 3cc795610b
3 changed files with 124 additions and 0 deletions

97
cmd/license-unregister.go Normal file
View File

@@ -0,0 +1,97 @@
// 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/fatih/color"
"github.com/minio/cli"
json "github.com/minio/colorjson"
"github.com/minio/mc/pkg/probe"
"github.com/minio/pkg/console"
)
const licUnregisterMsgTag = "licenseUnregisterMessage"
var licenseUnregisterCmd = cli.Command{
Name: "unregister",
Usage: "unregister from MinIO Subscription Network",
OnUsageError: onUsageError,
Action: mainLicenseUnregister,
Before: setGlobalsFromContext,
Hidden: true,
Flags: supportGlobalFlags,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} TARGET
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Unregister MinIO cluster at alias 'myminio' from SUBNET
{{.Prompt}} {{.HelpName}} myminio
`,
}
type licUnregisterMessage struct {
Status string `json:"status"`
Alias string `json:"-"`
}
// String colorized license unregister message
func (li licUnregisterMessage) String() string {
msg := fmt.Sprintf("%s unregistered successfully.", li.Alias)
return console.Colorize(licUnregisterMsgTag, msg)
}
// JSON jsonified license unregister message
func (li licUnregisterMessage) JSON() string {
jsonBytes, e := json.MarshalIndent(li, "", " ")
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
return string(jsonBytes)
}
// checkLicenseUnregisterSyntax - validate arguments passed by a user
func checkLicenseUnregisterSyntax(ctx *cli.Context) {
if len(ctx.Args()) != 1 {
showCommandHelpAndExit(ctx, "unregister", 1) // last argument is exit code
}
}
func mainLicenseUnregister(ctx *cli.Context) error {
console.SetColor(licUnregisterMsgTag, color.New(color.FgGreen, color.Bold))
checkLicenseUnregisterSyntax(ctx)
aliasedURL := ctx.Args().Get(0)
alias, _ := initSubnetConnectivity(ctx, aliasedURL, false)
apiKey, e := getSubnetAPIKey(alias)
fatalIf(probe.NewError(e), "Error in fetching SUBNET API Key:")
info := getAdminInfo(aliasedURL)
e = unregisterClusterFromSubnet(alias, info.DeploymentID, apiKey)
fatalIf(probe.NewError(e), "Could not unregister cluster from SUBNET:")
printMsg(licUnregisterMessage{Status: "success", Alias: alias})
return nil
}

View File

@@ -25,6 +25,7 @@ var licenseSubcommands = []cli.Command{
licenseRegisterCmd,
licenseInfoCmd,
licenseUpdateCmd,
licenseUnregisterCmd,
}
var licenseCmd = cli.Command{

View File

@@ -99,6 +99,10 @@ func subnetRegisterURL() string {
return subnetBaseURL() + "/api/cluster/register"
}
func subnetUnregisterURL(depID string) string {
return subnetBaseURL() + "/api/cluster/unregister?deploymentId=" + depID
}
func subnetOfflineRegisterURL(regToken string) string {
return subnetBaseURL() + "/cluster/register?token=" + regToken
}
@@ -555,6 +559,28 @@ func registerClusterOnSubnet(clusterRegInfo ClusterRegistrationInfo, alias strin
return extractAndSaveSubnetCreds(alias, resp)
}
func removeSubnetAuthConfig(alias string) {
setSubnetConfig(alias, "api_key", "")
setSubnetConfig(alias, "license", "")
}
// unregisterClusterFromSubnet - Unregisters the given cluster from SUBNET using given API key for auth
func unregisterClusterFromSubnet(alias string, depID string, apiKey string) error {
regURL, headers, e := subnetURLWithAuth(subnetUnregisterURL(depID), apiKey)
if e != nil {
return e
}
_, e = subnetPostReq(regURL, nil, headers)
if e != nil {
return e
}
removeSubnetAuthConfig(alias)
return nil
}
// extractAndSaveSubnetCreds - extract license from response and set it in minio config
func extractAndSaveSubnetCreds(alias string, resp string) (string, string, error) {
parsedResp := gjson.Parse(resp)