github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-replicate-remove.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  	"fmt"
    22  
    23  	"github.com/fatih/color"
    24  	"github.com/minio/cli"
    25  	json "github.com/minio/colorjson"
    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 adminReplicateRemoveFlags = []cli.Flag{
    32  	cli.BoolFlag{
    33  		Name:  "all",
    34  		Usage: "remove site replication from all participating sites",
    35  	},
    36  	cli.BoolFlag{
    37  		Name:  "force",
    38  		Usage: "force removal of site(s) from site replication configuration",
    39  	},
    40  }
    41  
    42  var adminReplicateRemoveCmd = cli.Command{
    43  	Name:          "remove",
    44  	ShortName:     "rm",
    45  	Usage:         "remove one or more sites from site replication",
    46  	Action:        mainAdminReplicationRemoveStatus,
    47  	OnUsageError:  onUsageError,
    48  	HiddenAliases: true,
    49  	Before:        setGlobalsFromContext,
    50  	Flags:         append(globalFlags, adminReplicateRemoveFlags...),
    51  	CustomHelpTemplate: `NAME:
    52    {{.HelpName}} - {{.Usage}}
    53  
    54  USAGE:
    55    {{.HelpName}} TARGET
    56  
    57  FLAGS:
    58    {{range .VisibleFlags}}{{.}}
    59    {{end}}
    60  
    61  EXAMPLES:
    62    1. Remove site replication for all sites:
    63       {{.Prompt}} {{.HelpName}} minio2 --all --force
    64  
    65    2. Remove site replication for site with site names alpha, baker from active cluster minio2:
    66       {{.Prompt}} {{.HelpName}} minio2 alpha baker --force
    67  `,
    68  }
    69  
    70  type srRemoveStatus struct {
    71  	madmin.ReplicateRemoveStatus
    72  	sites     []string
    73  	RemoveAll bool
    74  }
    75  
    76  func (i srRemoveStatus) JSON() string {
    77  	ds, e := json.MarshalIndent(i.ReplicateRemoveStatus, "", " ")
    78  	fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
    79  	return string(ds)
    80  }
    81  
    82  func (i srRemoveStatus) String() string {
    83  	if i.RemoveAll {
    84  		return console.Colorize("UserMessage", "All site(s) were removed successfully")
    85  	}
    86  	if i.ReplicateRemoveStatus.Status == madmin.ReplicateRemoveStatusSuccess {
    87  		return console.Colorize("UserMessage", fmt.Sprintf("Following site(s) %s were removed successfully", i.sites))
    88  	}
    89  	if len(i.sites) == 1 {
    90  		return console.Colorize("UserMessage", fmt.Sprintf("Following site %s was removed partially, some operations failed:\nERROR: '%s'", i.sites, i.ReplicateRemoveStatus.ErrDetail))
    91  	}
    92  	return console.Colorize("UserMessage", fmt.Sprintf("Following site(s) %s were removed partially, some operations failed: \nERROR: '%s'", i.sites, i.ReplicateRemoveStatus.ErrDetail))
    93  }
    94  
    95  func checkAdminReplicateRemoveSyntax(ctx *cli.Context) {
    96  	// Check argument count
    97  	argsNr := len(ctx.Args())
    98  	if ctx.IsSet("all") && argsNr > 1 {
    99  		fatalIf(errInvalidArgument().Trace(ctx.Args().Tail()...),
   100  			"")
   101  	}
   102  	if argsNr < 2 && !ctx.IsSet("all") {
   103  		fatalIf(errInvalidArgument().Trace(ctx.Args().Tail()...),
   104  			"Need at least two arguments to remove command.")
   105  	}
   106  	if !ctx.IsSet("force") {
   107  		fatalIf(errDummy().Trace(),
   108  			"Site removal requires --force flag. This operation is *IRREVERSIBLE*. Please review carefully before performing this *DANGEROUS* operation.")
   109  	}
   110  }
   111  
   112  func mainAdminReplicationRemoveStatus(ctx *cli.Context) error {
   113  	checkAdminReplicateRemoveSyntax(ctx)
   114  	console.SetColor("UserMessage", color.New(color.FgGreen))
   115  
   116  	// Get the alias parameter from cli
   117  	args := ctx.Args()
   118  	aliasedURL := args.Get(0)
   119  	var rreq madmin.SRRemoveReq
   120  	rreq.SiteNames = append(rreq.SiteNames, args.Tail()...)
   121  	rreq.RemoveAll = ctx.Bool("all")
   122  	// Create a new MinIO Admin Client
   123  	client, err := newAdminClient(aliasedURL)
   124  	fatalIf(err, "Unable to initialize admin connection.")
   125  
   126  	st, e := client.SiteReplicationRemove(globalContext, rreq)
   127  	fatalIf(probe.NewError(e).Trace(args...), "Unable to remove cluster replication")
   128  
   129  	printMsg(srRemoveStatus{
   130  		ReplicateRemoveStatus: st,
   131  		sites:                 args.Tail(),
   132  		RemoveAll:             rreq.RemoveAll,
   133  	})
   134  
   135  	return nil
   136  }