server.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "io/ioutil"
  7. "net"
  8. "os"
  9. "path/filepath"
  10. "reflect"
  11. "strconv"
  12. "time"
  13. "github.com/facebookgo/inject"
  14. "github.com/grafana/grafana/pkg/bus"
  15. "github.com/grafana/grafana/pkg/middleware"
  16. "github.com/grafana/grafana/pkg/registry"
  17. "github.com/grafana/grafana/pkg/services/dashboards"
  18. "github.com/grafana/grafana/pkg/services/provisioning"
  19. "golang.org/x/sync/errgroup"
  20. "github.com/grafana/grafana/pkg/api"
  21. "github.com/grafana/grafana/pkg/log"
  22. "github.com/grafana/grafana/pkg/login"
  23. "github.com/grafana/grafana/pkg/metrics"
  24. "github.com/grafana/grafana/pkg/plugins"
  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. // self registering services
  30. _ "github.com/grafana/grafana/pkg/extensions"
  31. _ "github.com/grafana/grafana/pkg/services/alerting"
  32. _ "github.com/grafana/grafana/pkg/services/cleanup"
  33. _ "github.com/grafana/grafana/pkg/services/notifications"
  34. _ "github.com/grafana/grafana/pkg/services/search"
  35. )
  36. func NewGrafanaServer() *GrafanaServerImpl {
  37. rootCtx, shutdownFn := context.WithCancel(context.Background())
  38. childRoutines, childCtx := errgroup.WithContext(rootCtx)
  39. return &GrafanaServerImpl{
  40. context: childCtx,
  41. shutdownFn: shutdownFn,
  42. childRoutines: childRoutines,
  43. log: log.New("server"),
  44. }
  45. }
  46. type GrafanaServerImpl struct {
  47. context context.Context
  48. shutdownFn context.CancelFunc
  49. childRoutines *errgroup.Group
  50. log log.Logger
  51. RouteRegister api.RouteRegister `inject:""`
  52. HttpServer *api.HTTPServer `inject:""`
  53. }
  54. func (g *GrafanaServerImpl) Start() error {
  55. g.initLogging()
  56. g.writePIDFile()
  57. // initSql
  58. sqlstore.NewEngine() // TODO: this should return an error
  59. sqlstore.EnsureAdminUser()
  60. metrics.Init(setting.Cfg)
  61. login.Init()
  62. social.NewOAuthService()
  63. pluginManager, err := plugins.NewPluginManager(g.context)
  64. if err != nil {
  65. return fmt.Errorf("Failed to start plugins. error: %v", err)
  66. }
  67. g.childRoutines.Go(func() error { return pluginManager.Run(g.context) })
  68. if err := provisioning.Init(g.context, setting.HomePath, setting.Cfg); err != nil {
  69. return fmt.Errorf("Failed to provision Grafana from config. error: %v", err)
  70. }
  71. tracingCloser, err := tracing.Init(setting.Cfg)
  72. if err != nil {
  73. return fmt.Errorf("Tracing settings is not valid. error: %v", err)
  74. }
  75. defer tracingCloser.Close()
  76. serviceGraph := inject.Graph{}
  77. serviceGraph.Provide(&inject.Object{Value: bus.GetBus()})
  78. serviceGraph.Provide(&inject.Object{Value: dashboards.NewProvisioningService()})
  79. serviceGraph.Provide(&inject.Object{Value: api.NewRouteRegister(middleware.RequestMetrics, middleware.RequestTracing)})
  80. serviceGraph.Provide(&inject.Object{Value: api.HTTPServer{}})
  81. services := registry.GetServices()
  82. // Add all services to dependency graph
  83. for _, service := range services {
  84. serviceGraph.Provide(&inject.Object{Value: service})
  85. }
  86. serviceGraph.Provide(&inject.Object{Value: g})
  87. // Inject dependencies to services
  88. if err := serviceGraph.Populate(); err != nil {
  89. return fmt.Errorf("Failed to populate service dependency: %v", err)
  90. }
  91. // Init & start services
  92. for _, service := range services {
  93. if registry.IsDisabled(service) {
  94. continue
  95. }
  96. g.log.Info("Initializing " + reflect.TypeOf(service).Elem().Name())
  97. if err := service.Init(); err != nil {
  98. return fmt.Errorf("Service init failed %v", err)
  99. }
  100. }
  101. // Start background services
  102. for index := range services {
  103. service, ok := services[index].(registry.BackgroundService)
  104. if !ok {
  105. continue
  106. }
  107. if registry.IsDisabled(services[index]) {
  108. continue
  109. }
  110. g.childRoutines.Go(func() error {
  111. err := service.Run(g.context)
  112. g.log.Info("Stopped "+reflect.TypeOf(service).Elem().Name(), "reason", err)
  113. return err
  114. })
  115. }
  116. sendSystemdNotification("READY=1")
  117. return g.startHttpServer()
  118. }
  119. func (g *GrafanaServerImpl) initLogging() {
  120. err := setting.NewConfigContext(&setting.CommandLineArgs{
  121. Config: *configFile,
  122. HomePath: *homePath,
  123. Args: flag.Args(),
  124. })
  125. if err != nil {
  126. fmt.Fprintf(os.Stderr, "Failed to start grafana. error: %s\n", err.Error())
  127. os.Exit(1)
  128. }
  129. g.log.Info("Starting "+setting.ApplicationName, "version", version, "commit", commit, "compiled", time.Unix(setting.BuildStamp, 0))
  130. setting.LogConfigurationInfo()
  131. }
  132. func (g *GrafanaServerImpl) startHttpServer() error {
  133. g.HttpServer.Init()
  134. err := g.HttpServer.Start(g.context)
  135. if err != nil {
  136. return fmt.Errorf("Fail to start server. error: %v", err)
  137. }
  138. return nil
  139. }
  140. func (g *GrafanaServerImpl) Shutdown(code int, reason string) {
  141. g.log.Info("Shutdown started", "code", code, "reason", reason)
  142. err := g.HttpServer.Shutdown(g.context)
  143. if err != nil {
  144. g.log.Error("Failed to shutdown server", "error", err)
  145. }
  146. g.shutdownFn()
  147. err = g.childRoutines.Wait()
  148. if err != nil && err != context.Canceled {
  149. g.log.Error("Server shutdown completed with an error", "error", err)
  150. }
  151. }
  152. func (g *GrafanaServerImpl) writePIDFile() {
  153. if *pidFile == "" {
  154. return
  155. }
  156. // Ensure the required directory structure exists.
  157. err := os.MkdirAll(filepath.Dir(*pidFile), 0700)
  158. if err != nil {
  159. g.log.Error("Failed to verify pid directory", "error", err)
  160. os.Exit(1)
  161. }
  162. // Retrieve the PID and write it.
  163. pid := strconv.Itoa(os.Getpid())
  164. if err := ioutil.WriteFile(*pidFile, []byte(pid), 0644); err != nil {
  165. g.log.Error("Failed to write pidfile", "error", err)
  166. os.Exit(1)
  167. }
  168. g.log.Info("Writing PID file", "path", *pidFile, "pid", pid)
  169. }
  170. func sendSystemdNotification(state string) error {
  171. notifySocket := os.Getenv("NOTIFY_SOCKET")
  172. if notifySocket == "" {
  173. return fmt.Errorf("NOTIFY_SOCKET environment variable empty or unset.")
  174. }
  175. socketAddr := &net.UnixAddr{
  176. Name: notifySocket,
  177. Net: "unixgram",
  178. }
  179. conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
  180. if err != nil {
  181. return err
  182. }
  183. _, err = conn.Write([]byte(state))
  184. conn.Close()
  185. return err
  186. }