github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-replicate-info.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  	"strconv"
    23  	"strings"
    24  
    25  	humanize "github.com/dustin/go-humanize"
    26  	"github.com/fatih/color"
    27  	"github.com/minio/cli"
    28  	json "github.com/minio/colorjson"
    29  	"github.com/minio/madmin-go/v3"
    30  	"github.com/minio/mc/pkg/probe"
    31  	"github.com/minio/pkg/v2/console"
    32  )
    33  
    34  var adminReplicateInfoCmd = cli.Command{
    35  	Name:         "info",
    36  	Usage:        "get site replication information",
    37  	Action:       mainAdminReplicationInfo,
    38  	OnUsageError: onUsageError,
    39  	Before:       setGlobalsFromContext,
    40  	Flags:        globalFlags,
    41  	CustomHelpTemplate: `NAME:
    42    {{.HelpName}} - {{.Usage}}
    43  
    44  USAGE:
    45    {{.HelpName}} ALIAS1
    46  
    47  FLAGS:
    48    {{range .VisibleFlags}}{{.}}
    49    {{end}}
    50  
    51  EXAMPLES:
    52    1. Get Site Replication information:
    53       {{.Prompt}} {{.HelpName}} minio1
    54  `,
    55  }
    56  
    57  type srInfo madmin.SiteReplicationInfo
    58  
    59  func (i srInfo) JSON() string {
    60  	bs, e := json.MarshalIndent(madmin.SiteReplicationInfo(i), "", " ")
    61  	fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
    62  	return string(bs)
    63  }
    64  
    65  func (i srInfo) String() string {
    66  	var messages []string
    67  	info := madmin.SiteReplicationInfo(i)
    68  	if info.Enabled {
    69  		messages = []string{
    70  			"SiteReplication enabled for:\n",
    71  		}
    72  		r := console.Colorize("THeaders", newPrettyTable(" | ",
    73  			Field{"Deployment ID", 36},
    74  
    75  			Field{"Name", 15},
    76  			Field{"Endpoint", 46},
    77  			Field{"Sync", 4},
    78  			Field{"Bandwidth", 10},
    79  			Field{"ILM Expiry Replication", 25},
    80  		).buildRow("Deployment ID", "Site Name", "Endpoint", "Sync", "Bandwidth", "ILM Expiry Replication"))
    81  		messages = append(messages, r)
    82  
    83  		r = console.Colorize("THeaders", newPrettyTable(" | ",
    84  			Field{"Deployment ID", 36},
    85  
    86  			Field{"Name", 15},
    87  			Field{"Endpoint", 46},
    88  			Field{"Sync", 4},
    89  			Field{"Bandwidth", 10},
    90  			Field{"ILM Expiry Replication", 25},
    91  		).buildRow("", "", "", "", "Per Bucket", ""))
    92  		messages = append(messages, r)
    93  		for _, peer := range info.Sites {
    94  			var chk string
    95  			if peer.SyncState == madmin.SyncEnabled {
    96  				chk = check
    97  			}
    98  			limit := "N/A" // N/A means cluster bandwidth is not configured
    99  			if peer.DefaultBandwidth.Limit > 0 {
   100  				limit = humanize.Bytes(uint64(peer.DefaultBandwidth.Limit * 8))
   101  				limit = fmt.Sprintf("%sb/s", limit[:len(limit)-1])
   102  			}
   103  			r := console.Colorize("TDetail", newPrettyTable(" | ",
   104  				Field{"Deployment ID", 36},
   105  				Field{"Name", 15},
   106  				Field{"Endpoint", 46},
   107  				Field{"Sync", 4},
   108  				Field{"Bandwidth", 10},
   109  				Field{"ILM Expiry Replication", 25},
   110  			).buildRow(peer.DeploymentID, peer.Name, peer.Endpoint, chk, limit, strconv.FormatBool(peer.ReplicateILMExpiry)))
   111  			messages = append(messages, r)
   112  		}
   113  	} else {
   114  		messages = []string{"SiteReplication is not enabled"}
   115  	}
   116  
   117  	return console.Colorize("UserMessage", strings.Join(messages, "\n"))
   118  }
   119  
   120  func mainAdminReplicationInfo(ctx *cli.Context) error {
   121  	{
   122  		// Check argument count
   123  		argsNr := len(ctx.Args())
   124  		if argsNr != 1 {
   125  			fatalIf(errInvalidArgument().Trace(ctx.Args().Tail()...),
   126  				"Need exactly one alias argument.")
   127  		}
   128  	}
   129  
   130  	console.SetColor("UserMessage", color.New(color.FgGreen))
   131  	console.SetColor("THeaders", color.New(color.Bold, color.FgHiWhite))
   132  	console.SetColor("TDetail", color.New(color.Bold, color.FgCyan))
   133  
   134  	// Get the alias parameter from cli
   135  	args := ctx.Args()
   136  	aliasedURL := args.Get(0)
   137  
   138  	// Create a new MinIO Admin Client
   139  	client, err := newAdminClient(aliasedURL)
   140  	fatalIf(err, "Unable to initialize admin connection.")
   141  
   142  	info, e := client.SiteReplicationInfo(globalContext)
   143  	fatalIf(probe.NewError(e).Trace(args...), "Unable to get cluster replication information")
   144  
   145  	printMsg(srInfo(info))
   146  
   147  	return nil
   148  }