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-12-01 07:02:05 -06:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2016-09-30 01:36:20 -05:00
|
|
|
"os"
|
2017-09-04 03:42:48 -05:00
|
|
|
"path/filepath"
|
2021-08-25 08:11:22 -05:00
|
|
|
"reflect"
|
2017-09-04 03:42:48 -05:00
|
|
|
"strconv"
|
2020-08-21 03:41:10 -05:00
|
|
|
"sync"
|
2021-08-25 08:11:22 -05:00
|
|
|
|
2023-01-30 02:26:42 -06:00
|
|
|
"golang.org/x/sync/errgroup"
|
2016-09-30 01:36:20 -05:00
|
|
|
|
2018-10-29 07:27:29 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api"
|
2018-10-26 03:40:33 -05:00
|
|
|
_ "github.com/grafana/grafana/pkg/extensions"
|
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"
|
2023-01-30 02:26:42 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/usagestats/statscollector"
|
2023-03-06 13:06:52 -06:00
|
|
|
"github.com/grafana/grafana/pkg/modules"
|
2019-04-23 03:24:47 -05:00
|
|
|
"github.com/grafana/grafana/pkg/registry"
|
2023-01-30 02:26:42 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
2021-08-04 07:44:37 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/provisioning"
|
2019-04-23 03:24:47 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2016-09-30 01:36:20 -05:00
|
|
|
)
|
|
|
|
|
2021-08-25 08:11:22 -05:00
|
|
|
// Options contains parameters for the New function.
|
|
|
|
type Options struct {
|
2020-08-21 03:41:10 -05:00
|
|
|
HomePath string
|
|
|
|
PidFile string
|
|
|
|
Version string
|
|
|
|
Commit string
|
|
|
|
BuildBranch string
|
|
|
|
Listener net.Listener
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new instance of Server.
|
2021-08-25 08:11:22 -05:00
|
|
|
func New(opts Options, cfg *setting.Cfg, httpServer *api.HTTPServer, roleRegistry accesscontrol.RoleRegistry,
|
|
|
|
provisioningService provisioning.ProvisioningService, backgroundServiceProvider registry.BackgroundServiceRegistry,
|
2022-04-28 04:06:49 -05:00
|
|
|
usageStatsProvidersRegistry registry.UsageStatsProvidersRegistry, statsCollectorService *statscollector.Service,
|
2023-03-06 13:06:52 -06:00
|
|
|
moduleService modules.Engine,
|
2021-08-25 08:11:22 -05:00
|
|
|
) (*Server, error) {
|
2022-04-28 04:06:49 -05:00
|
|
|
statsCollectorService.RegisterProviders(usageStatsProvidersRegistry.GetServices())
|
2023-03-06 13:06:52 -06:00
|
|
|
s, err := newServer(opts, cfg, httpServer, roleRegistry, provisioningService, backgroundServiceProvider, moduleService)
|
2021-08-25 08:11:22 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-07 02:44:06 -05:00
|
|
|
if err := s.init(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-25 08:11:22 -05:00
|
|
|
|
2021-04-07 02:44:06 -05:00
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 08:11:22 -05:00
|
|
|
func newServer(opts Options, cfg *setting.Cfg, httpServer *api.HTTPServer, roleRegistry accesscontrol.RoleRegistry,
|
2023-01-26 09:13:22 -06:00
|
|
|
provisioningService provisioning.ProvisioningService, backgroundServiceProvider registry.BackgroundServiceRegistry,
|
2023-03-06 13:06:52 -06:00
|
|
|
moduleService modules.Engine,
|
2021-08-25 08:11:22 -05:00
|
|
|
) (*Server, error) {
|
2016-09-30 01:36:20 -05:00
|
|
|
rootCtx, shutdownFn := context.WithCancel(context.Background())
|
|
|
|
childRoutines, childCtx := errgroup.WithContext(rootCtx)
|
|
|
|
|
2021-08-25 08:11:22 -05:00
|
|
|
s := &Server{
|
2022-09-06 14:21:25 -05:00
|
|
|
context: childCtx,
|
|
|
|
childRoutines: childRoutines,
|
|
|
|
HTTPServer: httpServer,
|
|
|
|
provisioningService: provisioningService,
|
|
|
|
roleRegistry: roleRegistry,
|
|
|
|
shutdownFn: shutdownFn,
|
|
|
|
shutdownFinished: make(chan struct{}),
|
|
|
|
log: log.New("server"),
|
|
|
|
cfg: cfg,
|
|
|
|
pidFile: opts.PidFile,
|
|
|
|
version: opts.Version,
|
|
|
|
commit: opts.Commit,
|
|
|
|
buildBranch: opts.BuildBranch,
|
|
|
|
backgroundServices: backgroundServiceProvider.GetServices(),
|
2023-03-06 13:06:52 -06:00
|
|
|
moduleService: moduleService,
|
2016-09-30 01:36:20 -05:00
|
|
|
}
|
2021-08-25 08:11:22 -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 {
|
2021-04-07 02:44:06 -05:00
|
|
|
context context.Context
|
|
|
|
shutdownFn context.CancelFunc
|
|
|
|
childRoutines *errgroup.Group
|
|
|
|
log log.Logger
|
|
|
|
cfg *setting.Cfg
|
|
|
|
shutdownOnce sync.Once
|
|
|
|
shutdownFinished chan struct{}
|
|
|
|
isInitialized bool
|
|
|
|
mtx sync.Mutex
|
2017-02-06 02:40:07 -06:00
|
|
|
|
2021-08-25 08:11:22 -05:00
|
|
|
pidFile string
|
|
|
|
version string
|
|
|
|
commit string
|
|
|
|
buildBranch string
|
|
|
|
backgroundServices []registry.BackgroundService
|
2019-10-14 07:18:22 -05:00
|
|
|
|
2022-09-06 14:21:25 -05:00
|
|
|
HTTPServer *api.HTTPServer
|
|
|
|
roleRegistry accesscontrol.RoleRegistry
|
|
|
|
provisioningService provisioning.ProvisioningService
|
2023-03-06 13:06:52 -06:00
|
|
|
moduleService modules.Engine
|
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
|
|
|
|
|
2022-12-24 21:33:18 -06:00
|
|
|
if err := s.writePIDFile(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-06 13:06:52 -06:00
|
|
|
// Initialize dskit modules.
|
|
|
|
if err := s.moduleService.Init(s.context); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-04-12 02:53:43 -05:00
|
|
|
if err := s.roleRegistry.RegisterFixedRoles(s.context); err != nil {
|
2021-08-04 07:44:37 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-18 10:06:19 -05:00
|
|
|
return s.provisioningService.RunInitProvisioners(s.context)
|
2020-08-21 03:41:10 -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.
|
2021-04-07 02:44:06 -05:00
|
|
|
func (s *Server) Run() error {
|
|
|
|
defer close(s.shutdownFinished)
|
|
|
|
|
|
|
|
if err := s.init(); err != nil {
|
|
|
|
return err
|
2020-08-21 03:41:10 -05:00
|
|
|
}
|
|
|
|
|
2023-03-06 13:06:52 -06:00
|
|
|
// Start dskit modules.
|
|
|
|
s.childRoutines.Go(func() error {
|
|
|
|
err := s.moduleService.Run(s.context)
|
|
|
|
if err != nil && !errors.Is(err, context.Canceled) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2021-08-25 08:11:22 -05:00
|
|
|
services := s.backgroundServices
|
2020-08-21 03:41:10 -05:00
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
// Start background services.
|
|
|
|
for _, svc := range services {
|
2021-08-25 08:11:22 -05:00
|
|
|
if registry.IsDisabled(svc) {
|
2018-04-27 06:41:58 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-25 08:11:22 -05:00
|
|
|
service := svc
|
|
|
|
serviceName := reflect.TypeOf(service).String()
|
2019-10-14 07:18:22 -05:00
|
|
|
s.childRoutines.Go(func() error {
|
2021-04-07 02:44:06 -05:00
|
|
|
select {
|
|
|
|
case <-s.context.Done():
|
|
|
|
return s.context.Err()
|
|
|
|
default:
|
2018-05-02 11:10:21 -05:00
|
|
|
}
|
2022-02-07 09:00:08 -06:00
|
|
|
s.log.Debug("Starting background service", "service", serviceName)
|
2019-11-18 02:49:08 -06:00
|
|
|
err := service.Run(s.context)
|
2021-04-07 02:44:06 -05:00
|
|
|
// Do not return context.Canceled error since errgroup.Group only
|
|
|
|
// returns the first error to the caller - thus we can miss a more
|
|
|
|
// interesting error.
|
|
|
|
if err != nil && !errors.Is(err, context.Canceled) {
|
2022-02-07 09:00:08 -06:00
|
|
|
s.log.Error("Stopped background service", "service", serviceName, "reason", err)
|
2021-08-25 08:11:22 -05:00
|
|
|
return fmt.Errorf("%s run error: %w", serviceName, err)
|
2018-05-02 11:10:21 -05:00
|
|
|
}
|
2022-02-07 09:00:08 -06:00
|
|
|
s.log.Debug("Stopped background service", "service", serviceName, "reason", err)
|
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
|
|
|
s.notifySystemd("READY=1")
|
2018-11-13 14:20:44 -06:00
|
|
|
|
2021-04-07 02:44:06 -05:00
|
|
|
s.log.Debug("Waiting on services...")
|
|
|
|
return s.childRoutines.Wait()
|
2017-10-09 12:27:06 -05:00
|
|
|
}
|
|
|
|
|
2021-04-07 02:44:06 -05:00
|
|
|
// Shutdown initiates Grafana graceful shutdown. This shuts down all
|
|
|
|
// running background services. Since Run blocks Shutdown supposed to
|
|
|
|
// be run from a separate goroutine.
|
2021-04-30 03:55:59 -05:00
|
|
|
func (s *Server) Shutdown(ctx context.Context, reason string) error {
|
|
|
|
var err error
|
2021-04-07 02:44:06 -05:00
|
|
|
s.shutdownOnce.Do(func() {
|
|
|
|
s.log.Info("Shutdown started", "reason", reason)
|
2023-03-06 13:06:52 -06:00
|
|
|
if err := s.moduleService.Shutdown(ctx); err != nil {
|
|
|
|
s.log.Error("Failed to shutdown modules", "error", err)
|
|
|
|
}
|
|
|
|
// Call cancel func to stop background services.
|
2021-04-07 02:44:06 -05:00
|
|
|
s.shutdownFn()
|
2021-04-30 03:55:59 -05:00
|
|
|
// Wait for server to shut down
|
|
|
|
select {
|
|
|
|
case <-s.shutdownFinished:
|
|
|
|
s.log.Debug("Finished waiting for server to shut down")
|
|
|
|
case <-ctx.Done():
|
|
|
|
s.log.Warn("Timed out while waiting for server to shut down")
|
|
|
|
err = fmt.Errorf("timeout waiting for shutdown")
|
|
|
|
}
|
2021-04-07 02:44:06 -05:00
|
|
|
})
|
2021-04-30 03:55:59 -05:00
|
|
|
|
|
|
|
return err
|
2018-05-02 07:14:42 -05:00
|
|
|
}
|
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
// writePIDFile retrieves the current process ID and writes it to file.
|
2022-12-24 21:33:18 -06:00
|
|
|
func (s *Server) writePIDFile() error {
|
2019-10-14 07:18:22 -05:00
|
|
|
if s.pidFile == "" {
|
2022-12-24 21:33:18 -06:00
|
|
|
return nil
|
2017-09-04 03:42:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2022-12-24 21:33:18 -06:00
|
|
|
return fmt.Errorf("failed to verify pid directory: %s", err)
|
2017-09-04 03:42:48 -05:00
|
|
|
}
|
|
|
|
|
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())
|
2022-08-10 08:37:51 -05:00
|
|
|
if err := os.WriteFile(s.pidFile, []byte(pid), 0644); err != nil {
|
2019-10-14 07:18:22 -05:00
|
|
|
s.log.Error("Failed to write pidfile", "error", err)
|
2022-12-24 21:33:18 -06:00
|
|
|
return fmt.Errorf("failed to write pidfile: %s", err)
|
2017-09-04 03:42:48 -05:00
|
|
|
}
|
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)
|
2022-12-24 21:33:18 -06:00
|
|
|
return nil
|
2017-09-04 03:42:48 -05:00
|
|
|
}
|
2017-12-01 07:02:05 -06:00
|
|
|
|
2019-10-14 07:18:22 -05:00
|
|
|
// 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
|
|
|
}
|