2020-03-04 10:29:00 +00:00
|
|
|
package configs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
"github.com/pkg/errors"
|
2020-03-09 18:17:55 +00:00
|
|
|
"github.com/statping/statping/database"
|
2020-03-10 05:24:35 +00:00
|
|
|
"github.com/statping/statping/types/checkins"
|
|
|
|
"github.com/statping/statping/types/core"
|
|
|
|
"github.com/statping/statping/types/failures"
|
|
|
|
"github.com/statping/statping/types/groups"
|
|
|
|
"github.com/statping/statping/types/hits"
|
|
|
|
"github.com/statping/statping/types/incidents"
|
|
|
|
"github.com/statping/statping/types/messages"
|
2020-03-14 03:13:20 +00:00
|
|
|
"github.com/statping/statping/types/notifications"
|
2020-03-09 18:17:55 +00:00
|
|
|
"github.com/statping/statping/types/null"
|
2020-03-10 05:24:35 +00:00
|
|
|
"github.com/statping/statping/types/services"
|
2020-03-09 18:17:55 +00:00
|
|
|
"github.com/statping/statping/types/users"
|
|
|
|
"github.com/statping/statping/utils"
|
2020-03-04 10:29:00 +00:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Connect will attempt to connect to the sqlite, postgres, or mysql database
|
2020-03-04 14:20:47 +00:00
|
|
|
func Connect(configs *DbConfig, retry bool) error {
|
2020-03-04 10:29:00 +00:00
|
|
|
postgresSSL := os.Getenv("POSTGRES_SSLMODE")
|
|
|
|
var conn string
|
|
|
|
var err error
|
|
|
|
|
|
|
|
switch configs.DbConn {
|
|
|
|
case "sqlite", "sqlite3", "memory":
|
|
|
|
if configs.DbConn == "memory" {
|
|
|
|
conn = "sqlite3"
|
2020-03-10 05:24:35 +00:00
|
|
|
configs.DbConn = ":memory:"
|
2020-03-04 10:29:00 +00:00
|
|
|
} else {
|
|
|
|
conn = findDbFile(configs)
|
2020-03-04 14:20:47 +00:00
|
|
|
configs.SqlFile = conn
|
2020-03-04 10:29:00 +00:00
|
|
|
log.Infof("SQL database file at: %s", configs.SqlFile)
|
|
|
|
configs.DbConn = "sqlite3"
|
|
|
|
}
|
|
|
|
case "mysql":
|
|
|
|
host := fmt.Sprintf("%v:%v", configs.DbHost, configs.DbPort)
|
|
|
|
conn = fmt.Sprintf("%v:%v@tcp(%v)/%v?charset=utf8&parseTime=True&loc=UTC&time_zone=%%27UTC%%27", configs.DbUser, configs.DbPass, host, configs.DbData)
|
|
|
|
case "postgres":
|
|
|
|
sslMode := "disable"
|
|
|
|
if postgresSSL != "" {
|
|
|
|
sslMode = postgresSSL
|
|
|
|
}
|
|
|
|
conn = fmt.Sprintf("host=%v port=%v user=%v dbname=%v password=%v timezone=UTC sslmode=%v", configs.DbHost, configs.DbPort, configs.DbUser, configs.DbData, configs.DbPass, sslMode)
|
|
|
|
case "mssql":
|
|
|
|
host := fmt.Sprintf("%v:%v", configs.DbHost, configs.DbPort)
|
|
|
|
conn = fmt.Sprintf("sqlserver://%v:%v@%v?database=%v", configs.DbUser, configs.DbPass, host, configs.DbData)
|
|
|
|
}
|
|
|
|
log.WithFields(utils.ToFields(configs, conn)).Debugln("attempting to connect to database")
|
|
|
|
|
|
|
|
dbSession, err := database.Openw(configs.DbConn, conn)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugln(fmt.Sprintf("Database connection error %s", err))
|
|
|
|
if retry {
|
|
|
|
log.Errorln(fmt.Sprintf("Database %s connection to '%s' is not available, trying again in 5 seconds...", configs.DbConn, configs.DbHost))
|
2020-03-05 05:38:56 +00:00
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
return Connect(configs, retry)
|
2020-03-04 10:29:00 +00:00
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-03-05 05:38:56 +00:00
|
|
|
|
|
|
|
apiKey := utils.Getenv("API_KEY", utils.RandomString(16)).(string)
|
|
|
|
apiSecret := utils.Getenv("API_SECRET", utils.RandomString(16)).(string)
|
|
|
|
configs.ApiKey = apiKey
|
|
|
|
configs.ApiSecret = apiSecret
|
|
|
|
|
2020-03-04 10:29:00 +00:00
|
|
|
log.WithFields(utils.ToFields(dbSession)).Debugln("connected to database")
|
|
|
|
|
2020-03-31 18:41:12 +00:00
|
|
|
maxOpenConn := utils.Getenv("MAX_OPEN_CONN", 25)
|
|
|
|
maxIdleConn := utils.Getenv("MAX_IDLE_CONN", 25)
|
|
|
|
maxLifeConn := utils.Getenv("MAX_LIFE_CONN", 5*time.Minute)
|
2020-03-04 10:29:00 +00:00
|
|
|
|
2020-03-31 18:41:12 +00:00
|
|
|
dbSession.DB().SetMaxOpenConns(maxOpenConn.(int))
|
2020-03-04 10:29:00 +00:00
|
|
|
dbSession.DB().SetMaxIdleConns(maxIdleConn.(int))
|
|
|
|
dbSession.DB().SetConnMaxLifetime(maxLifeConn.(time.Duration))
|
|
|
|
|
|
|
|
if dbSession.DB().Ping() == nil {
|
|
|
|
if utils.VerboseMode >= 4 {
|
2020-03-10 05:24:35 +00:00
|
|
|
dbSession.LogMode(true).Debug().SetLogger(gorm.Logger{log})
|
2020-03-04 10:29:00 +00:00
|
|
|
}
|
2020-03-23 02:50:30 +00:00
|
|
|
log.Infoln(fmt.Sprintf("Database %s connection was successful.", configs.DbConn))
|
2020-03-04 10:29:00 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 05:24:35 +00:00
|
|
|
configs.Db = dbSession
|
|
|
|
|
|
|
|
initModels(configs.Db)
|
|
|
|
|
2020-03-04 10:29:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-10 05:24:35 +00:00
|
|
|
func initModels(db database.Database) {
|
|
|
|
core.SetDB(db)
|
|
|
|
services.SetDB(db)
|
|
|
|
hits.SetDB(db)
|
|
|
|
failures.SetDB(db)
|
|
|
|
checkins.SetDB(db)
|
2020-03-14 03:13:20 +00:00
|
|
|
notifications.SetDB(db)
|
2020-03-10 05:24:35 +00:00
|
|
|
incidents.SetDB(db)
|
|
|
|
users.SetDB(db)
|
|
|
|
messages.SetDB(db)
|
|
|
|
groups.SetDB(db)
|
|
|
|
}
|
|
|
|
|
2020-03-09 15:15:15 +00:00
|
|
|
func CreateAdminUser(configs *DbConfig) error {
|
2020-03-04 10:29:00 +00:00
|
|
|
log.Infoln(fmt.Sprintf("Core database does not exist, creating now!"))
|
2020-03-04 14:20:47 +00:00
|
|
|
|
2020-03-06 22:18:06 +00:00
|
|
|
if configs.Username == "" && configs.Password == "" {
|
|
|
|
configs.Username = utils.Getenv("ADMIN_USER", "admin").(string)
|
|
|
|
configs.Password = utils.Getenv("ADMIN_PASSWORD", "admin").(string)
|
|
|
|
}
|
2020-03-04 10:29:00 +00:00
|
|
|
|
|
|
|
admin := &users.User{
|
2020-03-06 22:18:06 +00:00
|
|
|
Username: configs.Username,
|
|
|
|
Password: configs.Password,
|
2020-03-04 10:29:00 +00:00
|
|
|
Email: "info@admin.com",
|
|
|
|
Admin: null.NewNullBool(true),
|
|
|
|
}
|
2020-03-06 22:18:06 +00:00
|
|
|
|
2020-03-04 10:29:00 +00:00
|
|
|
if err := admin.Create(); err != nil {
|
|
|
|
return errors.Wrap(err, "error creating admin")
|
|
|
|
}
|
|
|
|
|
2020-03-04 14:20:47 +00:00
|
|
|
return nil
|
2020-03-04 10:29:00 +00:00
|
|
|
}
|