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