service.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "github.com/kardianos/service"
  6. "github.com/slackhq/nebula"
  7. "github.com/slackhq/nebula/config"
  8. "github.com/slackhq/nebula/logging"
  9. )
  10. var logger service.Logger
  11. type program struct {
  12. configPath *string
  13. configTest *bool
  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, *p.configTest, Build, l, nil)
  35. if err != nil {
  36. return err
  37. }
  38. p.control.Start()
  39. return nil
  40. }
  41. func (p *program) Stop(s service.Service) error {
  42. logger.Info("Nebula service stopping.")
  43. p.control.Stop()
  44. return nil
  45. }
  46. func doService(configPath *string, configTest *bool, build string, serviceFlag *string) error {
  47. if *configPath == "" {
  48. p, err := config.DefaultPath()
  49. if err != nil {
  50. return err
  51. }
  52. *configPath = p
  53. }
  54. svcConfig := &service.Config{
  55. Name: "Nebula",
  56. DisplayName: "Nebula Network Service",
  57. Description: "Nebula network connectivity daemon for encrypted communications",
  58. Arguments: []string{"-service", "run", "-config", *configPath},
  59. }
  60. prg := &program{
  61. configPath: configPath,
  62. configTest: configTest,
  63. build: build,
  64. }
  65. // Here are what the different loggers are doing:
  66. // - `log` is the standard go log utility, meant to be used while the process is still attached to stdout/stderr
  67. // - `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)
  68. // - 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
  69. s, err := service.New(prg, svcConfig)
  70. if err != nil {
  71. return err
  72. }
  73. errs := make(chan error, 5)
  74. logger, err = s.Logger(errs)
  75. if err != nil {
  76. return err
  77. }
  78. go func() {
  79. for {
  80. err := <-errs
  81. if err != nil {
  82. // Route any errors from the system logger to stdout as a best effort to notice issues there
  83. log.Print(err)
  84. }
  85. }
  86. }()
  87. switch *serviceFlag {
  88. case "run":
  89. if err := s.Run(); err != nil {
  90. // Route any errors to the system logger
  91. logger.Error(err)
  92. }
  93. default:
  94. if err := service.Control(s, *serviceFlag); err != nil {
  95. log.Printf("Valid actions: %q\n", service.ControlAction)
  96. return err
  97. }
  98. }
  99. return nil
  100. }