main.go 3.0 KB

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