2014-10-05 09:50:04 -05:00
|
|
|
// Copyright 2014 Unknwon
|
|
|
|
// Copyright 2014 Torkel Ödegaard
|
|
|
|
|
2014-10-04 06:33:20 -05:00
|
|
|
package setting
|
|
|
|
|
|
|
|
import (
|
2015-02-12 06:31:41 -06:00
|
|
|
"fmt"
|
2014-10-04 06:33:20 -05:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Unknwon/com"
|
2014-10-05 09:50:04 -05:00
|
|
|
"github.com/macaron-contrib/session"
|
2015-01-27 03:09:54 -06:00
|
|
|
"gopkg.in/ini.v1"
|
2014-10-05 09:50:04 -05:00
|
|
|
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/log"
|
2014-10-04 06:33:20 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Scheme string
|
|
|
|
|
|
|
|
const (
|
|
|
|
HTTP Scheme = "http"
|
|
|
|
HTTPS Scheme = "https"
|
|
|
|
)
|
|
|
|
|
2014-12-16 05:04:08 -06:00
|
|
|
const (
|
|
|
|
DEV string = "development"
|
|
|
|
PROD string = "production"
|
|
|
|
TEST string = "test"
|
|
|
|
)
|
|
|
|
|
2014-10-04 06:33:20 -05:00
|
|
|
var (
|
|
|
|
// App settings.
|
2014-12-16 05:04:08 -06:00
|
|
|
Env string = DEV
|
2014-10-04 06:33:20 -05:00
|
|
|
AppName string
|
|
|
|
AppUrl string
|
|
|
|
AppSubUrl string
|
|
|
|
|
2015-01-05 03:46:58 -06:00
|
|
|
// build
|
|
|
|
BuildVersion string
|
|
|
|
BuildCommit string
|
|
|
|
BuildStamp int64
|
|
|
|
|
2014-10-04 06:33:20 -05:00
|
|
|
// Log settings.
|
|
|
|
LogRootPath string
|
|
|
|
LogModes []string
|
|
|
|
LogConfigs []string
|
|
|
|
|
|
|
|
// Http server options
|
|
|
|
Protocol Scheme
|
|
|
|
Domain string
|
|
|
|
HttpAddr, HttpPort string
|
|
|
|
SshPort int
|
|
|
|
CertFile, KeyFile string
|
2014-10-05 09:50:04 -05:00
|
|
|
RouterLogging bool
|
|
|
|
StaticRootPath string
|
2015-01-14 03:34:14 -06:00
|
|
|
EnableGzip bool
|
2014-10-05 09:50:04 -05:00
|
|
|
|
2015-01-27 03:09:54 -06:00
|
|
|
// Security settings.
|
|
|
|
SecretKey string
|
|
|
|
LogInRememberDays int
|
|
|
|
CookieUserName string
|
|
|
|
CookieRememberName string
|
2015-01-29 08:46:54 -06:00
|
|
|
DisableUserSignUp bool
|
2015-01-27 03:09:54 -06:00
|
|
|
|
2015-01-27 08:14:53 -06:00
|
|
|
// single account
|
|
|
|
SingleAccountMode bool
|
|
|
|
DefaultAccountName string
|
|
|
|
DefaultAccountRole string
|
|
|
|
|
2015-01-07 09:37:24 -06:00
|
|
|
// Http auth
|
2015-01-27 08:45:27 -06:00
|
|
|
AdminUser string
|
|
|
|
AdminPassword string
|
|
|
|
|
|
|
|
AnonymousEnabled bool
|
|
|
|
AnonymousAccountName string
|
|
|
|
AnonymousAccountRole string
|
2015-01-07 09:37:24 -06:00
|
|
|
|
2014-10-05 09:50:04 -05:00
|
|
|
// Session settings.
|
2014-12-30 03:28:27 -06:00
|
|
|
SessionOptions session.Options
|
2014-10-04 06:33:20 -05:00
|
|
|
|
|
|
|
// Global setting objects.
|
2015-01-01 08:29:10 -06:00
|
|
|
WorkDir string
|
2015-01-27 03:09:54 -06:00
|
|
|
Cfg *ini.File
|
2014-10-04 06:33:20 -05:00
|
|
|
ConfRootPath string
|
|
|
|
CustomPath string // Custom directory path.
|
|
|
|
ProdMode bool
|
|
|
|
RunUser string
|
|
|
|
IsWindows bool
|
2014-10-06 14:31:54 -05:00
|
|
|
|
|
|
|
// PhantomJs Rendering
|
|
|
|
ImagesDir string
|
|
|
|
PhantomDir string
|
2014-10-04 06:33:20 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
IsWindows = runtime.GOOS == "windows"
|
|
|
|
log.NewLogger(0, "console", `{"level": 0}`)
|
2015-02-12 04:55:55 -06:00
|
|
|
WorkDir, _ = filepath.Abs(".")
|
2014-10-04 06:33:20 -05:00
|
|
|
}
|
|
|
|
|
2015-01-05 00:59:18 -06:00
|
|
|
func findConfigFiles() []string {
|
2015-01-01 08:29:10 -06:00
|
|
|
ConfRootPath = path.Join(WorkDir, "conf")
|
2015-01-05 00:59:18 -06:00
|
|
|
filenames := make([]string, 0)
|
2014-10-04 06:33:20 -05:00
|
|
|
|
2015-01-01 08:29:10 -06:00
|
|
|
configFile := path.Join(ConfRootPath, "grafana.ini")
|
|
|
|
if com.IsFile(configFile) {
|
2015-01-05 00:59:18 -06:00
|
|
|
filenames = append(filenames, configFile)
|
2015-01-01 08:29:10 -06:00
|
|
|
}
|
2015-01-05 00:59:18 -06:00
|
|
|
|
2015-01-01 08:29:10 -06:00
|
|
|
configFile = path.Join(ConfRootPath, "grafana.dev.ini")
|
|
|
|
if com.IsFile(configFile) {
|
2015-01-05 00:59:18 -06:00
|
|
|
filenames = append(filenames, configFile)
|
2015-01-01 08:29:10 -06:00
|
|
|
}
|
2015-01-05 00:59:18 -06:00
|
|
|
|
|
|
|
configFile = path.Join(ConfRootPath, "grafana.custom.ini")
|
2015-01-01 08:29:10 -06:00
|
|
|
if com.IsFile(configFile) {
|
2015-01-05 00:59:18 -06:00
|
|
|
filenames = append(filenames, configFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(filenames) == 0 {
|
|
|
|
log.Fatal(3, "Could not find any config file")
|
2014-10-04 06:33:20 -05:00
|
|
|
}
|
|
|
|
|
2015-01-05 00:59:18 -06:00
|
|
|
return filenames
|
2015-01-01 08:29:10 -06:00
|
|
|
}
|
2014-10-04 06:33:20 -05:00
|
|
|
|
2015-01-27 03:09:54 -06:00
|
|
|
func parseAppUrlAndSubUrl(section *ini.Section) (string, string) {
|
|
|
|
appUrl := section.Key("root_url").MustString("http://localhost:3000/")
|
|
|
|
if appUrl[len(appUrl)-1] != '/' {
|
|
|
|
appUrl += "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if has app suburl.
|
2015-01-30 07:21:32 -06:00
|
|
|
url, err := url.Parse(appUrl)
|
2015-01-27 03:09:54 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(4, "Invalid root_url(%s): %s", appUrl, err)
|
|
|
|
}
|
|
|
|
appSubUrl := strings.TrimSuffix(url.Path, "/")
|
|
|
|
|
|
|
|
return appUrl, appSubUrl
|
|
|
|
}
|
|
|
|
|
2015-02-06 07:17:40 -06:00
|
|
|
func ToAbsUrl(relativeUrl string) string {
|
2015-02-04 04:35:59 -06:00
|
|
|
return AppUrl + relativeUrl
|
|
|
|
}
|
|
|
|
|
2015-02-12 04:55:55 -06:00
|
|
|
func loadEnvVariableOverrides() {
|
2015-02-12 06:31:41 -06:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-12 04:55:55 -06:00
|
|
|
}
|
|
|
|
|
2015-02-15 15:57:49 -06:00
|
|
|
func NewConfigContext(config string) {
|
2015-01-05 00:59:18 -06:00
|
|
|
configFiles := findConfigFiles()
|
2015-01-01 08:29:10 -06:00
|
|
|
|
2015-02-15 15:57:49 -06:00
|
|
|
if config != "" {
|
|
|
|
configFiles = append(configFiles, config)
|
|
|
|
}
|
|
|
|
|
2015-01-01 08:29:10 -06:00
|
|
|
var err error
|
2014-10-04 06:33:20 -05:00
|
|
|
|
2015-01-27 03:09:54 -06:00
|
|
|
for i, file := range configFiles {
|
|
|
|
if i == 0 {
|
|
|
|
Cfg, err = ini.Load(configFiles[i])
|
|
|
|
} else {
|
|
|
|
err = Cfg.Append(configFiles[i])
|
|
|
|
}
|
2015-01-05 00:59:18 -06:00
|
|
|
|
|
|
|
if err != nil {
|
2015-01-27 03:09:54 -06:00
|
|
|
log.Fatal(4, "Fail to parse config file: %v, error: %v", file, err)
|
2015-01-05 00:59:18 -06:00
|
|
|
}
|
2014-10-04 06:33:20 -05:00
|
|
|
}
|
|
|
|
|
2015-02-12 04:55:55 -06:00
|
|
|
loadEnvVariableOverrides()
|
|
|
|
|
2015-01-27 03:09:54 -06:00
|
|
|
AppName = Cfg.Section("").Key("app_name").MustString("Grafana")
|
|
|
|
Env = Cfg.Section("").Key("app_mode").MustString("development")
|
2014-10-04 06:33:20 -05:00
|
|
|
|
2015-01-27 03:09:54 -06:00
|
|
|
server := Cfg.Section("server")
|
|
|
|
AppUrl, AppSubUrl = parseAppUrlAndSubUrl(server)
|
2014-10-04 06:33:20 -05:00
|
|
|
|
|
|
|
Protocol = HTTP
|
2015-01-27 03:09:54 -06:00
|
|
|
if server.Key("protocol").MustString("http") == "https" {
|
2014-10-04 06:33:20 -05:00
|
|
|
Protocol = HTTPS
|
2015-01-27 03:09:54 -06:00
|
|
|
CertFile = server.Key("cert_file").String()
|
|
|
|
KeyFile = server.Key("cert_file").String()
|
2014-10-04 06:33:20 -05:00
|
|
|
}
|
2015-01-27 03:09:54 -06:00
|
|
|
|
|
|
|
Domain = server.Key("domain").MustString("localhost")
|
|
|
|
HttpAddr = server.Key("http_addr").MustString("0.0.0.0")
|
|
|
|
HttpPort = server.Key("http_port").MustString("3000")
|
2014-10-05 08:34:24 -05:00
|
|
|
|
2015-01-27 03:09:54 -06:00
|
|
|
StaticRootPath = server.Key("static_root_path").MustString(path.Join(WorkDir, "webapp"))
|
|
|
|
RouterLogging = server.Key("router_logging").MustBool(false)
|
|
|
|
EnableGzip = server.Key("enable_gzip").MustBool(false)
|
|
|
|
|
|
|
|
security := Cfg.Section("security")
|
|
|
|
SecretKey = security.Key("secret_key").String()
|
|
|
|
LogInRememberDays = security.Key("login_remember_days").MustInt()
|
|
|
|
CookieUserName = security.Key("cookie_username").String()
|
|
|
|
CookieRememberName = security.Key("cookie_remember_name").String()
|
2015-01-29 08:46:54 -06:00
|
|
|
DisableUserSignUp = security.Key("disable_user_signup").MustBool(false)
|
2014-10-06 14:31:54 -05:00
|
|
|
|
2015-01-27 08:45:27 -06:00
|
|
|
// admin
|
|
|
|
AdminUser = security.Key("admin_user").String()
|
|
|
|
AdminPassword = security.Key("admin_password").String()
|
|
|
|
|
2015-01-27 08:14:53 -06:00
|
|
|
// single account
|
|
|
|
SingleAccountMode = Cfg.Section("account.single").Key("enabled").MustBool(false)
|
|
|
|
DefaultAccountName = Cfg.Section("account.single").Key("account_name").MustString("main")
|
|
|
|
DefaultAccountRole = Cfg.Section("account.single").Key("default_role").In("Editor", []string{"Editor", "Admin", "Viewer"})
|
|
|
|
|
2015-01-27 08:45:27 -06:00
|
|
|
// anonymous access
|
|
|
|
AnonymousEnabled = Cfg.Section("auth.anonymous").Key("enabled").MustBool(false)
|
|
|
|
AnonymousAccountName = Cfg.Section("auth.anonymous").Key("account_name").String()
|
|
|
|
AnonymousAccountRole = Cfg.Section("auth.anonymous").Key("account_role").String()
|
2015-01-07 09:37:24 -06:00
|
|
|
|
2014-10-06 14:31:54 -05:00
|
|
|
// PhantomJS rendering
|
|
|
|
ImagesDir = "data/png"
|
2015-01-20 10:53:20 -06:00
|
|
|
PhantomDir = "vendor/phantomjs"
|
2014-11-14 10:13:33 -06:00
|
|
|
|
2015-01-27 03:09:54 -06:00
|
|
|
LogRootPath = Cfg.Section("log").Key("root_path").MustString(path.Join(WorkDir, "/data/log"))
|
2014-10-05 09:50:04 -05:00
|
|
|
|
2015-01-15 07:44:15 -06:00
|
|
|
readSessionConfig()
|
|
|
|
}
|
2014-12-30 03:28:27 -06:00
|
|
|
|
2015-01-15 07:44:15 -06:00
|
|
|
func readSessionConfig() {
|
2015-01-27 03:09:54 -06:00
|
|
|
sec := Cfg.Section("session")
|
2014-12-30 03:28:27 -06:00
|
|
|
SessionOptions = session.Options{}
|
2015-01-27 03:09:54 -06:00
|
|
|
SessionOptions.Provider = sec.Key("provider").In("memory", []string{"memory", "file", "redis", "mysql"})
|
|
|
|
SessionOptions.ProviderConfig = strings.Trim(sec.Key("provider_config").String(), "\" ")
|
|
|
|
SessionOptions.CookieName = sec.Key("cookie_name").MustString("grafana_sess")
|
2014-12-30 03:28:27 -06:00
|
|
|
SessionOptions.CookiePath = AppSubUrl
|
2015-01-27 03:09:54 -06:00
|
|
|
SessionOptions.Secure = sec.Key("cookie_secure").MustBool()
|
|
|
|
SessionOptions.Gclifetime = Cfg.Section("session").Key("gc_interval_time").MustInt64(86400)
|
|
|
|
SessionOptions.Maxlifetime = Cfg.Section("session").Key("session_life_time").MustInt64(86400)
|
2014-12-30 03:28:27 -06:00
|
|
|
|
|
|
|
if SessionOptions.Provider == "file" {
|
|
|
|
os.MkdirAll(path.Dir(SessionOptions.ProviderConfig), os.ModePerm)
|
2014-10-05 09:50:04 -05:00
|
|
|
}
|
|
|
|
}
|