Commit d1767144 by Torkel Ödegaard

Reworking configuration loading and overriding

parent a991cda2
[run] [run]
init_cmds = [ init_cmds = [
["go", "build", "-o", "./bin/grafana"], ["go", "build", "-o", "./bin/grafana-server"],
["./bin/grafana", "web"] ["./bin/grafana-server"]
] ]
watch_all = true watch_all = true
watch_dirs = [ watch_dirs = [
...@@ -12,6 +12,6 @@ watch_dirs = [ ...@@ -12,6 +12,6 @@ watch_dirs = [
watch_exts = [".go", ".ini"] watch_exts = [".go", ".ini"]
build_delay = 1500 build_delay = 1500
cmds = [ cmds = [
["go", "build", "-o", "./bin/grafana"], ["go", "build", "-o", "./bin/grafana-server"],
["./bin/grafana", "web"] ["./bin/grafana-server"]
] ]
...@@ -209,7 +209,7 @@ func test(pkg string) { ...@@ -209,7 +209,7 @@ func test(pkg string) {
} }
func build(pkg string, tags []string) { func build(pkg string, tags []string) {
binary := "./bin/grafana" binary := "./bin/grafana-server"
if goos == "windows" { if goos == "windows" {
binary += ".exe" binary += ".exe"
} }
......
app_name = Grafana app_name = Grafana
app_mode = production app_mode = production
[paths]
; data_path ; data_path
; where rendered png images are temporarily stored ; where rendered png images are temporarily stored
; file based sessions are stored here (if file based session is configured below) ; file based sessions are stored here (if file based session is configured below)
; the database is stored here if sqlite3 database is used ; the database is stored here if sqlite3 database is used
; can be overriden from command line --data-path data = data
; defaults to `data` path relative to working directory logs = data/log
data_path =
[server] [server]
; protocol (http or https) ; protocol (http or https)
...@@ -21,7 +21,7 @@ domain = localhost ...@@ -21,7 +21,7 @@ domain = localhost
; the full public facing url ; the full public facing url
root_url = %(protocol)s://%(domain)s:%(http_port)s/ root_url = %(protocol)s://%(domain)s:%(http_port)s/
router_logging = false router_logging = false
; the path relative to the binary where the static (html/js/css) files are placed ; the path relative home path where frontend assets are located
static_root_path = public static_root_path = public
; enable gzip ; enable gzip
enable_gzip = false enable_gzip = false
...@@ -47,7 +47,7 @@ user = root ...@@ -47,7 +47,7 @@ user = root
password = password =
; For "postgres" only, either "disable", "require" or "verify-full" ; For "postgres" only, either "disable", "require" or "verify-full"
ssl_mode = disable ssl_mode = disable
; For "sqlite3" only, path relative to data_dir setting ; For "sqlite3" only, path relative to data_path setting
path = grafana.db path = grafana.db
[session] [session]
...@@ -55,7 +55,7 @@ path = grafana.db ...@@ -55,7 +55,7 @@ path = grafana.db
provider = file provider = file
; Provider config options ; Provider config options
; memory: not have any config yet ; memory: not have any config yet
; file: session dir path, is relative to grafana data_dir ; file: session dir path, is relative to grafana data_path
; redis: config like redis server addr, poolSize, password, e.g. `127.0.0.1:6379,100,grafana` ; redis: config like redis server addr, poolSize, password, e.g. `127.0.0.1:6379,100,grafana`
; mysql: go-sql-driver/mysql dsn config string, e.g. `user:password@tcp(127.0.0.1)/database_name` ; mysql: go-sql-driver/mysql dsn config string, e.g. `user:password@tcp(127.0.0.1)/database_name`
provider_config = sessions provider_config = sessions
...@@ -117,11 +117,6 @@ token_url = https://accounts.google.com/o/oauth2/token ...@@ -117,11 +117,6 @@ token_url = https://accounts.google.com/o/oauth2/token
; allowed_domains = mycompany.com othercompany.com ; allowed_domains = mycompany.com othercompany.com
[log] [log]
; root_path
; for deb or rpm package installs this is specified via command line
; change it in /etc/default/grafana, it defaults to /var/log/grafana
; for non package installs (running manually) defaults to `log` dir under data_dir
root_path =
; Either "console", "file", default is "console" ; Either "console", "file", default is "console"
; Use comma to separate multiple modes, e.g. "console, file" ; Use comma to separate multiple modes, e.g. "console, file"
mode = console, file mode = console, file
......
...@@ -64,15 +64,14 @@ func main() { ...@@ -64,15 +64,14 @@ func main() {
} }
func initRuntime() { func initRuntime() {
setting.NewConfigContext(&setting.CommandLineArgs{}) setting.NewConfigContext(&setting.CommandLineArgs{
Config: *configFile,
Args: flag.Args(),
})
log.Info("Starting Grafana") log.Info("Starting Grafana")
log.Info("Version: %v, Commit: %v, Build date: %v", setting.BuildVersion, setting.BuildCommit, time.Unix(setting.BuildStamp, 0)) log.Info("Version: %v, Commit: %v, Build date: %v", setting.BuildVersion, setting.BuildCommit, time.Unix(setting.BuildStamp, 0))
setting.LogLoadedConfigFiles() setting.LogConfigurationInfo()
log.Info("Working Path: %s", setting.WorkPath)
log.Info("Data Path: %s", setting.DataPath)
log.Info("Log Path: %s", setting.LogRootPath)
sqlstore.NewEngine() sqlstore.NewEngine()
sqlstore.EnsureAdminUser() sqlstore.EnsureAdminUser()
......
...@@ -86,7 +86,7 @@ func SetEngine(engine *xorm.Engine, enableLog bool) (err error) { ...@@ -86,7 +86,7 @@ func SetEngine(engine *xorm.Engine, enableLog bool) (err error) {
} }
if enableLog { if enableLog {
logPath := path.Join(setting.LogRootPath, "xorm.log") logPath := path.Join(setting.LogsPath, "xorm.log")
os.MkdirAll(path.Dir(logPath), os.ModePerm) os.MkdirAll(path.Dir(logPath), os.ModePerm)
f, err := os.Create(logPath) f, err := os.Create(logPath)
......
...@@ -10,12 +10,12 @@ import ( ...@@ -10,12 +10,12 @@ import (
func TestLoadingSettings(t *testing.T) { func TestLoadingSettings(t *testing.T) {
WorkDir, _ = filepath.Abs("../../") HomePath, _ = filepath.Abs("../../")
Convey("Testing loading settings from ini file", t, func() { Convey("Testing loading settings from ini file", t, func() {
Convey("Given the default ini files", func() { Convey("Given the default ini files", func() {
NewConfigContext("") NewConfigContext(&CommandLineArgs{})
So(AppName, ShouldEqual, "Grafana") So(AppName, ShouldEqual, "Grafana")
So(AdminUser, ShouldEqual, "admin") So(AdminUser, ShouldEqual, "admin")
...@@ -23,9 +23,63 @@ func TestLoadingSettings(t *testing.T) { ...@@ -23,9 +23,63 @@ func TestLoadingSettings(t *testing.T) {
Convey("Should be able to override via environment variables", func() { Convey("Should be able to override via environment variables", func() {
os.Setenv("GF_SECURITY_ADMIN_USER", "superduper") os.Setenv("GF_SECURITY_ADMIN_USER", "superduper")
NewConfigContext("") NewConfigContext(&CommandLineArgs{})
So(AdminUser, ShouldEqual, "superduper") So(AdminUser, ShouldEqual, "superduper")
So(DataPath, ShouldEqual, filepath.Join(HomePath, "data"))
So(LogsPath, ShouldEqual, filepath.Join(DataPath, "log"))
})
Convey("Should get property map from command line args array", func() {
props := getCommandLineProperties([]string{"cfg:test=value", "cfg:map.test=1"})
So(len(props), ShouldEqual, 2)
So(props["test"], ShouldEqual, "value")
So(props["map.test"], ShouldEqual, "1")
})
Convey("Should be able to override via command line", func() {
NewConfigContext(&CommandLineArgs{
Args: []string{"cfg:paths.data=/tmp/data", "cfg:paths.logs=/tmp/logs"},
})
So(DataPath, ShouldEqual, "/tmp/data")
So(LogsPath, ShouldEqual, "/tmp/logs")
})
Convey("Should be able to override defaults via command line", func() {
NewConfigContext(&CommandLineArgs{
Args: []string{"cfg:default.paths.data=/tmp/data"},
})
So(DataPath, ShouldEqual, "/tmp/data")
})
Convey("Defaults can be overriden in specified config file", func() {
NewConfigContext(&CommandLineArgs{
Args: []string{"cfg:default.paths.data=/tmp/data"},
Config: filepath.Join(HomePath, "tests/config-files/override.ini"),
})
So(DataPath, ShouldEqual, "/tmp/override")
})
Convey("Command line overrides specified config file", func() {
NewConfigContext(&CommandLineArgs{
Args: []string{"cfg:paths.data=/tmp/data"},
Config: filepath.Join(HomePath, "tests/config-files/override.ini"),
})
So(DataPath, ShouldEqual, "/tmp/data")
})
Convey("Can use environment variables in config values", func() {
os.Setenv("GF_DATA_PATH", "/tmp/env_override")
NewConfigContext(&CommandLineArgs{
Args: []string{"cfg:paths.data=${GF_DATA_PATH}"},
})
So(DataPath, ShouldEqual, "/tmp/env_override")
}) })
}) })
......
...@@ -55,7 +55,7 @@ module.exports = function(grunt) { ...@@ -55,7 +55,7 @@ module.exports = function(grunt) {
grunt.config('copy.backend_bin', { grunt.config('copy.backend_bin', {
cwd: 'bin', cwd: 'bin',
expand: true, expand: true,
src: ['grafana'], src: ['grafana-server'],
options: { mode: true}, options: { mode: true},
dest: '<%= tempDir %>/bin/' dest: '<%= tempDir %>/bin/'
}); });
......
[paths]
data = /tmp/override
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment