github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-config-set.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  	"fmt"
    22  	"strings"
    23  
    24  	"github.com/fatih/color"
    25  	"github.com/minio/cli"
    26  	json "github.com/minio/colorjson"
    27  	"github.com/minio/madmin-go/v3"
    28  	"github.com/minio/mc/pkg/probe"
    29  	"github.com/minio/pkg/v2/console"
    30  )
    31  
    32  var adminConfigSetCmd = cli.Command{
    33  	Name:         "set",
    34  	Usage:        "interactively set a config key parameters",
    35  	Before:       setGlobalsFromContext,
    36  	Action:       mainAdminConfigSet,
    37  	OnUsageError: onUsageError,
    38  	Flags:        append(adminConfigEnvFlags, globalFlags...),
    39  	CustomHelpTemplate: `NAME:
    40    {{.HelpName}} - {{.Usage}}
    41  
    42  USAGE:
    43    {{.HelpName}} TARGET
    44  
    45  FLAGS:
    46    {{range .VisibleFlags}}{{.}}
    47    {{end}}
    48  EXAMPLES:
    49    1. Enable webhook notification target for MinIO server.
    50       {{.Prompt}} {{.HelpName}} myminio/ notify_webhook endpoint="http://localhost:8080/minio/events"
    51  
    52    2. Change region name for the MinIO server to 'us-west-1'.
    53       {{.Prompt}} {{.HelpName}} myminio/ region name=us-west-1
    54  
    55    3. Change healing settings on a distributed MinIO server setup.
    56       {{.Prompt}} {{.HelpName}} mydist/ heal max_delay=300ms max_io=50
    57  `,
    58  }
    59  
    60  // configSetMessage container to hold locks information.
    61  type configSetMessage struct {
    62  	Status      string `json:"status"`
    63  	targetAlias string
    64  	restart     bool
    65  }
    66  
    67  // String colorized service status message.
    68  func (u configSetMessage) String() (msg string) {
    69  	msg += console.Colorize("SetConfigSuccess",
    70  		"Successfully applied new settings.")
    71  	if u.restart {
    72  		suggestion := color.RedString("mc admin service restart %s", u.targetAlias)
    73  		msg += console.Colorize("SetConfigSuccess",
    74  			fmt.Sprintf("\nPlease restart your server '%s'.", suggestion))
    75  	}
    76  	return
    77  }
    78  
    79  // JSON jsonified service status message.
    80  func (u configSetMessage) JSON() string {
    81  	u.Status = "success"
    82  	statusJSONBytes, e := json.MarshalIndent(u, "", " ")
    83  	fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
    84  
    85  	return string(statusJSONBytes)
    86  }
    87  
    88  // checkAdminConfigSetSyntax - validate all the passed arguments
    89  func checkAdminConfigSetSyntax(ctx *cli.Context) {
    90  	if !ctx.Args().Present() && len(ctx.Args()) < 1 {
    91  		showCommandHelpAndExit(ctx, 1) // last argument is exit code
    92  	}
    93  }
    94  
    95  // main config set function
    96  func mainAdminConfigSet(ctx *cli.Context) error {
    97  	// Check command arguments
    98  	checkAdminConfigSetSyntax(ctx)
    99  
   100  	// Set color preference of command outputs
   101  	console.SetColor("SetConfigSuccess", color.New(color.FgGreen, color.Bold))
   102  
   103  	// Get the alias parameter from cli
   104  	args := ctx.Args()
   105  	aliasedURL := args.Get(0)
   106  
   107  	// Create a new MinIO Admin Client
   108  	client, err := newAdminClient(aliasedURL)
   109  	fatalIf(err, "Unable to initialize admin connection.")
   110  
   111  	input := strings.Join(args.Tail(), " ")
   112  
   113  	if !strings.Contains(input, madmin.KvSeparator) {
   114  		// Call get config API
   115  		hr, e := client.HelpConfigKV(globalContext, args.Get(1), args.Get(2), ctx.IsSet("env"))
   116  		fatalIf(probe.NewError(e), "Unable to get help for the sub-system")
   117  
   118  		// Print
   119  		printMsg(configHelpMessage{
   120  			Value:   hr,
   121  			envOnly: ctx.IsSet("env"),
   122  		})
   123  
   124  		return nil
   125  
   126  	}
   127  
   128  	// Call set config API
   129  	restart, e := client.SetConfigKV(globalContext, input)
   130  	fatalIf(probe.NewError(e), "Unable to set '%s' to server", input)
   131  
   132  	// Print set config result
   133  	printMsg(configSetMessage{
   134  		targetAlias: aliasedURL,
   135  		restart:     restart,
   136  	})
   137  
   138  	return nil
   139  }