github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-rebalance-stop.go (about)

     1  // Copyright (c) 2022 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package cmd
    19  
    20  import (
    21  	"fmt"
    22  
    23  	"github.com/fatih/color"
    24  	"github.com/minio/cli"
    25  	json "github.com/minio/colorjson"
    26  	"github.com/minio/mc/pkg/probe"
    27  	"github.com/minio/pkg/v2/console"
    28  )
    29  
    30  var adminRebalanceStopCmd = cli.Command{
    31  	Name:         "stop",
    32  	Usage:        "stop an ongoing rebalance operation",
    33  	Action:       mainAdminRebalanceStop,
    34  	OnUsageError: onUsageError,
    35  	Before:       setGlobalsFromContext,
    36  	Flags:        globalFlags,
    37  	CustomHelpTemplate: `NAME:
    38    {{.HelpName}} - {{.Usage}}
    39  
    40  USAGE:
    41    {{.HelpName}} ALIAS
    42  
    43  FLAGS:
    44    {{range .VisibleFlags}}{{.}}
    45    {{end}}
    46  EXAMPLES:
    47    1. Stop an ongoing rebalance on a MinIO deployment with alias myminio
    48       {{.Prompt}} {{.HelpName}} myminio
    49  `,
    50  }
    51  
    52  type rebalanceStopMsg struct {
    53  	Status string `json:"status"`
    54  	Target string `json:"url"`
    55  }
    56  
    57  func (r rebalanceStopMsg) JSON() string {
    58  	r.Status = "success"
    59  	b, e := json.MarshalIndent(r, "", " ")
    60  	fatalIf(probe.NewError(e), "Unable to marshal to JSON")
    61  	return string(b)
    62  }
    63  
    64  func (r rebalanceStopMsg) String() string {
    65  	return console.Colorize("rebalanceStopMsg", fmt.Sprintf("Rebalance stopped for %s", r.Target))
    66  }
    67  
    68  func mainAdminRebalanceStop(ctx *cli.Context) error {
    69  	if len(ctx.Args()) != 1 {
    70  		showCommandHelpAndExit(ctx, 1)
    71  	}
    72  
    73  	console.SetColor("rebalanceStopMsg", color.New(color.FgGreen))
    74  
    75  	args := ctx.Args()
    76  	aliasedURL := args.Get(0)
    77  
    78  	// Create a new MinIO Admin Client
    79  	client, err := newAdminClient(aliasedURL)
    80  	fatalIf(err.Trace(aliasedURL), "Unable to initialize admin client")
    81  
    82  	fatalIf(probe.NewError(client.RebalanceStop(globalContext)), "Unable to stop rebalance operation")
    83  
    84  	printMsg(rebalanceStopMsg{
    85  		Target: aliasedURL,
    86  	})
    87  
    88  	return nil
    89  }