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
|
|
|
|
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"
|
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-05-02 12:54:07 -05:00
|
|
|
_ "github.com/grafana/grafana/pkg/metrics"
|
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-05-01 08:51:15 -05:00
|
|
|
_ "github.com/grafana/grafana/pkg/services/provisioning"
|
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"),
|
2018-04-30 09:21:04 -05:00
|
|
|
cfg: setting.NewCfg(),
|
2016-09-30 01:36:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type GrafanaServerImpl struct {
|
2018-05-02 11:10:21 -05:00
|
|
|
context context.Context
|
|
|
|
shutdownFn context.CancelFunc
|
|
|
|
childRoutines *errgroup.Group
|
|
|
|
log log.Logger
|
|
|
|
cfg *setting.Cfg
|
|
|
|
shutdownReason string
|
|
|
|
shutdownInProgress bool
|
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
|
|
|
}
|
|
|
|
|
2018-05-02 11:10:21 -05:00
|
|
|
func (g *GrafanaServerImpl) Run() error {
|
2018-04-30 09:21:04 -05:00
|
|
|
g.loadConfiguration()
|
2017-09-04 03:42:48 -05:00
|
|
|
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
|
|
|
|
2016-09-30 01:36:20 -05:00
|
|
|
login.Init()
|
|
|
|
social.NewOAuthService()
|
2017-09-22 09:04:06 -05:00
|
|
|
|
2018-04-30 09:21:04 -05:00
|
|
|
tracingCloser, err := tracing.Init(g.cfg.Raw)
|
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()})
|
2018-04-30 09:21:04 -05:00
|
|
|
serviceGraph.Provide(&inject.Object{Value: g.cfg})
|
2018-04-27 06:41:58 -05:00
|
|
|
serviceGraph.Provide(&inject.Object{Value: dashboards.NewProvisioningService()})
|
|
|
|
serviceGraph.Provide(&inject.Object{Value: api.NewRouteRegister(middleware.RequestMetrics, middleware.RequestTracing)})
|
2018-05-01 07:13:38 -05:00
|
|
|
|
|
|
|
// self registered services
|
2018-04-27 06:41:58 -05:00
|
|
|
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 {
|
2018-05-01 08:51:15 -05:00
|
|
|
return fmt.Errorf("Service init failed: %v", err)
|
2018-04-27 06:41:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2018-05-02 11:27:54 -05:00
|
|
|
// Skip starting new service when shutting down
|
|
|
|
// Can happen when service stop/return during startup
|
2018-05-02 11:10:21 -05:00
|
|
|
if g.shutdownInProgress {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-27 06:41:58 -05:00
|
|
|
err := service.Run(g.context)
|
2018-05-02 11:10:21 -05:00
|
|
|
|
|
|
|
// If error is not canceled then the service crashed
|
2018-05-08 04:59:27 -05:00
|
|
|
if err != context.Canceled && err != nil {
|
2018-05-02 11:10:21 -05:00
|
|
|
g.log.Error("Stopped "+reflect.TypeOf(service).Elem().Name(), "reason", err)
|
|
|
|
} else {
|
|
|
|
g.log.Info("Stopped "+reflect.TypeOf(service).Elem().Name(), "reason", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark that we are in shutdown mode
|
|
|
|
// So more services are not started
|
|
|
|
g.shutdownInProgress = true
|
2018-04-27 06:41:58 -05:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
sendSystemdNotification("READY=1")
|
2018-05-02 07:14:42 -05:00
|
|
|
|
|
|
|
return g.childRoutines.Wait()
|
2017-10-09 12:27:06 -05:00
|
|
|
}
|
|
|
|
|
2018-04-30 09:21:04 -05:00
|
|
|
func (g *GrafanaServerImpl) loadConfiguration() {
|
|
|
|
err := g.cfg.Load(&setting.CommandLineArgs{
|
2017-09-04 03:42:48 -05:00
|
|
|
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))
|
2018-04-30 09:21:04 -05:00
|
|
|
g.cfg.LogConfigSources()
|
2017-09-04 03:42:48 -05:00
|
|
|
}
|
|
|
|
|
2018-05-02 07:14:42 -05:00
|
|
|
func (g *GrafanaServerImpl) Shutdown(reason string) {
|
|
|
|
g.log.Info("Shutdown started", "reason", reason)
|
|
|
|
g.shutdownReason = reason
|
2018-05-02 11:10:21 -05:00
|
|
|
g.shutdownInProgress = true
|
2016-09-30 01:36:20 -05:00
|
|
|
|
2018-05-01 07:13:38 -05:00
|
|
|
// call cancel func on root context
|
2016-09-30 01:36:20 -05:00
|
|
|
g.shutdownFn()
|
2018-05-01 07:13:38 -05:00
|
|
|
|
2018-05-01 07:18:10 -05:00
|
|
|
// wait for child routines
|
2018-05-02 07:14:42 -05:00
|
|
|
g.childRoutines.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GrafanaServerImpl) Exit(reason error) {
|
|
|
|
// default exit code is 1
|
|
|
|
code := 1
|
|
|
|
|
|
|
|
if reason == context.Canceled && g.shutdownReason != "" {
|
|
|
|
reason = fmt.Errorf(g.shutdownReason)
|
|
|
|
code = 0
|
2017-12-08 07:11:48 -06:00
|
|
|
}
|
2018-05-02 07:14:42 -05:00
|
|
|
|
|
|
|
g.log.Error("Server shutdown", "reason", reason)
|
|
|
|
os.Exit(code)
|
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
|
|
|
|
}
|