service.go 3.0 KB

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