service.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "github.com/kardianos/service"
  7. "github.com/slackhq/nebula"
  8. "github.com/slackhq/nebula/config"
  9. "github.com/slackhq/nebula/logging"
  10. )
  11. var logger service.Logger
  12. type program struct {
  13. configPath *string
  14. build string
  15. control *nebula.Control
  16. }
  17. func (p *program) Start(s service.Service) error {
  18. // Start should not block.
  19. logger.Info("Nebula service starting.")
  20. l := newPlatformLogger()
  21. c := config.NewC(l)
  22. err := c.Load(*p.configPath)
  23. if err != nil {
  24. return fmt.Errorf("failed to load config: %s", err)
  25. }
  26. if err := logging.ApplyConfig(l, c); err != nil {
  27. return fmt.Errorf("failed to apply logging config: %s", err)
  28. }
  29. c.RegisterReloadCallback(func(c *config.C) {
  30. if err := logging.ApplyConfig(l, c); err != nil {
  31. l.Error("Failed to reconfigure logger on reload", "error", err)
  32. }
  33. })
  34. p.control, err = nebula.Main(c, false, Build, l, nil)
  35. if err != nil {
  36. return err
  37. }
  38. if err := p.control.Start(); err != nil {
  39. return err
  40. }
  41. // Nebula can stop itself on a fatal packet reader error, make sure to log it if it happens.
  42. go func() {
  43. if err := p.control.Wait(); err != nil {
  44. logger.Error(fmt.Sprintf("Nebula stopped due to fatal error: %v", err))
  45. os.Exit(2)
  46. }
  47. }()
  48. return nil
  49. }
  50. func (p *program) Stop(s service.Service) error {
  51. logger.Info("Nebula service stopping.")
  52. if p.control == nil {
  53. return nil
  54. }
  55. p.control.Stop()
  56. // block until nebula has fully drained before reporting stopped.
  57. // error logging is handled by Start.
  58. _ = p.control.Wait()
  59. return nil
  60. }
  61. func doService(configPath *string, build string, serviceFlag *string) error {
  62. if *configPath == "" {
  63. p, err := config.DefaultPath()
  64. if err != nil {
  65. return err
  66. }
  67. *configPath = p
  68. }
  69. svcConfig := &service.Config{
  70. Name: "Nebula",
  71. DisplayName: "Nebula Network Service",
  72. Description: "Nebula network connectivity daemon for encrypted communications",
  73. Arguments: []string{"-service", "run", "-config", *configPath},
  74. }
  75. prg := &program{
  76. configPath: configPath,
  77. build: build,
  78. }
  79. // Here are what the different loggers are doing:
  80. // - `log` is the standard go log utility, meant to be used while the process is still attached to stdout/stderr
  81. // - `logger` is the service log utility that may be attached to a special place depending on OS (Windows will have it attached to the event log)
  82. // - in program.Start we build a *slog.Logger via newPlatformLogger; on non-Windows that is a stdout-backed slog logger, on Windows it routes records through the service logger
  83. s, err := service.New(prg, svcConfig)
  84. if err != nil {
  85. return err
  86. }
  87. errs := make(chan error, 5)
  88. logger, err = s.Logger(errs)
  89. if err != nil {
  90. return err
  91. }
  92. go func() {
  93. for {
  94. err := <-errs
  95. if err != nil {
  96. // Route any errors from the system logger to stdout as a best effort to notice issues there
  97. log.Print(err)
  98. }
  99. }
  100. }()
  101. switch *serviceFlag {
  102. case "run":
  103. if err := s.Run(); err != nil {
  104. // Route any errors to the system logger and report the failure
  105. logger.Error(err)
  106. return err
  107. }
  108. default:
  109. if err := service.Control(s, *serviceFlag); err != nil {
  110. log.Printf("Valid actions: %q\n", service.ControlAction)
  111. return err
  112. }
  113. }
  114. return nil
  115. }