diff --git a/CHANGELOG.md b/CHANGELOG.md index 988bedb06d9..65932a2e467 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - [#3166](https://github.com/influxdata/influxdb/issues/3166): Sort the series keys inside of a tag set so output is deterministic. - [#1856](https://github.com/influxdata/influxdb/issues/1856): Add `elapsed` function that returns the time delta between subsequent points. - [#5502](https://github.com/influxdata/influxdb/issues/5502): Add checksum verification to TSM inspect tool +- [#6444](https://github.com/influxdata/influxdb/pull/6444): Allow setting the config path through an environment variable and default config path. ### Bugfixes diff --git a/cmd/influxd/run/command.go b/cmd/influxd/run/command.go index 76d499d1601..f689be938cc 100644 --- a/cmd/influxd/run/command.go +++ b/cmd/influxd/run/command.go @@ -87,7 +87,7 @@ func (cmd *Command) Run(args ...string) error { runtime.SetBlockProfileRate(int(1 * time.Second)) // Parse config - config, err := cmd.ParseConfig(options.ConfigPath) + config, err := cmd.ParseConfig(options.GetConfigPath()) if err != nil { return fmt.Errorf("parse config: %s", err) } @@ -239,3 +239,28 @@ type Options struct { CPUProfile string MemProfile string } + +// GetConfigPath returns the config path from the options. +// It will return a path by searching in this order: +// 1. The CLI option in ConfigPath +// 2. The environment variable INFLUXDB_CONFIG_PATH +// 3. The first influxdb.conf file on the path: +// - ~/.influxdb +// - /etc/influxdb +func (opt *Options) GetConfigPath() string { + if opt.ConfigPath != "" { + return opt.ConfigPath + } else if envVar := os.Getenv("INFLUXDB_CONFIG_PATH"); envVar != "" { + return envVar + } + + for _, path := range []string{ + os.ExpandEnv("${HOME}/.influxdb/influxdb.conf"), + "/etc/influxdb/influxdb.conf", + } { + if _, err := os.Stat(path); err == nil { + return path + } + } + return "" +} diff --git a/cmd/influxd/run/config_command.go b/cmd/influxd/run/config_command.go index 9194148dd72..377cdb77095 100644 --- a/cmd/influxd/run/config_command.go +++ b/cmd/influxd/run/config_command.go @@ -36,7 +36,8 @@ func (cmd *PrintConfigCommand) Run(args ...string) error { } // Parse config from path. - config, err := cmd.parseConfig(*configPath) + opt := Options{ConfigPath: *configPath} + config, err := cmd.parseConfig(opt.GetConfigPath()) if err != nil { return fmt.Errorf("parse config: %s", err) }