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:
parent
2c16b0f0f3
commit
79f798f67b
@ -5,9 +5,9 @@ import (
|
|||||||
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/go-xorm/xorm"
|
||||||
|
"github.com/grafana/grafana/pkg/log"
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
"github.com/grafana/grafana/pkg/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Migrator struct {
|
type Migrator struct {
|
||||||
@ -70,7 +70,7 @@ func (mg *Migrator) GetMigrationLog() (map[string]MigrationLog, error) {
|
|||||||
|
|
||||||
func (mg *Migrator) Start() error {
|
func (mg *Migrator) Start() error {
|
||||||
if mg.LogLevel <= log.INFO {
|
if mg.LogLevel <= log.INFO {
|
||||||
log.Info("Migrator:: Starting DB migration")
|
log.Info("Migrator: Starting DB migration")
|
||||||
}
|
}
|
||||||
|
|
||||||
logMap, err := mg.GetMigrationLog()
|
logMap, err := mg.GetMigrationLog()
|
||||||
@ -82,7 +82,7 @@ func (mg *Migrator) Start() error {
|
|||||||
_, exists := logMap[m.Id()]
|
_, exists := logMap[m.Id()]
|
||||||
if exists {
|
if exists {
|
||||||
if mg.LogLevel <= log.DEBUG {
|
if mg.LogLevel <= log.DEBUG {
|
||||||
log.Debug("Migrator:: Skipping migration: %v, Already executed", m.Id())
|
log.Debug("Migrator: Skipping migration: %v, Already executed", m.Id())
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -114,13 +114,13 @@ func (mg *Migrator) Start() error {
|
|||||||
|
|
||||||
func (mg *Migrator) exec(m Migration) error {
|
func (mg *Migrator) exec(m Migration) error {
|
||||||
if mg.LogLevel <= log.INFO {
|
if mg.LogLevel <= log.INFO {
|
||||||
log.Info("Migrator::exec migration id: %v", m.Id())
|
log.Info("Migrator: exec migration id: %v", m.Id())
|
||||||
}
|
}
|
||||||
|
|
||||||
err := mg.inTransaction(func(sess *xorm.Session) error {
|
err := mg.inTransaction(func(sess *xorm.Session) error {
|
||||||
_, err := sess.Exec(m.Sql(mg.dialect))
|
_, err := sess.Exec(m.Sql(mg.dialect))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(3, "Migrator::exec FAILED migration id: %v, err: %v", m.Id(), err)
|
log.Error(3, "Migrator: exec FAILED migration id: %v, err: %v", m.Id(), err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
package setting
|
package setting
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@ -148,7 +149,19 @@ func ToAbsUrl(relativeUrl string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func loadEnvVariableOverrides() {
|
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() {
|
func NewConfigContext() {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package setting
|
package setting
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -17,6 +18,15 @@ func TestLoadingSettings(t *testing.T) {
|
|||||||
NewConfigContext()
|
NewConfigContext()
|
||||||
|
|
||||||
So(AppName, ShouldEqual, "Grafana")
|
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")
|
||||||
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user