github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-service-freeze.go (about) 1 // Copyright (c) 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 "github.com/fatih/color" 22 "github.com/minio/cli" 23 json "github.com/minio/colorjson" 24 "github.com/minio/mc/pkg/probe" 25 "github.com/minio/pkg/v2/console" 26 ) 27 28 var adminServiceFreezeCmd = cli.Command{ 29 Name: "freeze", 30 Usage: "freeze S3 API calls on MinIO cluster", 31 Action: mainAdminServiceFreeze, 32 OnUsageError: onUsageError, 33 Before: setGlobalsFromContext, 34 Flags: globalFlags, 35 Hidden: true, // this command is hidden on purpose, please do not enable it. 36 CustomHelpTemplate: `NAME: 37 {{.HelpName}} - {{.Usage}} 38 39 USAGE: 40 {{.HelpName}} TARGET 41 42 FLAGS: 43 {{range .VisibleFlags}}{{.}} 44 {{end}} 45 EXAMPLES: 46 1. Freeze all S3 API calls on MinIO server at 'myminio/'. 47 {{.Prompt}} {{.HelpName}} myminio/ 48 `, 49 } 50 51 // serviceFreezeCommand is container for service freeze command success and failure messages. 52 type serviceFreezeCommand struct { 53 Status string `json:"status"` 54 ServerURL string `json:"serverURL"` 55 } 56 57 // String colorized service freeze command message. 58 func (s serviceFreezeCommand) String() string { 59 return console.Colorize("ServiceFreeze", "Freeze command successfully sent to `"+s.ServerURL+"`.") 60 } 61 62 // JSON jsonified service freeze command message. 63 func (s serviceFreezeCommand) JSON() string { 64 serviceFreezeJSONBytes, e := json.MarshalIndent(s, "", " ") 65 fatalIf(probe.NewError(e), "Unable to marshal into JSON.") 66 67 return string(serviceFreezeJSONBytes) 68 } 69 70 // checkAdminServiceFreezeSyntax - validate all the passed arguments 71 func checkAdminServiceFreezeSyntax(ctx *cli.Context) { 72 if len(ctx.Args()) != 1 { 73 showCommandHelpAndExit(ctx, 1) // last argument is exit code 74 } 75 } 76 77 func mainAdminServiceFreeze(ctx *cli.Context) error { 78 // Validate serivce freeze syntax. 79 checkAdminServiceFreezeSyntax(ctx) 80 81 // Set color. 82 console.SetColor("ServiceFreeze", color.New(color.FgGreen, color.Bold)) 83 console.SetColor("FailedServiceFreeze", color.New(color.FgRed, color.Bold)) 84 85 // Get the alias parameter from cli 86 args := ctx.Args() 87 aliasedURL := args.Get(0) 88 89 client, err := newAdminClient(aliasedURL) 90 fatalIf(err, "Unable to initialize admin connection.") 91 92 // Freeze the specified MinIO server 93 fatalIf(probe.NewError(client.ServiceFreezeV2(globalContext)), "Unable to freeze the server.") 94 95 // Success.. 96 printMsg(serviceFreezeCommand{Status: "success", ServerURL: aliasedURL}) 97 98 return nil 99 }