main.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/metrics"
  14. "github.com/grafana/grafana/pkg/plugins"
  15. "github.com/grafana/grafana/pkg/services/eventpublisher"
  16. "github.com/grafana/grafana/pkg/services/notifications"
  17. "github.com/grafana/grafana/pkg/services/search"
  18. "github.com/grafana/grafana/pkg/services/sqlstore"
  19. "github.com/grafana/grafana/pkg/setting"
  20. "github.com/grafana/grafana/pkg/social"
  21. )
  22. var version = "master"
  23. var commit = "NA"
  24. var buildstamp string
  25. var configFile = flag.String("config", "", "path to config file")
  26. var homePath = flag.String("homepath", "", "path to grafana install/home path, defaults to working directory")
  27. var pidFile = flag.String("pidfile", "", "path to pid file")
  28. func init() {
  29. runtime.GOMAXPROCS(runtime.NumCPU())
  30. }
  31. func main() {
  32. buildstampInt64, _ := strconv.ParseInt(buildstamp, 10, 64)
  33. setting.BuildVersion = version
  34. setting.BuildCommit = commit
  35. setting.BuildStamp = buildstampInt64
  36. go func() {
  37. c := make(chan os.Signal, 1)
  38. signal.Notify(c, os.Interrupt)
  39. <-c
  40. os.Exit(0)
  41. }()
  42. flag.Parse()
  43. writePIDFile()
  44. initRuntime()
  45. search.Init()
  46. social.NewOAuthService()
  47. eventpublisher.Init()
  48. plugins.Init()
  49. if err := notifications.Init(); err != nil {
  50. log.Fatal(3, "Notification service failed to initialize", err)
  51. }
  52. if setting.ReportingEnabled {
  53. go metrics.StartUsageReportLoop()
  54. }
  55. cmd.StartServer()
  56. log.Close()
  57. }
  58. func initRuntime() {
  59. setting.NewConfigContext(&setting.CommandLineArgs{
  60. Config: *configFile,
  61. HomePath: *homePath,
  62. Args: flag.Args(),
  63. })
  64. log.Info("Starting Grafana")
  65. log.Info("Version: %v, Commit: %v, Build date: %v", setting.BuildVersion, setting.BuildCommit, time.Unix(setting.BuildStamp, 0))
  66. setting.LogConfigurationInfo()
  67. sqlstore.NewEngine()
  68. sqlstore.EnsureAdminUser()
  69. }
  70. func writePIDFile() {
  71. if *pidFile == "" {
  72. return
  73. }
  74. // Ensure the required directory structure exists.
  75. err := os.MkdirAll(filepath.Dir(*pidFile), 0700)
  76. if err != nil {
  77. log.Fatal(3, "Failed to verify pid directory", err)
  78. }
  79. // Retrieve the PID and write it.
  80. pid := strconv.Itoa(os.Getpid())
  81. if err := ioutil.WriteFile(*pidFile, []byte(pid), 0644); err != nil {
  82. log.Fatal(3, "Failed to write pidfile", err)
  83. }
  84. }