service.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 fileExists(filename string) bool {
  42. _, err := os.Stat(filename)
  43. if os.IsNotExist(err) {
  44. return false
  45. }
  46. return true
  47. }
  48. func doService(configPath *string, configTest *bool, build string, serviceFlag *string) {
  49. if *configPath == "" {
  50. ex, err := os.Executable()
  51. if err != nil {
  52. panic(err)
  53. }
  54. *configPath = filepath.Dir(ex) + "/config.yaml"
  55. if !fileExists(*configPath) {
  56. *configPath = filepath.Dir(ex) + "/config.yml"
  57. }
  58. }
  59. svcConfig := &service.Config{
  60. Name: "Nebula",
  61. DisplayName: "Nebula Network Service",
  62. Description: "Nebula network connectivity daemon for encrypted communications",
  63. Arguments: []string{"-service", "run", "-config", *configPath},
  64. }
  65. prg := &program{
  66. configPath: configPath,
  67. configTest: configTest,
  68. build: build,
  69. }
  70. // Here are what the different loggers are doing:
  71. // - `log` is the standard go log utility, meant to be used while the process is still attached to stdout/stderr
  72. // - `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)
  73. // - above, in `Run` we create a `logrus.Logger` which is what nebula expects to use
  74. s, err := service.New(prg, svcConfig)
  75. if err != nil {
  76. log.Fatal(err)
  77. }
  78. errs := make(chan error, 5)
  79. logger, err = s.Logger(errs)
  80. if err != nil {
  81. log.Fatal(err)
  82. }
  83. go func() {
  84. for {
  85. err := <-errs
  86. if err != nil {
  87. // Route any errors from the system logger to stdout as a best effort to notice issues there
  88. log.Print(err)
  89. }
  90. }
  91. }()
  92. switch *serviceFlag {
  93. case "run":
  94. err = s.Run()
  95. if err != nil {
  96. // Route any errors to the system logger
  97. logger.Error(err)
  98. }
  99. default:
  100. err := service.Control(s, *serviceFlag)
  101. if err != nil {
  102. log.Printf("Valid actions: %q\n", service.ControlAction)
  103. log.Fatal(err)
  104. }
  105. return
  106. }
  107. }