diff --git a/cmd/admin-config-export.go b/cmd/admin-config-export.go new file mode 100644 index 00000000..a601d96f --- /dev/null +++ b/cmd/admin-config-export.go @@ -0,0 +1,100 @@ +/* + * 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 ( + "encoding/json" + + "github.com/minio/cli" + "github.com/minio/mc/pkg/probe" + "github.com/minio/minio/pkg/madmin" +) + +var adminConfigExportCmd = cli.Command{ + Name: "export", + Usage: "export config of a MinIO server/cluster", + Before: setGlobalsFromContext, + Action: mainAdminConfigExport, + Flags: globalFlags, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} TARGET + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. Export the current config from MinIO server + {{.Prompt}} {{.HelpName}} play/ > config.txt +`, +} + +// configExportMessage container to hold locks information. +type configExportMessage struct { + Status string `json:"status"` + Value madmin.Targets `json:"value"` +} + +// String colorized service status message. +func (u configExportMessage) String() string { + return u.Value.String() +} + +// JSON jsonified service status Message message. +func (u configExportMessage) JSON() string { + u.Status = "success" + statusJSONBytes, e := json.MarshalIndent(u, "", " ") + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") + + return string(statusJSONBytes) +} + +// checkAdminConfigExportSyntax - validate all the passed arguments +func checkAdminConfigExportSyntax(ctx *cli.Context) { + if !ctx.Args().Present() || len(ctx.Args()) > 1 { + cli.ShowCommandHelpAndExit(ctx, "export", 1) // last argument is exit code + } +} + +func mainAdminConfigExport(ctx *cli.Context) error { + + checkAdminConfigExportSyntax(ctx) + + // Export 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 + buf, e := client.GetConfig() + fatalIf(probe.NewError(e), "Cannot get server config") + + tgts, e := madmin.ParseSubSysTarget(buf) + fatalIf(probe.NewError(e), "Cannot get server config") + + // Print + printMsg(configExportMessage{ + Value: tgts, + }) + + return nil +} diff --git a/cmd/admin-config-import.go b/cmd/admin-config-import.go new file mode 100644 index 00000000..d56834f1 --- /dev/null +++ b/cmd/admin-config-import.go @@ -0,0 +1,107 @@ +/* + * 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 ( + "encoding/json" + "fmt" + "os" + + "github.com/fatih/color" + "github.com/minio/cli" + "github.com/minio/mc/pkg/console" + "github.com/minio/mc/pkg/probe" +) + +var adminConfigImportCmd = cli.Command{ + Name: "import", + Usage: "import a config to MinIO server/cluster", + Before: setGlobalsFromContext, + Action: mainAdminConfigImport, + Flags: globalFlags, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} TARGET + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. Import the new local config and apply to the MinIO server + {{.Prompt}} {{.HelpName}} play/ < config.txt +`, +} + +// configImportMessage container to hold locks information. +type configImportMessage struct { + Status string `json:"status"` + targetAlias string +} + +// String colorized service status message. +func (u configImportMessage) String() (msg string) { + msg += console.Colorize("SetConfigSuccess", + "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)) + return msg +} + +// JSON jsonified service status Message message. +func (u configImportMessage) JSON() string { + u.Status = "success" + statusJSONBytes, e := json.MarshalIndent(u, "", " ") + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") + + return string(statusJSONBytes) +} + +// checkAdminConfigImportSyntax - validate all the passed arguments +func checkAdminConfigImportSyntax(ctx *cli.Context) { + if !ctx.Args().Present() || len(ctx.Args()) > 1 { + cli.ShowCommandHelpAndExit(ctx, "import", 1) // last argument is exit code + } +} + +func mainAdminConfigImport(ctx *cli.Context) error { + + checkAdminConfigImportSyntax(ctx) + + // Set color preference of command outputs + console.SetColor("SetConfigSuccess", color.New(color.FgGreen, color.Bold)) + + // Import 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 set config API + fatalIf(probe.NewError(client.SetConfig(os.Stdin)), "Cannot set server config") + + // Print + printMsg(configImportMessage{ + targetAlias: aliasedURL, + }) + + return nil +} diff --git a/cmd/admin-config.go b/cmd/admin-config.go index a3ddcd05..265eeaec 100644 --- a/cmd/admin-config.go +++ b/cmd/admin-config.go @@ -28,6 +28,8 @@ var adminConfigCmd = cli.Command{ adminConfigSetCmd, adminConfigGetCmd, adminConfigDelCmd, + adminConfigImportCmd, + adminConfigExportCmd, adminConfigHistoryCmd, }, HideHelpCommand: true,