From 3dfe254a1112384c336d14734f2cb23366d92ab3 Mon Sep 17 00:00:00 2001 From: Bala FA Date: Fri, 9 Jun 2017 11:58:45 +0530 Subject: [PATCH] gateway: make each backend as subcommands. (#4506) Fixes #4450 --- cmd/gateway-main.go | 123 +++++++++++++++++++++++++++---------- cmd/gateway-startup-msg.go | 2 +- cmd/main.go | 8 ++- 3 files changed, 95 insertions(+), 38 deletions(-) diff --git a/cmd/gateway-main.go b/cmd/gateway-main.go index 82fc77711..adb2bf9d6 100644 --- a/cmd/gateway-main.go +++ b/cmd/gateway-main.go @@ -28,23 +28,22 @@ import ( "github.com/minio/cli" ) -var gatewayTemplate = `NAME: +const azureGatewayTemplate = `NAME: {{.HelpName}} - {{.Usage}} USAGE: - {{.HelpName}} {{if .VisibleFlags}}[FLAGS]{{end}} BACKEND [ENDPOINT] + {{.HelpName}} {{if .VisibleFlags}}[FLAGS]{{end}} [ENDPOINT] {{if .VisibleFlags}} FLAGS: {{range .VisibleFlags}}{{.}} {{end}}{{end}} -BACKEND: - azure: Microsoft Azure Blob Storage. Default ENDPOINT is https://core.windows.net - s3: Amazon Simple Storage Service (S3). Default ENDPOINT is https://s3.amazonaws.com +ENDPOINT: + Azure server endpoint. Default ENDPOINT is https://core.windows.net ENVIRONMENT VARIABLES: ACCESS: - MINIO_ACCESS_KEY: Username or access key of your storage backend. - MINIO_SECRET_KEY: Password or secret key of your storage backend. + MINIO_ACCESS_KEY: Username or access key of Azure storage. + MINIO_SECRET_KEY: Password or secret key of Azure storage. BROWSER: MINIO_BROWSER: To disable web browser access, set this value to "off". @@ -53,24 +52,18 @@ EXAMPLES: 1. Start minio gateway server for Azure Blob Storage backend. $ export MINIO_ACCESS_KEY=azureaccountname $ export MINIO_SECRET_KEY=azureaccountkey - $ {{.HelpName}} azure - - 2. Start minio gateway server for AWS S3 backend. - $ export MINIO_ACCESS_KEY=accesskey - $ export MINIO_SECRET_KEY=secretkey - $ {{.HelpName}} s3 - - 3. Start minio gateway server for S3 backend on custom endpoint. - $ export MINIO_ACCESS_KEY=Q3AM3UQ867SPQQA43P2F - $ export MINIO_SECRET_KEY=zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG - $ {{.HelpName}} s3 https://play.minio.io:9000 + $ {{.HelpName}} + 2. Start minio gateway server for Azure Blob Storage backend on custom endpoint. + $ export MINIO_ACCESS_KEY=azureaccountname + $ export MINIO_SECRET_KEY=azureaccountkey + $ {{.HelpName}} https://azure.example.com ` -var gatewayCmd = cli.Command{ - Name: "gateway", - Usage: "Start object storage gateway.", - Action: gatewayMain, - CustomHelpTemplate: gatewayTemplate, +var azureBackendCmd = cli.Command{ + Name: "azure", + Usage: "Microsoft Azure Blob Storage.", + Action: azureGatewayMain, + CustomHelpTemplate: azureGatewayTemplate, Flags: append(serverFlags, cli.BoolFlag{ Name: "quiet", @@ -80,6 +73,59 @@ var gatewayCmd = cli.Command{ HideHelpCommand: true, } +const s3GatewayTemplate = `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} {{if .VisibleFlags}}[FLAGS]{{end}} [ENDPOINT] +{{if .VisibleFlags}} +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}}{{end}} +ENDPOINT: + S3 server endpoint. Default ENDPOINT is https://s3.amazonaws.com + +ENVIRONMENT VARIABLES: + ACCESS: + MINIO_ACCESS_KEY: Username or access key of S3 storage. + MINIO_SECRET_KEY: Password or secret key of S3 storage. + + BROWSER: + MINIO_BROWSER: To disable web browser access, set this value to "off". + +EXAMPLES: + 1. Start minio gateway server for AWS S3 backend. + $ export MINIO_ACCESS_KEY=accesskey + $ export MINIO_SECRET_KEY=secretkey + $ {{.HelpName}} + + 2. Start minio gateway server for S3 backend on custom endpoint. + $ export MINIO_ACCESS_KEY=Q3AM3UQ867SPQQA43P2F + $ export MINIO_SECRET_KEY=zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG + $ {{.HelpName}} https://play.minio.io:9000 +` + +var s3BackendCmd = cli.Command{ + Name: "s3", + Usage: "Amazon Simple Storage Service (S3).", + Action: s3GatewayMain, + CustomHelpTemplate: s3GatewayTemplate, + Flags: append(serverFlags, + cli.BoolFlag{ + Name: "quiet", + Usage: "Disable startup banner.", + }, + ), + HideHelpCommand: true, +} + +var gatewayCmd = cli.Command{ + Name: "gateway", + Usage: "Start object storage gateway.", + HideHelpCommand: true, + Subcommands: []cli.Command{azureBackendCmd, s3BackendCmd}, +} + // Represents the type of the gateway backend. type gatewayBackend string @@ -120,7 +166,7 @@ func mustSetBrowserSettingFromEnv() { // // - Azure Blob Storage. // - Add your favorite backend here. -func newGatewayLayer(backendType, endpoint, accessKey, secretKey string, secure bool) (GatewayLayer, error) { +func newGatewayLayer(backendType gatewayBackend, endpoint, accessKey, secretKey string, secure bool) (GatewayLayer, error) { switch gatewayBackend(backendType) { case azureBackend: @@ -212,12 +258,26 @@ func validateGatewayArguments(serverAddr, endpointAddr string) error { return nil } -// Handler for 'minio gateway'. -func gatewayMain(ctx *cli.Context) { - if !ctx.Args().Present() || ctx.Args().First() == "help" { - cli.ShowCommandHelpAndExit(ctx, "gateway", 1) +// Handler for 'minio gateway azure' command line. +func azureGatewayMain(ctx *cli.Context) { + if ctx.Args().Present() && ctx.Args().First() == "help" { + cli.ShowCommandHelpAndExit(ctx, "azure", 1) } + gatewayMain(ctx, azureBackend) +} + +// Handler for 'minio gateway s3' command line. +func s3GatewayMain(ctx *cli.Context) { + if ctx.Args().Present() && ctx.Args().First() == "help" { + cli.ShowCommandHelpAndExit(ctx, "s3", 1) + } + + gatewayMain(ctx, s3Backend) +} + +// Handler for 'minio gateway'. +func gatewayMain(ctx *cli.Context, backendType gatewayBackend) { // Fetch access and secret key from env. accessKey, secretKey := mustGetGatewayCredsFromEnv() @@ -234,13 +294,8 @@ func gatewayMain(ctx *cli.Context) { log.EnableQuiet() } - // First argument is selected backend type. - backendType := ctx.Args().Get(0) - // Second argument is the endpoint address (optional) - endpointAddr := ctx.Args().Get(1) - serverAddr := ctx.String("address") - + endpointAddr := ctx.Args().Get(0) err := validateGatewayArguments(serverAddr, endpointAddr) fatalIf(err, "Invalid argument") diff --git a/cmd/gateway-startup-msg.go b/cmd/gateway-startup-msg.go index 31641a02d..b89a753f3 100644 --- a/cmd/gateway-startup-msg.go +++ b/cmd/gateway-startup-msg.go @@ -23,7 +23,7 @@ import ( ) // Prints the formatted startup message. -func printGatewayStartupMessage(apiEndPoints []string, accessKey, secretKey, backendType string) { +func printGatewayStartupMessage(apiEndPoints []string, accessKey, secretKey string, backendType gatewayBackend) { // Prints credential. printGatewayCommonMsg(apiEndPoints, accessKey, secretKey) diff --git a/cmd/main.go b/cmd/main.go index c95dbf62a..8f871beb3 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -18,6 +18,7 @@ package cmd import ( "os" + "path/filepath" "sort" "github.com/minio/cli" @@ -59,7 +60,7 @@ VERSION: ` + Version + `{{ "\n"}}` -func newApp() *cli.App { +func newApp(name string) *cli.App { // Collection of minio commands currently supported are. commands := []cli.Command{} @@ -108,7 +109,7 @@ func newApp() *cli.App { } app := cli.NewApp() - app.Name = "Minio" + app.Name = name app.Author = "Minio.io" app.Version = Version app.Usage = "Cloud Storage Server." @@ -137,7 +138,8 @@ func newApp() *cli.App { // Main main for minio server. func Main(args []string) { - app := newApp() + name := filepath.Base(args[0]) + app := newApp(name) // Run the app - exit on error. if err := app.Run(args); err != nil {