2018-06-10 01:31:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-03-06 22:18:06 +00:00
|
|
|
"fmt"
|
2020-02-29 23:36:31 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-04-04 00:28:09 +00:00
|
|
|
"github.com/statping/statping/database"
|
2020-03-09 18:17:55 +00:00
|
|
|
"github.com/statping/statping/handlers"
|
2020-04-04 00:28:09 +00:00
|
|
|
"github.com/statping/statping/notifiers"
|
2020-04-02 04:44:41 +00:00
|
|
|
"github.com/statping/statping/source"
|
2020-03-09 18:17:55 +00:00
|
|
|
"github.com/statping/statping/types/configs"
|
2020-04-04 00:28:09 +00:00
|
|
|
"github.com/statping/statping/types/core"
|
2020-03-09 18:17:55 +00:00
|
|
|
"github.com/statping/statping/types/services"
|
|
|
|
"github.com/statping/statping/utils"
|
2020-04-04 00:28:09 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2018-06-10 01:31:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-12-04 04:17:29 +00:00
|
|
|
// VERSION stores the current version of Statping
|
2018-10-07 04:48:33 +00:00
|
|
|
VERSION string
|
2018-12-04 04:17:29 +00:00
|
|
|
// COMMIT stores the git commit hash for this version of Statping
|
2020-04-04 00:28:09 +00:00
|
|
|
COMMIT string
|
|
|
|
ipAddress string
|
|
|
|
//grpcPort int
|
2019-12-28 09:01:07 +00:00
|
|
|
envFile string
|
|
|
|
verboseMode int
|
2018-10-07 05:04:06 +00:00
|
|
|
port int
|
2019-12-30 08:08:51 +00:00
|
|
|
log = utils.Log.WithField("type", "cmd")
|
2020-04-11 22:18:43 +00:00
|
|
|
confgs *configs.DbConfig
|
2018-06-10 01:31:13 +00:00
|
|
|
)
|
|
|
|
|
2020-04-04 00:28:09 +00:00
|
|
|
func init() {
|
|
|
|
core.New(VERSION)
|
2020-04-16 09:57:00 +00:00
|
|
|
rootCmd.AddCommand(versionCmd)
|
|
|
|
rootCmd.AddCommand(assetsCmd)
|
|
|
|
rootCmd.AddCommand(exportCmd)
|
|
|
|
rootCmd.AddCommand(importCmd)
|
|
|
|
rootCmd.AddCommand(sassCmd)
|
|
|
|
rootCmd.AddCommand(onceCmd)
|
2020-04-17 03:21:17 +00:00
|
|
|
rootCmd.AddCommand(envCmd)
|
|
|
|
rootCmd.AddCommand(resetCmd)
|
|
|
|
utils.InitCLI()
|
|
|
|
parseFlags(rootCmd)
|
2020-04-04 00:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// exit will return an error and return an exit code 1 due to this error
|
2020-03-03 06:42:37 +00:00
|
|
|
func exit(err error) {
|
2020-04-04 00:28:09 +00:00
|
|
|
utils.SentryErr(err)
|
2020-03-08 18:13:27 +00:00
|
|
|
Close()
|
2020-04-04 00:28:09 +00:00
|
|
|
log.Fatalln(err)
|
2020-03-03 06:42:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 00:28:09 +00:00
|
|
|
// Close will gracefully stop the database connection, and log file
|
|
|
|
func Close() {
|
|
|
|
utils.CloseLogs()
|
|
|
|
confgs.Close()
|
|
|
|
fmt.Println("Shutting down Statping")
|
2020-03-22 07:09:45 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 04:17:29 +00:00
|
|
|
// main will run the Statping application
|
2018-06-10 01:31:13 +00:00
|
|
|
func main() {
|
2020-04-16 09:57:00 +00:00
|
|
|
Execute()
|
|
|
|
}
|
|
|
|
|
|
|
|
// main will run the Statping application
|
|
|
|
func start() {
|
2018-06-30 00:57:05 +00:00
|
|
|
var err error
|
2019-12-19 16:48:59 +00:00
|
|
|
go sigterm()
|
2020-03-04 10:29:00 +00:00
|
|
|
|
|
|
|
if err := source.Assets(); err != nil {
|
|
|
|
exit(err)
|
|
|
|
}
|
|
|
|
|
2019-12-28 09:01:07 +00:00
|
|
|
utils.VerboseMode = verboseMode
|
2020-03-04 10:29:00 +00:00
|
|
|
|
2019-04-29 18:11:44 +00:00
|
|
|
if err := utils.InitLogs(); err != nil {
|
2019-12-30 11:12:14 +00:00
|
|
|
log.Errorf("Statping Log Error: %v\n", err)
|
2019-04-29 18:11:44 +00:00
|
|
|
}
|
2020-03-04 10:29:00 +00:00
|
|
|
|
2020-04-04 00:28:09 +00:00
|
|
|
log.Info(fmt.Sprintf("Starting Statping v%s", VERSION))
|
2018-11-21 08:45:55 +00:00
|
|
|
|
2020-04-16 09:57:00 +00:00
|
|
|
//if err := updateDisplay(); err != nil {
|
|
|
|
// log.Warnln(err)
|
|
|
|
//}
|
2020-03-03 06:42:37 +00:00
|
|
|
|
2020-03-10 05:24:35 +00:00
|
|
|
confgs, err = configs.LoadConfigs()
|
2020-03-04 10:29:00 +00:00
|
|
|
if err != nil {
|
|
|
|
if err := SetupMode(); err != nil {
|
2020-03-03 06:42:37 +00:00
|
|
|
exit(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-10 05:24:35 +00:00
|
|
|
if err = configs.ConnectConfigs(confgs); err != nil {
|
2020-03-04 10:29:00 +00:00
|
|
|
exit(err)
|
|
|
|
}
|
|
|
|
|
2020-03-22 07:09:45 +00:00
|
|
|
if !confgs.Db.HasTable("core") {
|
2020-03-10 15:52:07 +00:00
|
|
|
var srvs int64
|
2020-03-22 07:09:45 +00:00
|
|
|
if confgs.Db.HasTable(&services.Service{}) {
|
|
|
|
confgs.Db.Model(&services.Service{}).Count(&srvs)
|
|
|
|
if srvs > 0 {
|
|
|
|
exit(errors.Wrap(err, "there are already services setup."))
|
|
|
|
return
|
|
|
|
}
|
2020-03-10 15:52:07 +00:00
|
|
|
}
|
2020-03-09 15:15:15 +00:00
|
|
|
|
2020-03-10 05:24:35 +00:00
|
|
|
if err := confgs.DropDatabase(); err != nil {
|
2020-03-09 15:15:15 +00:00
|
|
|
exit(errors.Wrap(err, "error dropping database"))
|
|
|
|
}
|
|
|
|
|
2020-03-10 05:24:35 +00:00
|
|
|
if err := confgs.CreateDatabase(); err != nil {
|
2020-03-09 15:15:15 +00:00
|
|
|
exit(errors.Wrap(err, "error creating database"))
|
|
|
|
}
|
|
|
|
|
2020-03-10 05:24:35 +00:00
|
|
|
if err := configs.CreateAdminUser(confgs); err != nil {
|
2020-03-09 15:15:15 +00:00
|
|
|
exit(errors.Wrap(err, "error creating default admin user"))
|
|
|
|
}
|
|
|
|
|
2020-04-16 09:57:00 +00:00
|
|
|
if utils.Params.GetBool("SAMPLE_DATA") {
|
2020-04-15 13:05:22 +00:00
|
|
|
if err := configs.TriggerSamples(); err != nil {
|
|
|
|
exit(errors.Wrap(err, "error creating database"))
|
|
|
|
}
|
2020-03-09 15:15:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-11 04:37:52 +00:00
|
|
|
if err = confgs.DatabaseChanges(); err != nil {
|
|
|
|
exit(err)
|
|
|
|
}
|
|
|
|
|
2020-03-10 05:24:35 +00:00
|
|
|
if err := confgs.MigrateDatabase(); err != nil {
|
2020-03-04 10:29:00 +00:00
|
|
|
exit(err)
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
2020-02-26 06:39:54 +00:00
|
|
|
|
2019-12-30 11:12:14 +00:00
|
|
|
if err := mainProcess(); err != nil {
|
2020-03-03 06:42:37 +00:00
|
|
|
exit(err)
|
2019-12-30 11:12:14 +00:00
|
|
|
}
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 10:29:00 +00:00
|
|
|
func SetupMode() error {
|
|
|
|
return handlers.RunHTTPServer(ipAddress, port)
|
2019-12-28 09:01:07 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 16:48:59 +00:00
|
|
|
// sigterm will attempt to close the database connections gracefully
|
|
|
|
func sigterm() {
|
|
|
|
sigs := make(chan os.Signal, 1)
|
2020-03-06 22:18:06 +00:00
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
2019-12-19 16:48:59 +00:00
|
|
|
<-sigs
|
2019-12-28 09:01:07 +00:00
|
|
|
Close()
|
2020-03-21 21:00:42 +00:00
|
|
|
os.Exit(0)
|
2019-12-19 16:48:59 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 04:17:29 +00:00
|
|
|
// mainProcess will initialize the Statping application and run the HTTP server
|
2019-12-30 11:12:14 +00:00
|
|
|
func mainProcess() error {
|
2020-03-04 10:29:00 +00:00
|
|
|
if err := services.ServicesFromEnvFile(); err != nil {
|
2020-02-29 23:36:31 +00:00
|
|
|
errStr := "error 'SERVICE' environment variable"
|
|
|
|
log.Errorln(errStr)
|
|
|
|
return errors.Wrap(err, errStr)
|
2020-02-26 05:38:03 +00:00
|
|
|
}
|
2020-02-29 23:36:31 +00:00
|
|
|
|
2020-03-14 03:13:20 +00:00
|
|
|
if err := InitApp(); err != nil {
|
2020-02-26 05:38:03 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-03-04 10:29:00 +00:00
|
|
|
|
|
|
|
if err := handlers.RunHTTPServer(ipAddress, port); err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
return errors.Wrap(err, "http server")
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
2020-02-29 23:36:31 +00:00
|
|
|
return nil
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
2020-03-04 10:29:00 +00:00
|
|
|
|
2020-04-04 00:28:09 +00:00
|
|
|
// InitApp will start the Statping instance with a valid database connection
|
|
|
|
// This function will gather all services in database, add/init Notifiers,
|
|
|
|
// and start the database cleanup routine
|
|
|
|
func InitApp() error {
|
|
|
|
if _, err := core.Select(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := services.SelectAllServices(true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go services.CheckServices()
|
|
|
|
notifiers.InitNotifiers()
|
2020-04-11 22:18:43 +00:00
|
|
|
go database.Maintenance()
|
|
|
|
utils.SentryInit(&VERSION, core.App.AllowReports.Bool)
|
2020-04-04 00:28:09 +00:00
|
|
|
core.App.Setup = true
|
|
|
|
core.App.Started = utils.Now()
|
|
|
|
return nil
|
|
|
|
}
|