main.go 2.8 KB

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