github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-replicate-add.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  	"strings"
    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 adminReplicateAddFlags = []cli.Flag{
    32  	cli.BoolFlag{
    33  		Name:  "replicate-ilm-expiry",
    34  		Usage: "replicate ILM expiry rules",
    35  	},
    36  }
    37  
    38  var adminReplicateAddCmd = cli.Command{
    39  	Name:         "add",
    40  	Usage:        "add one or more sites for replication",
    41  	Action:       mainAdminReplicateAdd,
    42  	OnUsageError: onUsageError,
    43  	Before:       setGlobalsFromContext,
    44  	Flags:        append(globalFlags, adminReplicateAddFlags...),
    45  	CustomHelpTemplate: `NAME:
    46    {{.HelpName}} - {{.Usage}}
    47  
    48  USAGE:
    49    {{.HelpName}} ALIAS1 ALIAS2 [ALIAS3...]
    50  
    51  FLAGS:
    52    {{range .VisibleFlags}}{{.}}
    53    {{end}}
    54  
    55  EXAMPLES:
    56    1. Add a site for cluster-level replication:
    57       {{.Prompt}} {{.HelpName}} minio1 minio2
    58  
    59    2. Add a site for cluster-level replication with replication of ILM expiry rules:
    60       {{.Prompt}} {{.HelpName}} minio1 minio2 --replicate-ilm-expiry
    61  `,
    62  }
    63  
    64  type successMessage madmin.ReplicateAddStatus
    65  
    66  func (m successMessage) JSON() string {
    67  	bs, e := json.MarshalIndent(madmin.ReplicateAddStatus(m), "", " ")
    68  	fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
    69  	return string(bs)
    70  }
    71  
    72  func (m successMessage) String() string {
    73  	v := madmin.ReplicateAddStatus(m)
    74  	messages := []string{v.Status}
    75  
    76  	if v.ErrDetail != "" {
    77  		messages = append(messages, v.ErrDetail)
    78  	}
    79  	if v.InitialSyncErrorMessage != "" {
    80  		messages = append(messages, v.InitialSyncErrorMessage)
    81  	}
    82  	return console.Colorize("UserMessage", strings.Join(messages, "\n"))
    83  }
    84  
    85  func mainAdminReplicateAdd(ctx *cli.Context) error {
    86  	{
    87  		// Check argument count
    88  		argsNr := len(ctx.Args())
    89  		if argsNr < 2 {
    90  			fatalIf(errInvalidArgument().Trace(ctx.Args().Tail()...),
    91  				"Need at least two arguments to add command.")
    92  		}
    93  	}
    94  
    95  	console.SetColor("UserMessage", color.New(color.FgGreen))
    96  
    97  	// Get the alias parameter from cli
    98  	args := ctx.Args()
    99  	aliasedURL := args.Get(0)
   100  
   101  	// Create a new MinIO Admin Client
   102  	client, err := newAdminClient(aliasedURL)
   103  	fatalIf(err, "Unable to initialize admin connection.")
   104  
   105  	ps := make([]madmin.PeerSite, 0, len(ctx.Args()))
   106  	for _, clusterName := range ctx.Args() {
   107  		admClient, err := newAdminClient(clusterName)
   108  		fatalIf(err, "unable to initialize admin connection")
   109  
   110  		ak, sk := admClient.GetAccessAndSecretKey()
   111  		ps = append(ps, madmin.PeerSite{
   112  			Name:      clusterName,
   113  			Endpoint:  admClient.GetEndpointURL().String(),
   114  			AccessKey: ak,
   115  			SecretKey: sk,
   116  		})
   117  	}
   118  
   119  	var opts madmin.SRAddOptions
   120  	opts.ReplicateILMExpiry = ctx.Bool("replicate-ilm-expiry")
   121  	res, e := client.SiteReplicationAdd(globalContext, ps, opts)
   122  	fatalIf(probe.NewError(e).Trace(args...), "Unable to add sites for replication")
   123  
   124  	printMsg(successMessage(res))
   125  
   126  	return nil
   127  }