2019-11-19 19:55:02 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-06-30 20:48:58 +02:00
|
|
|
"fmt"
|
2019-11-19 19:55:02 +01:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/kardianos/service"
|
2020-07-01 00:53:30 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-11-19 19:55:02 +01:00
|
|
|
"github.com/slackhq/nebula"
|
|
|
|
)
|
|
|
|
|
|
|
|
var logger service.Logger
|
|
|
|
|
|
|
|
type program struct {
|
|
|
|
configPath *string
|
|
|
|
configTest *bool
|
|
|
|
build string
|
2020-09-18 16:20:09 +02:00
|
|
|
control *nebula.Control
|
2019-11-19 19:55:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *program) Start(s service.Service) error {
|
2019-11-28 17:25:34 +01:00
|
|
|
// Start should not block.
|
2020-09-18 16:20:09 +02:00
|
|
|
logger.Info("Nebula service starting.")
|
2019-11-19 19:55:02 +01:00
|
|
|
|
2021-03-26 15:46:30 +01:00
|
|
|
l := logrus.New()
|
|
|
|
l.Out = os.Stdout
|
|
|
|
|
|
|
|
config := nebula.NewConfig(l)
|
2020-06-30 20:48:58 +02:00
|
|
|
err := config.Load(*p.configPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to load config: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-09-18 16:20:09 +02:00
|
|
|
p.control, err = nebula.Main(config, *p.configTest, Build, l, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.control.Start()
|
|
|
|
return nil
|
2019-11-19 19:55:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *program) Stop(s service.Service) error {
|
2019-11-23 16:32:10 +01:00
|
|
|
logger.Info("Nebula service stopping.")
|
2020-09-18 16:20:09 +02:00
|
|
|
p.control.Stop()
|
2019-11-19 19:55:02 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func doService(configPath *string, configTest *bool, build string, serviceFlag *string) {
|
|
|
|
if *configPath == "" {
|
|
|
|
ex, err := os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
*configPath = filepath.Dir(ex) + "/config.yaml"
|
|
|
|
}
|
|
|
|
|
|
|
|
svcConfig := &service.Config{
|
|
|
|
Name: "Nebula",
|
|
|
|
DisplayName: "Nebula Network Service",
|
|
|
|
Description: "Nebula network connectivity daemon for encrypted communications",
|
|
|
|
Arguments: []string{"-service", "run", "-config", *configPath},
|
|
|
|
}
|
|
|
|
|
|
|
|
prg := &program{
|
|
|
|
configPath: configPath,
|
|
|
|
configTest: configTest,
|
|
|
|
build: build,
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := service.New(prg, svcConfig)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
errs := make(chan error, 5)
|
|
|
|
logger, err = s.Logger(errs)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
err := <-errs
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
switch *serviceFlag {
|
|
|
|
case "run":
|
|
|
|
err = s.Run()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
err := service.Control(s, *serviceFlag)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Valid actions: %q\n", service.ControlAction)
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|