main.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/search"
  16. "github.com/grafana/grafana/pkg/services/eventpublisher"
  17. "github.com/grafana/grafana/pkg/services/mailer"
  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. mailer.Init()
  50. if setting.ReportingEnabled {
  51. go metrics.StartUsageReportLoop()
  52. }
  53. cmd.StartServer()
  54. log.Close()
  55. }
  56. func initRuntime() {
  57. setting.NewConfigContext(&setting.CommandLineArgs{
  58. Config: *configFile,
  59. HomePath: *homePath,
  60. Args: flag.Args(),
  61. })
  62. log.Info("Starting Grafana")
  63. log.Info("Version: %v, Commit: %v, Build date: %v", setting.BuildVersion, setting.BuildCommit, time.Unix(setting.BuildStamp, 0))
  64. setting.LogConfigurationInfo()
  65. sqlstore.NewEngine()
  66. sqlstore.EnsureAdminUser()
  67. }
  68. func writePIDFile() {
  69. if *pidFile == "" {
  70. return
  71. }
  72. // Ensure the required directory structure exists.
  73. err := os.MkdirAll(filepath.Dir(*pidFile), 0700)
  74. if err != nil {
  75. log.Fatal(3, "Failed to verify pid directory", err)
  76. }
  77. // Retrieve the PID and write it.
  78. pid := strconv.Itoa(os.Getpid())
  79. if err := ioutil.WriteFile(*pidFile, []byte(pid), 0644); err != nil {
  80. log.Fatal(3, "Failed to write pidfile", err)
  81. }
  82. }