main.go 3.0 KB

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