main.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package main
  2. import (
  3. "flag"
  4. "io/ioutil"
  5. "os"
  6. "os/signal"
  7. "path/filepath"
  8. "runtime"
  9. "strconv"
  10. "syscall"
  11. "time"
  12. "github.com/grafana/grafana/pkg/cmd"
  13. "github.com/grafana/grafana/pkg/log"
  14. "github.com/grafana/grafana/pkg/login"
  15. "github.com/grafana/grafana/pkg/metrics"
  16. "github.com/grafana/grafana/pkg/models"
  17. "github.com/grafana/grafana/pkg/plugins"
  18. "github.com/grafana/grafana/pkg/services/eventpublisher"
  19. "github.com/grafana/grafana/pkg/services/notifications"
  20. "github.com/grafana/grafana/pkg/services/search"
  21. "github.com/grafana/grafana/pkg/services/sqlstore"
  22. "github.com/grafana/grafana/pkg/setting"
  23. "github.com/grafana/grafana/pkg/social"
  24. )
  25. var version = "master"
  26. var commit = "NA"
  27. var buildstamp string
  28. var configFile = flag.String("config", "", "path to config file")
  29. var homePath = flag.String("homepath", "", "path to grafana install/home path, defaults to working directory")
  30. var pidFile = flag.String("pidfile", "", "path to pid file")
  31. var exitChan = make(chan int)
  32. func init() {
  33. runtime.GOMAXPROCS(runtime.NumCPU())
  34. }
  35. func main() {
  36. buildstampInt64, _ := strconv.ParseInt(buildstamp, 10, 64)
  37. setting.BuildVersion = version
  38. setting.BuildCommit = commit
  39. setting.BuildStamp = buildstampInt64
  40. go listenToSystemSignels()
  41. flag.Parse()
  42. writePIDFile()
  43. initRuntime()
  44. search.Init()
  45. login.Init()
  46. social.NewOAuthService()
  47. eventpublisher.Init()
  48. plugins.Init()
  49. models.InitQuotaDefaults()
  50. if err := notifications.Init(); err != nil {
  51. log.Fatal(3, "Notification service failed to initialize", err)
  52. }
  53. if setting.ReportingEnabled {
  54. go metrics.StartUsageReportLoop()
  55. }
  56. cmd.StartServer()
  57. exitChan <- 0
  58. }
  59. func initRuntime() {
  60. setting.NewConfigContext(&setting.CommandLineArgs{
  61. Config: *configFile,
  62. HomePath: *homePath,
  63. Args: flag.Args(),
  64. })
  65. log.Info("Starting Grafana")
  66. log.Info("Version: %v, Commit: %v, Build date: %v", setting.BuildVersion, setting.BuildCommit, time.Unix(setting.BuildStamp, 0))
  67. setting.LogConfigurationInfo()
  68. sqlstore.NewEngine()
  69. sqlstore.EnsureAdminUser()
  70. }
  71. func writePIDFile() {
  72. if *pidFile == "" {
  73. return
  74. }
  75. // Ensure the required directory structure exists.
  76. err := os.MkdirAll(filepath.Dir(*pidFile), 0700)
  77. if err != nil {
  78. log.Fatal(3, "Failed to verify pid directory", err)
  79. }
  80. // Retrieve the PID and write it.
  81. pid := strconv.Itoa(os.Getpid())
  82. if err := ioutil.WriteFile(*pidFile, []byte(pid), 0644); err != nil {
  83. log.Fatal(3, "Failed to write pidfile", err)
  84. }
  85. }
  86. func listenToSystemSignels() {
  87. signalChan := make(chan os.Signal, 1)
  88. code := 0
  89. signal.Notify(signalChan, os.Interrupt)
  90. signal.Notify(signalChan, os.Kill)
  91. signal.Notify(signalChan, syscall.SIGTERM)
  92. select {
  93. case sig := <-signalChan:
  94. log.Info("Received signal %s. shutting down", sig)
  95. case code = <-exitChan:
  96. switch code {
  97. case 0:
  98. log.Info("Shutting down")
  99. default:
  100. log.Warn("Shutting down")
  101. }
  102. }
  103. log.Close()
  104. os.Exit(code)
  105. }