github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/replicate-reset-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  	"context"
    22  	"fmt"
    23  	"strings"
    24  	"time"
    25  
    26  	"github.com/fatih/color"
    27  	"github.com/minio/cli"
    28  	json "github.com/minio/colorjson"
    29  	"github.com/minio/mc/pkg/probe"
    30  	"github.com/minio/minio-go/v7/pkg/replication"
    31  	"github.com/minio/pkg/v2/console"
    32  )
    33  
    34  var replicateResyncStartFlags = []cli.Flag{
    35  	cli.StringFlag{
    36  		Name:  "older-than",
    37  		Usage: "replicate back objects older than value in duration string (e.g. 7d10h31s)",
    38  	},
    39  	cli.StringFlag{
    40  		Name:  "remote-bucket",
    41  		Usage: "remote bucket ARN",
    42  	},
    43  }
    44  
    45  var replicateResyncStartCmd = cli.Command{
    46  	Name:         "start",
    47  	Usage:        "start replicating back all previously replicated objects",
    48  	Action:       mainReplicateResyncStart,
    49  	OnUsageError: onUsageError,
    50  	Before:       setGlobalsFromContext,
    51  	Flags:        append(globalFlags, replicateResyncStartFlags...),
    52  	CustomHelpTemplate: `NAME:
    53     {{.HelpName}} - {{.Usage}}
    54  
    55  USAGE:
    56     {{.HelpName}} TARGET
    57  
    58  FLAGS:
    59     {{range .VisibleFlags}}{{.}}
    60     {{end}}
    61  EXAMPLES:
    62    1. Re-replicate previously replicated objects in bucket "mybucket" for alias "myminio" for remote target.
    63     {{.Prompt}} {{.HelpName}} myminio/mybucket --remote-bucket "arn:minio:replication::xxx:mybucket"
    64  
    65    2. Re-replicate all objects older than 60 days in bucket "mybucket" for remote bucket target.
    66     {{.Prompt}} {{.HelpName}} myminio/mybucket --older-than 60d --remote-bucket "arn:minio:replication::xxx:mybucket"
    67  `,
    68  }
    69  
    70  // checkReplicateResyncStartSyntax - validate all the passed arguments
    71  func checkReplicateResyncStartSyntax(ctx *cli.Context) {
    72  	if len(ctx.Args()) != 1 {
    73  		showCommandHelpAndExit(ctx, 1) // last argument is exit code
    74  	}
    75  	if ctx.String("remote-bucket") == "" {
    76  		fatal(errDummy().Trace(), "--remote-bucket flag needs to be specified.")
    77  	}
    78  }
    79  
    80  type replicateResyncMessage struct {
    81  	Op                string                        `json:"op"`
    82  	URL               string                        `json:"url"`
    83  	ResyncTargetsInfo replication.ResyncTargetsInfo `json:"resyncInfo"`
    84  	Status            string                        `json:"status"`
    85  	TargetArn         string                        `json:"targetArn"`
    86  }
    87  
    88  func (r replicateResyncMessage) JSON() string {
    89  	r.Status = "success"
    90  	jsonMessageBytes, e := json.MarshalIndent(r, "", " ")
    91  	fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
    92  	return string(jsonMessageBytes)
    93  }
    94  
    95  func (r replicateResyncMessage) String() string {
    96  	if len(r.ResyncTargetsInfo.Targets) == 1 {
    97  		return console.Colorize("replicateResyncMessage", fmt.Sprintf("Replication reset started for %s with ID %s", r.URL, r.ResyncTargetsInfo.Targets[0].ResetID))
    98  	}
    99  	return console.Colorize("replicateResyncMessage", fmt.Sprintf("Replication reset started for %s", r.URL))
   100  }
   101  
   102  func mainReplicateResyncStart(cliCtx *cli.Context) error {
   103  	ctx, cancelReplicateResyncStart := context.WithCancel(globalContext)
   104  	defer cancelReplicateResyncStart()
   105  
   106  	console.SetColor("replicateResyncMessage", color.New(color.FgGreen))
   107  
   108  	checkReplicateResyncStartSyntax(cliCtx)
   109  
   110  	// Get the alias parameter from cli
   111  	args := cliCtx.Args()
   112  	aliasedURL := args.Get(0)
   113  	// Create a new Client
   114  	client, err := newClient(aliasedURL)
   115  	fatalIf(err, "Unable to initialize connection.")
   116  	var olderThanStr string
   117  	var olderThan time.Duration
   118  	if cliCtx.IsSet("older-than") {
   119  		olderThanStr = cliCtx.String("older-than")
   120  		if olderThanStr != "" {
   121  			days, e := ParseDuration(olderThanStr)
   122  			if e != nil || !strings.ContainsAny(olderThanStr, "dwy") {
   123  				fatalIf(probe.NewError(e), "Unable to parse older-than=`"+olderThanStr+"`.")
   124  			}
   125  			if days == 0 {
   126  				fatalIf(probe.NewError(e), "older-than cannot be set to zero")
   127  			}
   128  			olderThan = time.Duration(days.Days())
   129  		}
   130  	}
   131  
   132  	rinfo, err := client.ResetReplication(ctx, olderThan, cliCtx.String("remote-bucket"))
   133  	fatalIf(err.Trace(args...), "Unable to reset replication")
   134  	printMsg(replicateResyncMessage{
   135  		Op:                cliCtx.Command.Name,
   136  		URL:               aliasedURL,
   137  		ResyncTargetsInfo: rinfo,
   138  	})
   139  	return nil
   140  }