mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Configuration file options can now be overriden using environment variables using GF_<SectionName>_<KeyName> syntax, if Section name contains dots in config they are replaced with underscores, and the section name and keyname needs to be all upper case, #1473
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
package setting
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
@@ -148,7 +149,19 @@ func ToAbsUrl(relativeUrl string) string {
|
||||
}
|
||||
|
||||
func loadEnvVariableOverrides() {
|
||||
for _, section := range Cfg.Sections() {
|
||||
for _, key := range section.Keys() {
|
||||
sectionName := strings.ToUpper(strings.Replace(section.Name(), ".", "_", -1))
|
||||
keyName := strings.ToUpper(strings.Replace(key.Name(), ".", "_", -1))
|
||||
envKey := fmt.Sprintf("GF_%s_%s", sectionName, keyName)
|
||||
envValue := os.Getenv(envKey)
|
||||
|
||||
if len(envValue) > 0 {
|
||||
log.Info("Setting: ENV override found: %s", envKey)
|
||||
key.SetValue(envValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewConfigContext() {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package setting
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
@@ -17,6 +18,15 @@ func TestLoadingSettings(t *testing.T) {
|
||||
NewConfigContext()
|
||||
|
||||
So(AppName, ShouldEqual, "Grafana")
|
||||
So(AdminUser, ShouldEqual, "admin")
|
||||
})
|
||||
|
||||
Convey("Should be able to override via environment variables", func() {
|
||||
os.Setenv("GF_SECURITY_ADMIN_USER", "superduper")
|
||||
NewConfigContext()
|
||||
|
||||
So(AdminUser, ShouldEqual, "superduper")
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user