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