forked from operable/go-relay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
212 lines (195 loc) · 4.57 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"runtime/pprof"
"strings"
"syscall"
log "github.com/Sirupsen/logrus"
"github.com/operable/go-relay/relay"
"github.com/operable/go-relay/relay/config"
)
const (
BAD_CONFIG = iota + 1
DOCKER_ERR
BUS_ERR
)
var configFile = flag.String("file", "", "Path to configuration file")
var cpuprofile = flag.String("cpuprofile", "", "Write CPU profile to file")
var memprofile = flag.String("memprofile", "", "Write memory profile to this file")
var devMode = flag.Bool("dev", false, "Enable developer mode")
// Populated by build script
var buildstamp string
var buildhash string
var buildtag string
var commanddrivertag string
var configLocations = []string{
"/etc/relay.conf",
"/usr/local/etc/relay.conf",
}
func init() {
displayVersionInfo()
log.SetFormatter(&log.TextFormatter{
DisableTimestamp: false,
FullTimestamp: true,
DisableSorting: true,
})
}
func displayVersionInfo() {
for _, arg := range os.Args {
if arg == "-v" || arg == "--version" || arg == "-version" {
if buildtag == "" {
buildtag = "<None>"
}
fmt.Printf("Git commit: %s\nGit tag: %s\nBuild timestamp: %s UTC\n",
buildhash, buildtag, buildstamp)
os.Exit(0)
}
}
}
func configureLogger(config *config.Config) {
if config.LogJSON == true {
log.SetFormatter(&log.JSONFormatter{})
}
switch config.LogPath {
case "stderr":
log.SetOutput(os.Stderr)
case "console":
fallthrough
case "stdout":
log.SetOutput(os.Stdout)
default:
logFile, err := os.OpenFile(config.LogPath, os.O_WRONLY | os.O_APPEND | os.O_CREATE, 0644)
if err != nil {
panic(err)
}
log.SetOutput(logFile)
}
switch config.LogLevel {
case "debug":
log.SetLevel(log.DebugLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "warn":
log.SetLevel(log.WarnLevel)
case "err":
fallthrough
case "error":
log.SetLevel(log.ErrorLevel)
default:
os.Stderr.Write([]byte(fmt.Sprintf("Unknown log level '%s'. Defaulting to info.\n",
config.LogLevel)))
log.SetLevel(log.InfoLevel)
}
}
func tryLoadingConfig(locations []string) config.RawConfig {
for _, location := range locations {
rawConfig, err := config.LoadConfig(location)
if err != nil {
log.Warnf("Error loading config file '%s': %s.", location, err)
continue
} else {
log.Infof("Using config file '%s'.", location)
}
return rawConfig
}
return make(config.RawConfig, 0)
}
func prepare() *config.Config {
flag.Parse()
locations := configLocations
if *configFile != "" {
locations = []string{
*configFile,
}
}
rawConfig := tryLoadingConfig(locations)
relayConfig, err := rawConfig.Parse(commanddrivertag)
if err != nil {
logMessage := ""
msgs := []string{}
for _, msg := range strings.Split(fmt.Sprintf("%s", err), ";") {
if msg != "" {
msgs = append(msgs, msg)
}
}
count := len(msgs) - 1
for i, msg := range msgs {
if i < count {
logMessage = fmt.Sprintf("%s %s\n", logMessage, msg)
} else {
logMessage = fmt.Sprintf("%s %s", logMessage, msg)
}
}
log.Errorf("Error configuring Relay:\n%s", logMessage)
log.Error("Relay start aborted.")
os.Exit(BAD_CONFIG)
return nil
}
relayConfig.DevMode = *devMode
configureLogger(relayConfig)
return relayConfig
}
func main() {
relayConfig := prepare()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal(err)
}
defer func() {
pprof.WriteHeapProfile(f)
f.Close()
}()
}
if err := relayConfig.Verify(); err != nil {
log.Error(err)
os.Exit(BAD_CONFIG)
return
}
log.Infof("Relay %s is initializing.", relayConfig.ID)
if relayConfig.DevMode == true {
log.Warn("Developer mode enabled.")
}
myRelay, err := relay.NewRelay(relayConfig)
if err != nil {
log.Error(err)
os.Exit(1)
}
if err := myRelay.Start(); err != nil {
log.Error(err)
os.Exit(1)
return
}
log.Infof("Relay %s online.", relayConfig.ID)
// Set up signal handlers
interruptChannel := make(chan os.Signal, 1)
signal.Notify(interruptChannel, syscall.SIGINT)
// Handle HUP signals by reopening logfiles
hupChannel := make(chan os.Signal, 1)
signal.Notify(hupChannel, syscall.SIGHUP)
go func() {
for {
<-hupChannel
configureLogger(relayConfig)
}
}()
// Wait until we get an interrupt signal
<-interruptChannel
// Shutdown
// Remove signal handler so Ctrl-C works
signal.Reset(syscall.SIGINT)
log.Info("Starting shut down.")
myRelay.Stop()
log.Infof("Relay %s shut down complete.", relayConfig.ID)
}