main.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package main
  2. import (
  3. "flag"
  4. "io/ioutil"
  5. "os"
  6. "os/signal"
  7. "path/filepath"
  8. "runtime"
  9. "strconv"
  10. "time"
  11. "github.com/grafana/grafana/pkg/cmd"
  12. "github.com/grafana/grafana/pkg/log"
  13. "github.com/grafana/grafana/pkg/login"
  14. "github.com/grafana/grafana/pkg/metrics"
  15. "github.com/grafana/grafana/pkg/plugins"
  16. "github.com/grafana/grafana/pkg/services/eventpublisher"
  17. "github.com/grafana/grafana/pkg/services/notifications"
  18. "github.com/grafana/grafana/pkg/services/search"
  19. "github.com/grafana/grafana/pkg/services/sqlstore"
  20. "github.com/grafana/grafana/pkg/setting"
  21. "github.com/grafana/grafana/pkg/social"
  22. )
  23. var version = "master"
  24. var commit = "NA"
  25. var buildstamp string
  26. var configFile = flag.String("config", "", "path to config file")
  27. var homePath = flag.String("homepath", "", "path to grafana install/home path, defaults to working directory")
  28. var pidFile = flag.String("pidfile", "", "path to pid file")
  29. func init() {
  30. runtime.GOMAXPROCS(runtime.NumCPU())
  31. }
  32. func main() {
  33. buildstampInt64, _ := strconv.ParseInt(buildstamp, 10, 64)
  34. setting.BuildVersion = version
  35. setting.BuildCommit = commit
  36. setting.BuildStamp = buildstampInt64
  37. go func() {
  38. c := make(chan os.Signal, 1)
  39. signal.Notify(c, os.Interrupt)
  40. <-c
  41. os.Exit(0)
  42. }()
  43. flag.Parse()
  44. writePIDFile()
  45. initRuntime()
  46. search.Init()
  47. login.Init()
  48. social.NewOAuthService()
  49. eventpublisher.Init()
  50. plugins.Init()
  51. if err := notifications.Init(); err != nil {
  52. log.Fatal(3, "Notification service failed to initialize", err)
  53. }
  54. if setting.ReportingEnabled {
  55. go metrics.StartUsageReportLoop()
  56. }
  57. cmd.StartServer()
  58. log.Close()
  59. }
  60. func initRuntime() {
  61. setting.NewConfigContext(&setting.CommandLineArgs{
  62. Config: *configFile,
  63. HomePath: *homePath,
  64. Args: flag.Args(),
  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. }