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"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
2016-09-30 01:36:20 -05:00
|
|
|
|
2018-04-27 06:41:58 -05:00
|
|
|
"github.com/facebookgo/inject"
|
2019-04-23 03:24:47 -05:00
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2018-10-29 07:27:29 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api"
|
2018-06-25 09:36:47 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/routing"
|
2018-04-27 06:41:58 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2018-10-26 03:40:33 -05:00
|
|
|
_ "github.com/grafana/grafana/pkg/extensions"
|
2019-06-13 05:15:52 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/localcache"
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2019-02-23 16:35:26 -06:00
|
|
|
_ "github.com/grafana/grafana/pkg/infra/metrics"
|
2019-03-08 13:49:16 -06:00
|
|
|
_ "github.com/grafana/grafana/pkg/infra/remotecache"
|
2018-12-11 06:45:58 -06:00
|
|
|
_ "github.com/grafana/grafana/pkg/infra/serverlock"
|
2019-02-23 16:24:27 -06:00
|
|
|
_ "github.com/grafana/grafana/pkg/infra/tracing"
|
2019-02-26 04:41:55 -06:00
|
|
|
_ "github.com/grafana/grafana/pkg/infra/usagestats"
|
2019-04-23 03:24:47 -05:00
|
|
|
"github.com/grafana/grafana/pkg/login"
|
|
|
|
"github.com/grafana/grafana/pkg/login/social"
|
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
2018-04-27 08:11:55 -05:00
|
|
|
_ "github.com/grafana/grafana/pkg/plugins"
|
2019-04-23 03:24:47 -05:00
|
|
|
"github.com/grafana/grafana/pkg/registry"
|
2018-04-27 06:41:58 -05:00
|
|
|
_ "github.com/grafana/grafana/pkg/services/alerting"
|
2019-02-06 10:02:57 -06:00
|
|
|
_ "github.com/grafana/grafana/pkg/services/auth"
|
2018-04-27 06:41:58 -05:00
|
|
|
_ "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-05-24 08:26:27 -05:00
|
|
|
_ "github.com/grafana/grafana/pkg/services/rendering"
|
2018-04-27 06:41:58 -05:00
|
|
|
_ "github.com/grafana/grafana/pkg/services/search"
|
2018-05-18 04:10:10 -05:00
|
|
|
_ "github.com/grafana/grafana/pkg/services/sqlstore"
|
2019-04-23 03:24:47 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2016-09-30 01:36:20 -05:00
|
|
|
)
|
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
// NewServer returns a new instance of Server.
|
|
|
|
func NewServer(configFile, homePath, pidFile string) *Server {
|
2016-09-30 01:36:20 -05:00
|
|
|
rootCtx, shutdownFn := context.WithCancel(context.Background())
|
|
|
|
childRoutines, childCtx := errgroup.WithContext(rootCtx)
|
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
return &Server{
|
2016-09-30 01:36:20 -05:00
|
|
|
context: childCtx,
|
|
|
|
shutdownFn: shutdownFn,
|
|
|
|
childRoutines: childRoutines,
|
|
|
|
log: log.New("server"),
|
2018-04-30 09:21:04 -05:00
|
|
|
cfg: setting.NewCfg(),
|
2019-10-14 07:18:22 -05:00
|
|
|
|
|
|
|
configFile: configFile,
|
|
|
|
homePath: homePath,
|
|
|
|
pidFile: pidFile,
|
2016-09-30 01:36:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
// Server is responsible for managing the lifecycle of services.
|
|
|
|
type Server 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
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
configFile string
|
|
|
|
homePath string
|
|
|
|
pidFile string
|
|
|
|
|
2018-06-25 09:36:47 -05:00
|
|
|
RouteRegister routing.RouteRegister `inject:""`
|
2019-10-14 07:18:22 -05:00
|
|
|
HTTPServer *api.HTTPServer `inject:""`
|
2016-09-30 01:36:20 -05:00
|
|
|
}
|
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
// Run initializes and starts services. This will block until all services have
|
|
|
|
// exited. To initiate shutdown, call the Shutdown method in another goroutine.
|
|
|
|
func (s *Server) Run() error {
|
|
|
|
s.loadConfiguration()
|
|
|
|
s.writePIDFile()
|
2017-09-04 03:42:48 -05:00
|
|
|
|
2018-10-30 06:31:28 -05:00
|
|
|
login.Init()
|
|
|
|
social.NewOAuthService()
|
2017-09-22 09:04:06 -05:00
|
|
|
|
2018-04-27 06:41:58 -05:00
|
|
|
services := registry.GetServices()
|
2016-09-30 01:36:20 -05:00
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
if err := s.buildServiceGraph(services); err != nil {
|
|
|
|
return err
|
2018-04-27 06:41:58 -05:00
|
|
|
}
|
2016-09-30 01:36:20 -05:00
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
// Initialize services.
|
2018-04-27 06:41:58 -05:00
|
|
|
for _, service := range services {
|
2018-05-18 04:10:10 -05:00
|
|
|
if registry.IsDisabled(service.Instance) {
|
2018-04-27 06:41:58 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
s.log.Info("Initializing " + service.Name)
|
2018-04-27 06:41:58 -05:00
|
|
|
|
2018-05-18 04:10:10 -05:00
|
|
|
if err := service.Instance.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
// Start background services.
|
|
|
|
for _, svc := range services {
|
|
|
|
// Variable is needed for accessing loop variable in function callback
|
|
|
|
descriptor := svc
|
|
|
|
|
|
|
|
service, ok := svc.Instance.(registry.BackgroundService)
|
2018-04-27 06:41:58 -05:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-05-18 04:10:10 -05:00
|
|
|
if registry.IsDisabled(descriptor.Instance) {
|
2018-04-27 06:41:58 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
s.childRoutines.Go(func() error {
|
|
|
|
// Don't start new services when server is shutting down.
|
|
|
|
if s.shutdownInProgress {
|
2018-05-02 11:10:21 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
if err := service.Run(s.context); err != nil {
|
|
|
|
if err != context.Canceled {
|
|
|
|
// Server has crashed.
|
|
|
|
s.log.Error("Stopped "+descriptor.Name, "reason", err)
|
|
|
|
} else {
|
|
|
|
s.log.Info("Stopped "+descriptor.Name, "reason", err)
|
|
|
|
}
|
2018-05-02 11:10:21 -05:00
|
|
|
}
|
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
s.shutdownInProgress = true
|
|
|
|
|
|
|
|
return nil
|
2018-04-27 06:41:58 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
notifySystemd("READY=1")
|
2018-11-13 14:20:44 -06:00
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
return s.childRoutines.Wait()
|
2017-10-09 12:27:06 -05:00
|
|
|
}
|
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
// Shutdown initiates a shutdown of the services, and waits for all services to
|
|
|
|
// exit.
|
|
|
|
func (s *Server) Shutdown(reason string) {
|
|
|
|
s.log.Info("Shutdown started", "reason", reason)
|
|
|
|
s.shutdownReason = reason
|
|
|
|
s.shutdownInProgress = true
|
2016-09-30 01:36:20 -05:00
|
|
|
|
2018-05-01 07:13:38 -05:00
|
|
|
// call cancel func on root context
|
2019-10-14 07:18:22 -05:00
|
|
|
s.shutdownFn()
|
2018-05-01 07:13:38 -05:00
|
|
|
|
2018-05-01 07:18:10 -05:00
|
|
|
// wait for child routines
|
2019-10-14 07:18:22 -05:00
|
|
|
s.childRoutines.Wait()
|
2018-05-02 07:14:42 -05:00
|
|
|
}
|
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
// ExitCode returns an exit code for a given error.
|
|
|
|
func (s *Server) ExitCode(reason error) int {
|
2018-05-02 07:14:42 -05:00
|
|
|
code := 1
|
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
if reason == context.Canceled && s.shutdownReason != "" {
|
|
|
|
reason = fmt.Errorf(s.shutdownReason)
|
2018-05-02 07:14:42 -05:00
|
|
|
code = 0
|
2017-12-08 07:11:48 -06:00
|
|
|
}
|
2018-05-02 07:14:42 -05:00
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
s.log.Error("Server shutdown", "reason", reason)
|
|
|
|
|
2018-06-30 00:48:26 -05:00
|
|
|
return code
|
2016-09-30 01:36:20 -05:00
|
|
|
}
|
2017-09-04 03:42:48 -05:00
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
// writePIDFile retrieves the current process ID and writes it to file.
|
|
|
|
func (s *Server) writePIDFile() {
|
|
|
|
if s.pidFile == "" {
|
2017-09-04 03:42:48 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the required directory structure exists.
|
2019-10-14 07:18:22 -05:00
|
|
|
err := os.MkdirAll(filepath.Dir(s.pidFile), 0700)
|
2017-09-04 03:42:48 -05:00
|
|
|
if err != nil {
|
2019-10-14 07:18:22 -05:00
|
|
|
s.log.Error("Failed to verify pid directory", "error", err)
|
2017-09-04 03:42:48 -05:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
// Retrieve the PID and write it to file.
|
2017-09-04 03:42:48 -05:00
|
|
|
pid := strconv.Itoa(os.Getpid())
|
2019-10-14 07:18:22 -05:00
|
|
|
if err := ioutil.WriteFile(s.pidFile, []byte(pid), 0644); err != nil {
|
|
|
|
s.log.Error("Failed to write pidfile", "error", err)
|
2017-09-04 03:42:48 -05:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2017-09-04 05:44:38 -05:00
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
s.log.Info("Writing PID file", "path", s.pidFile, "pid", pid)
|
2017-09-04 03:42:48 -05:00
|
|
|
}
|
2017-12-01 07:02:05 -06:00
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
// buildServiceGraph builds a graph of services and their dependencies.
|
|
|
|
func (s *Server) buildServiceGraph(services []*registry.Descriptor) error {
|
|
|
|
// Specify service dependencies.
|
|
|
|
objs := []interface{}{
|
|
|
|
bus.GetBus(),
|
|
|
|
s.cfg,
|
|
|
|
routing.NewRouteRegister(middleware.RequestMetrics, middleware.RequestTracing),
|
|
|
|
localcache.New(5*time.Minute, 10*time.Minute),
|
|
|
|
s,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, service := range services {
|
|
|
|
objs = append(objs, service.Instance)
|
|
|
|
}
|
|
|
|
|
|
|
|
var serviceGraph inject.Graph
|
|
|
|
|
|
|
|
// Provide services and their dependencies to the graph.
|
|
|
|
for _, obj := range objs {
|
|
|
|
if err := serviceGraph.Provide(&inject.Object{Value: obj}); err != nil {
|
|
|
|
return fmt.Errorf("Failed to provide object to the graph: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve services and their dependencies.
|
|
|
|
if err := serviceGraph.Populate(); err != nil {
|
|
|
|
return fmt.Errorf("Failed to populate service dependency: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadConfiguration loads settings and configuration from config files.
|
|
|
|
func (s *Server) loadConfiguration() {
|
|
|
|
args := &setting.CommandLineArgs{
|
|
|
|
Config: s.configFile,
|
|
|
|
HomePath: s.homePath,
|
|
|
|
Args: flag.Args(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.cfg.Load(args); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Failed to start grafana. error: %s\n", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.log.Info("Starting "+setting.ApplicationName,
|
|
|
|
"version", version,
|
|
|
|
"commit", commit,
|
|
|
|
"branch", buildBranch,
|
|
|
|
"compiled", time.Unix(setting.BuildStamp, 0),
|
|
|
|
)
|
|
|
|
|
|
|
|
s.cfg.LogConfigSources()
|
|
|
|
}
|
|
|
|
|
|
|
|
// notifySystemd sends state notifications to systemd.
|
|
|
|
func notifySystemd(state string) error {
|
2017-12-01 07:02:05 -06:00
|
|
|
notifySocket := os.Getenv("NOTIFY_SOCKET")
|
|
|
|
|
|
|
|
if notifySocket == "" {
|
2019-04-23 03:24:47 -05:00
|
|
|
return fmt.Errorf("NOTIFY_SOCKET environment variable empty or unset")
|
2017-12-01 07:02:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
socketAddr := &net.UnixAddr{
|
|
|
|
Name: notifySocket,
|
|
|
|
Net: "unixgram",
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-14 07:18:22 -05:00
|
|
|
defer conn.Close()
|
2017-12-01 07:02:05 -06:00
|
|
|
|
|
|
|
_, err = conn.Write([]byte(state))
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|