1
0

service.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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, r *config.Reload) {
  30. if err := logging.ApplyConfig(l, c); err != nil {
  31. l.Error("Failed to reconfigure logger on reload", "error", err)
  32. r.Failed()
  33. }
  34. })
  35. p.control, err = nebula.Main(c, false, Build, l, nil)
  36. if err != nil {
  37. return err
  38. }
  39. if err := p.control.Start(); err != nil {
  40. return err
  41. }
  42. // Nebula can stop itself on a fatal packet reader error, make sure to log it if it happens.
  43. go func() {
  44. if err := p.control.Wait(); err != nil {
  45. logger.Error(fmt.Sprintf("Nebula stopped due to fatal error: %v", err))
  46. os.Exit(2)
  47. }
  48. }()
  49. return nil
  50. }
  51. func (p *program) Stop(s service.Service) error {
  52. logger.Info("Nebula service stopping.")
  53. if p.control == nil {
  54. return nil
  55. }
  56. p.control.Stop()
  57. // block until nebula has fully drained before reporting stopped.
  58. // error logging is handled by Start.
  59. _ = p.control.Wait()
  60. return nil
  61. }
  62. func doService(configPath *string, build string, serviceFlag *string) error {
  63. if *configPath == "" {
  64. p, err := config.DefaultPath()
  65. if err != nil {
  66. return err
  67. }
  68. *configPath = p
  69. }
  70. svcConfig := &service.Config{
  71. Name: "Nebula",
  72. DisplayName: "Nebula Network Service",
  73. Description: "Nebula network connectivity daemon for encrypted communications",
  74. Arguments: []string{"-service", "run", "-config", *configPath},
  75. }
  76. prg := &program{
  77. configPath: configPath,
  78. build: build,
  79. }
  80. // Here are what the different loggers are doing:
  81. // - `log` is the standard go log utility, meant to be used while the process is still attached to stdout/stderr
  82. // - `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)
  83. // - 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
  84. s, err := service.New(prg, svcConfig)
  85. if err != nil {
  86. return err
  87. }
  88. errs := make(chan error, 5)
  89. logger, err = s.Logger(errs)
  90. if err != nil {
  91. return err
  92. }
  93. go func() {
  94. for {
  95. err := <-errs
  96. if err != nil {
  97. // Route any errors from the system logger to stdout as a best effort to notice issues there
  98. log.Print(err)
  99. }
  100. }
  101. }()
  102. switch *serviceFlag {
  103. case "run":
  104. if err := s.Run(); err != nil {
  105. // Route any errors to the system logger and report the failure
  106. logger.Error(err)
  107. return err
  108. }
  109. default:
  110. if err := service.Control(s, *serviceFlag); err != nil {
  111. log.Printf("Valid actions: %q\n", service.ControlAction)
  112. return err
  113. }
  114. }
  115. return nil
  116. }