Skip to content
This repository has been archived by the owner on Mar 26, 2020. It is now read-only.

Commit

Permalink
glusterd2: Allow setting config with environment variables
Browse files Browse the repository at this point in the history
Closes #1112
  • Loading branch information
kshlm authored and prashanthpai committed Aug 7, 2018
1 parent 1b2d9f7 commit b8f9625
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
53 changes: 40 additions & 13 deletions glusterd2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"expvar"
"net"
"path"
"strings"

"github.com/gluster/glusterd2/glusterd2/gdctx"
"github.com/gluster/glusterd2/glusterd2/store"
Expand All @@ -32,13 +33,13 @@ var (
// defaultPathPrefix is set by LDFLAGS
defaultPathPrefix = ""

defaultlocalstatedir = defaultPathPrefix + "/var/lib/glusterd2"
defaultlogdir = defaultPathPrefix + "/var/log/glusterd2"
defaultrundir = defaultPathPrefix + "/var/run/glusterd2"
defaultlocalstatedir = path.Join(defaultPathPrefix, "/var/lib/glusterd2")
defaultlogdir = path.Join(defaultPathPrefix, "/var/log/glusterd2")
defaultrundir = path.Join(defaultPathPrefix, "/var/run/glusterd2")
)

// parseFlags sets up the flags and parses them, this needs to be called before any other operation
func parseFlags() {
// initFlags sets up the flags and parses them, this needs to be called before any other operation
func initFlags() {
flag.String("localstatedir", defaultlocalstatedir, "Directory to store local state information.")
flag.String("rundir", defaultrundir, "Directory to store runtime data.")
flag.String("config", "", "Configuration file for GlusterD.")
Expand Down Expand Up @@ -117,7 +118,25 @@ func dumpConfigToLog() {
l.Debug("running with configuration")
}

func initConfig(confFile string) error {
// initConfig intializes GD2 configuration from various sources.
// The order of preference is,
// - explicitly set configs using config.Set
// - flags, if set
// - environment variables
// - config file
// - defaults set using config.SetDefault
// - flag defaults
func initConfig() error {
// Use config given by flags
config.BindPFlags(flag.CommandLine)

// Allow config values from environment environment variables.
// All options settable from the command line are available to be set this way.
// The environment variable should be in uppercase, prefixed with "GD2" and have "-" replaced by "_" to be used.
config.SetEnvPrefix("GD2")
config.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
config.AutomaticEnv()

// Read in configuration from file
// If a config file is not given try to read from default paths
// If a config file was given, read in configration from that file.
Expand All @@ -126,20 +145,28 @@ func initConfig(confFile string) error {
// Limit config to toml only to avoid confusion with multiple config types
config.SetConfigType("toml")

// If custom configuration is passed
confFile := config.GetString("config")
// If custom configuration is passed use it, if not try to use defaults
if confFile != "" {
config.SetConfigFile(confFile)
if err := config.MergeInConfig(); err != nil {
} else {
config.AddConfigPath(path.Join(defaultPathPrefix, "/etc/glusterd2"))
config.SetConfigName("glusterd2")
}
if err := config.ReadInConfig(); err != nil {
// Ignore error if config file is not found, error out otherwise
if _, ok := err.(config.ConfigFileNotFoundError); ok {
log.WithError(err).
WithField("file", confFile).
Error("failed to read config file")
WithField("file", config.ConfigFileUsed()).
Warn("failed to load config from file")
} else {
log.WithError(err).
WithField("file", config.ConfigFileUsed()).
Error("failed to load config from file")
return err
}
}

// Use config given by flags
config.BindPFlags(flag.CommandLine)

// Finally initialize missing config with defaults
err := setDefaults()

Expand Down
9 changes: 4 additions & 5 deletions glusterd2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func main() {
log.WithError(err).Fatal("Failed to get and set hostname or IP")
}

// Parse command-line arguments
parseFlags()
// Initalize and parse CLI flags
initFlags()

if showvers, _ := flag.CommandLine.GetBool("version"); showvers {
version.DumpVersionInfo()
Expand All @@ -50,9 +50,8 @@ func main() {
log.WithError(err).Fatal("Failed to initialize logging")
}

// Read config file
confFile, _ := flag.CommandLine.GetString("config")
if err := initConfig(confFile); err != nil {
// Initialize GD2 config
if err := initConfig(); err != nil {
log.WithError(err).Fatal("Failed to initialize config")
}

Expand Down

0 comments on commit b8f9625

Please sign in to comment.