github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-service-stop.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  	"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 adminServiceStopCmd = cli.Command{
    29  	Name:         "stop",
    30  	Usage:        "stop a MinIO cluster",
    31  	Action:       mainAdminServiceStop,
    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. Stop MinIO server represented by its alias 'play'.
    47       {{.Prompt}} {{.HelpName}} play/
    48  `,
    49  }
    50  
    51  // serviceStopMessage is container for make bucket success and failure messages.
    52  type serviceStopMessage struct {
    53  	Status    string `json:"status"`
    54  	ServerURL string `json:"serverURL"`
    55  }
    56  
    57  // String colorized make bucket message.
    58  func (s serviceStopMessage) String() string {
    59  	return console.Colorize("ServiceStop", "Stopped `"+s.ServerURL+"` successfully.")
    60  }
    61  
    62  // JSON jsonified make bucket message.
    63  func (s serviceStopMessage) JSON() string {
    64  	serviceStopJSONBytes, e := json.MarshalIndent(s, "", " ")
    65  	fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
    66  
    67  	return string(serviceStopJSONBytes)
    68  }
    69  
    70  // checkAdminServiceStopSyntax - validate all the passed arguments
    71  func checkAdminServiceStopSyntax(ctx *cli.Context) {
    72  	if len(ctx.Args()) == 0 || len(ctx.Args()) > 2 {
    73  		showCommandHelpAndExit(ctx, 1) // last argument is exit code
    74  	}
    75  }
    76  
    77  func mainAdminServiceStop(ctx *cli.Context) error {
    78  	// Validate serivce stop syntax.
    79  	checkAdminServiceStopSyntax(ctx)
    80  
    81  	// Set color.
    82  	console.SetColor("ServiceStop", color.New(color.FgGreen, color.Bold))
    83  
    84  	// Get the alias parameter from cli
    85  	args := ctx.Args()
    86  	aliasedURL := args.Get(0)
    87  
    88  	client, err := newAdminClient(aliasedURL)
    89  	fatalIf(err, "Unable to initialize admin connection.")
    90  
    91  	// Stop the specified MinIO server
    92  	fatalIf(probe.NewError(client.ServiceStopV2(globalContext)), "Unable to stop the server.")
    93  
    94  	// Success..
    95  	printMsg(serviceStopMessage{Status: "success", ServerURL: aliasedURL})
    96  	return nil
    97  }