From 9c0902350802e6a353d481b1e1c28858f0ae772b Mon Sep 17 00:00:00 2001 From: "Jonathan A. Sternberg" Date: Thu, 21 Apr 2016 15:54:45 -0400 Subject: [PATCH] Allow setting the config path through an environment variable The config path previously could only be specified through the command line options. This made it very difficult to set a default config path without using any option. Now the environment variable can be set so the default configuration path is set to a specific place, but can be overwritten using the command line option. The primary purpose of this is so the Docker container can have a default configuration file, but not have to parse the command line options to figure out if a different configuration file has been specified while still allowing the user to only type `influxd` and have the program start correctly. This might also help #6392 as it would allow a default configuration location to be included with the package by setting an environment variable. A default search path is also provided now with checking the following paths for a config file when none is specified: * `~/.influxdb/influxdb.conf` * `/etc/influxdb/influxdb.conf` The config command has also been modified to read this config file before outputting a sample config. --- CHANGELOG.md | 1 + cmd/influxd/run/command.go | 27 ++++++++++++++++++++++++++- cmd/influxd/run/config_command.go | 3 ++- 3 files changed, 29 insertions(+), 2 deletions(-) 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) }