diff --git a/CHANGELOG.md b/CHANGELOG.md index ee0dca5c0ac..766d92b268e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - XXXX-XX-XX + - Add search locations to load lakeFS configuration. More information on https://docs.lakefs.io/reference/configuration (#2355) ## v0.48.0 - 2021-08-22 diff --git a/cmd/lakefs/cmd/root.go b/cmd/lakefs/cmd/root.go index 8edd20adda1..cab1693ab1a 100644 --- a/cmd/lakefs/cmd/root.go +++ b/cmd/lakefs/cmd/root.go @@ -1,8 +1,10 @@ package cmd import ( + "errors" "fmt" "os" + "path" "strings" "sync" @@ -50,22 +52,15 @@ func init() { func initConfig() { logger := logging.Default().WithField("phase", "startup") if cfgFile != "" { - logger.WithField("file", cfgFile).Info("configuration file") + logger.WithField("file", cfgFile).Info("Configuration file") // Use config file from the flag. viper.SetConfigFile(cfgFile) } else { - logger.Info("search for configuration file .lakefs") - // Find home directory. - home, err := homedir.Dir() - if err != nil { - fmt.Println(err) - os.Exit(1) - } - - // Search config in home directory with name ".lakefs" (without extension). - viper.AddConfigPath(home) viper.SetConfigType("yaml") - viper.SetConfigName(".lakefs") + viper.SetConfigName("config") + viper.AddConfigPath(".") + viper.AddConfigPath(path.Join(getHomeDir(), ".lakefs")) + viper.AddConfigPath("/etc/lakefs") } viper.SetEnvPrefix("LAKEFS") @@ -73,23 +68,46 @@ func initConfig() { // read in environment variables viper.AutomaticEnv() - // If a config file is found, read it in. + // read configuration file err := viper.ReadInConfig() - logger = logger.WithField("file", viper.ConfigFileUsed()) - - if err == nil { - logger.Info("loaded configuration from file") - } else if _, ok := err.(viper.ConfigFileNotFoundError); !ok { - logger.WithError(err).Fatal("failed to read config file") + logger = logger.WithField("file", viper.ConfigFileUsed()) // should be called after SetConfigFile + var errFileNotFound viper.ConfigFileNotFoundError + if err != nil && !errors.As(err, &errFileNotFound) { + logger.WithError(err).Fatal("Failed to find a config file") + } + // fallback - try to load the previous supported $HOME/.lakefs.yaml + // if err is set it will be file-not-found based on previous check + if err != nil { + fallbackCfgFile := path.Join(getHomeDir(), ".lakefs.yaml") + if cfgFile != fallbackCfgFile { + viper.SetConfigFile(fallbackCfgFile) + logger = logger.WithField("file", viper.ConfigFileUsed()) // should be called after SetConfigFile + err = viper.ReadInConfig() + if err != nil && !os.IsNotExist(err) { + logger.WithError(err).Fatal("Failed to read config file") + } + } } // setup config used by the executed command cfg, err = config.NewConfig() if err != nil { - logger.WithError(err).Fatal("load config") + logger.WithError(err).Fatal("Load config") + } else { + logger.Info("Config loaded") } err = cfg.Validate() if err != nil { - logger.WithError(err).Fatal("invalid config") + logger.WithError(err).Fatal("Invalid config") + } +} + +// getHomeDir find and return the home directory +func getHomeDir() string { + home, err := homedir.Dir() + if err != nil { + fmt.Println("Get home directory -", err) + os.Exit(1) } + return home } diff --git a/docs/deploy/docker.md b/docs/deploy/docker.md index b85fce3960b..09d55905432 100644 --- a/docs/deploy/docker.md +++ b/docs/deploy/docker.md @@ -28,7 +28,7 @@ Here is a minimal example, but you can see the [reference](../reference/configur