auto install for DB_CONN env - notifier updated

pull/21/head
Hunter Long 2018-07-11 21:49:18 -07:00
parent 62e88dcffe
commit a4f9387de0
10 changed files with 142 additions and 42 deletions

View File

@ -2,14 +2,19 @@ package core
import ( import (
"errors" "errors"
"fmt"
"github.com/go-yaml/yaml" "github.com/go-yaml/yaml"
"github.com/hunterlong/statup/types" "github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/utils"
"io/ioutil" "io/ioutil"
"os" "os"
"time"
) )
func LoadConfig() (*types.Config, error) { func LoadConfig() (*types.Config, error) {
if os.Getenv("DB_CONN") != "" { if os.Getenv("DB_CONN") != "" {
utils.Log(1, "DB_CONN environment variable was found")
//time.Sleep(20 * time.Second)
return LoadUsingEnv() return LoadUsingEnv()
} }
Configs = new(types.Config) Configs = new(types.Config)
@ -51,12 +56,65 @@ func LoadUsingEnv() (*types.Config, error) {
if os.Getenv("USE_CDN") == "true" { if os.Getenv("USE_CDN") == "true" {
CoreApp.UseCdn = true CoreApp.UseCdn = true
} }
dbConfig := &DbConfig{
DbConn: os.Getenv("DB_CONN"),
DbHost: os.Getenv("DB_HOST"),
DbUser: os.Getenv("DB_USER"),
DbPass: os.Getenv("DB_PASS"),
DbData: os.Getenv("DB_DATABASE"),
DbPort: 5432,
Project: "Statup - " + os.Getenv("NAME"),
Description: "New Statup Installation",
Domain: os.Getenv("DOMAIN"),
Username: "admin",
Password: "admin",
Email: "info@localhost.com",
}
err := DbConnection(dbConfig.DbConn)
if err != nil {
utils.Log(4, err)
return nil, err
}
exists, err := DbSession.Collection("core").Find().Exists()
if !exists {
utils.Log(1, fmt.Sprintf("Core database does not exist, creating now!"))
DropDatabase()
CreateDatabase()
CoreApp = &Core{
Name: dbConfig.Project,
Description: dbConfig.Description,
Config: "config.yml",
ApiKey: utils.NewSHA1Hash(9),
ApiSecret: utils.NewSHA1Hash(16),
Domain: dbConfig.Domain,
MigrationId: time.Now().Unix(),
}
CoreApp.DbConnection = dbConfig.DbConn
err := CoreApp.Insert()
if err != nil {
utils.Log(3, err)
}
admin := &User{
Username: "admin",
Password: "admin",
Email: "info@admin.com",
Admin: true,
}
admin.Create()
LoadSampleData()
return Configs, err
}
return Configs, nil return Configs, nil
} }
func ifOr(val, def string) string {
if val == "" {
return def
}
return val
}

View File

@ -58,6 +58,12 @@ func NewCore() *Core {
return CoreApp return CoreApp
} }
func (c *Core) Insert() error {
col := DbSession.Collection("core")
_, err := col.Insert(c)
return err
}
func InitApp() { func InitApp() {
SelectCore() SelectCore()
notifiers.Collections = DbSession.Collection("communication") notifiers.Collections = DbSession.Collection("communication")
@ -121,6 +127,11 @@ func SelectLastMigration() (int64, error) {
func SelectCore() (*Core, error) { func SelectCore() (*Core, error) {
var c *Core var c *Core
exists := DbSession.Collection("core").Exists()
if !exists {
return nil, errors.New("core database has not been setup yet.")
}
err := DbSession.Collection("core").Find().One(&c) err := DbSession.Collection("core").Find().One(&c)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -135,6 +135,9 @@ func (c *DbConfig) Save() error {
} }
CoreApp, err = SelectCore() CoreApp, err = SelectCore()
if err != nil {
utils.Log(4, err)
}
CoreApp.DbConnection = c.DbConn CoreApp.DbConnection = c.DbConn
return err return err
@ -207,7 +210,7 @@ func RunDatabaseUpgrades() error {
} }
func DropDatabase() { func DropDatabase() {
fmt.Println("Dropping Tables...") utils.Log(1, "Dropping Database Tables...")
down, _ := SqlBox.String("down.sql") down, _ := SqlBox.String("down.sql")
requests := strings.Split(down, ";") requests := strings.Split(down, ";")
for _, request := range requests { for _, request := range requests {
@ -219,7 +222,7 @@ func DropDatabase() {
} }
func CreateDatabase() { func CreateDatabase() {
fmt.Println("Creating Tables...") utils.Log(1, "Creating Database Tables...")
sql := "postgres_up.sql" sql := "postgres_up.sql"
if CoreApp.DbConnection == "mysql" { if CoreApp.DbConnection == "mysql" {
sql = "mysql_up.sql" sql = "mysql_up.sql"
@ -236,7 +239,7 @@ func CreateDatabase() {
} }
//secret := NewSHA1Hash() //secret := NewSHA1Hash()
//db.QueryRow("INSERT INTO core (secret, version) VALUES ($1, $2);", secret, VERSION).Scan() //db.QueryRow("INSERT INTO core (secret, version) VALUES ($1, $2);", secret, VERSION).Scan()
fmt.Println("Database Created") utils.Log(1, "Database Created")
//SampleData() //SampleData()
} }

View File

@ -58,6 +58,7 @@ func SelectAllServices() ([]*Service, error) {
err := col.All(&srvcs) err := col.All(&srvcs)
if err != nil { if err != nil {
utils.Log(3, err) utils.Log(3, err)
return nil, err
} }
for _, s := range srvcs { for _, s := range srvcs {
s.Checkins = s.SelectAllCheckins() s.Checkins = s.SelectAllCheckins()

View File

@ -73,15 +73,15 @@ func LoadSampleData() error {
utils.Log(3, fmt.Sprintf("Error creating Service %v: %v", id, err)) utils.Log(3, fmt.Sprintf("Error creating Service %v: %v", id, err))
} }
checkin := &Checkin{ //checkin := &Checkin{
Service: s2.Id, // Service: s2.Id,
Interval: 30, // Interval: 30,
Api: utils.NewSHA1Hash(18), // Api: utils.NewSHA1Hash(18),
} //}
id, err = checkin.Create() //id, err = checkin.Create()
if err != nil { //if err != nil {
utils.Log(3, fmt.Sprintf("Error creating Checkin %v: %v", id, err)) // utils.Log(3, fmt.Sprintf("Error creating Checkin %v: %v", id, err))
} //}
//for i := 0; i < 3; i++ { //for i := 0; i < 3; i++ {
// s1.Check() // s1.Check()

View File

@ -1,7 +1,6 @@
package handlers package handlers
import ( import (
"fmt"
"github.com/hunterlong/statup/core" "github.com/hunterlong/statup/core"
"github.com/hunterlong/statup/notifiers" "github.com/hunterlong/statup/notifiers"
"github.com/hunterlong/statup/utils" "github.com/hunterlong/statup/utils"
@ -140,6 +139,11 @@ func SaveNotificationHandler(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
utils.Log(3, err) utils.Log(3, err)
} }
msg := fmt.Sprintf("%v - %v - %v", notifierId, notifer, enabled)
w.Write([]byte(msg)) if notifer.Enabled {
notify := notifiers.SelectNotifier(notifer.Id)
go notify.Run()
}
http.Redirect(w, r, "/settings", http.StatusSeeOther)
} }

View File

@ -65,7 +65,7 @@ func mainProcess() {
var err error var err error
err = core.DbConnection(core.Configs.Connection) err = core.DbConnection(core.Configs.Connection)
if err != nil { if err != nil {
utils.Log(3, err) utils.Log(4, fmt.Sprintf("could not connect to database: %v", err))
} }
core.RunDatabaseUpgrades() core.RunDatabaseUpgrades()

View File

@ -111,19 +111,19 @@ func (u *Email) Run() error {
// ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS // ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS
func (u *Email) OnFailure() error { func (u *Email) OnFailure() error {
if u.Enabled {
utils.Log(1, fmt.Sprintf("Notification %v is receiving a failure notification.", u.Method)) utils.Log(1, fmt.Sprintf("Notification %v is receiving a failure notification.", u.Method))
// Do failing stuff here! // Do failing stuff here!
}
return nil return nil
} }
// ON SERVICE SUCCESS, DO YOUR OWN FUNCTIONS // ON SERVICE SUCCESS, DO YOUR OWN FUNCTIONS
func (u *Email) OnSuccess() error { func (u *Email) OnSuccess() error {
utils.Log(1, fmt.Sprintf("Notification %v is receiving a successful notification.", u.Method)) if u.Enabled {
utils.Log(1, fmt.Sprintf("Notification %v is receiving a failure notification.", u.Method))
// Do checking or any successful things here // Do failing stuff here!
}
return nil return nil
} }

View File

@ -70,6 +70,12 @@ func (n *Notification) isInDatabase() (bool, error) {
return Collections.Find("id", n.Id).Exists() return Collections.Find("id", n.Id).Exists()
} }
func SelectNotification(id int64) (*Notification, error) {
var notifier *Notification
err := Collections.Find("id", id).One(&notifier)
return notifier, err
}
func (n *Notification) Update() (*Notification, error) { func (n *Notification) Update() (*Notification, error) {
n.CreatedAt = time.Now() n.CreatedAt = time.Now()
err := Collections.Find("id", n.Id).Update(n) err := Collections.Find("id", n.Id).Update(n)
@ -97,6 +103,15 @@ func Select(id int64) *Notification {
return notifier return notifier
} }
func SelectNotifier(id int64) Notifier {
var notifier Notifier
for _, n := range AllCommunications {
notif := n.(Notifier)
return notif
}
return notifier
}
func (f NotificationForm) Value() string { func (f NotificationForm) Value() string {
notifier := Select(f.id) notifier := Select(f.id)
return notifier.GetValue(f.DbField) return notifier.GetValue(f.DbField)

View File

@ -46,11 +46,19 @@ func (u *Slack) Select() *Notification {
// WHEN NOTIFIER LOADS // WHEN NOTIFIER LOADS
func (u *Slack) Init() error { func (u *Slack) Init() error {
err := SendSlack("its online")
u.Install() err := u.Install()
if err == nil {
notifier, _ := SelectNotification(u.Id)
forms := u.Form
u.Notification = notifier
u.Form = forms
if u.Enabled {
go u.Run()
}
}
//go u.Run()
return err return err
} }
@ -84,19 +92,19 @@ func SendSlack(msg string) error {
// ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS // ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS
func (u *Slack) OnFailure() error { func (u *Slack) OnFailure() error {
if u.Enabled {
utils.Log(1, fmt.Sprintf("Notification %v is receiving a failure notification.", u.Method)) utils.Log(1, fmt.Sprintf("Notification %v is receiving a failure notification.", u.Method))
// Do failing stuff here! // Do failing stuff here!
}
return nil return nil
} }
// ON SERVICE SUCCESS, DO YOUR OWN FUNCTIONS // ON SERVICE SUCCESS, DO YOUR OWN FUNCTIONS
func (u *Slack) OnSuccess() error { func (u *Slack) OnSuccess() error {
if u.Enabled {
utils.Log(1, fmt.Sprintf("Notification %v is receiving a successful notification.", u.Method)) utils.Log(1, fmt.Sprintf("Notification %v is receiving a successful notification.", u.Method))
// Do checking or any successful things here // Do checking or any successful things here
}
return nil return nil
} }