Debian Codegraph @

cmd/nebula-service/main.go

raw
1 package main
2
3 import (
4 "flag"
5 "fmt"
6 "os"
7 "runtime/debug"
8 "strings"
9
10 "github.com/slackhq/nebula"
11 "github.com/slackhq/nebula/config"
12 "github.com/slackhq/nebula/logging"
13 "github.com/slackhq/nebula/util"
14 )
15
16 // A version string that can be set with
17 //
18 // -ldflags "-X main.Build=SOMEVERSION"
19 //
20 // at compile-time.
21 var Build string
22
23 func init() {
24 if Build == "" {
25 info, ok := debug.ReadBuildInfo()
26 if !ok {
27 return
28 }
29
30 Build = strings.TrimPrefix(info.Main.Version, "v")
31 }
32 }
33
34 func main() {
35 serviceFlag := flag.String("service", "", "Control the system service.")
36 configPath := flag.String("config", "", "Path to either a file or directory to load configuration from")
37 configTest := flag.Bool("test", false, "Test the config and print the end result. Non zero exit indicates a faulty config")
38 printVersion := flag.Bool("version", false, "Print version")
39 printUsage := flag.Bool("help", false, "Print command line usage")
40
41 flag.Parse()
42
43 if *printVersion {
44 fmt.Printf("Version: %s\n", Build)
45 os.Exit(0)
46 }
47
48 if *printUsage {
49 flag.Usage()
50 os.Exit(0)
51 }
52
53 l := logging.NewLogger(os.Stdout)
54
55 if *serviceFlag != "" {
56 if *configTest {
57 fmt.Println("-test is not supported with -service, run the config test without -service")
58 os.Exit(1)
59 }
60
61 if err := doService(configPath, Build, serviceFlag); err != nil {
62 l.Error("Service command failed", "error", err)
63 os.Exit(1)
64 }
65 return
66 }
67
68 if *configPath == "" {
69 p, err := config.DefaultPath()
70 if err != nil {
71 fmt.Println(err)
72 os.Exit(1)
73 }
74 *configPath = p
75 }
76
77 c := config.NewC(l)
78 err := c.Load(*configPath)
79 if err != nil {
80 fmt.Printf("failed to load config: %s", err)
81 os.Exit(1)
82 }
83
84 if err := logging.ApplyConfig(l, c); err != nil {
85 fmt.Printf("failed to apply logging config: %s", err)
86 os.Exit(1)
87 }
88 c.RegisterReloadCallback(func(c *config.C) {
89 if err := logging.ApplyConfig(l, c); err != nil {
90 l.Error("Failed to reconfigure logger on reload", "error", err)
91 }
92 })
93
94 ctrl, err := nebula.Main(c, *configTest, Build, l, nil)
95 if err != nil {
96 util.LogWithContextIfNeeded("Failed to start", err, l)
97 os.Exit(1)
98 }
99
100 if !*configTest {
101 if err := ctrl.Start(); err != nil {
102 util.LogWithContextIfNeeded("Error while running", err, l)
103 os.Exit(1)
104 }
105
106 go ctrl.ShutdownBlock()
107
108 if err := ctrl.Wait(); err != nil {
109 l.Error("Nebula stopped due to fatal error", "error", err)
110 os.Exit(2)
111 }
112
113 l.Info("Goodbye")
114 }
115
116 os.Exit(0)
117 }