-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
108 lines (102 loc) · 2.46 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"errors"
"flag"
"fmt"
"net"
"os"
"os/signal"
"syscall"
"gnd.la/log"
)
var (
defaultConfigDir = fmt.Sprintf("/etc/%s", AppName)
governatorVersion = "1.0"
gitVersion = ""
)
func testConfigurations(g *Governator) {
configs, err := g.parseConfigs()
if err != nil {
die(err)
}
ok := true
for _, v := range configs {
fmt.Println("checking", v.Name)
if v.Err != nil {
fmt.Fprintf(os.Stderr, "error in %s: %s\n", v.Name, v.Err)
ok = false
}
}
if ok {
fmt.Println("configurations OK")
}
}
func die(err error) {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
func main() {
var (
daemon = flag.Bool("D", false, "Run in daemon mode")
debug = flag.Bool("d", false, "Enable debug logging")
testConfig = flag.Bool("t", false, "Test configuration files")
configDir = flag.String("c", defaultConfigDir, "Configuration directory")
serverAddr = flag.String("daemon", "unix://"+socketPath, "Daemon URL to listen on in daemon mode or to connect to in client mode")
printVersion = flag.Bool("V", false, "Print version and exit")
)
flag.Parse()
if *debug {
log.SetLevel(log.LDebug)
}
switch {
case *printVersion:
fmt.Println(governatorVersion, gitVersion)
case *testConfig:
g, err := NewGovernator(*configDir)
if err != nil {
die(fmt.Errorf("error initializing daemon: %s", err))
}
testConfigurations(g)
case *daemon:
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Signal(syscall.SIGTERM), os.Kill)
if os.Geteuid() != 0 {
die(errors.New("govenator daemon must be run as root"))
}
g, err := NewGovernator(*configDir)
if err != nil {
die(fmt.Errorf("error initializing daemon: %s", err))
}
g.ServerAddr = *serverAddr
if err := g.LoadServices(); err != nil {
die(fmt.Errorf("error loading services: %s", err))
}
go func() {
// Wait for signal
<-c
g.StopRunning()
}()
if err := g.Run(); err != nil {
die(fmt.Errorf("error starting daemon: %s", err))
}
default:
ok, err := clientMain(*serverAddr, flag.Args())
if err != nil {
if oe, ok := err.(*net.OpError); ok {
switch {
case oe.Err == syscall.EACCES:
fmt.Fprint(os.Stderr, "can't connect to governator, permission denied\n")
os.Exit(1)
case oe.Err == syscall.ENOENT:
fmt.Fprint(os.Stderr, "governator daemon is not running\n")
os.Exit(1)
}
}
fmt.Fprintf(os.Stderr, "error running client: %s\n", err)
os.Exit(1)
}
if !ok {
os.Exit(1)
}
}
}