github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-config-restore.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  
    23  	"github.com/fatih/color"
    24  	"github.com/minio/cli"
    25  	json "github.com/minio/colorjson"
    26  	"github.com/minio/mc/pkg/probe"
    27  	"github.com/minio/pkg/v2/console"
    28  )
    29  
    30  var adminConfigRestoreCmd = cli.Command{
    31  	Name:         "restore",
    32  	Usage:        "rollback back changes to a specific config history",
    33  	Before:       setGlobalsFromContext,
    34  	Action:       mainAdminConfigRestore,
    35  	OnUsageError: onUsageError,
    36  	Flags:        globalFlags,
    37  	CustomHelpTemplate: `NAME:
    38    {{.HelpName}} - {{.Usage}}
    39  
    40  USAGE:
    41    {{.HelpName}} TARGET RESTOREID
    42  
    43  FLAGS:
    44    {{range .VisibleFlags}}{{.}}
    45    {{end}}
    46  EXAMPLES:
    47    1. Restore 'restore-id' history key value on MinIO server.
    48       {{.Prompt}} {{.HelpName}} play/ <restore-id>
    49  `,
    50  }
    51  
    52  // configRestoreMessage container to hold locks information.
    53  type configRestoreMessage struct {
    54  	Status      string `json:"status"`
    55  	RestoreID   string `json:"restoreID"`
    56  	targetAlias string
    57  }
    58  
    59  // String colorized service status message.
    60  func (u configRestoreMessage) String() (msg string) {
    61  	suggestion := fmt.Sprintf("mc admin service restart %s", u.targetAlias)
    62  	msg += console.Colorize("ConfigRestoreMessage",
    63  		fmt.Sprintf("Please restart your server with `%s`.\n", suggestion))
    64  	msg += console.Colorize("ConfigRestoreMessage", "Restored "+u.RestoreID+" kv successfully.")
    65  	return msg
    66  }
    67  
    68  // JSON jsonified service status Message message.
    69  func (u configRestoreMessage) JSON() string {
    70  	u.Status = "success"
    71  	statusJSONBytes, e := json.MarshalIndent(u, "", " ")
    72  	fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
    73  
    74  	return string(statusJSONBytes)
    75  }
    76  
    77  // checkAdminConfigRestoreSyntax - validate all the passed arguments
    78  func checkAdminConfigRestoreSyntax(ctx *cli.Context) {
    79  	if !ctx.Args().Present() || len(ctx.Args()) > 2 {
    80  		showCommandHelpAndExit(ctx, 1) // last argument is exit code
    81  	}
    82  }
    83  
    84  func mainAdminConfigRestore(ctx *cli.Context) error {
    85  	checkAdminConfigRestoreSyntax(ctx)
    86  
    87  	console.SetColor("ConfigRestoreMessage", color.New(color.FgGreen))
    88  
    89  	// Get the alias parameter from cli
    90  	args := ctx.Args()
    91  	aliasedURL := args.Get(0)
    92  
    93  	// Create a new MinIO Admin Client
    94  	client, err := newAdminClient(aliasedURL)
    95  	fatalIf(err, "Unable to initialize admin connection.")
    96  
    97  	// Call get config API
    98  	fatalIf(probe.NewError(client.RestoreConfigHistoryKV(globalContext, args.Get(1))), "Unable to restore server configuration.")
    99  
   100  	// Print
   101  	printMsg(configRestoreMessage{
   102  		RestoreID:   args.Get(1),
   103  		targetAlias: aliasedURL,
   104  	})
   105  
   106  	return nil
   107  }