Skip to content

Commit

Permalink
mc admin config set/get/del/history/help
Browse files Browse the repository at this point in the history
Related to minio/minio#8392
  • Loading branch information
harshavardhana committed Oct 29, 2019
1 parent 90a60d3 commit 10a66fd
Show file tree
Hide file tree
Showing 12 changed files with 720 additions and 104 deletions.
131 changes: 131 additions & 0 deletions cmd/admin-config-del.go
@@ -0,0 +1,131 @@
/*
* 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"
"io/ioutil"
"os"
"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. Remove MQTT notifcation target 'target1' on MinIO server.
{{.Prompt}} {{.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",
"Deleting 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 !ctx.Args().Present() {
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 del config API
input := strings.Join(args.Tail(), " ")
if len(input) == 0 {
b, err := ioutil.ReadAll(os.Stdin)
fatalIf(probe.NewError(err), "Cannot set server configuration file.")
input = string(b)
}
fatalIf(probe.NewError(client.DelConfigKV(input)),
"Cannot set server configuration file.")

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

return nil
}
40 changes: 24 additions & 16 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,9 +17,12 @@
package cmd

import (
"strings"

"github.com/minio/cli"
json "github.com/minio/mc/pkg/colorjson"
"github.com/minio/mc/pkg/probe"
"github.com/minio/minio/pkg/madmin"
)

var adminConfigGetCmd = cli.Command{
Expand All @@ -38,23 +41,32 @@ FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Get server configuration of a MinIO server/cluster.
{{.Prompt}} {{.HelpName}} play/
1. Get the current region setting on MinIO server.
{{.Prompt}} {{.HelpName}} play/ region
# US east region setting
name="us-east-1" state="on"
2. Get the current notification settings for MQTT target on MinIO server
{{.Prompt}} {{.HelpName}} myminio/ notify_mqtt
# Notification settings for MQTT broker
notify_mqtt broker="" password="" queue_dir="" queue_limit="0" reconnect_interval="0s" state="off" keep_alive_interval="0s" qos="0" topic="" username=""
3. Get the current compression settings on MinIO server
{{.Prompt}} {{.HelpName}} myminio/ compression
# Compression settings for csv and text files only
compression extensions=".txt,.csv" mime_types="text/*" state="on"
`,
}

// configGetMessage container to hold locks information.
type configGetMessage struct {
Status string `json:"status"`
Config map[string]interface{} `json:"config"`
Status string `json:"status"`
Value madmin.Targets `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.String()
}

// JSON jsonified service status Message message.
Expand All @@ -68,7 +80,7 @@ func (u configGetMessage) JSON() string {

// checkAdminConfigGetSyntax - validate all the passed arguments
func checkAdminConfigGetSyntax(ctx *cli.Context) {
if len(ctx.Args()) == 0 || len(ctx.Args()) > 2 {
if !ctx.Args().Present() || len(ctx.Args()) > 2 {
cli.ShowCommandHelpAndExit(ctx, "get", 1) // last argument is exit code
}
}
Expand All @@ -86,16 +98,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: buf,
})

return nil
Expand Down
112 changes: 112 additions & 0 deletions cmd/admin-config-help.go
@@ -0,0 +1,112 @@
/*
* 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.
* 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 (
"encoding/base64"
"io/ioutil"

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

var helpFlags = []cli.Flag{
cli.BoolFlag{
Name: "env",
Usage: "list all the env only help",
},
}

var adminConfigHelpCmd = cli.Command{
Name: "help",
Usage: "help returns help for each sub-system",
Before: setGlobalsFromContext,
Action: mainAdminConfigHelp,
Flags: append(append([]cli.Flag{}, globalFlags...), helpFlags...),
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} TARGET
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Return help for 'region' settings on MinIO server.
{{.Prompt}} {{.HelpName}} play/ region
2. Return help for 'compression' settings, specifically 'extensions' key on MinIO server.
{{.Prompt}} {{.HelpName}} myminio/ compression extensions
`,
}

// configHelpMessage container to hold locks information.
type configHelpMessage struct {
Status string `json:"status"`
Value string `json:"value"`
}

// String colorized service status message.
func (u configHelpMessage) String() string {
return u.Value
}

// JSON jsonified service status Message message.
func (u configHelpMessage) JSON() string {
u.Status = "success"
u.Value = base64.StdEncoding.EncodeToString([]byte(u.Value))
statusJSONBytes, e := json.MarshalIndent(u, "", " ")
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")

return string(statusJSONBytes)
}

// checkAdminConfigHelpSyntax - validate all the passed arguments
func checkAdminConfigHelpSyntax(ctx *cli.Context) {
if !ctx.Args().Present() || len(ctx.Args()) > 3 {
cli.ShowCommandHelpAndExit(ctx, "help", 1) // last argument is exit code
}
}

func mainAdminConfigHelp(ctx *cli.Context) error {

checkAdminConfigHelpSyntax(ctx)

// Help 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
hr, e := client.HelpConfigKV(args.Get(1), args.Get(2), ctx.IsSet("env"))
fatalIf(probe.NewError(e), "Cannot get help for the sub-system")

buf, e := ioutil.ReadAll(hr)
fatalIf(probe.NewError(e), "Cannot get help for the sub-system")

// Print
printMsg(configHelpMessage{
Value: string(buf),
})

return nil
}

0 comments on commit 10a66fd

Please sign in to comment.