github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/batch-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 "os" 24 25 "github.com/fatih/color" 26 "github.com/minio/cli" 27 json "github.com/minio/colorjson" 28 "github.com/minio/madmin-go/v3" 29 "github.com/minio/mc/pkg/probe" 30 "github.com/minio/pkg/v2/console" 31 ) 32 33 var batchStartCmd = cli.Command{ 34 Name: "start", 35 Usage: "start a new batch job", 36 Action: mainBatchStart, 37 OnUsageError: onUsageError, 38 Before: setGlobalsFromContext, 39 Flags: globalFlags, 40 CustomHelpTemplate: `NAME: 41 {{.HelpName}} - {{.Usage}} 42 43 USAGE: 44 {{.HelpName}} TARGET JOBFILE 45 46 FLAGS: 47 {{range .VisibleFlags}}{{.}} 48 {{end}} 49 EXAMPLES: 50 1. Start a new batch 'replication' job: 51 {{.Prompt}} {{.HelpName}} myminio ./replication.yaml 52 `, 53 } 54 55 // batchStartMessage container for file batchStart messages 56 type batchStartMessage struct { 57 Status string `json:"status"` 58 Result madmin.BatchJobResult `json:"result"` 59 } 60 61 // String colorized batchStart message 62 func (c batchStartMessage) String() string { 63 return console.Colorize("BatchStart", fmt.Sprintf("Successfully started '%s' job `%s` on '%s'", c.Result.Type, c.Result.ID, c.Result.Started)) 64 } 65 66 // JSON jsonified batchStart message 67 func (c batchStartMessage) JSON() string { 68 c.Status = "success" 69 batchStartMessageBytes, e := json.MarshalIndent(c, "", " ") 70 fatalIf(probe.NewError(e), "Unable to marshal into JSON.") 71 72 return string(batchStartMessageBytes) 73 } 74 75 // checkBatchStartSyntax - validate all the passed arguments 76 func checkBatchStartSyntax(ctx *cli.Context) { 77 if len(ctx.Args()) != 2 { 78 showCommandHelpAndExit(ctx, 1) // last argument is exit code 79 } 80 } 81 82 // mainBatchStart is the handle for "mc batch create" command. 83 func mainBatchStart(ctx *cli.Context) error { 84 checkBatchStartSyntax(ctx) 85 86 console.SetColor("BatchStart", color.New(color.FgGreen, color.Bold)) 87 88 // Get the alias parameter from cli 89 args := ctx.Args() 90 aliasedURL := args.Get(0) 91 92 // Start a new MinIO Admin Client 93 adminClient, err := newAdminClient(aliasedURL) 94 fatalIf(err, "Unable to initialize admin connection.") 95 96 buf, e := os.ReadFile(args.Get(1)) 97 fatalIf(probe.NewError(e), "Unable to read %s", args.Get(1)) 98 99 ctxt, cancel := context.WithCancel(globalContext) 100 defer cancel() 101 102 res, e := adminClient.StartBatchJob(ctxt, string(buf)) 103 fatalIf(probe.NewError(e), "Unable to start job") 104 105 printMsg(batchStartMessage{ 106 Status: "success", 107 Result: res, 108 }) 109 return nil 110 }