main.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "os/signal"
  8. "path/filepath"
  9. "runtime"
  10. "strconv"
  11. "syscall"
  12. "time"
  13. "github.com/grafana/grafana/pkg/log"
  14. "github.com/grafana/grafana/pkg/login"
  15. "github.com/grafana/grafana/pkg/metrics"
  16. "github.com/grafana/grafana/pkg/plugins"
  17. "github.com/grafana/grafana/pkg/services/eventpublisher"
  18. "github.com/grafana/grafana/pkg/services/notifications"
  19. "github.com/grafana/grafana/pkg/services/search"
  20. "github.com/grafana/grafana/pkg/services/sqlstore"
  21. "github.com/grafana/grafana/pkg/setting"
  22. "github.com/grafana/grafana/pkg/social"
  23. )
  24. var version = "3.1.0"
  25. var commit = "NA"
  26. var buildstamp string
  27. var build_date string
  28. var configFile = flag.String("config", "", "path to config file")
  29. var homePath = flag.String("homepath", "", "path to grafana install/home path, defaults to working directory")
  30. var pidFile = flag.String("pidfile", "", "path to pid file")
  31. var exitChan = make(chan int)
  32. func init() {
  33. runtime.GOMAXPROCS(runtime.NumCPU())
  34. }
  35. func main() {
  36. v := flag.Bool("v", false, "prints current version and exits")
  37. flag.Parse()
  38. if *v {
  39. fmt.Printf("Version %s (commit: %s)\n", version, commit)
  40. os.Exit(0)
  41. }
  42. buildstampInt64, _ := strconv.ParseInt(buildstamp, 10, 64)
  43. setting.BuildVersion = version
  44. setting.BuildCommit = commit
  45. setting.BuildStamp = buildstampInt64
  46. go listenToSystemSignels()
  47. flag.Parse()
  48. writePIDFile()
  49. initRuntime()
  50. search.Init()
  51. login.Init()
  52. social.NewOAuthService()
  53. eventpublisher.Init()
  54. plugins.Init()
  55. metrics.Init()
  56. if err := notifications.Init(); err != nil {
  57. log.Fatal(3, "Notification service failed to initialize", err)
  58. }
  59. StartServer()
  60. exitChan <- 0
  61. }
  62. func initRuntime() {
  63. err := setting.NewConfigContext(&setting.CommandLineArgs{
  64. Config: *configFile,
  65. HomePath: *homePath,
  66. Args: flag.Args(),
  67. })
  68. if err != nil {
  69. log.Fatal(3, err.Error())
  70. }
  71. log.Info("Starting Grafana")
  72. log.Info("Version: %v, Commit: %v, Build date: %v", setting.BuildVersion, setting.BuildCommit, time.Unix(setting.BuildStamp, 0))
  73. setting.LogConfigurationInfo()
  74. sqlstore.NewEngine()
  75. sqlstore.EnsureAdminUser()
  76. }
  77. func writePIDFile() {
  78. if *pidFile == "" {
  79. return
  80. }
  81. // Ensure the required directory structure exists.
  82. err := os.MkdirAll(filepath.Dir(*pidFile), 0700)
  83. if err != nil {
  84. log.Fatal(3, "Failed to verify pid directory", err)
  85. }
  86. // Retrieve the PID and write it.
  87. pid := strconv.Itoa(os.Getpid())
  88. if err := ioutil.WriteFile(*pidFile, []byte(pid), 0644); err != nil {
  89. log.Fatal(3, "Failed to write pidfile", err)
  90. }
  91. }
  92. func listenToSystemSignels() {
  93. signalChan := make(chan os.Signal, 1)
  94. code := 0
  95. signal.Notify(signalChan, os.Interrupt)
  96. signal.Notify(signalChan, os.Kill)
  97. signal.Notify(signalChan, syscall.SIGTERM)
  98. select {
  99. case sig := <-signalChan:
  100. log.Info("Received signal %s. shutting down", sig)
  101. case code = <-exitChan:
  102. switch code {
  103. case 0:
  104. log.Info("Shutting down")
  105. default:
  106. log.Warn("Shutting down")
  107. }
  108. }
  109. log.Close()
  110. os.Exit(code)
  111. }