server.go 4.6 KB

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