From a4afb65a8d3312c34cf7bfb7cf2ae04f75ec0fcc Mon Sep 17 00:00:00 2001 From: Tudor Golubenco Date: Thu, 11 Aug 2016 14:29:44 +0300 Subject: [PATCH] Look for the config files relative to the path.config flag This is a proposal fix for #2171. There's a bit of a chicken and egg problem here, since defining the paths requires the configuration file and the other way around. The implemented logic is to: * if the `-path.config` flag is used, look for the config file relative to it * if not, but `-path.home` flag is used, look for the config file relative to the home path * else, look into the binary location, mostly for backwards compatibility I'm not sure we need the last point, we could leave it relative to the cwd, like most tools would do it. But this requires a BWC break. --- CHANGELOG.asciidoc | 6 +++ .../packer/platforms/debian/beatname.sh.j2 | 3 +- libbeat/cfgfile/cfgfile.go | 37 ++++++++++++------- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 240436c59f4b..eed5965df2ac 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -15,6 +15,8 @@ https://github.com/elastic/beats/compare/v5.0.0-alpha5...master[Check the HEAD d *Affecting all Beats* - Change Elasticsearch output index configuration to be based on format strings. If index has been configured, no date will be appended anymore to the index name. {pull}2119[2119] - Replace `output.kafka.use_type` by `output.kafka.topic` accepting a format string. {pull}2188[2188] +- If the path specified by the `-c` flag is not absolute and `-path.config` is not specified, it + is considered relative to the current working directory. {pull}2245[2245] *Metricbeat* - Change field type system.process.cpu.start_time from keyword to date. {issue}1565[1565] @@ -36,6 +38,8 @@ https://github.com/elastic/beats/compare/v5.0.0-alpha5...master[Check the HEAD d *Affecting all Beats* - Fix Elasticsearch structured error response parsing error. {issue}2229[2229] +- Fixed the run script to allow the overriding of the configuration file. {issue}2171[2171] + *Metricbeat* *Packetbeat* @@ -51,6 +55,7 @@ https://github.com/elastic/beats/compare/v5.0.0-alpha5...master[Check the HEAD d ==== Added *Affecting all Beats* + - Add script to generate the Kibana index-pattern from fields.yml. {pull}2122[2122] - Enhance redis output key selection based on format string. {pull}2169[2169] - Configurable redis `keys` using filters and format strings. {pull}2169[2169] @@ -63,6 +68,7 @@ https://github.com/elastic/beats/compare/v5.0.0-alpha5...master[Check the HEAD d - Add kafka version setting (optional) enabling kafka broker version support. {pull}2190[2190] - Add kafka message timestamp if at least version 0.10 is configured. {pull}2190[2190] - Enhance contains condition to work on fields that are arrays of strings. {issue}2237[2237] +- Lookup the configuration file relative to the `-path.config` CLI flag. {pull}2245[2245] *Metricbeat* diff --git a/dev-tools/packer/platforms/debian/beatname.sh.j2 b/dev-tools/packer/platforms/debian/beatname.sh.j2 index 9e8e8b779ce4..b11ce800dd62 100644 --- a/dev-tools/packer/platforms/debian/beatname.sh.j2 +++ b/dev-tools/packer/platforms/debian/beatname.sh.j2 @@ -1,10 +1,9 @@ #!/bin/bash -# Script to run {.beat_name} in foreground with the same path settings that +# Script to run {{.beat_name}} in foreground with the same path settings that # the init script / systemd unit file would do. /usr/share/{{.beat_name}}/bin/{{.beat_name}} -e \ - -c /etc/{{.beat_name}}/{{.beat_name}}.yml \ -path.home /usr/share/{{.beat_name}} \ -path.config /etc/{{.beat_name}} \ -path.data /var/lib/{{.beat_name}} \ diff --git a/libbeat/cfgfile/cfgfile.go b/libbeat/cfgfile/cfgfile.go index 5320622ec282..a1f65b15fc03 100644 --- a/libbeat/cfgfile/cfgfile.go +++ b/libbeat/cfgfile/cfgfile.go @@ -14,7 +14,7 @@ var ( // The default config cannot include the beat name as it is not initialized // when this variable is created. See ChangeDefaultCfgfileFlag which should // be called prior to flags.Parse(). - configfiles = flagArgList("c", "beat.yml", "Configuration file `path`") + configfiles = flagArgList("c", "beat.yml", "Configuration file, relative to path.config") overwrites = common.NewFlagConfig(nil, nil, "E", "Configuration overwrite") testConfig = flag.Bool("configtest", false, "Test configuration and exit.") @@ -29,7 +29,8 @@ var ( }) // home-path CLI flag (initialized in init) - homePath *string + homePath *string + configPath *string ) func init() { @@ -39,7 +40,7 @@ func init() { } homePath = makePathFlag("path.home", "Home path") - makePathFlag("path.config", "Configuration path") + configPath = makePathFlag("path.config", "Configuration path") makePathFlag("path.data", "Data path") makePathFlag("path.logs", "Logs path") } @@ -55,15 +56,7 @@ func mustNewConfigFrom(from interface{}) *common.Config { // ChangeDefaultCfgfileFlag replaces the value and default value for the `-c` // flag so that it reflects the beat name. func ChangeDefaultCfgfileFlag(beatName string) error { - path, err := filepath.Abs(filepath.Dir(os.Args[0])) - if err != nil { - return fmt.Errorf("Failed to set default config file location because "+ - "the absolute path to %s could not be obtained. %v", - os.Args[0], err) - } - - path = filepath.Join(path, beatName+".yml") - configfiles.SetDefault(path) + configfiles.SetDefault(beatName + ".yml") return nil } @@ -105,9 +98,27 @@ func Load(path string) (*common.Config, error) { var config *common.Config var err error + cfgpath := "" + if *configPath != "" { + cfgpath = *configPath + } else if *homePath != "" { + cfgpath = *homePath + } + if path == "" { - config, err = common.LoadFiles(configfiles.list...) + list := []string{} + for _, cfg := range configfiles.list { + if !filepath.IsAbs(cfg) { + list = append(list, filepath.Join(cfgpath, cfg)) + } else { + list = append(list, cfg) + } + } + config, err = common.LoadFiles(list...) } else { + if !filepath.IsAbs(path) { + path = filepath.Join(cfgpath, path) + } config, err = common.LoadFile(path) } if err != nil {