diff --git a/cmd/auto-complete.go b/cmd/auto-complete.go index c73cdc76..18c23e1e 100644 --- a/cmd/auto-complete.go +++ b/cmd/auto-complete.go @@ -270,6 +270,7 @@ var completeCmds = map[string]complete.Predictor{ "/replicate/rm": s3Complete{deepLevel: 2}, "/replicate/export": s3Complete{deepLevel: 2}, "/replicate/import": s3Complete{deepLevel: 2}, + "/replicate/status": s3Complete{deepLevel: 2}, "/tag/list": s3Completer, "/tag/remove": s3Completer, diff --git a/cmd/client-fs.go b/cmd/client-fs.go index 245e2bb0..d1fac37a 100644 --- a/cmd/client-fs.go +++ b/cmd/client-fs.go @@ -1162,6 +1162,14 @@ func (f *fsClient) RemoveReplication(ctx context.Context) *probe.Error { } +// GetReplicationMetrics - Get replication metrics for a given bucket, not implemented. +func (f *fsClient) GetReplicationMetrics(ctx context.Context) (replication.Metrics, *probe.Error) { + return replication.Metrics{}, probe.NewError(APINotImplemented{ + API: "GetReplicationMetrics", + APIType: "filesystem", + }) +} + // Get encryption info for a bucket, not implemented. func (f *fsClient) GetEncryption(ctx context.Context) (string, string, *probe.Error) { return "", "", probe.NewError(APINotImplemented{ diff --git a/cmd/client-s3.go b/cmd/client-s3.go index 29164f23..5cc1ed2f 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -2463,6 +2463,20 @@ func (c *S3Client) SetReplication(ctx context.Context, cfg *replication.Config, return nil } +// GetReplicationMetrics - Get replication metrics for a given bucket. +func (c *S3Client) GetReplicationMetrics(ctx context.Context) (replication.Metrics, *probe.Error) { + bucket, _ := c.url2BucketAndObject() + if bucket == "" { + return replication.Metrics{}, probe.NewError(BucketNameEmpty{}) + } + + metrics, e := c.api.GetBucketReplicationMetrics(ctx, bucket) + if e != nil { + return replication.Metrics{}, probe.NewError(e) + } + return metrics, nil +} + // GetEncryption - gets bucket encryption info. func (c *S3Client) GetEncryption(ctx context.Context) (algorithm, keyID string, err *probe.Error) { bucket, _ := c.url2BucketAndObject() diff --git a/cmd/client.go b/cmd/client.go index 467869d2..67e4679e 100644 --- a/cmd/client.go +++ b/cmd/client.go @@ -157,6 +157,7 @@ type Client interface { GetReplication(ctx context.Context) (replication.Config, *probe.Error) SetReplication(ctx context.Context, cfg *replication.Config, opts replication.Options) *probe.Error RemoveReplication(ctx context.Context) *probe.Error + GetReplicationMetrics(ctx context.Context) (replication.Metrics, *probe.Error) // Encryption operations GetEncryption(ctx context.Context) (string, string, *probe.Error) SetEncryption(ctx context.Context, algorithm, kmsKeyID string) *probe.Error diff --git a/cmd/replicate-main.go b/cmd/replicate-main.go index db322b2b..b4a0c36d 100644 --- a/cmd/replicate-main.go +++ b/cmd/replicate-main.go @@ -22,6 +22,7 @@ var replicateSubcommands = []cli.Command{ replicateAddCmd, replicateEditCmd, replicateListCmd, + replicateStatusCmd, replicateExportCmd, replicateImportCmd, replicateRemoveCmd, diff --git a/cmd/replicate-status.go b/cmd/replicate-status.go new file mode 100644 index 00000000..fc7ec8a1 --- /dev/null +++ b/cmd/replicate-status.go @@ -0,0 +1,139 @@ +/* + * MinIO Client (C) 2021 MinIO, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cmd + +import ( + "context" + + humanize "github.com/dustin/go-humanize" + "github.com/fatih/color" + "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" + "github.com/minio/mc/pkg/probe" + "github.com/minio/minio-go/v7/pkg/replication" + "github.com/minio/minio/pkg/console" +) + +var replicateStatusCmd = cli.Command{ + Name: "status", + Usage: "show server side replication status", + Action: mainReplicateStatus, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: globalFlags, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} TARGET + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. Get server side replication metrics for bucket "mybucket" for alias "myminio". + {{.Prompt}} {{.HelpName}} myminio/mybucket +`, +} + +// checkReplicateStatusSyntax - validate all the passed arguments +func checkReplicateStatusSyntax(ctx *cli.Context) { + if len(ctx.Args()) != 1 { + cli.ShowCommandHelpAndExit(ctx, "status", 1) // last argument is exit code + } +} + +type replicateStatusMessage struct { + Op string `json:"op"` + URL string `json:"url"` + Status string `json:"status"` + ReplicationStatus replication.Metrics `json:"replicationStatus"` +} + +func (s replicateStatusMessage) JSON() string { + s.Status = "success" + jsonMessageBytes, e := json.MarshalIndent(s, "", " ") + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") + return string(jsonMessageBytes) +} + +func printReplicateStatusHeader() { + if globalJSON { + return + } + maxLen := 15 + console.Println(console.Colorize("Headers", newPrettyTable(" | ", + Field{"Status", 20}, + Field{"Size", maxLen}, + Field{"Count", maxLen}, + ).buildRow("Replication Status", "Size (Bytes)", "Count"))) +} + +func (s replicateStatusMessage) String() string { + maxLen := 15 + var contents = [][]string{ + {"Pending", humanize.IBytes(s.ReplicationStatus.PendingSize), humanize.Comma(int64(s.ReplicationStatus.PendingCount))}, + {"Failed", humanize.IBytes(s.ReplicationStatus.FailedSize), humanize.Comma(int64(s.ReplicationStatus.FailedCount))}, + {"Replicated", humanize.IBytes(s.ReplicationStatus.ReplicatedSize), ""}, + {"Replica", humanize.IBytes(s.ReplicationStatus.ReplicaSize), ""}, + } + var rows string + var theme = []string{"Pending", "Failed", "Replica", "Replica"} + for i, row := range contents { + th := theme[i] + if row[1] == "0 B" && i == 1 { + th = theme[0] + } + r := console.Colorize(th, newPrettyTable(" | ", + Field{"Status", 20}, + Field{"Size", maxLen}, + Field{"Count", maxLen}, + ).buildRow(row[0], row[1], row[2])+"\n") + rows += r + } + return console.Colorize("replicateStatusMessage", rows) +} + +func mainReplicateStatus(cliCtx *cli.Context) error { + ctx, cancelReplicateStatus := context.WithCancel(globalContext) + defer cancelReplicateStatus() + + console.SetColor("Headers", color.New(color.FgGreen)) + console.SetColor("Replica", color.New(color.Bold, color.FgCyan)) + console.SetColor("Failed", color.New(color.Bold, color.FgRed)) + console.SetColor("Pending", color.New(color.Bold, color.FgWhite)) + + checkReplicateStatusSyntax(cliCtx) + + // Get the alias parameter from cli + args := cliCtx.Args() + aliasedURL := args.Get(0) + // Create a new Client + client, err := newClient(aliasedURL) + fatalIf(err, "Unable to initialize connection.") + replicateStatus, err := client.GetReplicationMetrics(ctx) + fatalIf(err.Trace(args...), "Unable to get replication status") + + printReplicateStatusHeader() + printMsg(replicateStatusMessage{ + Op: "status", + URL: aliasedURL, + ReplicationStatus: replicateStatus, + }) + + return nil +} diff --git a/docs/minio-client-complete-guide.md b/docs/minio-client-complete-guide.md index e91252bf..7e0f5718 100644 --- a/docs/minio-client-complete-guide.md +++ b/docs/minio-client-complete-guide.md @@ -1863,3 +1863,8 @@ Replication configuration successfully set on `myminio/mybucket`. ``` mc replicate export myminio/mybucket > /data/replicate/config ``` +*Example: Show replication status of `mybucket` on alias `myminio`* + +``` +mc replicate status myminio/mybucket +``` \ No newline at end of file diff --git a/go.mod b/go.mod index 4d88e416..149b595e 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/mattn/go-runewidth v0.0.9 // indirect github.com/minio/cli v1.22.0 github.com/minio/minio v0.0.0-20210407151610-0a1db6d41be0 - github.com/minio/minio-go/v7 v7.0.11-0.20210319012211-5a0d16291a2e + github.com/minio/minio-go/v7 v7.0.11-0.20210407221404-ba867dba7ee1 github.com/minio/sha256-simd v1.0.0 github.com/mitchellh/go-homedir v1.1.0 github.com/pkg/profile v1.3.0 diff --git a/go.sum b/go.sum index db1aede1..75bcd2d7 100644 --- a/go.sum +++ b/go.sum @@ -338,6 +338,9 @@ github.com/minio/minio v0.0.0-20210407151610-0a1db6d41be0/go.mod h1:XNPkjbDN/j7c github.com/minio/minio-go/v7 v7.0.11-0.20210302210017-6ae69c73ce78/go.mod h1:mTh2uJuAbEqdhMVl6CMIIZLUeiMiWtJR4JB8/5g2skw= github.com/minio/minio-go/v7 v7.0.11-0.20210319012211-5a0d16291a2e h1:IH4CNckd+Z+h5W9ee4QEoRD/fyw9o330PVy9l5Ogd4U= github.com/minio/minio-go/v7 v7.0.11-0.20210319012211-5a0d16291a2e/go.mod h1:mTh2uJuAbEqdhMVl6CMIIZLUeiMiWtJR4JB8/5g2skw= +github.com/minio/minio-go/v7 v7.0.11-0.20210407221404-ba867dba7ee1 h1:x0jnjGGnb0PaewBIH9dbTaLJUNVKYoQOjjdqwz9PwdQ= +github.com/minio/minio-go/v7 v7.0.11-0.20210407221404-ba867dba7ee1/go.mod h1:mTh2uJuAbEqdhMVl6CMIIZLUeiMiWtJR4JB8/5g2skw= +github.com/minio/selfupdate v0.3.1 h1:BWEFSNnrZVMUWXbXIgLDNDjbejkmpAmZvy/nCz1HlEs= github.com/minio/selfupdate v0.3.1/go.mod h1:b8ThJzzH7u2MkF6PcIra7KaXO9Khf6alWPvMSyTDCFM= github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM= github.com/minio/sha256-simd v1.0.0 h1:v1ta+49hkWZyvaKwrQB8elexRqm6Y0aMLjCNsrYxo6g=