2014-11-14 10:13:33 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
2015-04-08 07:10:04 -05:00
|
|
|
"path/filepath"
|
2014-11-14 10:13:33 -06:00
|
|
|
"strings"
|
|
|
|
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/log"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2015-02-24 04:46:34 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore/migrations"
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2014-11-20 05:11:07 -06:00
|
|
|
|
2015-12-22 07:10:34 -06:00
|
|
|
"github.com/go-sql-driver/mysql"
|
2015-01-07 07:05:50 -06:00
|
|
|
_ "github.com/go-sql-driver/mysql"
|
2014-11-14 10:13:33 -06:00
|
|
|
"github.com/go-xorm/xorm"
|
2015-01-16 08:28:44 -06:00
|
|
|
_ "github.com/lib/pq"
|
2014-11-14 10:13:33 -06:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
2015-11-24 10:17:21 -06:00
|
|
|
type MySQLConfig struct {
|
2015-12-22 07:10:34 -06:00
|
|
|
SslMode string
|
|
|
|
CaCertPath string
|
2015-11-24 10:17:21 -06:00
|
|
|
ClientKeyPath string
|
|
|
|
ClientCertPath string
|
|
|
|
ServerCertName string
|
|
|
|
}
|
|
|
|
|
2014-11-14 10:13:33 -06:00
|
|
|
var (
|
2015-01-20 07:15:48 -06:00
|
|
|
x *xorm.Engine
|
|
|
|
dialect migrator.Dialect
|
2014-11-14 10:13:33 -06:00
|
|
|
|
|
|
|
HasEngine bool
|
|
|
|
|
|
|
|
DbCfg struct {
|
|
|
|
Type, Host, Name, User, Pwd, Path, SslMode string
|
|
|
|
}
|
|
|
|
|
2015-11-24 10:17:21 -06:00
|
|
|
mysqlConfig MySQLConfig
|
2016-06-07 02:29:47 -05:00
|
|
|
UseSQLite3 bool
|
|
|
|
sqlog log.Logger = log.New("sqlstore")
|
2014-11-14 10:13:33 -06:00
|
|
|
)
|
|
|
|
|
2015-01-15 07:44:15 -06:00
|
|
|
func EnsureAdminUser() {
|
2015-04-01 09:05:42 -05:00
|
|
|
statsQuery := m.GetSystemStatsQuery{}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&statsQuery); err != nil {
|
|
|
|
log.Fatal(3, "Could not determine if admin user exists: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if statsQuery.Result.UserCount > 0 {
|
|
|
|
return
|
2015-01-15 07:44:15 -06:00
|
|
|
}
|
2015-04-01 09:05:42 -05:00
|
|
|
|
|
|
|
cmd := m.CreateUserCommand{}
|
|
|
|
cmd.Login = setting.AdminUser
|
|
|
|
cmd.Email = setting.AdminUser + "@localhost"
|
|
|
|
cmd.Password = setting.AdminPassword
|
|
|
|
cmd.IsAdmin = true
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
|
|
|
log.Error(3, "Failed to create default admin user", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Created default admin user: %v", setting.AdminUser)
|
2014-11-14 10:13:33 -06:00
|
|
|
}
|
|
|
|
|
2015-01-15 07:44:15 -06:00
|
|
|
func NewEngine() {
|
|
|
|
x, err := getEngine()
|
2014-12-16 14:05:49 -06:00
|
|
|
|
|
|
|
if err != nil {
|
2016-06-07 02:29:47 -05:00
|
|
|
sqlog.Crit("Fail to connect to database", "error", err)
|
|
|
|
os.Exit(1)
|
2014-11-14 10:13:33 -06:00
|
|
|
}
|
2014-12-16 14:05:49 -06:00
|
|
|
|
2016-02-29 06:42:07 -06:00
|
|
|
err = SetEngine(x, setting.Env == setting.DEV)
|
2014-12-16 14:05:49 -06:00
|
|
|
|
|
|
|
if err != nil {
|
2016-06-07 07:27:56 -05:00
|
|
|
sqlog.Error("Fail to initialize orm engine", "error", err)
|
2016-06-07 02:29:47 -05:00
|
|
|
os.Exit(1)
|
2014-11-14 10:13:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-16 14:05:49 -06:00
|
|
|
func SetEngine(engine *xorm.Engine, enableLog bool) (err error) {
|
|
|
|
x = engine
|
2015-01-20 07:15:48 -06:00
|
|
|
dialect = migrator.NewDialect(x.DriverName())
|
2014-12-16 14:05:49 -06:00
|
|
|
|
2015-01-20 07:15:48 -06:00
|
|
|
migrator := migrator.NewMigrator(x)
|
2015-02-24 04:46:34 -06:00
|
|
|
migrations.AddMigrations(migrator)
|
2015-01-19 03:44:16 -06:00
|
|
|
|
|
|
|
if err := migrator.Start(); err != nil {
|
|
|
|
return fmt.Errorf("Sqlstore::Migration failed err: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2014-11-14 10:13:33 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEngine() (*xorm.Engine, error) {
|
2014-12-16 14:05:49 -06:00
|
|
|
LoadConfig()
|
|
|
|
|
2014-11-14 10:13:33 -06:00
|
|
|
cnnstr := ""
|
|
|
|
switch DbCfg.Type {
|
|
|
|
case "mysql":
|
2015-12-23 15:06:28 -06:00
|
|
|
protocol := "tcp"
|
|
|
|
if strings.HasPrefix(DbCfg.Host, "/") {
|
|
|
|
protocol = "unix"
|
|
|
|
}
|
|
|
|
|
|
|
|
cnnstr = fmt.Sprintf("%s:%s@%s(%s)/%s?charset=utf8",
|
|
|
|
DbCfg.User, DbCfg.Pwd, protocol, DbCfg.Host, DbCfg.Name)
|
2016-01-07 10:08:31 -06:00
|
|
|
|
2015-11-24 10:17:21 -06:00
|
|
|
if mysqlConfig.SslMode == "true" || mysqlConfig.SslMode == "skip-verify" {
|
|
|
|
tlsCert, err := makeCert("custom", mysqlConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mysql.RegisterTLSConfig("custom", tlsCert)
|
|
|
|
cnnstr += "&tls=custom"
|
|
|
|
}
|
2014-11-14 10:13:33 -06:00
|
|
|
case "postgres":
|
|
|
|
var host, port = "127.0.0.1", "5432"
|
|
|
|
fields := strings.Split(DbCfg.Host, ":")
|
|
|
|
if len(fields) > 0 && len(strings.TrimSpace(fields[0])) > 0 {
|
|
|
|
host = fields[0]
|
|
|
|
}
|
|
|
|
if len(fields) > 1 && len(strings.TrimSpace(fields[1])) > 0 {
|
|
|
|
port = fields[1]
|
|
|
|
}
|
2016-02-12 03:10:07 -06:00
|
|
|
if DbCfg.Pwd == "" {
|
|
|
|
DbCfg.Pwd = "''"
|
|
|
|
}
|
|
|
|
if DbCfg.User == "" {
|
|
|
|
DbCfg.User = "''"
|
|
|
|
}
|
|
|
|
cnnstr = fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=%s", DbCfg.User, DbCfg.Pwd, host, port, DbCfg.Name, DbCfg.SslMode)
|
2014-11-14 10:13:33 -06:00
|
|
|
case "sqlite3":
|
2015-04-08 07:10:04 -05:00
|
|
|
if !filepath.IsAbs(DbCfg.Path) {
|
|
|
|
DbCfg.Path = filepath.Join(setting.DataPath, DbCfg.Path)
|
|
|
|
}
|
2014-11-14 10:13:33 -06:00
|
|
|
os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
|
2015-03-26 06:00:52 -05:00
|
|
|
cnnstr = "file:" + DbCfg.Path + "?cache=shared&mode=rwc&_loc=Local"
|
2014-11-14 10:13:33 -06:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Unknown database type: %s", DbCfg.Type)
|
|
|
|
}
|
2014-12-16 14:05:49 -06:00
|
|
|
|
2016-06-07 02:29:47 -05:00
|
|
|
sqlog.Info("Initializing DB", "dbtype", DbCfg.Type)
|
2014-11-14 10:13:33 -06:00
|
|
|
return xorm.NewEngine(DbCfg.Type, cnnstr)
|
|
|
|
}
|
2014-12-16 14:05:49 -06:00
|
|
|
|
|
|
|
func LoadConfig() {
|
2015-01-27 03:09:54 -06:00
|
|
|
sec := setting.Cfg.Section("database")
|
|
|
|
|
|
|
|
DbCfg.Type = sec.Key("type").String()
|
2014-12-16 14:05:49 -06:00
|
|
|
if DbCfg.Type == "sqlite3" {
|
|
|
|
UseSQLite3 = true
|
|
|
|
}
|
2015-01-27 03:09:54 -06:00
|
|
|
DbCfg.Host = sec.Key("host").String()
|
|
|
|
DbCfg.Name = sec.Key("name").String()
|
|
|
|
DbCfg.User = sec.Key("user").String()
|
2014-12-16 14:05:49 -06:00
|
|
|
if len(DbCfg.Pwd) == 0 {
|
2015-01-27 03:09:54 -06:00
|
|
|
DbCfg.Pwd = sec.Key("password").String()
|
2014-12-16 14:05:49 -06:00
|
|
|
}
|
2015-01-27 03:09:54 -06:00
|
|
|
DbCfg.SslMode = sec.Key("ssl_mode").String()
|
|
|
|
DbCfg.Path = sec.Key("path").MustString("data/grafana.db")
|
2015-11-24 10:17:21 -06:00
|
|
|
|
|
|
|
if DbCfg.Type == "mysql" {
|
|
|
|
mysqlConfig.SslMode = DbCfg.SslMode
|
|
|
|
mysqlConfig.CaCertPath = sec.Key("ca_cert_path").String()
|
|
|
|
mysqlConfig.ClientKeyPath = sec.Key("client_key_path").String()
|
|
|
|
mysqlConfig.ClientCertPath = sec.Key("client_cert_path").String()
|
|
|
|
mysqlConfig.ServerCertName = sec.Key("server_cert_name").String()
|
|
|
|
}
|
2014-12-16 14:05:49 -06:00
|
|
|
}
|