github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-rebalance-start.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 adminRebalanceStartCmd = cli.Command{
    31  	Name:         "start",
    32  	Usage:        "start rebalance operation",
    33  	Action:       mainAdminRebalanceStart,
    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  xEXAMPLES:
    47    1. Start rebalance on a MinIO deployment with alias myminio
    48       {{.Prompt}} {{.HelpName}} myminio
    49  `,
    50  }
    51  
    52  type rebalanceStartMsg struct {
    53  	Status string `json:"status"`
    54  	Target string `json:"url"`
    55  	ID     string `json:"id"`
    56  }
    57  
    58  func (r rebalanceStartMsg) JSON() string {
    59  	r.Status = "success"
    60  	b, e := json.MarshalIndent(r, "", " ")
    61  	fatalIf(probe.NewError(e), "Unable to marshal to JSON")
    62  	return string(b)
    63  }
    64  
    65  func (r rebalanceStartMsg) String() string {
    66  	return console.Colorize("rebalanceStartMsg", fmt.Sprintf("Rebalance started for %s", r.Target))
    67  }
    68  
    69  func mainAdminRebalanceStart(ctx *cli.Context) error {
    70  	if len(ctx.Args()) != 1 {
    71  		showCommandHelpAndExit(ctx, 1)
    72  	}
    73  
    74  	console.SetColor("rebalanceStartMsg", color.New(color.FgGreen))
    75  
    76  	args := ctx.Args()
    77  	aliasedURL := args.Get(0)
    78  
    79  	// Create a new MinIO Admin Client
    80  	client, err := newAdminClient(aliasedURL)
    81  	fatalIf(err.Trace(aliasedURL), "Unable to initialize admin client")
    82  
    83  	id, e := client.RebalanceStart(globalContext)
    84  	fatalIf(probe.NewError(e), "Unable to start rebalance")
    85  
    86  	printMsg(rebalanceStartMsg{
    87  		Target: aliasedURL,
    88  		ID:     id,
    89  	})
    90  
    91  	return nil
    92  }