main.go 2.1 KB

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