From c065be656cebc6927044c492e1194b366e8c4342 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Mon, 19 Oct 2015 00:46:03 -0700 Subject: [PATCH] Implement min-free-disk as a subcommand, deprecate flag --- flags.go | 6 ---- main.go | 1 - pkg/fs/fs-bucket.go | 2 +- server-main.go | 73 ++++++++++++++++++++++++++------------------- 4 files changed, 44 insertions(+), 38 deletions(-) diff --git a/flags.go b/flags.go index 7ccdb6f1e..f810dc422 100644 --- a/flags.go +++ b/flags.go @@ -28,12 +28,6 @@ var ( Usage: "ADDRESS:PORT for cloud storage access.", } - minFreeDiskFlag = cli.StringFlag{ - Name: "min-free-disk", - Value: "10%", - Usage: "Minimum free disk space required for cloud storage.", - } - ratelimitFlag = cli.IntFlag{ Name: "ratelimit", Hide: true, diff --git a/main.go b/main.go index 4d6ac09ac..e6fd8cd68 100644 --- a/main.go +++ b/main.go @@ -103,7 +103,6 @@ func registerApp() *cli.App { // register all flags registerFlag(addressFlag) - registerFlag(minFreeDiskFlag) registerFlag(ratelimitFlag) registerFlag(anonymousFlag) registerFlag(certFlag) diff --git a/pkg/fs/fs-bucket.go b/pkg/fs/fs-bucket.go index c3106785b..dcdab70c2 100644 --- a/pkg/fs/fs-bucket.go +++ b/pkg/fs/fs-bucket.go @@ -111,7 +111,7 @@ func (fs Filesystem) MakeBucket(bucket, acl string) *probe.Error { bucketDir := filepath.Join(fs.path, bucket) // check if bucket exists - if _, err := os.Stat(bucketDir); err == nil { + if _, err = os.Stat(bucketDir); err == nil { return probe.NewError(BucketExists{ Bucket: bucket, }) diff --git a/server-main.go b/server-main.go index e197df341..eee03404b 100644 --- a/server-main.go +++ b/server-main.go @@ -54,7 +54,7 @@ EXAMPLES: $ minio --address 192.168.1.101:9000 {{.Name}} /home/shared 4. Start minio server with minimum free disk threshold to 5% - $ minio --min-free-disk 5% {{.Name}} /home/shared/Pictures + $ minio {{.Name}} min-free-disk 5% /home/shared/Pictures `, } @@ -143,32 +143,6 @@ func parsePercentToInt(s string, bitSize int) (int64, *probe.Error) { return p, nil } -func getServerConfig(c *cli.Context) serverConfig { - path := strings.TrimSpace(c.Args().First()) - if path == "" { - fatalIf(probe.NewError(errInvalidArgument), "Path argument cannot be empty.", nil) - } - certFile := c.GlobalString("cert") - keyFile := c.GlobalString("key") - if (certFile != "" && keyFile == "") || (certFile == "" && keyFile != "") { - fatalIf(probe.NewError(errInvalidArgument), "Both certificate and key are required to enable https.", nil) - } - minFreeDisk, err := parsePercentToInt(c.GlobalString("min-free-disk"), 64) - fatalIf(err.Trace(c.GlobalString("min-free-disk")), "Unable to parse minimum free disk parameter.", nil) - - tls := (certFile != "" && keyFile != "") - return serverConfig{ - Address: c.GlobalString("address"), - Anonymous: c.GlobalBool("anonymous"), - Path: path, - MinFreeDisk: minFreeDisk, - TLS: tls, - CertFile: certFile, - KeyFile: keyFile, - RateLimit: c.GlobalInt("ratelimit"), - } -} - func getAuth() (*AuthConfig, *probe.Error) { if err := createAuthConfigPath(); err != nil { return nil, err.Trace() @@ -241,18 +215,57 @@ func fetchAuth() *probe.Error { return nil } -func serverMain(c *cli.Context) { +func checkServerSyntax(c *cli.Context) { if !c.Args().Present() || c.Args().First() == "help" { cli.ShowCommandHelpAndExit(c, "server", 1) } + if len(c.Args()) > 3 { + fatalIf(probe.NewError(errInvalidArgument), "Unnecessary arguments passed. Please refer ‘mc server help’", nil) + } + path := strings.TrimSpace(c.Args().Last()) + if path == "" { + fatalIf(probe.NewError(errInvalidArgument), "Path argument cannot be empty.", nil) + } +} + +func serverMain(c *cli.Context) { + checkServerSyntax(c) err := fetchAuth() fatalIf(err.Trace(), "Failed to generate keys for minio.", nil) - if _, err := os.Stat(c.Args().First()); err != nil { + path := strings.TrimSpace(c.Args().Last()) + // Last argument is always path + if _, err := os.Stat(path); err != nil { fatalIf(probe.NewError(err), "Unable to validate the path", nil) } - apiServerConfig := getServerConfig(c) + certFile := c.GlobalString("cert") + keyFile := c.GlobalString("key") + if (certFile != "" && keyFile == "") || (certFile == "" && keyFile != "") { + fatalIf(probe.NewError(errInvalidArgument), "Both certificate and key are required to enable https.", nil) + } + var minFreeDisk int64 + // Only if args are greater than or equal to 2 verify if the proper variables are passed. + if len(c.Args()) >= 2 { + if c.Args().Get(0) == "min-free-disk" { + minFreeDisk, err = parsePercentToInt(c.Args().Get(1), 64) + fatalIf(err.Trace(c.Args().Get(2)), "Unable to parse minimum free disk parameter.", nil) + } + if c.Args().Get(0) != "min-free-disk" { + fatalIf(probe.NewError(errInvalidArgument), "Invalid arguments passed. ‘"+strings.Join(c.Args(), " ")+"’", nil) + } + } + tls := (certFile != "" && keyFile != "") + apiServerConfig := serverConfig{ + Address: c.GlobalString("address"), + Anonymous: c.GlobalBool("anonymous"), + Path: path, + MinFreeDisk: minFreeDisk, + TLS: tls, + CertFile: certFile, + KeyFile: keyFile, + RateLimit: c.GlobalInt("ratelimit"), + } err = startServer(apiServerConfig) errorIf(err.Trace(), "Failed to start the minio server.", nil) }