main.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. serviceFlag := flag.String("service", "", "Control the system service.")
  30. configPath := flag.String("config", "", "Path to either a file or directory to load configuration from")
  31. configTest := flag.Bool("test", false, "Test the config and print the end result. Non zero exit indicates a faulty config")
  32. printVersion := flag.Bool("version", false, "Print version")
  33. printUsage := flag.Bool("help", false, "Print command line usage")
  34. flag.Parse()
  35. if *printVersion {
  36. fmt.Printf("Version: %s\n", Build)
  37. os.Exit(0)
  38. }
  39. if *printUsage {
  40. flag.Usage()
  41. os.Exit(0)
  42. }
  43. l := logging.NewLogger(os.Stdout)
  44. if *serviceFlag != "" {
  45. if *configTest {
  46. fmt.Println("-test is not supported with -service, run the config test without -service")
  47. os.Exit(1)
  48. }
  49. if err := doService(configPath, Build, serviceFlag); err != nil {
  50. l.Error("Service command failed", "error", err)
  51. os.Exit(1)
  52. }
  53. return
  54. }
  55. if *configPath == "" {
  56. p, err := config.DefaultPath()
  57. if err != nil {
  58. fmt.Println(err)
  59. os.Exit(1)
  60. }
  61. *configPath = p
  62. }
  63. c := config.NewC(l)
  64. err := c.Load(*configPath)
  65. if err != nil {
  66. fmt.Printf("failed to load config: %s", err)
  67. os.Exit(1)
  68. }
  69. if err := logging.ApplyConfig(l, c); err != nil {
  70. fmt.Printf("failed to apply logging config: %s", err)
  71. os.Exit(1)
  72. }
  73. c.RegisterReloadCallback(func(c *config.C) {
  74. if err := logging.ApplyConfig(l, c); err != nil {
  75. l.Error("Failed to reconfigure logger on reload", "error", err)
  76. }
  77. })
  78. ctrl, err := nebula.Main(c, *configTest, Build, l, nil)
  79. if err != nil {
  80. util.LogWithContextIfNeeded("Failed to start", err, l)
  81. os.Exit(1)
  82. }
  83. if !*configTest {
  84. if err := ctrl.Start(); err != nil {
  85. util.LogWithContextIfNeeded("Error while running", err, l)
  86. os.Exit(1)
  87. }
  88. go ctrl.ShutdownBlock()
  89. if err := ctrl.Wait(); err != nil {
  90. l.Error("Nebula stopped due to fatal error", "error", err)
  91. os.Exit(2)
  92. }
  93. l.Info("Goodbye")
  94. }
  95. os.Exit(0)
  96. }