Skip to content

Commit

Permalink
mc admin config set/get/del
Browse files Browse the repository at this point in the history
Related to minio/minio#8392
  • Loading branch information
harshavardhana committed Oct 15, 2019
1 parent 990a7b6 commit 77d12f4
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 452 deletions.
123 changes: 123 additions & 0 deletions cmd/admin-config-del.go
@@ -0,0 +1,123 @@
/*
* MinIO Client (C) 2019 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cmd

import (
"fmt"
"strings"

"github.com/fatih/color"
"github.com/minio/cli"
json "github.com/minio/mc/pkg/colorjson"
"github.com/minio/mc/pkg/console"
"github.com/minio/mc/pkg/probe"
)

var adminConfigDelCmd = cli.Command{
Name: "del",
Usage: "delete a key from MinIO server/cluster.",
Before: setGlobalsFromContext,
Action: mainAdminConfigDel,
Flags: globalFlags,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} TARGET
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Enable worm mode on MinIO server/cluster.
$ {{.HelpName}} myminio/ notify_mqtt:target1
`,
}

// configDelMessage container to hold locks information.
type configDelMessage struct {
Status string `json:"status"`
delConfigStatus bool
targetAlias string
}

// String colorized service status message.
func (u configDelMessage) String() (msg string) {
// Print the general set config status
if u.delConfigStatus {
msg += console.Colorize("DelConfigSuccess",
"Deleting key has been successful.\n")
suggestion := fmt.Sprintf("mc admin service restart %s", u.targetAlias)
msg += console.Colorize("DelConfigSuccess",
fmt.Sprintf("Please restart your server with `%s`.\n", suggestion))
} else {
msg += console.Colorize("DelConfigFailure",
"Delting new MinIO configuration file has failed.\n")
}
return
}

// JSON jsonified service status message.
func (u configDelMessage) JSON() string {
if u.delConfigStatus {
u.Status = "success"
} else {
u.Status = "error"
}
statusJSONBytes, e := json.MarshalIndent(u, "", " ")
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")

return string(statusJSONBytes)
}

// checkAdminConfigDelSyntax - validate all the passed arguments
func checkAdminConfigDelSyntax(ctx *cli.Context) {
if len(ctx.Args()) == 0 {
cli.ShowCommandHelpAndExit(ctx, "del", 1) // last argument is exit code
}
}

// main config set function
func mainAdminConfigDel(ctx *cli.Context) error {

// Check command arguments
checkAdminConfigDelSyntax(ctx)

// Del color preference of command outputs
console.SetColor("DelConfigSuccess", color.New(color.FgGreen, color.Bold))
console.SetColor("DelConfigFailure", color.New(color.FgRed, color.Bold))

// Get the alias parameter from cli
args := ctx.Args()
aliasedURL := args.Get(0)

// Create a new MinIO Admin Client
client, err := newAdminClient(aliasedURL)
fatalIf(err, "Unable to initialize admin connection.")

// Call get config API
fatalIf(probe.NewError(client.DelConfigKV(strings.Join(args.Tail(), " "))),
"Cannot set server configuration file.")

// Print set config result
printMsg(configDelMessage{
delConfigStatus: true,
targetAlias: aliasedURL,
})

return nil
}
26 changes: 11 additions & 15 deletions cmd/admin-config-get.go
@@ -1,5 +1,5 @@
/*
* MinIO Client (C) 2017 MinIO, Inc.
* MinIO Client (C) 2017-2019 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,8 @@
package cmd

import (
"strings"

"github.com/minio/cli"
json "github.com/minio/mc/pkg/colorjson"
"github.com/minio/mc/pkg/probe"
Expand All @@ -38,23 +40,21 @@ FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Get server configuration of a MinIO server/cluster.
{{.Prompt}} {{.HelpName}} play/
1. Get the current status of 'worm' mode on MinIO server/cluster.
$ {{.HelpName}} play/ worm
state="off"
`,
}

// configGetMessage container to hold locks information.
type configGetMessage struct {
Status string `json:"status"`
Config map[string]interface{} `json:"config"`
Status string `json:"status"`
Value string `json:"value"`
}

// String colorized service status message.
func (u configGetMessage) String() string {
config, e := json.MarshalIndent(u.Config, "", " ")
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")

return string(config)
return u.Value
}

// JSON jsonified service status Message message.
Expand Down Expand Up @@ -86,16 +86,12 @@ func mainAdminConfigGet(ctx *cli.Context) error {
fatalIf(err, "Unable to initialize admin connection.")

// Call get config API
c, e := client.GetConfig()
buf, e := client.GetConfigKV(strings.Join(args.Tail(), " "))
fatalIf(probe.NewError(e), "Cannot get server configuration file.")

config := map[string]interface{}{}
e = json.Unmarshal(c, &config)
fatalIf(probe.NewError(e), "Cannot unmarshal server configuration file.")

// Print
printMsg(configGetMessage{
Config: config,
Value: string(buf),
})

return nil
Expand Down
17 changes: 9 additions & 8 deletions cmd/admin-config-set.go
@@ -1,5 +1,5 @@
/*
* MinIO Client (C) 2017 MinIO, Inc.
* MinIO Client (C) 2017-2019 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,7 @@ package cmd

import (
"fmt"
"os"
"strings"

"github.com/fatih/color"
"github.com/minio/cli"
Expand All @@ -29,7 +29,7 @@ import (

var adminConfigSetCmd = cli.Command{
Name: "set",
Usage: "set new config file to a MinIO server/cluster.",
Usage: "set key to MinIO server/cluster.",
Before: setGlobalsFromContext,
Action: mainAdminConfigSet,
Flags: globalFlags,
Expand All @@ -43,8 +43,8 @@ FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Set server configuration of a MinIO server/cluster.
{{.Prompt}} cat myconfig | {{.HelpName}} myminio/
1. Enable worm mode on MinIO server/cluster.
$ {{.HelpName}} myminio/ worm state="on"
`,
}

Expand All @@ -60,7 +60,7 @@ func (u configSetMessage) String() (msg string) {
// Print the general set config status
if u.setConfigStatus {
msg += console.Colorize("SetConfigSuccess",
"Setting new MinIO configuration file has been successful.\n")
"Setting new key has been successful.\n")
suggestion := fmt.Sprintf("mc admin service restart %s", u.targetAlias)
msg += console.Colorize("SetConfigSuccess",
fmt.Sprintf("Please restart your server with `%s`.\n", suggestion))
Expand All @@ -86,7 +86,7 @@ func (u configSetMessage) JSON() string {

// checkAdminConfigSetSyntax - validate all the passed arguments
func checkAdminConfigSetSyntax(ctx *cli.Context) {
if len(ctx.Args()) == 0 || len(ctx.Args()) > 2 {
if len(ctx.Args()) == 0 {
cli.ShowCommandHelpAndExit(ctx, "set", 1) // last argument is exit code
}
}
Expand All @@ -110,7 +110,8 @@ func mainAdminConfigSet(ctx *cli.Context) error {
fatalIf(err, "Unable to initialize admin connection.")

// Call get config API
fatalIf(probe.NewError(client.SetConfig(os.Stdin)), "Cannot set server configuration file.")
fatalIf(probe.NewError(client.SetConfigKV(strings.Join(args.Tail(), " "))),
"Cannot set server configuration file.")

// Print set config result
printMsg(configSetMessage{
Expand Down
5 changes: 3 additions & 2 deletions cmd/admin-config.go
Expand Up @@ -20,13 +20,14 @@ import "github.com/minio/cli"

var adminConfigCmd = cli.Command{
Name: "config",
Usage: "manage configuration file",
Usage: "manage MinIO configuration",
Action: mainAdminConfig,
Before: setGlobalsFromContext,
Flags: globalFlags,
Subcommands: []cli.Command{
adminConfigGetCmd,
adminConfigSetCmd,
adminConfigGetCmd,
adminConfigDelCmd,
},
HideHelpCommand: true,
}
Expand Down
22 changes: 22 additions & 0 deletions go.mod
Expand Up @@ -4,12 +4,23 @@ go 1.13

require (
github.com/cheggaaa/pb v1.0.28
github.com/coreos/bbolt v1.3.3 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/dustin/go-humanize v1.0.0
github.com/fatih/color v1.7.0
github.com/golang/groupcache v0.0.0-20191002201903-404acd9df4cc // indirect
github.com/gopherjs/gopherjs v0.0.0-20190328170749-bb2674552d8f // indirect
github.com/gorilla/websocket v1.4.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf
github.com/jonboulle/clockwork v0.1.0 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/mattn/go-colorable v0.1.1
github.com/mattn/go-isatty v0.0.7
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/minio/cli v1.22.0
github.com/minio/minio v0.0.0-20191012020709-c33bae057f2f
github.com/minio/minio-go/v6 v6.0.39
Expand All @@ -19,11 +30,22 @@ require (
github.com/pkg/xattr v0.4.1
github.com/posener/complete v1.2.2-0.20190702141536-6ffe496ea953
github.com/rjeczalik/notify v0.9.2
github.com/smartystreets/assertions v0.0.0-20190401211740-f487f9de1cd3 // indirect
github.com/soheilhy/cmux v0.1.4 // indirect
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 // indirect
github.com/ugorji/go v1.1.5-pre // indirect
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
go.etcd.io/bbolt v1.3.3 // indirect
go.uber.org/multierr v1.1.0 // indirect
go.uber.org/zap v1.10.0 // indirect
golang.org/x/net v0.0.0-20190923162816-aa69164e4478
golang.org/x/text v0.3.2
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127
gopkg.in/cheggaaa/pb.v1 v1.0.28 // indirect
gopkg.in/h2non/filetype.v1 v1.0.5
gopkg.in/yaml.v2 v2.2.2
)

replace github.com/gorilla/rpc v1.2.0+incompatible => github.com/gorilla/rpc v1.2.0

replace github.com/minio/minio => ../minio

0 comments on commit 77d12f4

Please sign in to comment.