Skip to content

Commit

Permalink
search for default config on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielc committed Jul 19, 2016
1 parent 7890a7d commit 7d77bbb
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
- [#710](https://github.com/influxdata/kapacitor/pull/710): Fix infinite loop when parsing unterminated regex in TICKscript.
- [#711](https://github.com/influxdata/kapacitor/issue/711): Fix where database name with quotes breaks subscription startup logic.
- [#719](https://github.com/influxdata/kapacitor/pull/719): Fix panic on replay.
- [#723](https://github.com/influxdata/kapacitor/pull/723): BREAKING: Search for valid configuration on startup in ~/.kapacitor and /etc/kapacitor/.
This is so that the -config CLI flag is not required if the configuration is found in a standard location.
The configuration file being used is always logged to STDERR.

## v1.0.0-beta3 [2016-07-09]

Expand Down
6 changes: 3 additions & 3 deletions cmd/kapacitord/run/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (cmd *Command) Run(args ...string) error {
fmt.Print(logo)

// Parse config
config, err := cmd.ParseConfig(options.ConfigPath)
config, err := cmd.ParseConfig(FindConfigPath(options.ConfigPath))
if err != nil {
return fmt.Errorf("parse config: %s", err)
}
Expand Down Expand Up @@ -206,11 +206,11 @@ func (cmd *Command) writePIDFile(path string) error {
func (cmd *Command) ParseConfig(path string) (*server.Config, error) {
// Use demo configuration if no config path is specified.
if path == "" {
log.Println("no configuration provided, using default settings")
log.Println("No configuration provided, using default settings")
return server.NewDemoConfig()
}

log.Printf("Using configuration at: %s\n", path)
log.Println("Using configuration at:", path)

config := server.NewConfig()
if _, err := toml.DecodeFile(path, &config); err != nil {
Expand Down
40 changes: 37 additions & 3 deletions cmd/kapacitord/run/config_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"io"
"log"
"os"

"github.com/BurntSushi/toml"
Expand Down Expand Up @@ -38,7 +39,7 @@ func (cmd *PrintConfigCommand) Run(args ...string) error {
}

// Parse config from path.
config, err := cmd.parseConfig(*configPath)
config, err := cmd.parseConfig(FindConfigPath(*configPath))
if err != nil {
return fmt.Errorf("parse config: %s", err)
}
Expand All @@ -64,14 +65,47 @@ func (cmd *PrintConfigCommand) Run(args ...string) error {
return nil
}

// FindConfigPath returns the config path specified or searches for a valid config path.
// It will return a path by searching in this order:
// 1. The given configPath
// 2. The environment variable KAPACITOR_CONFIG_PATH
// 3. The first non empty kapacitor.conf file in the path:
// - ~/.kapacitor/
// - /etc/kapacitor/
func FindConfigPath(configPath string) string {
if configPath != "" {
if configPath == os.DevNull {
return ""
}
return configPath
} else if envVar := os.Getenv("KAPACITOR_CONFIG_PATH"); envVar != "" {
return envVar
}

for _, path := range []string{
os.ExpandEnv("${HOME}/.kapacitor/kapacitor.conf"),
"/etc/kapacitor/kapacitor.conf",
} {
if fi, err := os.Stat(path); err == nil && fi.Size() != 0 {
return path
}
}
return ""
}

// ParseConfig parses the config at path.
// Returns a demo configuration if path is blank.
func (cmd *PrintConfigCommand) parseConfig(path string) (*server.Config, error) {
config, err := server.NewDemoConfig()
if err != nil {
config = server.NewConfig()
}

if path == "" {
return server.NewDemoConfig()
return config, nil
}

config := server.NewConfig()
log.Println("Merging with configuration at:", path)
if _, err := toml.DecodeFile(path, &config); err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions tickdoc.conf
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
root = "/kapacitor/v1.0b/nodes"
root = "/kapacitor/v1.0/nodes"
page-header = '''---
title: {{ .Title }}
note: Auto generated by tickdoc

menu:
kapacitor_10b:
kapacitor_1:
name: {{ .Name }}
identifier: {{ .Identifier }}
weight: {{ .Weight }}
Expand Down

0 comments on commit 7d77bbb

Please sign in to comment.