github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-replicate-resync-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 "fmt" 22 "strings" 23 24 "github.com/fatih/color" 25 "github.com/minio/cli" 26 json "github.com/minio/colorjson" 27 "github.com/minio/madmin-go/v3" 28 "github.com/minio/mc/pkg/probe" 29 "github.com/minio/pkg/v2/console" 30 ) 31 32 var adminReplicateResyncCancelCmd = cli.Command{ 33 Name: "cancel", 34 Usage: "cancel ongoing resync operation", 35 Action: mainAdminReplicateResyncCancel, 36 OnUsageError: onUsageError, 37 Before: setGlobalsFromContext, 38 Flags: globalFlags, 39 CustomHelpTemplate: `NAME: 40 {{.HelpName}} - {{.Usage}} 41 42 USAGE: 43 {{.HelpName}} ALIAS1 ALIAS2 44 45 FLAGS: 46 {{range .VisibleFlags}}{{.}} 47 {{end}} 48 49 EXAMPLES: 50 1. Cancel ongoing resync of bucket data from minio1 to minio2 51 {{.Prompt}} {{.HelpName}} minio1 minio2 52 `, 53 } 54 55 type resyncCancelMessage madmin.SRResyncOpStatus 56 57 func (m resyncCancelMessage) JSON() string { 58 bs, e := json.MarshalIndent(madmin.SRResyncOpStatus(m), "", " ") 59 fatalIf(probe.NewError(e), "Unable to marshal into JSON.") 60 return string(bs) 61 } 62 63 func (m resyncCancelMessage) String() string { 64 v := madmin.SRResyncOpStatus(m) 65 messages := []string{} 66 th := "ResyncMessage" 67 if v.ErrDetail != "" { 68 messages = append(messages, v.ErrDetail) 69 th = "ResyncErr" 70 } else { 71 messages = append(messages, fmt.Sprintf("Site resync with ID %s canceled successfully.", v.ResyncID)) 72 } 73 return console.Colorize(th, strings.Join(messages, "\n")) 74 } 75 76 func mainAdminReplicateResyncCancel(ctx *cli.Context) error { 77 { 78 // Check argument count 79 argsNr := len(ctx.Args()) 80 if argsNr != 2 { 81 cli.ShowCommandHelpAndExit(ctx, "cancel", 1) // last argument is exit code 82 } 83 } 84 console.SetColor("ResyncMessage", color.New(color.FgGreen)) 85 86 // Get the alias parameter from cli 87 args := ctx.Args() 88 aliasedURL := args.Get(0) 89 90 // Create a new MinIO Admin Client 91 client, err := newAdminClient(aliasedURL) 92 fatalIf(err, "Unable to initialize admin connection.") 93 info, e := client.SiteReplicationInfo(globalContext) 94 fatalIf(probe.NewError(e), "Unable to fetch site replication info.") 95 96 peerClient := getClient(args.Get(1)) 97 peerAdmInfo, e := peerClient.ServerInfo(globalContext) 98 fatalIf(probe.NewError(e), "Unable to fetch server info of the peer.") 99 100 var peer madmin.PeerInfo 101 for _, site := range info.Sites { 102 if peerAdmInfo.DeploymentID == site.DeploymentID { 103 peer = site 104 } 105 } 106 if peer.DeploymentID == "" { 107 fatalIf(errInvalidArgument().Trace(ctx.Args().Tail()...), 108 "alias provided is not part of cluster replication.") 109 } 110 res, e := client.SiteReplicationResyncOp(globalContext, peer, madmin.SiteResyncCancel) 111 fatalIf(probe.NewError(e).Trace(args...), "Unable to cancel replication resync") 112 113 printMsg(resyncCancelMessage(res)) 114 115 return nil 116 }