diff --git a/cmd/auto-complete.go b/cmd/auto-complete.go index 0c67d7c6..13f50ff8 100644 --- a/cmd/auto-complete.go +++ b/cmd/auto-complete.go @@ -499,9 +499,11 @@ var completeCmds = map[string]complete.Predictor{ "/batch/list": aliasCompleter, "/batch/status": aliasCompleter, "/batch/describe": aliasCompleter, - "/quota/set": aliasCompleter, - "/quota/info": aliasCompleter, - "/quota/clear": aliasCompleter, + "/batch/cancel": aliasCompleter, + + "/quota/set": aliasCompleter, + "/quota/info": aliasCompleter, + "/quota/clear": aliasCompleter, } // flagsToCompleteFlags transforms a cli.Flag to complete.Flags diff --git a/cmd/batch-cancel.go b/cmd/batch-cancel.go new file mode 100644 index 00000000..9596c7a2 --- /dev/null +++ b/cmd/batch-cancel.go @@ -0,0 +1,111 @@ +// Copyright (c) 2015-2022 MinIO, Inc. +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package cmd + +import ( + "context" + "fmt" + + "github.com/fatih/color" + "github.com/minio/cli" + json "github.com/minio/colorjson" + "github.com/minio/mc/pkg/probe" + "github.com/minio/pkg/console" +) + +var batchCancelFlags = []cli.Flag{ + cli.StringFlag{ + Name: "id", + Usage: "job id", + }, +} + +var batchCancelCmd = cli.Command{ + Name: "cancel", + Usage: "cancel ongoing batch job", + Action: mainBatchCancel, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: append(batchCancelFlags, globalFlags...), + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} TARGET JOBFILE + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. Cancel ongoing batch job: + {{.Prompt}} {{.HelpName}} myminio +`, +} + +// batchCancelMessage container for file batchCancel messages +type batchCancelMessage struct { + Status string `json:"status"` + JobID string `json:"job-id"` +} + +// String colorized batchCancel message +func (c batchCancelMessage) String() string { + return console.Colorize("batchCancel", fmt.Sprintf("Successfully canceled batch job `%s`", c.JobID)) +} + +// JSON jsonified batchCancel message +func (c batchCancelMessage) JSON() string { + c.Status = "success" + batchCancelMessageBytes, e := json.MarshalIndent(c, "", " ") + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") + + return string(batchCancelMessageBytes) +} + +// checkBatchCancelSyntax - validate all the passed arguments +func checkBatchCancelSyntax(ctx *cli.Context) { + if len(ctx.Args()) != 2 { + showCommandHelpAndExit(ctx, 1) // last argument is exit code + } +} + +// mainBatchCancel is the handle for "mc batch cancel" command. +func mainBatchCancel(ctx *cli.Context) error { + checkBatchCancelSyntax(ctx) + + console.SetColor("BatchCancel", color.New(color.FgGreen, color.Bold)) + + // Get the alias parameter from cli + args := ctx.Args() + aliasedURL := args.Get(0) + jobID := args.Get(1) + // Start a new MinIO Admin Client + adminClient, err := newAdminClient(aliasedURL) + fatalIf(err, "Unable to initialize admin connection.") + ctxt, cancel := context.WithCancel(globalContext) + defer cancel() + + e := adminClient.CancelBatchJob(ctxt, jobID) + fatalIf(probe.NewError(e), "Unable to cancel job") + + printMsg(batchCancelMessage{ + Status: "Canceled", + JobID: jobID, + }) + return nil +} diff --git a/cmd/batch-main.go b/cmd/batch-main.go index d70f8d24..486d1967 100644 --- a/cmd/batch-main.go +++ b/cmd/batch-main.go @@ -26,7 +26,7 @@ var batchSubcommands = []cli.Command{ batchStatusCmd, batchDescribeCmd, // batchSuspendResumeCmd, - // batchStopCmd, + batchCancelCmd, } var batchCmd = cli.Command{ diff --git a/go.mod b/go.mod index 147e9cf8..45c1ba2d 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,7 @@ require ( github.com/minio/cli v1.24.2 github.com/minio/colorjson v1.0.4 github.com/minio/filepath v1.0.0 - github.com/minio/madmin-go/v2 v2.0.15 + github.com/minio/madmin-go/v2 v2.0.17-0.20230318001302-4f013bfc73a4 github.com/minio/md5-simd v1.1.2 // indirect github.com/minio/minio-go/v7 v7.0.49 github.com/minio/pkg v1.6.3 diff --git a/go.sum b/go.sum index e0b2a1a1..bf6bf0e3 100644 --- a/go.sum +++ b/go.sum @@ -517,6 +517,8 @@ github.com/minio/filepath v1.0.0/go.mod h1:/nRZA2ldl5z6jT9/KQuvZcQlxZIMQoFFQPvEX github.com/minio/madmin-go v1.6.6/go.mod h1:ATvkBOLiP3av4D++2v1UEHC/QzsGtgXD5kYvvRYzdKs= github.com/minio/madmin-go/v2 v2.0.15 h1:da/wvVryJXDpkE/gck+tsHfmd9XInaWvJmDKp/sBwuk= github.com/minio/madmin-go/v2 v2.0.15/go.mod h1:8bL1RMNkblIENFSgGYjeHrzUx9PxROb7OqfNuMU9ivE= +github.com/minio/madmin-go/v2 v2.0.17-0.20230318001302-4f013bfc73a4 h1:p9HPxBaX4u/VjpsqY04i9S8K4rmcskOyqK3vcxh11I8= +github.com/minio/madmin-go/v2 v2.0.17-0.20230318001302-4f013bfc73a4/go.mod h1:8bL1RMNkblIENFSgGYjeHrzUx9PxROb7OqfNuMU9ivE= github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34= github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM= github.com/minio/minio-go/v7 v7.0.41/go.mod h1:nCrRzjoSUQh8hgKKtu3Y708OLvRLtuASMg2/nvmbarw=