2016-09-30 01:36:20 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-09-04 03:42:48 -05:00
|
|
|
"flag"
|
|
|
|
"io/ioutil"
|
2016-09-30 01:36:20 -05:00
|
|
|
"os"
|
2017-09-04 03:42:48 -05:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
2016-09-30 01:36:20 -05:00
|
|
|
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api"
|
|
|
|
"github.com/grafana/grafana/pkg/log"
|
|
|
|
"github.com/grafana/grafana/pkg/login"
|
|
|
|
"github.com/grafana/grafana/pkg/metrics"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
|
|
|
"github.com/grafana/grafana/pkg/services/alerting"
|
|
|
|
"github.com/grafana/grafana/pkg/services/cleanup"
|
|
|
|
"github.com/grafana/grafana/pkg/services/notifications"
|
|
|
|
"github.com/grafana/grafana/pkg/services/search"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
"github.com/grafana/grafana/pkg/social"
|
2017-09-11 01:42:52 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tracing"
|
2016-09-30 01:36:20 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewGrafanaServer() models.GrafanaServer {
|
|
|
|
rootCtx, shutdownFn := context.WithCancel(context.Background())
|
|
|
|
childRoutines, childCtx := errgroup.WithContext(rootCtx)
|
|
|
|
|
|
|
|
return &GrafanaServerImpl{
|
|
|
|
context: childCtx,
|
|
|
|
shutdownFn: shutdownFn,
|
|
|
|
childRoutines: childRoutines,
|
|
|
|
log: log.New("server"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type GrafanaServerImpl struct {
|
|
|
|
context context.Context
|
|
|
|
shutdownFn context.CancelFunc
|
|
|
|
childRoutines *errgroup.Group
|
|
|
|
log log.Logger
|
2017-02-06 02:40:07 -06:00
|
|
|
|
|
|
|
httpServer *api.HttpServer
|
2016-09-30 01:36:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GrafanaServerImpl) Start() {
|
|
|
|
go listenToSystemSignals(g)
|
|
|
|
|
2017-09-04 03:42:48 -05:00
|
|
|
g.initLogging()
|
|
|
|
g.writePIDFile()
|
|
|
|
|
2016-09-30 01:36:20 -05:00
|
|
|
initSql()
|
2017-09-06 15:00:17 -05:00
|
|
|
metrics.Init(setting.Cfg)
|
2016-09-30 01:36:20 -05:00
|
|
|
search.Init()
|
|
|
|
login.Init()
|
|
|
|
social.NewOAuthService()
|
|
|
|
plugins.Init()
|
|
|
|
|
2017-09-11 01:42:52 -05:00
|
|
|
closer, err := tracing.Init(setting.Cfg)
|
2017-09-10 07:13:57 -05:00
|
|
|
if err != nil {
|
2017-09-11 01:42:52 -05:00
|
|
|
g.log.Error("Tracing settings is not valid", "error", err)
|
2017-09-10 07:13:57 -05:00
|
|
|
g.Shutdown(1, "Startup failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer closer.Close()
|
|
|
|
|
2016-09-30 01:36:20 -05:00
|
|
|
// init alerting
|
2017-01-25 06:32:26 -06:00
|
|
|
if setting.AlertingEnabled && setting.ExecuteAlerts {
|
2016-09-30 01:36:20 -05:00
|
|
|
engine := alerting.NewEngine()
|
|
|
|
g.childRoutines.Go(func() error { return engine.Run(g.context) })
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup service
|
|
|
|
cleanUpService := cleanup.NewCleanUpService()
|
|
|
|
g.childRoutines.Go(func() error { return cleanUpService.Run(g.context) })
|
|
|
|
|
2017-09-11 01:42:52 -05:00
|
|
|
if err = notifications.Init(); err != nil {
|
2017-09-10 07:13:57 -05:00
|
|
|
g.log.Error("Notification service failed to initialize", "error", err)
|
2016-09-30 01:36:20 -05:00
|
|
|
g.Shutdown(1, "Startup failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
g.startHttpServer()
|
|
|
|
}
|
|
|
|
|
2017-09-04 03:42:48 -05:00
|
|
|
func (g *GrafanaServerImpl) initLogging() {
|
|
|
|
err := setting.NewConfigContext(&setting.CommandLineArgs{
|
|
|
|
Config: *configFile,
|
|
|
|
HomePath: *homePath,
|
|
|
|
Args: flag.Args(),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
g.log.Error(err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
g.log.Info("Starting Grafana", "version", version, "commit", commit, "compiled", time.Unix(setting.BuildStamp, 0))
|
|
|
|
setting.LogConfigurationInfo()
|
|
|
|
}
|
|
|
|
|
2016-09-30 01:36:20 -05:00
|
|
|
func (g *GrafanaServerImpl) startHttpServer() {
|
2017-02-06 02:40:07 -06:00
|
|
|
g.httpServer = api.NewHttpServer()
|
2016-12-21 07:36:32 -06:00
|
|
|
|
2017-02-06 02:40:07 -06:00
|
|
|
err := g.httpServer.Start(g.context)
|
2016-09-30 01:36:20 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
g.log.Error("Fail to start server", "error", err)
|
|
|
|
g.Shutdown(1, "Startup failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GrafanaServerImpl) Shutdown(code int, reason string) {
|
2016-09-30 06:25:54 -05:00
|
|
|
g.log.Info("Shutdown started", "code", code, "reason", reason)
|
2016-09-30 01:36:20 -05:00
|
|
|
|
2017-02-06 02:40:07 -06:00
|
|
|
err := g.httpServer.Shutdown(g.context)
|
|
|
|
if err != nil {
|
|
|
|
g.log.Error("Failed to shutdown server", "error", err)
|
|
|
|
}
|
|
|
|
|
2016-09-30 01:36:20 -05:00
|
|
|
g.shutdownFn()
|
2017-02-06 02:40:07 -06:00
|
|
|
err = g.childRoutines.Wait()
|
2016-09-30 01:36:20 -05:00
|
|
|
|
2016-09-30 06:25:54 -05:00
|
|
|
g.log.Info("Shutdown completed", "reason", err)
|
2016-09-30 01:36:20 -05:00
|
|
|
log.Close()
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
2017-09-04 03:42:48 -05:00
|
|
|
|
|
|
|
func (g *GrafanaServerImpl) writePIDFile() {
|
|
|
|
if *pidFile == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the required directory structure exists.
|
|
|
|
err := os.MkdirAll(filepath.Dir(*pidFile), 0700)
|
|
|
|
if err != nil {
|
|
|
|
g.log.Error("Failed to verify pid directory", "error", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the PID and write it.
|
|
|
|
pid := strconv.Itoa(os.Getpid())
|
|
|
|
if err := ioutil.WriteFile(*pidFile, []byte(pid), 0644); err != nil {
|
|
|
|
g.log.Error("Failed to write pidfile", "error", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2017-09-04 05:44:38 -05:00
|
|
|
|
|
|
|
g.log.Info("Writing PID file", "path", *pidFile, "pid", pid)
|
2017-09-04 03:42:48 -05:00
|
|
|
}
|