This repository has been archived by the owner on Jul 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
168 lines (133 loc) · 3.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
Revere is an alerting system for medium-sized microservices architectures.
Usage:
revere [-conf env.json] [-mode mode,...]
Configuration
The -conf flag specifies the path to a JSON file configuring Revere's static
environment. When no file is specified, Revere uses its default settings.
See github.com/yext/revere/env.EnvJSONModel for the structure the JSON in this
file should take.
Modes
The -mode flag specifies the comma-separated modes that this invocation of
Revere should run.
The initdb mode initializes Revere's database storage. Depending on whether
there are existing Revere tables in the database specified by the environment
configuration, this mode either creates a new storage area from scratch or
updates an existing area to the current schema. This mode cannot be combined
with any other modes.
The daemon mode runs the daemon that monitors systems and generates alerts.
The web mode serves the HTTP UI for administering Revere and viewing its
current state.
The -mode flag defaults to daemon,web.
*/
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/signal"
"strings"
log "github.com/sirupsen/logrus"
"github.com/juju/errors"
"golang.org/x/sys/unix"
"github.com/yext/revere/daemon"
"github.com/yext/revere/env"
"github.com/yext/revere/web/server"
)
var (
conf = flag.String("conf", "", "JSON `file` configuring Revere's static environment")
mode = flag.String("mode", "daemon,web", "comma-separated `modes` to run")
logLevel = flag.String("logLevel", "warn", "Logrus `level` to log at")
)
func main() {
flag.Parse()
if flag.NArg() > 0 {
fmt.Fprintf(os.Stderr, "unexpected arguments: %v\n", flag.Args())
flag.Usage()
os.Exit(2)
}
err := initLog()
ifErrPrintAndExit(err)
env, err := loadEnv()
ifErrPrintAndExit(err)
modes, err := parseMode()
ifErrPrintAndExit(err)
if modes[0] == "initdb" {
err := env.DB.Init()
ifErrPrintAndExit(err)
return
}
for _, mode := range modes {
switch mode {
case "daemon":
d := daemon.New(env)
d.Start()
defer d.Stop()
case "web":
w := server.New(env)
w.Start()
defer w.Stop()
}
}
waitForExitSignal()
log.Info("Caught exit signal. Revere is shutting down...")
}
func initLog() error {
level, err := log.ParseLevel(*logLevel)
if err != nil {
return errors.Mask(err)
}
log.SetLevel(level)
return nil
}
func loadEnv() (*env.Env, error) {
if *conf == "" {
return nil, errors.New("No configuration file provided")
}
desc := "env conf " + *conf
json, err := ioutil.ReadFile(*conf)
if err != nil {
return nil, errors.Maskf(err, "load %s", desc)
}
env, err := env.New(json)
if err != nil {
return nil, errors.Maskf(err, "load %s", desc)
}
return env, nil
}
func parseMode() ([]string, error) {
modes := make(map[string]bool)
for _, m := range strings.Split(*mode, ",") {
switch m {
case "daemon", "initdb", "web":
if modes[m] {
return nil, errors.New("duplicate mode " + m)
}
modes[m] = true
default:
return nil, errors.New("unknown mode " + m)
}
}
if modes["initdb"] && len(modes) > 1 {
return nil, errors.New("initdb cannot be combined with other modes")
}
modesSlice := make([]string, len(modes))
i := 0
for m := range modes {
modesSlice[i] = m
i++
}
return modesSlice, nil
}
func waitForExitSignal() {
c := make(chan os.Signal, 1)
signal.Notify(c, unix.SIGHUP, unix.SIGINT, unix.SIGTERM)
<-c
}
func ifErrPrintAndExit(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
os.Exit(1)
}
}