main.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. if buildstampInt64 == 0 {
  44. buildstampInt64 = time.Now().Unix()
  45. }
  46. setting.BuildVersion = version
  47. setting.BuildCommit = commit
  48. setting.BuildStamp = buildstampInt64
  49. go listenToSystemSignels()
  50. flag.Parse()
  51. writePIDFile()
  52. initRuntime()
  53. metrics.Init()
  54. search.Init()
  55. login.Init()
  56. social.NewOAuthService()
  57. eventpublisher.Init()
  58. plugins.Init()
  59. if err := notifications.Init(); err != nil {
  60. log.Fatal(3, "Notification service failed to initialize", err)
  61. }
  62. StartServer()
  63. exitChan <- 0
  64. }
  65. func initRuntime() {
  66. err := setting.NewConfigContext(&setting.CommandLineArgs{
  67. Config: *configFile,
  68. HomePath: *homePath,
  69. Args: flag.Args(),
  70. })
  71. if err != nil {
  72. log.Fatal(3, err.Error())
  73. }
  74. logger := log.New("main")
  75. logger.Info("Starting Grafana", "version", version, "commit", commit, "compiled", time.Unix(setting.BuildStamp, 0))
  76. setting.LogConfigurationInfo()
  77. sqlstore.NewEngine()
  78. sqlstore.EnsureAdminUser()
  79. }
  80. func writePIDFile() {
  81. if *pidFile == "" {
  82. return
  83. }
  84. // Ensure the required directory structure exists.
  85. err := os.MkdirAll(filepath.Dir(*pidFile), 0700)
  86. if err != nil {
  87. log.Fatal(3, "Failed to verify pid directory", err)
  88. }
  89. // Retrieve the PID and write it.
  90. pid := strconv.Itoa(os.Getpid())
  91. if err := ioutil.WriteFile(*pidFile, []byte(pid), 0644); err != nil {
  92. log.Fatal(3, "Failed to write pidfile", err)
  93. }
  94. }
  95. func listenToSystemSignels() {
  96. signalChan := make(chan os.Signal, 1)
  97. code := 0
  98. signal.Notify(signalChan, os.Interrupt, os.Kill, syscall.SIGTERM)
  99. select {
  100. case sig := <-signalChan:
  101. log.Info("Received signal %s. shutting down", sig)
  102. case code = <-exitChan:
  103. switch code {
  104. case 0:
  105. log.Info("Shutting down")
  106. default:
  107. log.Warn("Shutting down")
  108. }
  109. }
  110. log.Close()
  111. os.Exit(code)
  112. }