2
0

service.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "path/filepath"
  7. "github.com/kardianos/service"
  8. "github.com/sirupsen/logrus"
  9. "github.com/slackhq/nebula"
  10. "github.com/slackhq/nebula/config"
  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 := logrus.New()
  23. HookLogger(l)
  24. c := config.NewC(l)
  25. err := c.Load(*p.configPath)
  26. if err != nil {
  27. return fmt.Errorf("failed to load config: %s", err)
  28. }
  29. p.control, err = nebula.Main(c, *p.configTest, Build, l, nil)
  30. if err != nil {
  31. return err
  32. }
  33. p.control.Start()
  34. return nil
  35. }
  36. func (p *program) Stop(s service.Service) error {
  37. logger.Info("Nebula service stopping.")
  38. p.control.Stop()
  39. return nil
  40. }
  41. func doService(configPath *string, configTest *bool, build string, serviceFlag *string) {
  42. if *configPath == "" {
  43. ex, err := os.Executable()
  44. if err != nil {
  45. panic(err)
  46. }
  47. *configPath = filepath.Dir(ex) + "/config.yaml"
  48. }
  49. svcConfig := &service.Config{
  50. Name: "Nebula",
  51. DisplayName: "Nebula Network Service",
  52. Description: "Nebula network connectivity daemon for encrypted communications",
  53. Arguments: []string{"-service", "run", "-config", *configPath},
  54. }
  55. prg := &program{
  56. configPath: configPath,
  57. configTest: configTest,
  58. build: build,
  59. }
  60. // Here are what the different loggers are doing:
  61. // - `log` is the standard go log utility, meant to be used while the process is still attached to stdout/stderr
  62. // - `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)
  63. // - above, in `Run` we create a `logrus.Logger` which is what nebula expects to use
  64. s, err := service.New(prg, svcConfig)
  65. if err != nil {
  66. log.Fatal(err)
  67. }
  68. errs := make(chan error, 5)
  69. logger, err = s.Logger(errs)
  70. if err != nil {
  71. log.Fatal(err)
  72. }
  73. go func() {
  74. for {
  75. err := <-errs
  76. if err != nil {
  77. // Route any errors from the system logger to stdout as a best effort to notice issues there
  78. log.Print(err)
  79. }
  80. }
  81. }()
  82. switch *serviceFlag {
  83. case "run":
  84. err = s.Run()
  85. if err != nil {
  86. // Route any errors to the system logger
  87. logger.Error(err)
  88. }
  89. default:
  90. err := service.Control(s, *serviceFlag)
  91. if err != nil {
  92. log.Printf("Valid actions: %q\n", service.ControlAction)
  93. log.Fatal(err)
  94. }
  95. return
  96. }
  97. }