main.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "runtime/debug"
  7. "strings"
  8. "github.com/slackhq/nebula"
  9. "github.com/slackhq/nebula/config"
  10. "github.com/slackhq/nebula/logging"
  11. "github.com/slackhq/nebula/util"
  12. )
  13. // A version string that can be set with
  14. //
  15. // -ldflags "-X main.Build=SOMEVERSION"
  16. //
  17. // at compile-time.
  18. var Build string
  19. func init() {
  20. if Build == "" {
  21. info, ok := debug.ReadBuildInfo()
  22. if !ok {
  23. return
  24. }
  25. Build = strings.TrimPrefix(info.Main.Version, "v")
  26. }
  27. }
  28. func main() {
  29. configPath := flag.String("config", "", "Path to either a file or directory to load configuration from")
  30. configTest := flag.Bool("test", false, "Test the config and print the end result. Non zero exit indicates a faulty config")
  31. printVersion := flag.Bool("version", false, "Print version")
  32. printUsage := flag.Bool("help", false, "Print command line usage")
  33. flag.Parse()
  34. if *printVersion {
  35. fmt.Printf("Version: %s\n", Build)
  36. os.Exit(0)
  37. }
  38. if *printUsage {
  39. flag.Usage()
  40. os.Exit(0)
  41. }
  42. if *configPath == "" {
  43. fmt.Println("-config flag must be set")
  44. flag.Usage()
  45. os.Exit(1)
  46. }
  47. l := logging.NewLogger(os.Stdout)
  48. c := config.NewC(l)
  49. err := c.Load(*configPath)
  50. if err != nil {
  51. fmt.Printf("failed to load config: %s", err)
  52. os.Exit(1)
  53. }
  54. if err := logging.ApplyConfig(l, c); err != nil {
  55. fmt.Printf("failed to apply logging config: %s", err)
  56. os.Exit(1)
  57. }
  58. c.RegisterReloadCallback(func(c *config.C) {
  59. if err := logging.ApplyConfig(l, c); err != nil {
  60. l.Error("Failed to reconfigure logger on reload", "error", err)
  61. }
  62. })
  63. ctrl, err := nebula.Main(c, *configTest, Build, l, nil)
  64. if err != nil {
  65. util.LogWithContextIfNeeded("Failed to start", err, l)
  66. os.Exit(1)
  67. }
  68. if !*configTest {
  69. wait, err := ctrl.Start()
  70. if err != nil {
  71. util.LogWithContextIfNeeded("Error while running", err, l)
  72. os.Exit(1)
  73. }
  74. go ctrl.ShutdownBlock()
  75. notifyReady(l)
  76. if err := wait(); err != nil {
  77. l.Error("Nebula stopped due to fatal error", "error", err)
  78. os.Exit(2)
  79. }
  80. l.Info("Goodbye")
  81. }
  82. os.Exit(0)
  83. }