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

     1  // Copyright (c) 2015-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  	"path/filepath"
    22  
    23  	humanize "github.com/dustin/go-humanize"
    24  	"github.com/fatih/color"
    25  	"github.com/minio/cli"
    26  	"github.com/minio/madmin-go/v3"
    27  	"github.com/minio/mc/pkg/probe"
    28  	"github.com/minio/pkg/v2/console"
    29  )
    30  
    31  var adminDecommissionCancelCmd = cli.Command{
    32  	Name:         "cancel",
    33  	Usage:        "cancel an ongoing decommissioning of a pool",
    34  	Action:       mainAdminDecommissionCancel,
    35  	OnUsageError: onUsageError,
    36  	Before:       setGlobalsFromContext,
    37  	Flags:        globalFlags,
    38  	CustomHelpTemplate: `NAME:
    39    {{.HelpName}} - {{.Usage}}
    40  
    41  USAGE:
    42    {{.HelpName}} TARGET
    43  
    44  FLAGS:
    45    {{range .VisibleFlags}}{{.}}
    46    {{end}}
    47  EXAMPLES:
    48    1. Cancel an ongoing decommissioning of a pool.
    49       {{.Prompt}} {{.HelpName}} myminio/ http://server{5...8}/disk{1...4}
    50  
    51    2. Cancel all ongoing decommissioning of pools.
    52       {{.Prompt}} {{.HelpName}} myminio/
    53  `,
    54  }
    55  
    56  // checkAdminDecommissionCancelSyntax - validate all the passed arguments
    57  func checkAdminDecommissionCancelSyntax(ctx *cli.Context) {
    58  	if len(ctx.Args()) > 2 || len(ctx.Args()) == 0 {
    59  		showCommandHelpAndExit(ctx, 1) // last argument is exit code
    60  	}
    61  }
    62  
    63  // mainAdminDecommissionCancel is the handle for "mc admin decommission cancel" command.
    64  func mainAdminDecommissionCancel(ctx *cli.Context) error {
    65  	checkAdminDecommissionCancelSyntax(ctx)
    66  
    67  	// Get the alias parameter from cli
    68  	args := ctx.Args()
    69  	aliasedURL := args.Get(0)
    70  	aliasedURL = filepath.Clean(aliasedURL)
    71  
    72  	// Create a new MinIO Admin Client
    73  	client, err := newAdminClient(aliasedURL)
    74  	fatalIf(err, "Unable to initialize admin connection.")
    75  
    76  	if pool := args.Get(1); pool != "" {
    77  		e := client.CancelDecommissionPool(globalContext, pool)
    78  		fatalIf(probe.NewError(e).Trace(args...), "Unable to cancel decommissioning, please try again")
    79  		return nil
    80  	}
    81  
    82  	poolStatuses, e := client.ListPoolsStatus(globalContext)
    83  	fatalIf(probe.NewError(e).Trace(args...), "Unable to get status for all pools")
    84  
    85  	var newPoolStatuses []madmin.PoolStatus
    86  	for _, pool := range poolStatuses {
    87  		if pool.Decommission != nil {
    88  			if pool.Decommission.StartTime.IsZero() {
    89  				continue
    90  			}
    91  			if pool.Decommission.Complete {
    92  				continue
    93  			}
    94  		}
    95  		newPoolStatuses = append(newPoolStatuses, pool)
    96  	}
    97  
    98  	dspOrder := []col{colGreen} // Header
    99  	for i := 0; i < len(newPoolStatuses); i++ {
   100  		dspOrder = append(dspOrder, colGrey)
   101  	}
   102  	var printColors []*color.Color
   103  	for _, c := range dspOrder {
   104  		printColors = append(printColors, getPrintCol(c))
   105  	}
   106  
   107  	tbl := console.NewTable(printColors, []bool{false, false, false, false}, 0)
   108  
   109  	cellText := make([][]string, len(newPoolStatuses)+1)
   110  	cellText[0] = []string{
   111  		"ID",
   112  		"Pools",
   113  		"Capacity",
   114  		"Status",
   115  	}
   116  	for idx, pool := range poolStatuses {
   117  		idx++
   118  		totalSize := uint64(pool.Decommission.TotalSize)
   119  		currentSize := uint64(pool.Decommission.CurrentSize)
   120  		capacity := humanize.IBytes(totalSize-currentSize) + " (used) / " + humanize.IBytes(totalSize) + " (total)"
   121  		status := ""
   122  		if pool.Decommission != nil {
   123  			if pool.Decommission.StartTime.IsZero() {
   124  				continue
   125  			}
   126  			if pool.Decommission.Complete {
   127  				continue
   128  			}
   129  			status = "Draining"
   130  		}
   131  		cellText[idx] = []string{
   132  			humanize.Ordinal(pool.ID + 1),
   133  			pool.CmdLine,
   134  			capacity,
   135  			status,
   136  		}
   137  	}
   138  	return tbl.DisplayTable(cellText)
   139  }