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-04-09 05:16:59 -05:00
|
|
|
"bytes"
|
2015-04-19 02:29:08 -05:00
|
|
|
"encoding/json"
|
2015-02-12 06:31:41 -06:00
|
|
|
"fmt"
|
2014-10-04 06:33:20 -05:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2015-04-09 05:16:59 -05:00
|
|
|
"regexp"
|
2014-10-04 06:33:20 -05:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
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"
|
2015-04-19 02:14:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
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
|
|
|
|
|
2015-04-09 05:16:59 -05:00
|
|
|
// Paths
|
|
|
|
LogsPath string
|
|
|
|
HomePath string
|
|
|
|
DataPath string
|
|
|
|
|
2014-10-04 06:33:20 -05:00
|
|
|
// Log settings.
|
2015-04-09 05:16:59 -05:00
|
|
|
LogModes []string
|
2015-04-19 02:29:08 -05:00
|
|
|
LogConfigs []util.DynMap
|
2014-10-04 06:33:20 -05:00
|
|
|
|
|
|
|
// 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
|
2015-05-05 04:21:06 -05:00
|
|
|
EnforceDomain 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-05-01 01:40:13 -05:00
|
|
|
DisableGravatar bool
|
2015-01-27 03:09:54 -06:00
|
|
|
|
2015-03-11 10:19:29 -05:00
|
|
|
// User settings
|
|
|
|
AllowUserSignUp bool
|
|
|
|
AllowUserOrgCreate bool
|
|
|
|
AutoAssignOrg bool
|
|
|
|
AutoAssignOrgRole string
|
2015-06-01 09:29:45 -05:00
|
|
|
ViewerRoleMode string
|
2015-01-27 08:14:53 -06:00
|
|
|
|
2015-01-07 09:37:24 -06:00
|
|
|
// Http auth
|
2015-01-27 08:45:27 -06:00
|
|
|
AdminUser string
|
|
|
|
AdminPassword string
|
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
AnonymousEnabled bool
|
|
|
|
AnonymousOrgName string
|
|
|
|
AnonymousOrgRole string
|
2015-01-07 09:37:24 -06:00
|
|
|
|
2015-05-01 04:55:59 -05:00
|
|
|
// Auth proxy settings
|
|
|
|
AuthProxyEnabled bool
|
|
|
|
AuthProxyHeaderName string
|
|
|
|
AuthProxyHeaderProperty string
|
|
|
|
AuthProxyAutoSignUp bool
|
|
|
|
|
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-27 03:09:54 -06:00
|
|
|
Cfg *ini.File
|
2014-10-04 06:33:20 -05:00
|
|
|
ConfRootPath string
|
|
|
|
IsWindows bool
|
2014-10-06 14:31:54 -05:00
|
|
|
|
|
|
|
// PhantomJs Rendering
|
|
|
|
ImagesDir string
|
|
|
|
PhantomDir string
|
2015-03-03 03:18:24 -06:00
|
|
|
|
2015-04-09 05:16:59 -05:00
|
|
|
// for logging purposes
|
|
|
|
configFiles []string
|
|
|
|
appliedCommandLineProperties []string
|
|
|
|
appliedEnvOverrides []string
|
2015-03-22 14:14:00 -05:00
|
|
|
|
2015-03-27 11:13:44 -05:00
|
|
|
ReportingEnabled bool
|
|
|
|
GoogleAnalyticsId string
|
2015-06-04 02:34:42 -05:00
|
|
|
|
|
|
|
// LDAP
|
|
|
|
LdapEnabled bool
|
|
|
|
LdapUrls []string
|
2014-10-04 06:33:20 -05:00
|
|
|
)
|
|
|
|
|
2015-04-08 07:10:04 -05:00
|
|
|
type CommandLineArgs struct {
|
2015-04-12 02:15:49 -05:00
|
|
|
Config string
|
|
|
|
HomePath string
|
|
|
|
Args []string
|
2015-04-08 07:10:04 -05:00
|
|
|
}
|
|
|
|
|
2014-10-04 06:33:20 -05:00
|
|
|
func init() {
|
|
|
|
IsWindows = runtime.GOOS == "windows"
|
2015-05-01 15:26:16 -05:00
|
|
|
log.NewLogger(0, "console", `{"level": 0}`)
|
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-04-09 05:16:59 -05:00
|
|
|
func applyEnvVariableOverrides() {
|
|
|
|
appliedEnvOverrides = make([]string, 0)
|
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 {
|
|
|
|
key.SetValue(envValue)
|
2015-04-09 05:16:59 -05:00
|
|
|
appliedEnvOverrides = append(appliedEnvOverrides, fmt.Sprintf("%s=%s", envKey, envValue))
|
2015-02-12 06:31:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-12 04:55:55 -06:00
|
|
|
}
|
|
|
|
|
2015-04-09 05:16:59 -05:00
|
|
|
func applyCommandLineDefaultProperties(props map[string]string) {
|
|
|
|
appliedCommandLineProperties = make([]string, 0)
|
|
|
|
for _, section := range Cfg.Sections() {
|
|
|
|
for _, key := range section.Keys() {
|
|
|
|
keyString := fmt.Sprintf("default.%s.%s", section.Name(), key.Name())
|
|
|
|
value, exists := props[keyString]
|
|
|
|
if exists {
|
|
|
|
key.SetValue(value)
|
|
|
|
appliedCommandLineProperties = append(appliedCommandLineProperties, fmt.Sprintf("%s=%s", keyString, value))
|
|
|
|
}
|
2015-04-08 13:31:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-09 05:16:59 -05:00
|
|
|
func applyCommandLineProperties(props map[string]string) {
|
|
|
|
for _, section := range Cfg.Sections() {
|
|
|
|
for _, key := range section.Keys() {
|
|
|
|
keyString := fmt.Sprintf("%s.%s", section.Name(), key.Name())
|
|
|
|
value, exists := props[keyString]
|
|
|
|
if exists {
|
|
|
|
key.SetValue(value)
|
|
|
|
appliedCommandLineProperties = append(appliedCommandLineProperties, fmt.Sprintf("%s=%s", keyString, value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-15 15:57:49 -06:00
|
|
|
|
2015-04-09 05:16:59 -05:00
|
|
|
func getCommandLineProperties(args []string) map[string]string {
|
|
|
|
props := make(map[string]string)
|
2014-10-04 06:33:20 -05:00
|
|
|
|
2015-04-09 05:16:59 -05:00
|
|
|
for _, arg := range args {
|
|
|
|
if !strings.HasPrefix(arg, "cfg:") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
trimmed := strings.TrimPrefix(arg, "cfg:")
|
|
|
|
parts := strings.Split(trimmed, "=")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
log.Fatal(3, "Invalid command line argument", arg)
|
|
|
|
return nil
|
2015-01-27 03:09:54 -06:00
|
|
|
}
|
2015-01-05 00:59:18 -06:00
|
|
|
|
2015-04-09 05:16:59 -05:00
|
|
|
props[parts[0]] = parts[1]
|
|
|
|
}
|
|
|
|
return props
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeAbsolute(path string, root string) string {
|
|
|
|
if filepath.IsAbs(path) {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
return filepath.Join(root, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func evalEnvVarExpression(value string) string {
|
|
|
|
regex := regexp.MustCompile(`\${(\w+)}`)
|
|
|
|
return regex.ReplaceAllStringFunc(value, func(envVar string) string {
|
|
|
|
envVar = strings.TrimPrefix(envVar, "${")
|
|
|
|
envVar = strings.TrimSuffix(envVar, "}")
|
|
|
|
envValue := os.Getenv(envVar)
|
|
|
|
return envValue
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func evalConfigValues() {
|
|
|
|
for _, section := range Cfg.Sections() {
|
|
|
|
for _, key := range section.Keys() {
|
|
|
|
key.SetValue(evalEnvVarExpression(key.Value()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-10 03:58:32 -05:00
|
|
|
func loadSpecifedConfigFile(configFile string) {
|
2015-04-12 02:15:49 -05:00
|
|
|
if configFile == "" {
|
|
|
|
configFile = filepath.Join(HomePath, "conf/custom.ini")
|
|
|
|
// return without error if custom file does not exist
|
|
|
|
if !pathExists(configFile) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-10 03:58:32 -05:00
|
|
|
userConfig, err := ini.Load(configFile)
|
2015-04-10 09:20:01 -05:00
|
|
|
userConfig.BlockMode = false
|
2015-04-10 03:58:32 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(3, "Failed to parse %v, %v", configFile, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, section := range userConfig.Sections() {
|
|
|
|
for _, key := range section.Keys() {
|
|
|
|
if key.Value() == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultSec, err := Cfg.GetSection(section.Name())
|
|
|
|
if err != nil {
|
2015-05-12 05:20:03 -05:00
|
|
|
log.Error(3, "Unknown config section %s defined in %s", section.Name(), configFile)
|
|
|
|
continue
|
2015-04-10 03:58:32 -05:00
|
|
|
}
|
|
|
|
defaultKey, err := defaultSec.GetKey(key.Name())
|
|
|
|
if err != nil {
|
2015-05-12 05:20:03 -05:00
|
|
|
log.Error(3, "Unknown config key %s defined in section %s, in file", key.Name(), section.Name(), configFile)
|
|
|
|
continue
|
2015-04-10 03:58:32 -05:00
|
|
|
}
|
|
|
|
defaultKey.SetValue(key.Value())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
configFiles = append(configFiles, configFile)
|
|
|
|
}
|
|
|
|
|
2015-04-09 05:16:59 -05:00
|
|
|
func loadConfiguration(args *CommandLineArgs) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// load config defaults
|
|
|
|
defaultConfigFile := path.Join(HomePath, "conf/defaults.ini")
|
|
|
|
configFiles = append(configFiles, defaultConfigFile)
|
|
|
|
|
|
|
|
Cfg, err = ini.Load(defaultConfigFile)
|
2015-04-10 09:20:01 -05:00
|
|
|
Cfg.BlockMode = false
|
2015-04-09 05:16:59 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(3, "Failed to parse defaults.ini, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// command line props
|
|
|
|
commandLineProps := getCommandLineProperties(args.Args)
|
|
|
|
// load default overrides
|
|
|
|
applyCommandLineDefaultProperties(commandLineProps)
|
|
|
|
|
2015-05-14 03:15:46 -05:00
|
|
|
// init logging before specific config so we can log errors from here on
|
|
|
|
DataPath = makeAbsolute(Cfg.Section("paths").Key("data").String(), HomePath)
|
|
|
|
initLogging(args)
|
|
|
|
|
2015-04-09 05:16:59 -05:00
|
|
|
// load specified config file
|
2015-04-12 02:15:49 -05:00
|
|
|
loadSpecifedConfigFile(args.Config)
|
2014-10-04 06:33:20 -05:00
|
|
|
|
2015-04-09 05:16:59 -05:00
|
|
|
// apply environment overrides
|
|
|
|
applyEnvVariableOverrides()
|
|
|
|
|
|
|
|
// apply command line overrides
|
|
|
|
applyCommandLineProperties(commandLineProps)
|
2015-04-08 07:10:04 -05:00
|
|
|
|
2015-04-09 05:16:59 -05:00
|
|
|
// evaluate config values containing environment variables
|
|
|
|
evalConfigValues()
|
2015-05-14 03:15:46 -05:00
|
|
|
|
|
|
|
// update data path and logging config
|
|
|
|
DataPath = makeAbsolute(Cfg.Section("paths").Key("data").String(), HomePath)
|
|
|
|
initLogging(args)
|
2015-04-09 05:16:59 -05:00
|
|
|
}
|
|
|
|
|
2015-04-12 02:15:49 -05:00
|
|
|
func pathExists(path string) bool {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if err == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func setHomePath(args *CommandLineArgs) {
|
|
|
|
if args.HomePath != "" {
|
|
|
|
HomePath = args.HomePath
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
HomePath, _ = filepath.Abs(".")
|
|
|
|
// check if homepath is correct
|
|
|
|
if pathExists(filepath.Join(HomePath, "conf/defaults.ini")) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// try down one path
|
|
|
|
if pathExists(filepath.Join(HomePath, "../conf/defaults.ini")) {
|
|
|
|
HomePath = filepath.Join(HomePath, "../")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-09 05:16:59 -05:00
|
|
|
func NewConfigContext(args *CommandLineArgs) {
|
2015-04-12 02:15:49 -05:00
|
|
|
setHomePath(args)
|
2015-04-09 05:16:59 -05:00
|
|
|
loadConfiguration(args)
|
|
|
|
|
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()
|
2015-03-11 13:44:31 -05:00
|
|
|
KeyFile = server.Key("cert_key").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")
|
2015-04-12 02:15:49 -05:00
|
|
|
StaticRootPath = makeAbsolute(server.Key("static_root_path").String(), HomePath)
|
2015-01-27 03:09:54 -06:00
|
|
|
RouterLogging = server.Key("router_logging").MustBool(false)
|
|
|
|
EnableGzip = server.Key("enable_gzip").MustBool(false)
|
2015-05-05 04:21:06 -05:00
|
|
|
EnforceDomain = server.Key("enforce_domain").MustBool(false)
|
2015-01-27 03:09:54 -06:00
|
|
|
|
|
|
|
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-05-01 01:40:13 -05:00
|
|
|
DisableGravatar = security.Key("disable_gravatar").MustBool(true)
|
|
|
|
|
2015-01-27 08:45:27 -06:00
|
|
|
// admin
|
|
|
|
AdminUser = security.Key("admin_user").String()
|
|
|
|
AdminPassword = security.Key("admin_password").String()
|
|
|
|
|
2015-03-11 10:19:29 -05:00
|
|
|
users := Cfg.Section("users")
|
|
|
|
AllowUserSignUp = users.Key("allow_sign_up").MustBool(true)
|
|
|
|
AllowUserOrgCreate = users.Key("allow_org_create").MustBool(true)
|
|
|
|
AutoAssignOrg = users.Key("auto_assign_org").MustBool(true)
|
|
|
|
AutoAssignOrgRole = users.Key("auto_assign_org_role").In("Editor", []string{"Editor", "Admin", "Viewer"})
|
2015-06-01 09:29:45 -05:00
|
|
|
ViewerRoleMode = users.Key("viewer_role_mode").In("default", []string{"default", "strinct"})
|
2015-01-27 08:14:53 -06:00
|
|
|
|
2015-01-27 08:45:27 -06:00
|
|
|
// anonymous access
|
|
|
|
AnonymousEnabled = Cfg.Section("auth.anonymous").Key("enabled").MustBool(false)
|
2015-02-23 13:07:49 -06:00
|
|
|
AnonymousOrgName = Cfg.Section("auth.anonymous").Key("org_name").String()
|
|
|
|
AnonymousOrgRole = Cfg.Section("auth.anonymous").Key("org_role").String()
|
2015-01-07 09:37:24 -06:00
|
|
|
|
2015-05-01 04:55:59 -05:00
|
|
|
// auth proxy
|
|
|
|
authProxy := Cfg.Section("auth.proxy")
|
|
|
|
AuthProxyEnabled = authProxy.Key("enabled").MustBool(false)
|
|
|
|
AuthProxyHeaderName = authProxy.Key("header_name").String()
|
|
|
|
AuthProxyHeaderProperty = authProxy.Key("header_property").String()
|
|
|
|
AuthProxyAutoSignUp = authProxy.Key("auto_sign_up").MustBool(true)
|
|
|
|
|
2014-10-06 14:31:54 -05:00
|
|
|
// PhantomJS rendering
|
2015-04-08 07:10:04 -05:00
|
|
|
ImagesDir = filepath.Join(DataPath, "png")
|
2015-04-09 05:16:59 -05:00
|
|
|
PhantomDir = filepath.Join(HomePath, "vendor/phantomjs")
|
2014-11-14 10:13:33 -06:00
|
|
|
|
2015-03-27 11:13:44 -05:00
|
|
|
analytics := Cfg.Section("analytics")
|
|
|
|
ReportingEnabled = analytics.Key("reporting_enabled").MustBool(true)
|
|
|
|
GoogleAnalyticsId = analytics.Key("google_analytics_ua_id").String()
|
2015-03-22 14:14:00 -05:00
|
|
|
|
2015-06-04 02:34:42 -05:00
|
|
|
ldapSec := Cfg.Section("auth.ldap")
|
|
|
|
LdapEnabled = ldapSec.Key("enabled").MustBool(false)
|
|
|
|
LdapUrls = ldapSec.Key("urls").Strings(" ")
|
|
|
|
|
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-04-01 02:45:42 -05:00
|
|
|
SessionOptions.Provider = sec.Key("provider").In("memory", []string{"memory", "file", "redis", "mysql", "postgres"})
|
2015-01-27 03:09:54 -06:00
|
|
|
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)
|
2015-04-07 12:21:14 -05:00
|
|
|
SessionOptions.IDLength = 16
|
2014-12-30 03:28:27 -06:00
|
|
|
|
|
|
|
if SessionOptions.Provider == "file" {
|
2015-04-09 05:16:59 -05:00
|
|
|
SessionOptions.ProviderConfig = makeAbsolute(SessionOptions.ProviderConfig, DataPath)
|
2014-12-30 03:28:27 -06:00
|
|
|
os.MkdirAll(path.Dir(SessionOptions.ProviderConfig), os.ModePerm)
|
2014-10-05 09:50:04 -05:00
|
|
|
}
|
2015-04-08 01:59:12 -05:00
|
|
|
|
|
|
|
if SessionOptions.CookiePath == "" {
|
|
|
|
SessionOptions.CookiePath = "/"
|
|
|
|
}
|
2014-10-05 09:50:04 -05:00
|
|
|
}
|
2015-03-03 03:18:24 -06:00
|
|
|
|
2015-04-19 02:14:50 -05:00
|
|
|
var logLevels = map[string]int{
|
|
|
|
"Trace": 0,
|
|
|
|
"Debug": 1,
|
|
|
|
"Info": 2,
|
|
|
|
"Warn": 3,
|
|
|
|
"Error": 4,
|
|
|
|
"Critical": 5,
|
2015-03-03 03:18:24 -06:00
|
|
|
}
|
|
|
|
|
2015-04-08 07:10:04 -05:00
|
|
|
func initLogging(args *CommandLineArgs) {
|
2015-03-03 03:18:24 -06:00
|
|
|
// Get and check log mode.
|
|
|
|
LogModes = strings.Split(Cfg.Section("log").Key("mode").MustString("console"), ",")
|
2015-04-09 05:16:59 -05:00
|
|
|
LogsPath = makeAbsolute(Cfg.Section("paths").Key("logs").String(), HomePath)
|
2015-04-08 07:10:04 -05:00
|
|
|
|
2015-04-19 02:29:08 -05:00
|
|
|
LogConfigs = make([]util.DynMap, len(LogModes))
|
2015-03-03 03:18:24 -06:00
|
|
|
for i, mode := range LogModes {
|
|
|
|
mode = strings.TrimSpace(mode)
|
|
|
|
sec, err := Cfg.GetSection("log." + mode)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(4, "Unknown log mode: %s", mode)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log level.
|
|
|
|
levelName := Cfg.Section("log."+mode).Key("level").In("Trace",
|
|
|
|
[]string{"Trace", "Debug", "Info", "Warn", "Error", "Critical"})
|
|
|
|
level, ok := logLevels[levelName]
|
|
|
|
if !ok {
|
|
|
|
log.Fatal(4, "Unknown log level: %s", levelName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate log configuration.
|
|
|
|
switch mode {
|
|
|
|
case "console":
|
2015-04-19 02:29:08 -05:00
|
|
|
LogConfigs[i] = util.DynMap{"level": level}
|
2015-03-03 03:18:24 -06:00
|
|
|
case "file":
|
2015-04-19 02:14:50 -05:00
|
|
|
logPath := sec.Key("file_name").MustString(filepath.Join(LogsPath, "grafana.log"))
|
|
|
|
os.MkdirAll(filepath.Dir(logPath), os.ModePerm)
|
2015-04-19 02:29:08 -05:00
|
|
|
LogConfigs[i] = util.DynMap{
|
|
|
|
"level": level,
|
2015-04-19 02:14:50 -05:00
|
|
|
"filename": logPath,
|
2015-04-19 02:29:08 -05:00
|
|
|
"rotate": sec.Key("log_rotate").MustBool(true),
|
2015-04-19 02:14:50 -05:00
|
|
|
"maxlines": sec.Key("max_lines").MustInt(1000000),
|
2015-04-19 02:29:08 -05:00
|
|
|
"maxsize": 1 << uint(sec.Key("max_size_shift").MustInt(28)),
|
|
|
|
"daily": sec.Key("daily_rotate").MustBool(true),
|
|
|
|
"maxdays": sec.Key("max_days").MustInt(7),
|
2015-04-19 02:14:50 -05:00
|
|
|
}
|
2015-03-03 03:18:24 -06:00
|
|
|
case "conn":
|
2015-04-19 02:29:08 -05:00
|
|
|
LogConfigs[i] = util.DynMap{
|
|
|
|
"level": level,
|
2015-04-19 02:14:50 -05:00
|
|
|
"reconnectOnMsg": sec.Key("reconnect_on_msg").MustBool(),
|
2015-04-19 02:29:08 -05:00
|
|
|
"reconnect": sec.Key("reconnect").MustBool(),
|
|
|
|
"net": sec.Key("protocol").In("tcp", []string{"tcp", "unix", "udp"}),
|
|
|
|
"addr": sec.Key("addr").MustString(":7020"),
|
2015-04-19 02:14:50 -05:00
|
|
|
}
|
2015-03-03 03:18:24 -06:00
|
|
|
case "smtp":
|
2015-04-19 02:29:08 -05:00
|
|
|
LogConfigs[i] = util.DynMap{
|
|
|
|
"level": level,
|
|
|
|
"user": sec.Key("user").MustString("example@example.com"),
|
|
|
|
"passwd": sec.Key("passwd").MustString("******"),
|
|
|
|
"host": sec.Key("host").MustString("127.0.0.1:25"),
|
2015-04-19 02:14:50 -05:00
|
|
|
"receivers": sec.Key("receivers").MustString("[]"),
|
2015-04-19 02:29:08 -05:00
|
|
|
"subject": sec.Key("subject").MustString("Diagnostic message from serve"),
|
2015-04-19 02:14:50 -05:00
|
|
|
}
|
2015-03-03 03:18:24 -06:00
|
|
|
case "database":
|
2015-04-19 02:29:08 -05:00
|
|
|
LogConfigs[i] = util.DynMap{
|
|
|
|
"level": level,
|
2015-04-19 02:14:50 -05:00
|
|
|
"driver": sec.Key("driver").String(),
|
2015-04-19 02:29:08 -05:00
|
|
|
"conn": sec.Key("conn").String(),
|
2015-04-19 02:14:50 -05:00
|
|
|
}
|
2015-03-03 03:18:24 -06:00
|
|
|
}
|
|
|
|
|
2015-04-19 02:29:08 -05:00
|
|
|
cfgJsonBytes, _ := json.Marshal(LogConfigs[i])
|
|
|
|
log.NewLogger(Cfg.Section("log").Key("buffer_len").MustInt64(10000), mode, string(cfgJsonBytes))
|
2015-03-03 03:18:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-09 05:16:59 -05:00
|
|
|
func LogConfigurationInfo() {
|
|
|
|
var text bytes.Buffer
|
|
|
|
text.WriteString("Configuration Info\n")
|
|
|
|
|
|
|
|
text.WriteString("Config files:\n")
|
|
|
|
for i, file := range configFiles {
|
|
|
|
text.WriteString(fmt.Sprintf(" [%d]: %s\n", i, file))
|
2015-03-03 03:18:24 -06:00
|
|
|
}
|
2015-04-09 05:16:59 -05:00
|
|
|
|
|
|
|
if len(appliedCommandLineProperties) > 0 {
|
|
|
|
text.WriteString("Command lines overrides:\n")
|
|
|
|
for i, prop := range appliedCommandLineProperties {
|
|
|
|
text.WriteString(fmt.Sprintf(" [%d]: %s\n", i, prop))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(appliedEnvOverrides) > 0 {
|
|
|
|
text.WriteString("\tEnvironment variables used:\n")
|
|
|
|
for i, prop := range appliedCommandLineProperties {
|
|
|
|
text.WriteString(fmt.Sprintf(" [%d]: %s\n", i, prop))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
text.WriteString("Paths:\n")
|
|
|
|
text.WriteString(fmt.Sprintf(" home: %s\n", HomePath))
|
|
|
|
text.WriteString(fmt.Sprintf(" data: %s\n", DataPath))
|
|
|
|
text.WriteString(fmt.Sprintf(" logs: %s\n", LogsPath))
|
|
|
|
|
|
|
|
log.Info(text.String())
|
2015-03-03 03:18:24 -06:00
|
|
|
}
|