github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-config-reset.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/mc/pkg/probe"
    28  	"github.com/minio/pkg/v2/console"
    29  )
    30  
    31  var adminConfigEnvFlags = []cli.Flag{
    32  	cli.BoolFlag{
    33  		Name:  "env",
    34  		Usage: "list all the env only help",
    35  	},
    36  }
    37  
    38  var adminConfigResetCmd = cli.Command{
    39  	Name:         "reset",
    40  	Usage:        "interactively reset a config key parameters",
    41  	Before:       setGlobalsFromContext,
    42  	Action:       mainAdminConfigReset,
    43  	OnUsageError: onUsageError,
    44  	Flags:        append(adminConfigEnvFlags, globalFlags...),
    45  	CustomHelpTemplate: `NAME:
    46    {{.HelpName}} - {{.Usage}}
    47  
    48  USAGE:
    49    {{.HelpName}} TARGET [CONFIG-KEY...]
    50  
    51  FLAGS:
    52    {{range .VisibleFlags}}{{.}}
    53    {{end}}
    54  EXAMPLES:
    55    1. Reset MQTT notifcation target 'name1' settings to default values.
    56       {{.Prompt}} {{.HelpName}} myminio/ notify_mqtt:name1
    57    2. Reset compression's 'extensions' setting to default value.
    58       {{.Prompt}} {{.HelpName}} myminio/ compression extensions
    59    3. Reset site name and site region to default values.
    60       {{.Prompt}} {{.HelpName}} myminio/ site name region
    61  `,
    62  }
    63  
    64  // configResetMessage container to hold locks information.
    65  type configResetMessage struct {
    66  	Status      string `json:"status"`
    67  	targetAlias string
    68  	key         string
    69  	restart     bool
    70  }
    71  
    72  // String colorized service status message.
    73  func (u configResetMessage) String() (msg string) {
    74  	msg += console.Colorize("ResetConfigSuccess",
    75  		fmt.Sprintf("'%s' is successfully reset.", u.key))
    76  	if u.restart {
    77  		suggestion := fmt.Sprintf("mc admin service restart %s", u.targetAlias)
    78  		msg += console.Colorize("ResetConfigSuccess",
    79  			fmt.Sprintf("\nPlease restart your server with `%s`.", suggestion))
    80  	}
    81  	return
    82  }
    83  
    84  // JSON jsonified service status message.
    85  func (u configResetMessage) JSON() string {
    86  	u.Status = "success"
    87  	statusJSONBytes, e := json.MarshalIndent(u, "", " ")
    88  	fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
    89  
    90  	return string(statusJSONBytes)
    91  }
    92  
    93  // checkAdminConfigResetSyntax - validate all the passed arguments
    94  func checkAdminConfigResetSyntax(ctx *cli.Context) {
    95  	if !ctx.Args().Present() {
    96  		showCommandHelpAndExit(ctx, 1) // last argument is exit code
    97  	}
    98  }
    99  
   100  // main config set function
   101  func mainAdminConfigReset(ctx *cli.Context) error {
   102  	// Check command arguments
   103  	checkAdminConfigResetSyntax(ctx)
   104  
   105  	// Reset color preference of command outputs
   106  	console.SetColor("ResetConfigSuccess", color.New(color.FgGreen, color.Bold))
   107  	console.SetColor("ResetConfigFailure", color.New(color.FgRed, color.Bold))
   108  
   109  	// Get the alias parameter from cli
   110  	args := ctx.Args()
   111  	aliasedURL := args.Get(0)
   112  
   113  	// Create a new MinIO Admin Client
   114  	client, err := newAdminClient(aliasedURL)
   115  	fatalIf(err, "Unable to initialize admin connection.")
   116  
   117  	if len(ctx.Args()) == 1 {
   118  		// Call get config API
   119  		hr, e := client.HelpConfigKV(globalContext, "", "", ctx.IsSet("env"))
   120  		fatalIf(probe.NewError(e), "Unable to get help for the sub-system")
   121  
   122  		// Print
   123  		printMsg(configHelpMessage{
   124  			Value:   hr,
   125  			envOnly: ctx.IsSet("env"),
   126  		})
   127  
   128  		return nil
   129  	}
   130  
   131  	input := strings.Join(args.Tail(), " ")
   132  	// Check if user has attempted to set values
   133  	for _, k := range args.Tail() {
   134  		if strings.Contains(k, "=") {
   135  			e := fmt.Errorf("new settings may not be provided for sub-system keys")
   136  			fatalIf(probe.NewError(e), "Unable to reset '%s' on the server", args.Tail()[0])
   137  		}
   138  	}
   139  
   140  	// Call reset config API
   141  	restart, e := client.DelConfigKV(globalContext, input)
   142  	fatalIf(probe.NewError(e), "Unable to reset '%s' on the server", input)
   143  
   144  	// Print set config result
   145  	printMsg(configResetMessage{
   146  		targetAlias: aliasedURL,
   147  		restart:     restart,
   148  		key:         input,
   149  	})
   150  
   151  	return nil
   152  }