main.go 2.1 KB

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