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. alertingInit "github.com/grafana/grafana/pkg/services/alerting/init"
  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 = "3.1.0"
  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. if buildstampInt64 == 0 {
  45. buildstampInt64 = time.Now().Unix()
  46. }
  47. setting.BuildVersion = version
  48. setting.BuildCommit = commit
  49. setting.BuildStamp = buildstampInt64
  50. go listenToSystemSignels()
  51. flag.Parse()
  52. writePIDFile()
  53. initRuntime()
  54. metrics.Init()
  55. search.Init()
  56. login.Init()
  57. social.NewOAuthService()
  58. eventpublisher.Init()
  59. plugins.Init()
  60. alertingInit.Init()
  61. if err := notifications.Init(); err != nil {
  62. log.Fatal(3, "Notification service failed to initialize", err)
  63. }
  64. StartServer()
  65. exitChan <- 0
  66. }
  67. func initRuntime() {
  68. err := setting.NewConfigContext(&setting.CommandLineArgs{
  69. Config: *configFile,
  70. HomePath: *homePath,
  71. Args: flag.Args(),
  72. })
  73. if err != nil {
  74. log.Fatal(3, err.Error())
  75. }
  76. logger := log.New("main")
  77. logger.Info("Starting Grafana", "version", version, "commit", commit, "compiled", time.Unix(setting.BuildStamp, 0))
  78. setting.LogConfigurationInfo()
  79. sqlstore.NewEngine()
  80. sqlstore.EnsureAdminUser()
  81. }
  82. func writePIDFile() {
  83. if *pidFile == "" {
  84. return
  85. }
  86. // Ensure the required directory structure exists.
  87. err := os.MkdirAll(filepath.Dir(*pidFile), 0700)
  88. if err != nil {
  89. log.Fatal(3, "Failed to verify pid directory", err)
  90. }
  91. // Retrieve the PID and write it.
  92. pid := strconv.Itoa(os.Getpid())
  93. if err := ioutil.WriteFile(*pidFile, []byte(pid), 0644); err != nil {
  94. log.Fatal(3, "Failed to write pidfile", err)
  95. }
  96. }
  97. func listenToSystemSignels() {
  98. signalChan := make(chan os.Signal, 1)
  99. code := 0
  100. signal.Notify(signalChan, os.Interrupt, os.Kill, 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. }