1
0

service.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/localapi"
  10. "github.com/slackhq/nebula/logging"
  11. )
  12. var logger service.Logger
  13. type program struct {
  14. configPath *string
  15. build string
  16. control *nebula.Control
  17. local *localapi.Endpoints
  18. }
  19. func (p *program) Start(s service.Service) error {
  20. // Start should not block.
  21. logger.Info("Nebula service starting.")
  22. l := newPlatformLogger()
  23. c := config.NewC(l)
  24. err := c.Load(*p.configPath)
  25. if err != nil {
  26. return fmt.Errorf("failed to load config: %s", err)
  27. }
  28. if err := logging.ApplyConfig(l, c); err != nil {
  29. return fmt.Errorf("failed to apply logging config: %s", err)
  30. }
  31. c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
  32. if err := logging.ApplyConfig(l, c); err != nil {
  33. l.Error("Failed to reconfigure logger on reload", "error", err)
  34. r.Failed()
  35. }
  36. })
  37. p.control, err = nebula.Main(c, false, Build, l, nil)
  38. if err != nil {
  39. return err
  40. }
  41. if err := p.control.Start(); err != nil {
  42. return err
  43. }
  44. // Now that we're started, stand up the local APIs.
  45. p.local = localapi.Wire(p.control.Context(), l, c, p.control.Commands())
  46. // Nebula can stop itself on a fatal packet reader error, make sure to log it if it happens.
  47. go func() {
  48. if err := p.control.Wait(); err != nil {
  49. logger.Error(fmt.Sprintf("Nebula stopped due to fatal error: %v", err))
  50. // os.Exit skips deferred calls and races the sockets' own context watchers,
  51. // so they are removed here, as cmd/nebula does.
  52. p.local.Stop()
  53. os.Exit(2)
  54. }
  55. }()
  56. return nil
  57. }
  58. func (p *program) Stop(s service.Service) error {
  59. logger.Info("Nebula service stopping.")
  60. if p.local != nil {
  61. p.local.Stop()
  62. }
  63. if p.control == nil {
  64. return nil
  65. }
  66. p.control.Stop()
  67. // block until nebula has fully drained before reporting stopped.
  68. // error logging is handled by Start.
  69. _ = p.control.Wait()
  70. return nil
  71. }
  72. func doService(configPath *string, build string, serviceFlag *string) error {
  73. if *configPath == "" {
  74. p, err := config.DefaultPath()
  75. if err != nil {
  76. return err
  77. }
  78. *configPath = p
  79. }
  80. svcConfig := &service.Config{
  81. Name: "Nebula",
  82. DisplayName: "Nebula Network Service",
  83. Description: "Nebula network connectivity daemon for encrypted communications",
  84. Arguments: []string{"-service", "run", "-config", *configPath},
  85. }
  86. prg := &program{
  87. configPath: configPath,
  88. build: build,
  89. }
  90. // Here are what the different loggers are doing:
  91. // - `log` is the standard go log utility, meant to be used while the process is still attached to stdout/stderr
  92. // - `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)
  93. // - 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
  94. s, err := service.New(prg, svcConfig)
  95. if err != nil {
  96. return err
  97. }
  98. errs := make(chan error, 5)
  99. logger, err = s.Logger(errs)
  100. if err != nil {
  101. return err
  102. }
  103. go func() {
  104. for {
  105. err := <-errs
  106. if err != nil {
  107. // Route any errors from the system logger to stdout as a best effort to notice issues there
  108. log.Print(err)
  109. }
  110. }
  111. }()
  112. switch *serviceFlag {
  113. case "run":
  114. if err := s.Run(); err != nil {
  115. // Route any errors to the system logger and report the failure
  116. logger.Error(err)
  117. return err
  118. }
  119. default:
  120. if err := service.Control(s, *serviceFlag); err != nil {
  121. log.Printf("Valid actions: %q\n", service.ControlAction)
  122. return err
  123. }
  124. }
  125. return nil
  126. }