main.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/cmd/internal/ctl"
  10. "github.com/slackhq/nebula/config"
  11. "github.com/slackhq/nebula/localapi"
  12. "github.com/slackhq/nebula/logging"
  13. "github.com/slackhq/nebula/util"
  14. )
  15. // A version string that can be set with
  16. //
  17. // -ldflags "-X main.Build=SOMEVERSION"
  18. //
  19. // at compile-time.
  20. var Build string
  21. func init() {
  22. if Build == "" {
  23. info, ok := debug.ReadBuildInfo()
  24. if !ok {
  25. return
  26. }
  27. Build = strings.TrimPrefix(info.Main.Version, "v")
  28. }
  29. }
  30. func main() {
  31. // commands are either `nebula ctl ...` or `nebula -<flag> ...`
  32. if code, done := ctl.Subcommand(os.Args); done {
  33. os.Exit(code)
  34. }
  35. serviceFlag := flag.String("service", "", "Control the system service.")
  36. configPath := flag.String("config", "", "Path to either a file or directory to load configuration from")
  37. configTest := flag.Bool("test", false, "Test the config and print the end result. Non zero exit indicates a faulty config")
  38. printVersion := flag.Bool("version", false, "Print version")
  39. printUsage := flag.Bool("help", false, "Print command line usage")
  40. flag.Parse()
  41. // A word after the daemon's flags is `ctl` in the other place an operator puts it,
  42. // or a mistyped subcommand, and either way not a daemon to start; see Trailing.
  43. if code, done := ctl.Trailing(flag.Args(), *configPath); done {
  44. os.Exit(code)
  45. }
  46. if *printVersion {
  47. fmt.Printf("Version: %s\n", Build)
  48. os.Exit(0)
  49. }
  50. if *printUsage {
  51. flag.Usage()
  52. os.Exit(0)
  53. }
  54. l := logging.NewLogger(os.Stdout)
  55. if *serviceFlag != "" {
  56. if *configTest {
  57. fmt.Println("-test is not supported with -service, run the config test without -service")
  58. os.Exit(1)
  59. }
  60. if err := doService(configPath, Build, serviceFlag); err != nil {
  61. l.Error("Service command failed", "error", err)
  62. os.Exit(1)
  63. }
  64. return
  65. }
  66. if *configPath == "" {
  67. p, err := config.DefaultPath()
  68. if err != nil {
  69. fmt.Println(err)
  70. os.Exit(1)
  71. }
  72. *configPath = p
  73. }
  74. c := config.NewC(l)
  75. err := c.Load(*configPath)
  76. if err != nil {
  77. fmt.Printf("failed to load config: %s", err)
  78. os.Exit(1)
  79. }
  80. if err := logging.ApplyConfig(l, c); err != nil {
  81. fmt.Printf("failed to apply logging config: %s", err)
  82. os.Exit(1)
  83. }
  84. c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
  85. if err := logging.ApplyConfig(l, c); err != nil {
  86. l.Error("Failed to reconfigure logger on reload", "error", err)
  87. r.Failed()
  88. }
  89. })
  90. ctrl, err := nebula.Main(c, *configTest, Build, l, nil)
  91. if err != nil {
  92. util.LogWithContextIfNeeded("Failed to start", err, l)
  93. os.Exit(1)
  94. }
  95. if !*configTest {
  96. if err := ctrl.Start(); err != nil {
  97. util.LogWithContextIfNeeded("Error while running", err, l)
  98. os.Exit(1)
  99. }
  100. local := localapi.Wire(ctrl.Context(), l, c, ctrl.Commands())
  101. go ctrl.ShutdownBlock()
  102. err := ctrl.Wait()
  103. // os.Exit skips deferred calls, so the sockets are removed here, before either
  104. // exit below, rather than left to a deferred Stop that would never run.
  105. local.Stop()
  106. if err != nil {
  107. l.Error("Nebula stopped due to fatal error", "error", err)
  108. os.Exit(2)
  109. }
  110. l.Info("Goodbye")
  111. }
  112. os.Exit(0)
  113. }