2020-08-21 03:41:10 -05:00
|
|
|
package server
|
2016-09-30 01:36:20 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-31 02:45:20 -05:00
|
|
|
"errors"
|
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"
|
2020-08-21 03:41:10 -05:00
|
|
|
"sync"
|
2017-09-04 03:42:48 -05:00
|
|
|
"time"
|
2016-09-30 01:36:20 -05:00
|
|
|
|
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"
|
2020-10-19 09:58:16 -05: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"
|
2020-12-03 08:18:10 -06:00
|
|
|
_ "github.com/grafana/grafana/pkg/services/librarypanels"
|
2020-11-12 07:11:30 -06:00
|
|
|
_ "github.com/grafana/grafana/pkg/services/ngalert"
|
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
|
|
|
)
|
|
|
|
|
2020-08-21 03:41:10 -05:00
|
|
|
// Config contains parameters for the New function.
|
|
|
|
type Config struct {
|
|
|
|
ConfigFile string
|
|
|
|
HomePath string
|
|
|
|
PidFile string
|
|
|
|
Version string
|
|
|
|
Commit string
|
|
|
|
BuildBranch string
|
|
|
|
Listener net.Listener
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new instance of Server.
|
|
|
|
func New(cfg Config) (*Server, error) {
|
2016-09-30 01:36:20 -05:00
|
|
|
rootCtx, shutdownFn := context.WithCancel(context.Background())
|
|
|
|
childRoutines, childCtx := errgroup.WithContext(rootCtx)
|
|
|
|
|
2020-08-21 03:41:10 -05:00
|
|
|
s := &Server{
|
2016-09-30 01:36:20 -05:00
|
|
|
context: childCtx,
|
|
|
|
shutdownFn: shutdownFn,
|
|
|
|
childRoutines: childRoutines,
|
|
|
|
log: log.New("server"),
|
2021-01-12 00:42:32 -06:00
|
|
|
// Need to use the singleton setting.Cfg instance, to make sure we use the same as is injected in the DI
|
|
|
|
// graph
|
|
|
|
cfg: setting.GetCfg(),
|
2019-10-14 07:18:22 -05:00
|
|
|
|
2020-08-21 03:41:10 -05:00
|
|
|
configFile: cfg.ConfigFile,
|
|
|
|
homePath: cfg.HomePath,
|
|
|
|
pidFile: cfg.PidFile,
|
|
|
|
version: cfg.Version,
|
|
|
|
commit: cfg.Commit,
|
|
|
|
buildBranch: cfg.BuildBranch,
|
2021-01-07 04:36:13 -06:00
|
|
|
listener: cfg.Listener,
|
2020-08-21 03:41:10 -05:00
|
|
|
}
|
2021-01-07 04:36:13 -06:00
|
|
|
|
|
|
|
if err := s.init(); err != nil {
|
|
|
|
return nil, err
|
2016-09-30 01:36:20 -05:00
|
|
|
}
|
2020-08-21 03:41:10 -05:00
|
|
|
|
|
|
|
return s, nil
|
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
|
2020-08-21 03:41:10 -05:00
|
|
|
isInitialized bool
|
|
|
|
mtx sync.Mutex
|
2021-01-07 04:36:13 -06:00
|
|
|
listener net.Listener
|
2017-02-06 02:40:07 -06:00
|
|
|
|
2020-08-21 03:41:10 -05:00
|
|
|
configFile string
|
|
|
|
homePath string
|
|
|
|
pidFile string
|
|
|
|
version string
|
|
|
|
commit string
|
|
|
|
buildBranch string
|
2019-10-14 07:18:22 -05:00
|
|
|
|
2020-08-21 03:41:10 -05:00
|
|
|
HTTPServer *api.HTTPServer `inject:""`
|
2016-09-30 01:36:20 -05:00
|
|
|
}
|
|
|
|
|
2020-08-21 03:41:10 -05:00
|
|
|
// init initializes the server and its services.
|
2021-01-07 04:36:13 -06:00
|
|
|
func (s *Server) init() error {
|
2020-08-21 03:41:10 -05:00
|
|
|
s.mtx.Lock()
|
|
|
|
defer s.mtx.Unlock()
|
|
|
|
|
|
|
|
if s.isInitialized {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
s.isInitialized = true
|
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
s.loadConfiguration()
|
|
|
|
s.writePIDFile()
|
2020-10-19 09:58:16 -05:00
|
|
|
if err := metrics.SetEnvironmentInformation(s.cfg.MetricsGrafanaEnvironmentInfo); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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()
|
2020-08-21 03:41:10 -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
|
|
|
|
2021-01-07 04:36:13 -06:00
|
|
|
if s.listener != nil {
|
|
|
|
for _, service := range services {
|
2020-08-21 03:41:10 -05:00
|
|
|
if httpS, ok := service.Instance.(*api.HTTPServer); ok {
|
|
|
|
// Configure the api.HTTPServer if necessary
|
|
|
|
// Hopefully we can find a better solution, maybe with a more advanced DI framework, f.ex. Dig?
|
2021-01-07 04:36:13 -06:00
|
|
|
s.log.Debug("Using provided listener for HTTP server")
|
|
|
|
httpS.Listener = s.listener
|
2020-08-21 03:41:10 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-27 06:41:58 -05:00
|
|
|
}
|
|
|
|
|
2020-08-21 03:41:10 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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() (err error) {
|
2021-01-07 04:36:13 -06:00
|
|
|
if err = s.init(); err != nil {
|
2020-08-21 03:41:10 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
services := registry.GetServices()
|
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
// Start background services.
|
|
|
|
for _, svc := range services {
|
|
|
|
service, ok := svc.Instance.(registry.BackgroundService)
|
2018-04-27 06:41:58 -05:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-10-15 09:44:15 -05:00
|
|
|
if registry.IsDisabled(svc.Instance) {
|
2018-04-27 06:41:58 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-10-15 09:44:15 -05:00
|
|
|
// Variable is needed for accessing loop variable in callback
|
|
|
|
descriptor := svc
|
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-11-18 02:49:08 -06:00
|
|
|
err := service.Run(s.context)
|
|
|
|
if err != nil {
|
2020-10-05 06:30:46 -05:00
|
|
|
// Mark that we are in shutdown mode
|
|
|
|
// So no more services are started
|
|
|
|
s.shutdownInProgress = true
|
2020-11-19 06:34:28 -06:00
|
|
|
if !errors.Is(err, context.Canceled) {
|
2019-10-14 07:18:22 -05:00
|
|
|
// Server has crashed.
|
|
|
|
s.log.Error("Stopped "+descriptor.Name, "reason", err)
|
|
|
|
} else {
|
2020-04-14 05:18:45 -05:00
|
|
|
s.log.Debug("Stopped "+descriptor.Name, "reason", err)
|
2019-10-14 07:18:22 -05:00
|
|
|
}
|
2019-11-18 02:49:08 -06:00
|
|
|
|
|
|
|
return err
|
2018-05-02 11:10:21 -05:00
|
|
|
}
|
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
return nil
|
2018-04-27 06:41:58 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-15 09:44:15 -05:00
|
|
|
defer func() {
|
|
|
|
s.log.Debug("Waiting on services...")
|
2020-07-31 02:45:20 -05:00
|
|
|
if waitErr := s.childRoutines.Wait(); waitErr != nil && !errors.Is(waitErr, context.Canceled) {
|
2019-10-15 09:44:15 -05:00
|
|
|
s.log.Error("A service failed", "err", waitErr)
|
|
|
|
if err == nil {
|
|
|
|
err = waitErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
s.notifySystemd("READY=1")
|
2018-11-13 14:20:44 -06:00
|
|
|
|
2020-08-21 03:41:10 -05:00
|
|
|
return nil
|
2017-10-09 12:27:06 -05:00
|
|
|
}
|
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
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
|
2020-07-31 02:45:20 -05:00
|
|
|
if err := s.childRoutines.Wait(); err != nil && !errors.Is(err, context.Canceled) {
|
2019-10-15 09:44:15 -05:00
|
|
|
s.log.Error("Failed waiting for services to shutdown", "err", err)
|
|
|
|
}
|
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
|
|
|
|
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(reason, context.Canceled) && s.shutdownReason != "" {
|
2019-10-14 07:18:22 -05:00
|
|
|
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,
|
2020-12-01 08:04:59 -06:00
|
|
|
routing.NewRouteRegister(middleware.RequestTracing, middleware.RequestMetrics(s.cfg)),
|
2019-10-14 07:18:22 -05:00
|
|
|
localcache.New(5*time.Minute, 10*time.Minute),
|
|
|
|
s,
|
|
|
|
}
|
2021-01-07 04:36:13 -06:00
|
|
|
return registry.BuildServiceGraph(objs, services)
|
2019-10-14 07:18:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
2020-08-21 03:41:10 -05:00
|
|
|
"version", s.version,
|
|
|
|
"commit", s.commit,
|
|
|
|
"branch", s.buildBranch,
|
2019-10-14 07:18:22 -05:00
|
|
|
"compiled", time.Unix(setting.BuildStamp, 0),
|
|
|
|
)
|
|
|
|
|
|
|
|
s.cfg.LogConfigSources()
|
|
|
|
}
|
|
|
|
|
|
|
|
// notifySystemd sends state notifications to systemd.
|
2019-10-15 09:44:15 -05:00
|
|
|
func (s *Server) notifySystemd(state string) {
|
2017-12-01 07:02:05 -06:00
|
|
|
notifySocket := os.Getenv("NOTIFY_SOCKET")
|
|
|
|
if notifySocket == "" {
|
2019-10-15 09:44:15 -05:00
|
|
|
s.log.Debug(
|
|
|
|
"NOTIFY_SOCKET environment variable empty or unset, can't send systemd notification")
|
|
|
|
return
|
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 {
|
2019-10-15 09:44:15 -05:00
|
|
|
s.log.Warn("Failed to connect to systemd", "err", err, "socket", notifySocket)
|
|
|
|
return
|
2017-12-01 07:02:05 -06:00
|
|
|
}
|
2020-12-15 02:32:06 -06:00
|
|
|
defer func() {
|
|
|
|
if err := conn.Close(); err != nil {
|
|
|
|
s.log.Warn("Failed to close connection", "err", err)
|
|
|
|
}
|
|
|
|
}()
|
2017-12-01 07:02:05 -06:00
|
|
|
|
|
|
|
_, err = conn.Write([]byte(state))
|
2019-10-15 09:44:15 -05:00
|
|
|
if err != nil {
|
|
|
|
s.log.Warn("Failed to write notification to systemd", "err", err)
|
|
|
|
}
|
2017-12-01 07:02:05 -06:00
|
|
|
}
|