From 98d17d2a97e6db184a6b86104624610dfe124d17 Mon Sep 17 00:00:00 2001 From: Bala FA Date: Fri, 3 Mar 2017 03:51:30 +0530 Subject: [PATCH] Remove globalQuiet and globalConfigDir global variables (#3830) --- cmd/admin-rpc-client.go | 19 +++----- cmd/admin-rpc-server.go | 33 ++++---------- cmd/certs.go | 88 ++++++++++++------------------------ cmd/certs_test.go | 11 ++--- cmd/config-migrate.go | 73 ++++++------------------------ cmd/config-migrate_test.go | 8 ++-- cmd/config-old.go | 66 ++++++--------------------- cmd/config-v14.go | 15 ++---- cmd/config-v14_test.go | 4 +- cmd/config.go | 93 ++++++++++++++++---------------------- cmd/globals.go | 23 ---------- cmd/main.go | 35 ++------------ cmd/prepare-storage-msg.go | 4 +- cmd/server-main.go | 26 +++++++++-- cmd/server-mux_test.go | 8 ++-- cmd/server-startup-msg.go | 5 -- cmd/test-utils_test.go | 4 +- cmd/utils.go | 9 ++++ 18 files changed, 169 insertions(+), 355 deletions(-) diff --git a/cmd/admin-rpc-client.go b/cmd/admin-rpc-client.go index 9241731e1..a1a482878 100644 --- a/cmd/admin-rpc-client.go +++ b/cmd/admin-rpc-client.go @@ -19,6 +19,7 @@ package cmd import ( "encoding/json" "errors" + "fmt" "net/url" "os" "path" @@ -176,20 +177,12 @@ func (rc remoteAdminClient) WriteTmpConfig(tmpFileName string, configBytes []byt // CommitConfig - Move the new config in tmpFileName onto config.json // on a local node. func (lc localAdminClient) CommitConfig(tmpFileName string) error { - configDir, err := getConfigPath() - if err != nil { - errorIf(err, "Failed to get config directory path.") - return err - } + configFile := getConfigFile() + tmpConfigFile := filepath.Join(getConfigDir(), tmpFileName) - configFilePath := filepath.Join(configDir, globalMinioConfigFile) - err = os.Rename(filepath.Join(configDir, tmpFileName), configFilePath) - if err != nil { - errorIf(err, "Failed to rename to config.json") - return err - } - - return nil + err := os.Rename(tmpConfigFile, configFile) + errorIf(err, fmt.Sprintf("Failed to rename %s to %s", tmpConfigFile, configFile)) + return err } // CommitConfig - Move the new config in tmpFileName onto config.json diff --git a/cmd/admin-rpc-server.go b/cmd/admin-rpc-server.go index 6109b9c43..855044a64 100644 --- a/cmd/admin-rpc-server.go +++ b/cmd/admin-rpc-server.go @@ -19,6 +19,7 @@ package cmd import ( "encoding/json" "errors" + "fmt" "io/ioutil" "net/rpc" "os" @@ -175,17 +176,9 @@ type WriteConfigReply struct { } func writeTmpConfigCommon(tmpFileName string, configBytes []byte) error { - configDir, err := getConfigPath() - if err != nil { - errorIf(err, "Failed to get config path") - return err - } - - err = ioutil.WriteFile(filepath.Join(configDir, tmpFileName), configBytes, 0666) - if err != nil { - errorIf(err, "Failed to write to temporary config file.") - return err - } + tmpConfigFile := filepath.Join(getConfigDir(), tmpFileName) + err := ioutil.WriteFile(tmpConfigFile, configBytes, 0666) + errorIf(err, fmt.Sprintf("Failed to write to temporary config file %s", tmpConfigFile)) return err } @@ -214,20 +207,12 @@ type CommitConfigReply struct { // CommitConfig - Renames the temporary file into config.json on this node. func (s *adminCmd) CommitConfig(cArgs *CommitConfigArgs, cReply *CommitConfigReply) error { - configDir, err := getConfigPath() - if err != nil { - errorIf(err, "Failed to get config path.") - return err - } - - configFilePath := filepath.Join(configDir, globalMinioConfigFile) - err = os.Rename(filepath.Join(configDir, cArgs.FileName), configFilePath) - if err != nil { - errorIf(err, "Failed to rename config file.") - return err - } + configFile := getConfigFile() + tmpConfigFile := filepath.Join(getConfigDir(), cArgs.FileName) - return nil + err := os.Rename(tmpConfigFile, configFile) + errorIf(err, fmt.Sprintf("Failed to rename %s to %s", tmpConfigFile, configFile)) + return err } // registerAdminRPCRouter - registers RPC methods for service status, diff --git a/cmd/certs.go b/cmd/certs.go index ce24bf37f..4c8311122 100644 --- a/cmd/certs.go +++ b/cmd/certs.go @@ -21,90 +21,62 @@ import ( "encoding/pem" "errors" "io/ioutil" - "os" "path/filepath" ) -// createCertsPath create certs path. -func createCertsPath() error { - certsPath, err := getCertsPath() - if err != nil { - return err - } - if err := os.MkdirAll(certsPath, 0700); err != nil { - return err - } - rootCAsPath := filepath.Join(certsPath, globalMinioCertsCADir) - return os.MkdirAll(rootCAsPath, 0700) -} - // getCertsPath get certs path. -func getCertsPath() (string, error) { - var certsPath string - configDir, err := getConfigPath() - if err != nil { - return "", err - } - certsPath = filepath.Join(configDir, globalMinioCertsDir) - return certsPath, nil +func getCertsPath() string { + return filepath.Join(getConfigDir(), globalMinioCertsDir) } -// mustGetCertsPath must get certs path. -func mustGetCertsPath() string { - certsPath, err := getCertsPath() - fatalIf(err, "Failed to get certificate path.") - return certsPath +// getCertFile must get cert file. +func getCertFile() string { + return filepath.Join(getCertsPath(), globalMinioCertFile) } -// mustGetCertFile must get cert file. -func mustGetCertFile() string { - return filepath.Join(mustGetCertsPath(), globalMinioCertFile) +// getKeyFile must get key file. +func getKeyFile() string { + return filepath.Join(getCertsPath(), globalMinioKeyFile) } -// mustGetKeyFile must get key file. -func mustGetKeyFile() string { - return filepath.Join(mustGetCertsPath(), globalMinioKeyFile) +// createCertsPath create certs path. +func createCertsPath() error { + rootCAsPath := filepath.Join(getCertsPath(), globalMinioCertsCADir) + return mkdirAll(rootCAsPath, 0700) } -// mustGetCAFiles must get the list of the CA certificates stored in minio config dir -func mustGetCAFiles() (caCerts []string) { - CAsDir := filepath.Join(mustGetCertsPath(), globalMinioCertsCADir) - caFiles, _ := ioutil.ReadDir(CAsDir) - for _, cert := range caFiles { - caCerts = append(caCerts, filepath.Join(CAsDir, cert.Name())) +// getCAFiles must get the list of the CA certificates stored in minio config dir +func getCAFiles() (caCerts []string) { + CAsDir := filepath.Join(getCertsPath(), globalMinioCertsCADir) + if caFiles, err := ioutil.ReadDir(CAsDir); err == nil { + // Ignore any error. + for _, cert := range caFiles { + caCerts = append(caCerts, filepath.Join(CAsDir, cert.Name())) + } } - return + return caCerts } -// mustGetSystemCertPool returns empty cert pool in case of error (windows) -func mustGetSystemCertPool() *x509.CertPool { +// getSystemCertPool returns empty cert pool in case of error (windows) +func getSystemCertPool() *x509.CertPool { pool, err := x509.SystemCertPool() if err != nil { - return x509.NewCertPool() + pool = x509.NewCertPool() } + return pool } // isCertFileExists verifies if cert file exists, returns true if // found, false otherwise. func isCertFileExists() bool { - st, e := os.Stat(filepath.Join(mustGetCertsPath(), globalMinioCertFile)) - // If file exists and is regular return true. - if e == nil && st.Mode().IsRegular() { - return true - } - return false + return isFile(getCertFile()) } // isKeyFileExists verifies if key file exists, returns true if found, // false otherwise. func isKeyFileExists() bool { - st, e := os.Stat(filepath.Join(mustGetCertsPath(), globalMinioKeyFile)) - // If file exists and is regular return true. - if e == nil && st.Mode().IsRegular() { - return true - } - return false + return isFile(getKeyFile()) } // isSSL - returns true with both cert and key exists. @@ -114,7 +86,7 @@ func isSSL() bool { // Reads certificated file and returns a list of parsed certificates. func readCertificateChain() ([]*x509.Certificate, error) { - bytes, err := ioutil.ReadFile(mustGetCertFile()) + bytes, err := ioutil.ReadFile(getCertFile()) if err != nil { return nil, err } @@ -149,12 +121,12 @@ func parseCertificateChain(bytes []byte) ([]*x509.Certificate, error) { // loadRootCAs fetches CA files provided in minio config and adds them to globalRootCAs // Currently under Windows, there is no way to load system + user CAs at the same time func loadRootCAs() { - caFiles := mustGetCAFiles() + caFiles := getCAFiles() if len(caFiles) == 0 { return } // Get system cert pool, and empty cert pool under Windows because it is not supported - globalRootCAs = mustGetSystemCertPool() + globalRootCAs = getSystemCertPool() // Load custom root CAs for client requests for _, caFile := range caFiles { caCert, err := ioutil.ReadFile(caFile) diff --git a/cmd/certs_test.go b/cmd/certs_test.go index a68f8176e..5d8c649a9 100644 --- a/cmd/certs_test.go +++ b/cmd/certs_test.go @@ -25,10 +25,7 @@ import ( // Make sure we have a valid certs path. func TestGetCertsPath(t *testing.T) { - path, err := getCertsPath() - if err != nil { - t.Error(err) - } + path := getCertsPath() if path == "" { t.Errorf("expected path to not be an empty string, got: '%s'", path) } @@ -42,18 +39,18 @@ func TestGetCertsPath(t *testing.T) { } // This will error if something goes wrong, so just call it. - mustGetCertsPath() + getCertsPath() } // Ensure that the certificate and key file getters contain their respective // file name and endings. func TestGetFiles(t *testing.T) { - file := mustGetCertFile() + file := getCertFile() if !strings.Contains(file, globalMinioCertFile) { t.Errorf("CertFile does not contain %s", globalMinioCertFile) } - file = mustGetKeyFile() + file = getKeyFile() if !strings.Contains(file, globalMinioKeyFile) { t.Errorf("KeyFile does not contain %s", globalMinioKeyFile) } diff --git a/cmd/config-migrate.go b/cmd/config-migrate.go index e38d1bf4c..e45bdfae5 100644 --- a/cmd/config-migrate.go +++ b/cmd/config-migrate.go @@ -94,15 +94,10 @@ func purgeV1() error { } if cv1.Version == "1" { - console.Println("Removed unsupported config version ‘1’.") - /// Purge old fsUsers.json file - configPath, err := getConfigPath() - if err != nil { - return fmt.Errorf("Unable to retrieve config path. %v", err) - } - - configFile := filepath.Join(configPath, "fsUsers.json") + // Purge old fsUsers.json file + configFile := filepath.Join(getConfigDir(), "fsUsers.json") removeAll(configFile) + console.Println("Removed unsupported config version ‘1’.") return nil } return fmt.Errorf("Failed to migrate unrecognized config version ‘" + cv1.Version + "’.") @@ -158,12 +153,7 @@ func migrateV2ToV3() error { return fmt.Errorf("Unable to initialize config. %v", err) } - configFile, err := getConfigFile() - if err != nil { - return fmt.Errorf("Unable to get config file. %v", err) - } - - // Migrate the config. + configFile := getConfigFile() err = qc.Save(configFile) if err != nil { return fmt.Errorf("Failed to migrate config from ‘"+cv2.Version+"’ to ‘"+srvConfig.Version+"’ failed. %v", err) @@ -205,11 +195,8 @@ func migrateV3ToV4() error { if err != nil { return fmt.Errorf("Unable to initialize the quick config. %v", err) } - configFile, err := getConfigFile() - if err != nil { - return fmt.Errorf("Unable to get config file. %v", err) - } + configFile := getConfigFile() err = qc.Save(configFile) if err != nil { return fmt.Errorf("Failed to migrate config from ‘"+cv3.Version+"’ to ‘"+srvConfig.Version+"’ failed. %v", err) @@ -254,11 +241,8 @@ func migrateV4ToV5() error { if err != nil { return fmt.Errorf("Unable to initialize the quick config. %v", err) } - configFile, err := getConfigFile() - if err != nil { - return fmt.Errorf("Unable to get config file. %v", err) - } + configFile := getConfigFile() err = qc.Save(configFile) if err != nil { return fmt.Errorf("Failed to migrate config from ‘"+cv4.Version+"’ to ‘"+srvConfig.Version+"’ failed. %v", err) @@ -330,11 +314,8 @@ func migrateV5ToV6() error { if err != nil { return fmt.Errorf("Unable to initialize the quick config. %v", err) } - configFile, err := getConfigFile() - if err != nil { - return fmt.Errorf("Unable to get config file. %v", err) - } + configFile := getConfigFile() err = qc.Save(configFile) if err != nil { return fmt.Errorf("Failed to migrate config from ‘"+cv5.Version+"’ to ‘"+srvConfig.Version+"’ failed. %v", err) @@ -394,11 +375,8 @@ func migrateV6ToV7() error { if err != nil { return fmt.Errorf("Unable to initialize the quick config. %v", err) } - configFile, err := getConfigFile() - if err != nil { - return fmt.Errorf("Unable to get config file. %v", err) - } + configFile := getConfigFile() err = qc.Save(configFile) if err != nil { return fmt.Errorf("Failed to migrate config from ‘"+cv6.Version+"’ to ‘"+srvConfig.Version+"’ failed. %v", err) @@ -465,11 +443,8 @@ func migrateV7ToV8() error { if err != nil { return fmt.Errorf("Unable to initialize the quick config. %v", err) } - configFile, err := getConfigFile() - if err != nil { - return fmt.Errorf("Unable to get config file. %v", err) - } + configFile := getConfigFile() err = qc.Save(configFile) if err != nil { return fmt.Errorf("Failed to migrate config from ‘"+cv7.Version+"’ to ‘"+srvConfig.Version+"’ failed. %v", err) @@ -544,11 +519,8 @@ func migrateV8ToV9() error { return fmt.Errorf("Unable to initialize the quick config. %v", err) } - configFile, err := getConfigFile() - if err != nil { - return fmt.Errorf("Unable to get config file. %v", err) - } + configFile := getConfigFile() err = qc.Save(configFile) if err != nil { return fmt.Errorf( @@ -629,11 +601,8 @@ func migrateV9ToV10() error { return fmt.Errorf("Unable to initialize the quick config. %v", err) } - configFile, err := getConfigFile() - if err != nil { - return fmt.Errorf("Unable to get config file. %v", err) - } + configFile := getConfigFile() err = qc.Save(configFile) if err != nil { return fmt.Errorf( @@ -717,11 +686,8 @@ func migrateV10ToV11() error { return fmt.Errorf("Unable to initialize the quick config. %v", err) } - configFile, err := getConfigFile() - if err != nil { - return fmt.Errorf("Unable to get config file. %v", err) - } + configFile := getConfigFile() err = qc.Save(configFile) if err != nil { return fmt.Errorf( @@ -823,11 +789,8 @@ func migrateV11ToV12() error { return fmt.Errorf("Unable to initialize the quick config. %v", err) } - configFile, err := getConfigFile() - if err != nil { - return fmt.Errorf("Unable to get config file. %v", err) - } + configFile := getConfigFile() err = qc.Save(configFile) if err != nil { return fmt.Errorf( @@ -920,11 +883,8 @@ func migrateV12ToV13() error { return fmt.Errorf("Unable to initialize the quick config. %v", err) } - configFile, err := getConfigFile() - if err != nil { - return fmt.Errorf("Unable to get config file. %v", err) - } + configFile := getConfigFile() err = qc.Save(configFile) if err != nil { return fmt.Errorf( @@ -1022,11 +982,8 @@ func migrateV13ToV14() error { return fmt.Errorf("Unable to initialize the quick config. %v", err) } - configFile, err := getConfigFile() - if err != nil { - return fmt.Errorf("Unable to get config file. %v", err) - } + configFile := getConfigFile() err = qc.Save(configFile) if err != nil { return fmt.Errorf( diff --git a/cmd/config-migrate_test.go b/cmd/config-migrate_test.go index c35b2ef89..359936be3 100644 --- a/cmd/config-migrate_test.go +++ b/cmd/config-migrate_test.go @@ -31,7 +31,7 @@ func TestServerConfigMigrateV1(t *testing.T) { // remove the root directory after the test ends. defer removeAll(rootPath) - setGlobalConfigPath(rootPath) + setConfigDir(rootPath) // Create a V1 config json file and store it configJSON := "{ \"version\":\"1\", \"accessKeyId\":\"abcde\", \"secretAccessKey\":\"abcdefgh\"}" @@ -65,7 +65,7 @@ func TestServerConfigMigrateInexistentConfig(t *testing.T) { // remove the root directory after the test ends. defer removeAll(rootPath) - setGlobalConfigPath(rootPath) + setConfigDir(rootPath) configPath := rootPath + "/" + globalMinioConfigFile // Remove config file @@ -120,7 +120,7 @@ func TestServerConfigMigrateV2toV14(t *testing.T) { // remove the root directory after the test ends. defer removeAll(rootPath) - setGlobalConfigPath(rootPath) + setConfigDir(rootPath) configPath := rootPath + "/" + globalMinioConfigFile // Create a corrupted config file @@ -174,7 +174,7 @@ func TestServerConfigMigrateFaultyConfig(t *testing.T) { // remove the root directory after the test ends. defer removeAll(rootPath) - setGlobalConfigPath(rootPath) + setConfigDir(rootPath) configPath := rootPath + "/" + globalMinioConfigFile // Create a corrupted config file diff --git a/cmd/config-old.go b/cmd/config-old.go index d8fa5867f..7f65003e3 100644 --- a/cmd/config-old.go +++ b/cmd/config-old.go @@ -50,11 +50,7 @@ type configV1 struct { // loadConfigV1 load config func loadConfigV1() (*configV1, error) { - configPath, err := getConfigPath() - if err != nil { - return nil, err - } - configFile := filepath.Join(configPath, "fsUsers.json") + configFile := filepath.Join(getConfigDir(), "fsUsers.json") config, err := loadOldConfig(configFile, &configV1{Version: "1"}) if config == nil { return nil, err @@ -86,10 +82,7 @@ type configV2 struct { // loadConfigV2 load config version '2'. func loadConfigV2() (*configV2, error) { - configFile, err := getConfigFile() - if err != nil { - return nil, err - } + configFile := getConfigFile() config, err := loadOldConfig(configFile, &configV2{Version: "2"}) if config == nil { return nil, err @@ -152,10 +145,7 @@ type configV3 struct { // loadConfigV3 load config version '3'. func loadConfigV3() (*configV3, error) { - configFile, err := getConfigFile() - if err != nil { - return nil, err - } + configFile := getConfigFile() config, err := loadOldConfig(configFile, &configV3{Version: "3"}) if config == nil { return nil, err @@ -195,10 +185,7 @@ type configV4 struct { // loadConfigV4 load config version '4'. func loadConfigV4() (*configV4, error) { - configFile, err := getConfigFile() - if err != nil { - return nil, err - } + configFile := getConfigFile() config, err := loadOldConfig(configFile, &configV4{Version: "4"}) if config == nil { return nil, err @@ -265,10 +252,7 @@ type configV5 struct { // loadConfigV5 load config version '5'. func loadConfigV5() (*configV5, error) { - configFile, err := getConfigFile() - if err != nil { - return nil, err - } + configFile := getConfigFile() config, err := loadOldConfig(configFile, &configV5{Version: "5"}) if config == nil { return nil, err @@ -299,10 +283,7 @@ type configV6 struct { // loadConfigV6 load config version '6'. func loadConfigV6() (*configV6, error) { - configFile, err := getConfigFile() - if err != nil { - return nil, err - } + configFile := getConfigFile() config, err := loadOldConfig(configFile, &configV6{Version: "6"}) if config == nil { return nil, err @@ -352,10 +333,7 @@ type serverConfigV7 struct { // loadConfigV7 load config version '7'. func loadConfigV7() (*serverConfigV7, error) { - configFile, err := getConfigFile() - if err != nil { - return nil, err - } + configFile := getConfigFile() config, err := loadOldConfig(configFile, &serverConfigV7{Version: "7"}) if config == nil { return nil, err @@ -384,10 +362,7 @@ type serverConfigV8 struct { // loadConfigV8 load config version '8'. func loadConfigV8() (*serverConfigV8, error) { - configFile, err := getConfigFile() - if err != nil { - return nil, err - } + configFile := getConfigFile() config, err := loadOldConfig(configFile, &serverConfigV8{Version: "8"}) if config == nil { return nil, err @@ -415,10 +390,7 @@ type serverConfigV9 struct { } func loadConfigV9() (*serverConfigV9, error) { - configFile, err := getConfigFile() - if err != nil { - return nil, err - } + configFile := getConfigFile() config, err := loadOldConfig(configFile, &serverConfigV9{Version: "9"}) if config == nil { return nil, err @@ -444,10 +416,7 @@ type serverConfigV10 struct { } func loadConfigV10() (*serverConfigV10, error) { - configFile, err := getConfigFile() - if err != nil { - return nil, err - } + configFile := getConfigFile() config, err := loadOldConfig(configFile, &serverConfigV10{Version: "10"}) if config == nil { return nil, err @@ -484,10 +453,7 @@ type serverConfigV11 struct { } func loadConfigV11() (*serverConfigV11, error) { - configFile, err := getConfigFile() - if err != nil { - return nil, err - } + configFile := getConfigFile() config, err := loadOldConfig(configFile, &serverConfigV11{Version: "11"}) if config == nil { return nil, err @@ -512,10 +478,7 @@ type serverConfigV12 struct { } func loadConfigV12() (*serverConfigV12, error) { - configFile, err := getConfigFile() - if err != nil { - return nil, err - } + configFile := getConfigFile() config, err := loadOldConfig(configFile, &serverConfigV12{Version: "12"}) if config == nil { return nil, err @@ -540,10 +503,7 @@ type serverConfigV13 struct { } func loadConfigV13() (*serverConfigV13, error) { - configFile, err := getConfigFile() - if err != nil { - return nil, err - } + configFile := getConfigFile() config, err := loadOldConfig(configFile, &serverConfigV13{Version: "13"}) if config == nil { return nil, err diff --git a/cmd/config-v14.go b/cmd/config-v14.go index fa4e3b0b3..14ae16725 100644 --- a/cmd/config-v14.go +++ b/cmd/config-v14.go @@ -98,7 +98,7 @@ func newConfig(envParams envParams) error { } // Create config path. - if err := createConfigPath(); err != nil { + if err := createConfigDir(); err != nil { return err } @@ -116,12 +116,8 @@ func newConfig(envParams envParams) error { // loadConfig - loads a new config from disk, overrides params from env // if found and valid func loadConfig(envParams envParams) error { - configFile, err := getConfigFile() - if err != nil { - return err - } - - if _, err = os.Stat(configFile); err != nil { + configFile := getConfigFile() + if _, err := os.Stat(configFile); err != nil { return err } @@ -229,10 +225,7 @@ func (s serverConfigV14) Save() error { defer serverConfigMu.RUnlock() // get config file. - configFile, err := getConfigFile() - if err != nil { - return err - } + configFile := getConfigFile() // initialize quick. qc, err := quick.New(&s) diff --git a/cmd/config-v14_test.go b/cmd/config-v14_test.go index 63f0c4ebd..d3ca1aba4 100644 --- a/cmd/config-v14_test.go +++ b/cmd/config-v14_test.go @@ -112,7 +112,7 @@ func TestServerConfig(t *testing.T) { } // Do this only once here. - setGlobalConfigPath(rootPath) + setConfigDir(rootPath) // Initialize server config. if err := loadConfig(envParams{}); err != nil { @@ -143,7 +143,7 @@ func TestServerConfigWithEnvs(t *testing.T) { } // Do this only once here. - setGlobalConfigPath(rootPath) + setConfigDir(rootPath) // Init config initConfig() diff --git a/cmd/config.go b/cmd/config.go index 618bc2388..59dd831ab 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -1,5 +1,5 @@ /* - * Minio Cloud Storage, (C) 2015, 2016 Minio, Inc. + * Minio Cloud Storage, (C) 2015, 2016, 2017 Minio, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,77 +17,62 @@ package cmd import ( - "os" "path/filepath" "sync" - "github.com/minio/go-homedir" + homedir "github.com/minio/go-homedir" + "github.com/minio/mc/pkg/console" ) -// configPath for custom config path only for testing purposes -var customConfigPath string -var configMu sync.Mutex +// ConfigDir - configuration directory with locking. +type ConfigDir struct { + sync.Mutex + dir string +} + +// Set - saves given directory as configuration directory. +func (config *ConfigDir) Set(dir string) { + config.Lock() + defer config.Unlock() -// Sets a new config path. -func setGlobalConfigPath(configPath string) { - configMu.Lock() - defer configMu.Unlock() - customConfigPath = configPath + config.dir = dir } -// getConfigPath get server config path -func getConfigPath() (string, error) { - configMu.Lock() - defer configMu.Unlock() +// Get - returns current configuration directory. +func (config *ConfigDir) Get() string { + config.Lock() + defer config.Unlock() - if customConfigPath != "" { - return customConfigPath, nil - } + return config.dir +} + +func mustGetDefaultConfigDir() string { homeDir, err := homedir.Dir() if err != nil { - return "", err + console.Fatalln("Unable to get home directory.", err) } - configPath := filepath.Join(homeDir, globalMinioConfigDir) - return configPath, nil + + return filepath.Join(homeDir, globalMinioConfigDir) } -// mustGetConfigPath must get server config path. -func mustGetConfigPath() string { - configPath, err := getConfigPath() - if err != nil { - return "" - } - return configPath +var configDir = &ConfigDir{dir: mustGetDefaultConfigDir()} + +func setConfigDir(dir string) { + configDir.Set(dir) } -// createConfigPath create server config path. -func createConfigPath() error { - configPath, err := getConfigPath() - if err != nil { - return err - } - return os.MkdirAll(configPath, 0700) +func getConfigDir() string { + return configDir.Get() } -// isConfigFileExists - returns true if config file exists. -func isConfigFileExists() bool { - path, err := getConfigFile() - if err != nil { - return false - } - st, err := os.Stat(path) - // If file exists and is regular return true. - if err == nil && st.Mode().IsRegular() { - return true - } - return false +func createConfigDir() error { + return mkdirAll(getConfigDir(), 0700) } -// getConfigFile get server config file. -func getConfigFile() (string, error) { - configPath, err := getConfigPath() - if err != nil { - return "", err - } - return filepath.Join(configPath, globalMinioConfigFile), nil +func getConfigFile() string { + return filepath.Join(getConfigDir(), globalMinioConfigFile) +} + +func isConfigFileExists() bool { + return isFile(getConfigFile()) } diff --git a/cmd/globals.go b/cmd/globals.go index 4017c7a81..4f04ce0e9 100644 --- a/cmd/globals.go +++ b/cmd/globals.go @@ -24,8 +24,6 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/fatih/color" - "github.com/minio/cli" - "github.com/minio/mc/pkg/console" ) // minio configuration related constants. @@ -60,10 +58,6 @@ const ( ) var ( - globalQuiet = false // quiet flag set via command line. - globalConfigDir = mustGetConfigPath() // config-dir flag set via command line - // Add new global flags here. - // Indicates if the running minio server is distributed setup. globalIsDistXL = false @@ -131,20 +125,3 @@ var ( colorBold = color.New(color.Bold).SprintFunc() colorBlue = color.New(color.FgBlue).SprintfFunc() ) - -// Parse command arguments and set global variables accordingly -func setGlobalsFromContext(c *cli.Context) { - // Set config dir - switch { - case c.IsSet("config-dir"): - globalConfigDir = c.String("config-dir") - case c.GlobalIsSet("config-dir"): - globalConfigDir = c.GlobalString("config-dir") - } - if globalConfigDir == "" { - console.Fatalf("Unable to get config file. Config directory is empty.") - } - - // Set global quiet flag. - globalQuiet = c.Bool("quiet") || c.GlobalBool("quiet") -} diff --git a/cmd/main.go b/cmd/main.go index 18d3ab20a..d31406d36 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -31,7 +31,7 @@ var ( globalFlags = []cli.Flag{ cli.StringFlag{ Name: "config-dir, C", - Value: mustGetConfigPath(), + Value: getConfigDir(), Usage: "Path to configuration directory.", }, cli.BoolFlag{ @@ -133,26 +133,10 @@ func registerApp() *cli.App { return app } -// Verify main command syntax. -func checkMainSyntax(c *cli.Context) { - configPath, err := getConfigPath() - if err != nil { - console.Fatalf("Unable to obtain user's home directory. \nError: %s\n", err) - } - if configPath == "" { - console.Fatalln("Config directory cannot be empty, please specify --config-dir .") - } -} - // Check for updates and print a notification message func checkUpdate() { - // Do not print update messages, if quiet flag is set. - if !globalQuiet { - older, downloadURL, err := getUpdateInfo(1 * time.Second) - if err != nil { - // Its OK to ignore any errors during getUpdateInfo() here. - return - } + // Its OK to ignore any errors during getUpdateInfo() here. + if older, downloadURL, err := getUpdateInfo(1 * time.Second); err == nil { if older > time.Duration(0) { console.Println(colorizeUpdateMessage(downloadURL, older)) } @@ -179,7 +163,7 @@ func initConfig() { if err := newConfig(envs); err != nil { console.Fatalf("Unable to initialize minio config for the first time. Err: %s.\n", err) } - console.Println("Created minio configuration file successfully at " + mustGetConfigPath()) + console.Println("Created minio configuration file successfully at " + getConfigDir()) return } @@ -194,12 +178,6 @@ func initConfig() { // Generic Minio initialization to create/load config, prepare loggers, etc.. func minioInit(ctx *cli.Context) { - // Set global variables after parsing passed arguments - setGlobalsFromContext(ctx) - - // Sets new config directory. - setGlobalConfigPath(globalConfigDir) - // Is TLS configured?. globalIsSSL = isSSL() @@ -217,11 +195,6 @@ func minioInit(ctx *cli.Context) { // Main main for minio server. func Main(args []string, exitFn func(int)) { app := registerApp() - app.Before = func(c *cli.Context) error { - // Valid input arguments to main. - checkMainSyntax(c) - return nil - } // Start profiler if env is set. if profiler := os.Getenv("_MINIO_PROFILER"); profiler != "" { diff --git a/cmd/prepare-storage-msg.go b/cmd/prepare-storage-msg.go index 445bbefca..58e4d70e1 100644 --- a/cmd/prepare-storage-msg.go +++ b/cmd/prepare-storage-msg.go @@ -46,9 +46,7 @@ func printOnceFn() printOnceFunc { var once sync.Once return func(msg string) { once.Do(func() { - if !globalQuiet { - console.Println(msg) - } + console.Println(msg) }) } } diff --git a/cmd/server-main.go b/cmd/server-main.go index 75ce32465..d06765d2c 100644 --- a/cmd/server-main.go +++ b/cmd/server-main.go @@ -30,6 +30,7 @@ import ( "runtime" "github.com/minio/cli" + "github.com/minio/mc/pkg/console" ) var serverFlags = []cli.Flag{ @@ -358,11 +359,28 @@ func serverMain(c *cli.Context) { cli.ShowCommandHelpAndExit(c, "server", 1) } + // Get quiet flag from command line argument. + quietFlag := c.Bool("quiet") || c.GlobalBool("quiet") + + // Get configuration directory from command line argument. + configDir := c.String("config-dir") + if !c.IsSet("config-dir") && c.GlobalIsSet("config-dir") { + configDir = c.GlobalString("config-dir") + } + if configDir == "" { + console.Fatalf("Configuration directory cannot be empty.") + } + + // Set configuration directory. + setConfigDir(configDir) + // Initializes server config, certs, logging and system settings. initServerConfig(c) // Check for new updates from dl.minio.io. - checkUpdate() + if !quietFlag { + checkUpdate() + } // Server address. serverAddr := c.String("address") @@ -442,7 +460,7 @@ func serverMain(c *cli.Context) { go func() { cert, key := "", "" if globalIsSSL { - cert, key = mustGetCertFile(), mustGetKeyFile() + cert, key = getCertFile(), getKeyFile() } fatalIf(apiServer.ListenAndServe(cert, key), "Failed to start minio server.") }() @@ -458,7 +476,9 @@ func serverMain(c *cli.Context) { globalObjLayerMutex.Unlock() // Prints the formatted startup message once object layer is initialized. - printStartupMessage(apiEndPoints) + if !quietFlag { + printStartupMessage(apiEndPoints) + } // Set uptime time after object layer has initialized. globalBootTime = time.Now().UTC() diff --git a/cmd/server-mux_test.go b/cmd/server-mux_test.go index 24802abed..c7c39278e 100644 --- a/cmd/server-mux_test.go +++ b/cmd/server-mux_test.go @@ -358,8 +358,8 @@ func TestServerListenAndServeTLS(t *testing.T) { if err != nil { t.Fatal(err) } - certFile := mustGetCertFile() - keyFile := mustGetKeyFile() + certFile := getCertFile() + keyFile := getKeyFile() defer os.RemoveAll(certFile) defer os.RemoveAll(keyFile) @@ -420,8 +420,8 @@ func TestServerListenAndServeTLS(t *testing.T) { // generateTestCert creates a cert and a key used for testing only func generateTestCert(host string) error { - certPath := mustGetCertFile() - keyPath := mustGetKeyFile() + certPath := getCertFile() + keyPath := getKeyFile() priv, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { return err diff --git a/cmd/server-startup-msg.go b/cmd/server-startup-msg.go index 53718aa74..dbdecdca6 100644 --- a/cmd/server-startup-msg.go +++ b/cmd/server-startup-msg.go @@ -44,11 +44,6 @@ func getFormatStr(strLen int, padding int) string { // Prints the formatted startup message. func printStartupMessage(apiEndPoints []string) { - // If quiet flag is set do not print startup message. - if globalQuiet { - return - } - // Prints credential, region and browser access. printServerCommonMsg(apiEndPoints) diff --git a/cmd/test-utils_test.go b/cmd/test-utils_test.go index 63387ef69..7d518f267 100644 --- a/cmd/test-utils_test.go +++ b/cmd/test-utils_test.go @@ -440,7 +440,7 @@ func StartTestPeersRPCServer(t TestErrHandler, instanceType string) TestServer { // Sets the global config path to empty string. func resetGlobalConfigPath() { - setGlobalConfigPath("") + setConfigDir("") } // sets globalObjectAPI to `nil`. @@ -519,7 +519,7 @@ func newTestConfig(bucketLocation string) (rootPath string, err error) { } // Do this only once here. - setGlobalConfigPath(rootPath) + setConfigDir(rootPath) // Initialize server config. if err = newConfig(envParams{}); err != nil { diff --git a/cmd/utils.go b/cmd/utils.go index 46166147c..49657cf13 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -262,3 +262,12 @@ func getBrowserFromEnv() (string, error) { globalIsEnvBrowser = true return strings.ToLower(b), nil } + +// isFile - returns whether given path is a file or not. +func isFile(path string) bool { + if fi, err := os.Stat(path); err == nil { + return fi.Mode().IsRegular() + } + + return false +}