2016-09-30 01:36:20 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-09-04 03:42:48 -05:00
|
|
|
"flag"
|
2017-12-01 07:02:05 -06:00
|
|
|
"fmt"
|
2017-09-04 03:42:48 -05:00
|
|
|
"io/ioutil"
|
2017-12-01 07:02:05 -06:00
|
|
|
"net"
|
2016-09-30 01:36:20 -05:00
|
|
|
"os"
|
2017-09-04 03:42:48 -05:00
|
|
|
"path/filepath"
|
2018-04-27 06:41:58 -05:00
|
|
|
"reflect"
|
2017-09-04 03:42:48 -05:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2016-09-30 01:36:20 -05:00
|
|
|
|
2018-04-27 06:41:58 -05:00
|
|
|
"github.com/facebookgo/inject"
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
|
|
"github.com/grafana/grafana/pkg/registry"
|
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
2017-10-23 02:02:55 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/provisioning"
|
|
|
|
|
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"
|
2017-10-09 12:27:06 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
2016-09-30 01:36:20 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2017-10-23 02:02:55 -05:00
|
|
|
|
2016-09-30 01:36:20 -05:00
|
|
|
"github.com/grafana/grafana/pkg/social"
|
2017-09-11 01:42:52 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tracing"
|
2018-04-27 06:41:58 -05:00
|
|
|
|
2018-04-27 06:01:32 -05:00
|
|
|
// self registering services
|
2018-04-27 06:41:58 -05:00
|
|
|
_ "github.com/grafana/grafana/pkg/extensions"
|
2018-04-27 08:11:55 -05:00
|
|
|
_ "github.com/grafana/grafana/pkg/plugins"
|
2018-04-27 06:41:58 -05:00
|
|
|
_ "github.com/grafana/grafana/pkg/services/alerting"
|
|
|
|
_ "github.com/grafana/grafana/pkg/services/cleanup"
|
2018-04-27 06:01:32 -05:00
|
|
|
_ "github.com/grafana/grafana/pkg/services/notifications"
|
2018-04-27 06:41:58 -05:00
|
|
|
_ "github.com/grafana/grafana/pkg/services/search"
|
2016-09-30 01:36:20 -05:00
|
|
|
)
|
|
|
|
|
2017-12-08 07:11:48 -06:00
|
|
|
func NewGrafanaServer() *GrafanaServerImpl {
|
2016-09-30 01:36:20 -05:00
|
|
|
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
|
|
|
|
2018-04-27 06:01:32 -05:00
|
|
|
RouteRegister api.RouteRegister `inject:""`
|
|
|
|
HttpServer *api.HTTPServer `inject:""`
|
2016-09-30 01:36:20 -05:00
|
|
|
}
|
|
|
|
|
2017-12-08 07:11:48 -06:00
|
|
|
func (g *GrafanaServerImpl) Start() error {
|
2017-09-04 03:42:48 -05:00
|
|
|
g.initLogging()
|
|
|
|
g.writePIDFile()
|
|
|
|
|
2018-04-27 06:41:58 -05:00
|
|
|
// initSql
|
|
|
|
sqlstore.NewEngine() // TODO: this should return an error
|
|
|
|
sqlstore.EnsureAdminUser()
|
2017-10-23 02:02:55 -05:00
|
|
|
|
2017-09-06 15:00:17 -05:00
|
|
|
metrics.Init(setting.Cfg)
|
2016-09-30 01:36:20 -05:00
|
|
|
login.Init()
|
|
|
|
social.NewOAuthService()
|
2017-09-22 09:04:06 -05:00
|
|
|
|
2017-11-23 04:29:06 -06:00
|
|
|
if err := provisioning.Init(g.context, setting.HomePath, setting.Cfg); err != nil {
|
2017-12-08 07:11:48 -06:00
|
|
|
return fmt.Errorf("Failed to provision Grafana from config. error: %v", err)
|
2017-10-23 02:02:55 -05:00
|
|
|
}
|
|
|
|
|
2017-09-22 09:04:06 -05:00
|
|
|
tracingCloser, err := tracing.Init(setting.Cfg)
|
2017-09-10 07:13:57 -05:00
|
|
|
if err != nil {
|
2017-12-08 07:11:48 -06:00
|
|
|
return fmt.Errorf("Tracing settings is not valid. error: %v", err)
|
2017-09-10 07:13:57 -05:00
|
|
|
}
|
2017-09-22 09:04:06 -05:00
|
|
|
defer tracingCloser.Close()
|
2017-09-10 07:13:57 -05:00
|
|
|
|
2018-04-27 06:41:58 -05:00
|
|
|
serviceGraph := inject.Graph{}
|
|
|
|
serviceGraph.Provide(&inject.Object{Value: bus.GetBus()})
|
|
|
|
serviceGraph.Provide(&inject.Object{Value: dashboards.NewProvisioningService()})
|
|
|
|
serviceGraph.Provide(&inject.Object{Value: api.NewRouteRegister(middleware.RequestMetrics, middleware.RequestTracing)})
|
|
|
|
serviceGraph.Provide(&inject.Object{Value: api.HTTPServer{}})
|
|
|
|
services := registry.GetServices()
|
2016-09-30 01:36:20 -05:00
|
|
|
|
2018-04-27 06:41:58 -05:00
|
|
|
// Add all services to dependency graph
|
|
|
|
for _, service := range services {
|
|
|
|
serviceGraph.Provide(&inject.Object{Value: service})
|
2016-09-30 01:36:20 -05:00
|
|
|
}
|
|
|
|
|
2018-04-27 06:41:58 -05:00
|
|
|
serviceGraph.Provide(&inject.Object{Value: g})
|
2017-12-08 07:11:48 -06:00
|
|
|
|
2018-04-27 06:41:58 -05:00
|
|
|
// Inject dependencies to services
|
|
|
|
if err := serviceGraph.Populate(); err != nil {
|
|
|
|
return fmt.Errorf("Failed to populate service dependency: %v", err)
|
|
|
|
}
|
2016-09-30 01:36:20 -05:00
|
|
|
|
2018-04-27 06:41:58 -05:00
|
|
|
// Init & start services
|
|
|
|
for _, service := range services {
|
|
|
|
if registry.IsDisabled(service) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
g.log.Info("Initializing " + reflect.TypeOf(service).Elem().Name())
|
|
|
|
|
|
|
|
if err := service.Init(); err != nil {
|
|
|
|
return fmt.Errorf("Service init failed %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start background services
|
|
|
|
for index := range services {
|
|
|
|
service, ok := services[index].(registry.BackgroundService)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if registry.IsDisabled(services[index]) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
g.childRoutines.Go(func() error {
|
|
|
|
err := service.Run(g.context)
|
|
|
|
g.log.Info("Stopped "+reflect.TypeOf(service).Elem().Name(), "reason", err)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
sendSystemdNotification("READY=1")
|
|
|
|
return g.startHttpServer()
|
2017-10-09 12:27:06 -05:00
|
|
|
}
|
|
|
|
|
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 {
|
2018-03-28 15:51:21 -05:00
|
|
|
fmt.Fprintf(os.Stderr, "Failed to start grafana. error: %s\n", err.Error())
|
2017-09-04 03:42:48 -05:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2018-04-27 06:41:58 -05:00
|
|
|
g.log.Info("Starting "+setting.ApplicationName, "version", version, "commit", commit, "compiled", time.Unix(setting.BuildStamp, 0))
|
2017-09-04 03:42:48 -05:00
|
|
|
setting.LogConfigurationInfo()
|
|
|
|
}
|
|
|
|
|
2017-12-08 07:11:48 -06:00
|
|
|
func (g *GrafanaServerImpl) startHttpServer() error {
|
2018-04-27 06:41:58 -05:00
|
|
|
g.HttpServer.Init()
|
2016-12-21 07:36:32 -06:00
|
|
|
|
2018-04-27 06:41:58 -05:00
|
|
|
err := g.HttpServer.Start(g.context)
|
2016-09-30 01:36:20 -05:00
|
|
|
|
|
|
|
if err != nil {
|
2017-12-08 07:11:48 -06:00
|
|
|
return fmt.Errorf("Fail to start server. error: %v", err)
|
2016-09-30 01:36:20 -05:00
|
|
|
}
|
2017-12-08 07:11:48 -06:00
|
|
|
|
|
|
|
return nil
|
2016-09-30 01:36:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-04-27 06:41:58 -05:00
|
|
|
err := g.HttpServer.Shutdown(g.context)
|
2017-02-06 02:40:07 -06:00
|
|
|
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()
|
2017-12-08 07:11:48 -06:00
|
|
|
if err != nil && err != context.Canceled {
|
|
|
|
g.log.Error("Server shutdown completed with an error", "error", err)
|
|
|
|
}
|
2016-09-30 01:36:20 -05:00
|
|
|
}
|
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
|
|
|
}
|
2017-12-01 07:02:05 -06:00
|
|
|
|
2017-12-08 07:11:48 -06:00
|
|
|
func sendSystemdNotification(state string) error {
|
2017-12-01 07:02:05 -06:00
|
|
|
notifySocket := os.Getenv("NOTIFY_SOCKET")
|
|
|
|
|
|
|
|
if notifySocket == "" {
|
|
|
|
return fmt.Errorf("NOTIFY_SOCKET environment variable empty or unset.")
|
|
|
|
}
|
|
|
|
|
|
|
|
socketAddr := &net.UnixAddr{
|
|
|
|
Name: notifySocket,
|
|
|
|
Net: "unixgram",
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = conn.Write([]byte(state))
|
|
|
|
|
|
|
|
conn.Close()
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|