statping/cmd/main.go

213 lines
5.0 KiB
Go
Raw Normal View History

2018-06-10 01:31:13 +00:00
package main
import (
2020-03-06 22:18:06 +00:00
"flag"
"fmt"
2020-02-29 23:36:31 +00:00
"github.com/pkg/errors"
"github.com/statping/statping/database"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/handlers"
"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"
"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"
"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
COMMIT string
ipAddress string
//grpcPort int
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
)
// parseFlags will parse the application flags
// -ip = 0.0.0.0 IP address for outgoing HTTP server
// -port = 8080 Port number for outgoing HTTP server
2020-02-26 05:38:03 +00:00
// environment variables WILL overwrite flags
2018-08-16 06:22:20 +00:00
func parseFlags() {
2020-02-26 05:38:03 +00:00
envPort := utils.Getenv("PORT", 8080).(int)
envIpAddress := utils.Getenv("IP", "0.0.0.0").(string)
envVerbose := utils.Getenv("VERBOSE", 2).(int)
//envGrpcPort := utils.Getenv("GRPC_PORT", 0).(int)
2020-02-26 05:38:03 +00:00
flag.StringVar(&ipAddress, "ip", envIpAddress, "IP address to run the Statping HTTP server")
flag.StringVar(&envFile, "env", "", "IP address to run the Statping HTTP server")
2020-02-26 05:38:03 +00:00
flag.IntVar(&port, "port", envPort, "Port to run the HTTP server")
//flag.IntVar(&grpcPort, "grpc", envGrpcPort, "Port to run the gRPC server")
2020-02-26 05:38:03 +00:00
flag.IntVar(&verboseMode, "verbose", envVerbose, "Run in verbose mode to see detailed logs (1 - 4)")
2018-08-16 06:22:20 +00:00
flag.Parse()
}
func init() {
core.New(VERSION)
}
// 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) {
utils.SentryErr(err)
2020-03-08 18:13:27 +00:00
Close()
log.Fatalln(err)
2020-03-03 06:42:37 +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() {
2018-06-30 00:57:05 +00:00
var err error
go sigterm()
2020-03-04 10:29:00 +00:00
2018-08-16 06:22:20 +00:00
parseFlags()
2020-03-04 10:29:00 +00:00
if err := source.Assets(); err != nil {
exit(err)
}
utils.VerboseMode = verboseMode
2020-03-04 10:29:00 +00:00
if err := utils.InitLogs(); err != nil {
2019-12-30 11:12:14 +00:00
log.Errorf("Statping Log Error: %v\n", err)
}
2020-03-04 10:29:00 +00:00
args := flag.Args()
2018-08-16 06:22:20 +00:00
if len(args) >= 1 {
2018-10-11 16:53:13 +00:00
err := catchCLI(args)
if err != nil {
2018-08-16 06:22:20 +00:00
if err.Error() == "end" {
os.Exit(0)
2020-03-03 06:42:37 +00:00
return
2018-08-16 06:22:20 +00:00
}
2020-03-03 06:42:37 +00:00
exit(err)
}
2018-06-24 06:17:31 +00:00
}
log.Info(fmt.Sprintf("Starting Statping v%s", VERSION))
2020-03-03 06:42:37 +00:00
if err := updateDisplay(); err != nil {
log.Warnln(err)
}
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"))
}
if err := configs.TriggerSamples(); err != nil {
exit(errors.Wrap(err, "error creating database"))
}
}
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
2020-03-14 03:13:20 +00:00
//log.Infoln("Migrating Notifiers...")
//if err := notifier.Migrate(); err != nil {
// exit(errors.Wrap(err, "error migrating notifiers"))
//}
//log.Infoln("Notifiers Migrated")
2020-03-09 15:15:15 +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)
}
// 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)
<-sigs
Close()
2020-03-21 21:00:42 +00:00
os.Exit(0)
}
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
// 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)
core.App.Setup = true
core.App.Started = utils.Now()
return nil
}