server.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "io/ioutil"
  7. "net"
  8. "os"
  9. "path/filepath"
  10. "strconv"
  11. "time"
  12. "github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
  13. "github.com/grafana/grafana/pkg/services/provisioning"
  14. "golang.org/x/sync/errgroup"
  15. "github.com/grafana/grafana/pkg/api"
  16. "github.com/grafana/grafana/pkg/log"
  17. "github.com/grafana/grafana/pkg/login"
  18. "github.com/grafana/grafana/pkg/metrics"
  19. "github.com/grafana/grafana/pkg/models"
  20. "github.com/grafana/grafana/pkg/plugins"
  21. "github.com/grafana/grafana/pkg/services/alerting"
  22. "github.com/grafana/grafana/pkg/services/cleanup"
  23. "github.com/grafana/grafana/pkg/services/notifications"
  24. "github.com/grafana/grafana/pkg/services/search"
  25. "github.com/grafana/grafana/pkg/services/sqlstore"
  26. "github.com/grafana/grafana/pkg/setting"
  27. "github.com/grafana/grafana/pkg/social"
  28. "github.com/grafana/grafana/pkg/tracing"
  29. )
  30. func NewGrafanaServer() models.GrafanaServer {
  31. rootCtx, shutdownFn := context.WithCancel(context.Background())
  32. childRoutines, childCtx := errgroup.WithContext(rootCtx)
  33. return &GrafanaServerImpl{
  34. context: childCtx,
  35. shutdownFn: shutdownFn,
  36. childRoutines: childRoutines,
  37. log: log.New("server"),
  38. }
  39. }
  40. type GrafanaServerImpl struct {
  41. context context.Context
  42. shutdownFn context.CancelFunc
  43. childRoutines *errgroup.Group
  44. log log.Logger
  45. httpServer *api.HttpServer
  46. }
  47. func (g *GrafanaServerImpl) Start() {
  48. go listenToSystemSignals(g)
  49. g.initLogging()
  50. g.writePIDFile()
  51. initSql()
  52. metrics.Init(setting.Cfg)
  53. search.Init()
  54. login.Init()
  55. social.NewOAuthService()
  56. pluginCloser, err := plugins.Init()
  57. if err != nil {
  58. g.log.Error("failed to start plugins", "error", err)
  59. g.Shutdown(1, "Startup failed")
  60. }
  61. defer pluginCloser()
  62. if err := provisioning.Init(g.context, setting.HomePath, setting.Cfg); err != nil {
  63. logger.Error("Failed to provision Grafana from config", "error", err)
  64. g.Shutdown(1, "Startup failed")
  65. return
  66. }
  67. tracingCloser, err := tracing.Init(setting.Cfg)
  68. if err != nil {
  69. g.log.Error("Tracing settings is not valid", "error", err)
  70. g.Shutdown(1, "Startup failed")
  71. return
  72. }
  73. defer tracingCloser.Close()
  74. // init alerting
  75. if setting.AlertingEnabled && setting.ExecuteAlerts {
  76. engine := alerting.NewEngine()
  77. g.childRoutines.Go(func() error { return engine.Run(g.context) })
  78. }
  79. // cleanup service
  80. cleanUpService := cleanup.NewCleanUpService()
  81. g.childRoutines.Go(func() error { return cleanUpService.Run(g.context) })
  82. if err = notifications.Init(); err != nil {
  83. g.log.Error("Notification service failed to initialize", "error", err)
  84. g.Shutdown(1, "Startup failed")
  85. return
  86. }
  87. SendSystemdNotification("READY=1")
  88. g.startHttpServer()
  89. }
  90. func initSql() {
  91. sqlstore.NewEngine()
  92. sqlstore.EnsureAdminUser()
  93. }
  94. func (g *GrafanaServerImpl) initLogging() {
  95. err := setting.NewConfigContext(&setting.CommandLineArgs{
  96. Config: *configFile,
  97. HomePath: *homePath,
  98. Args: flag.Args(),
  99. })
  100. if err != nil {
  101. g.log.Error(err.Error())
  102. os.Exit(1)
  103. }
  104. g.log.Info("Starting Grafana", "version", version, "commit", commit, "compiled", time.Unix(setting.BuildStamp, 0))
  105. setting.LogConfigurationInfo()
  106. }
  107. func (g *GrafanaServerImpl) startHttpServer() {
  108. g.httpServer = api.NewHttpServer()
  109. err := g.httpServer.Start(g.context)
  110. if err != nil {
  111. g.log.Error("Fail to start server", "error", err)
  112. g.Shutdown(1, "Startup failed")
  113. return
  114. }
  115. }
  116. func (g *GrafanaServerImpl) Shutdown(code int, reason string) {
  117. g.log.Info("Shutdown started", "code", code, "reason", reason)
  118. err := g.httpServer.Shutdown(g.context)
  119. if err != nil {
  120. g.log.Error("Failed to shutdown server", "error", err)
  121. }
  122. g.shutdownFn()
  123. err = g.childRoutines.Wait()
  124. g.log.Info("Shutdown completed", "reason", err)
  125. log.Close()
  126. os.Exit(code)
  127. }
  128. func (g *GrafanaServerImpl) writePIDFile() {
  129. if *pidFile == "" {
  130. return
  131. }
  132. // Ensure the required directory structure exists.
  133. err := os.MkdirAll(filepath.Dir(*pidFile), 0700)
  134. if err != nil {
  135. g.log.Error("Failed to verify pid directory", "error", err)
  136. os.Exit(1)
  137. }
  138. // Retrieve the PID and write it.
  139. pid := strconv.Itoa(os.Getpid())
  140. if err := ioutil.WriteFile(*pidFile, []byte(pid), 0644); err != nil {
  141. g.log.Error("Failed to write pidfile", "error", err)
  142. os.Exit(1)
  143. }
  144. g.log.Info("Writing PID file", "path", *pidFile, "pid", pid)
  145. }
  146. func SendSystemdNotification(state string) error {
  147. notifySocket := os.Getenv("NOTIFY_SOCKET")
  148. if notifySocket == "" {
  149. return fmt.Errorf("NOTIFY_SOCKET environment variable empty or unset.")
  150. }
  151. socketAddr := &net.UnixAddr{
  152. Name: notifySocket,
  153. Net: "unixgram",
  154. }
  155. conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
  156. if err != nil {
  157. return err
  158. }
  159. _, err = conn.Write([]byte(state))
  160. conn.Close()
  161. return err
  162. }