github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-replicate-resync-start.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 adminReplicateResyncStartCmd = cli.Command{
    33  	Name:         "start",
    34  	Usage:        "start resync to site",
    35  	Action:       mainAdminReplicateResyncStart,
    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. Resync bucket data from minio1 to minio2
    51       {{.Prompt}} {{.HelpName}} minio1 minio2
    52  `,
    53  }
    54  
    55  type resyncMessage madmin.SRResyncOpStatus
    56  
    57  func (m resyncMessage) 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 resyncMessage) 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 started with ID %s", v.ResyncID))
    72  	}
    73  	return console.Colorize(th, strings.Join(messages, "\n"))
    74  }
    75  
    76  func mainAdminReplicateResyncStart(ctx *cli.Context) error {
    77  	{
    78  		// Check argument count
    79  		argsNr := len(ctx.Args())
    80  		if argsNr != 2 {
    81  			cli.ShowCommandHelpAndExit(ctx, "start", 1) // last argument is exit code
    82  		}
    83  	}
    84  
    85  	console.SetColor("ResyncMessage", color.New(color.FgGreen))
    86  	console.SetColor("ResyncErr", color.New(color.FgRed))
    87  
    88  	// Get the alias parameter from cli
    89  	args := ctx.Args()
    90  
    91  	// Create a new MinIO Admin Client
    92  	client, err := newAdminClient(args.Get(0))
    93  	fatalIf(err, "Unable to initialize admin connection.")
    94  	info, e := client.SiteReplicationInfo(globalContext)
    95  	fatalIf(probe.NewError(e), "Unable to fetch site replication info.")
    96  
    97  	peerClient := getClient(args.Get(1))
    98  	peerAdmInfo, e := peerClient.ServerInfo(globalContext)
    99  	fatalIf(probe.NewError(e), "Unable to fetch server info of the peer.")
   100  
   101  	var peer madmin.PeerInfo
   102  	for _, site := range info.Sites {
   103  		if peerAdmInfo.DeploymentID == site.DeploymentID {
   104  			peer = site
   105  		}
   106  	}
   107  	if peer.DeploymentID == "" {
   108  		fatalIf(errInvalidArgument().Trace(ctx.Args().Tail()...),
   109  			"alias provided is not part of cluster replication.")
   110  	}
   111  	res, e := client.SiteReplicationResyncOp(globalContext, peer, madmin.SiteResyncStart)
   112  	fatalIf(probe.NewError(e).Trace(args...), "Unable to start replication resync")
   113  	printMsg(resyncMessage(res))
   114  
   115  	return nil
   116  }