-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
248 lines (193 loc) · 6.93 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// SPDX-FileCopyrightText: 2021 Lightmeter <hello@lightmeter.io>
//
// SPDX-License-Identifier: AGPL-3.0-only
package main
import (
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"gitlab.com/lightmeter/controlcenter/auth"
"gitlab.com/lightmeter/controlcenter/config"
"gitlab.com/lightmeter/controlcenter/lmsqlite3"
"gitlab.com/lightmeter/controlcenter/logeater/announcer"
"gitlab.com/lightmeter/controlcenter/logeater/dirlogsource"
"gitlab.com/lightmeter/controlcenter/logeater/dirwatcher"
"gitlab.com/lightmeter/controlcenter/logeater/filelogsource"
"gitlab.com/lightmeter/controlcenter/logeater/logsource"
"gitlab.com/lightmeter/controlcenter/logeater/socketsource"
"gitlab.com/lightmeter/controlcenter/logeater/transform"
"gitlab.com/lightmeter/controlcenter/pkg/runner"
"gitlab.com/lightmeter/controlcenter/server"
"gitlab.com/lightmeter/controlcenter/subcommand"
"gitlab.com/lightmeter/controlcenter/tracking"
"gitlab.com/lightmeter/controlcenter/util/errorutil"
"gitlab.com/lightmeter/controlcenter/util/timeutil"
"gitlab.com/lightmeter/controlcenter/version"
"gitlab.com/lightmeter/controlcenter/workspace"
)
func changeUserInfo(conf config.Config) {
if !(len(conf.ChangeUserInfoNewEmail) > 0 || len(conf.ChangeUserInfoNewName) > 0 || len(conf.PasswordToReset) > 0) {
errorutil.Dief(nil, "No new user info to be changed")
}
subcommand.PerformUserInfoChange(
conf.WorkspaceDirectory, conf.EmailToChange,
conf.ChangeUserInfoNewEmail, conf.ChangeUserInfoNewName,
conf.PasswordToReset,
)
}
func main() {
conf, err := config.Parse(os.Args[1:], os.LookupEnv)
if err != nil {
errorutil.Dief(errorutil.Wrap(err), "Could not parse command-line arguments or environment variables")
}
if conf.GenerateDovecotConfig {
setupDovecotConfig(conf.DovecotConfigIsOld)
return
}
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339}).With().Str("service", "controlcenter").Caller().Logger()
zerolog.SetGlobalLevel(conf.LogLevel)
if conf.ShowVersion {
version.PrintVersion()
return
}
liabilityDisclaimer := `This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions; see here for details: https://lightmeter.io/lmcc-license.`
log.Info().Msg(liabilityDisclaimer)
lmsqlite3.Initialize(lmsqlite3.Options{})
if len(conf.EmailToChange) > 0 {
changeUserInfo(conf)
return
}
ws, logReader, err := buildWorkspaceAndLogReader(conf)
if err != nil {
errorutil.Dief(errorutil.Wrap(err), "Error creating / opening workspace directory for storing application files: %s. Try specifying a different directory (using -workspace), or check you have permission to write to the specified location.", conf.WorkspaceDirectory)
}
done, cancel := runner.Run(ws)
// only import logs and exit when they end. Does not start web server.
// It's useful for benchmarking importing logs.
if conf.ImportOnly {
err := logReader.Run()
if err != nil {
errorutil.Dief(err, "Error reading logs")
}
cancel()
err = done()
errorutil.MustSucceed(err)
log.Info().Msg("Importing has finished. Bye!")
return
}
// from here on, workspace is never cancellable!
go func() {
err := done()
errorutil.Dief(err, "Error: Workspace execution has ended, which should never happen here!")
}()
go func() {
err := logReader.Run()
if err != nil {
errorutil.Dief(err, "Error reading logs")
}
}()
httpServer := server.HttpServer{
Workspace: ws,
WorkspaceDirectory: conf.WorkspaceDirectory,
Timezone: conf.Timezone,
Address: conf.Address,
IsBehindReverseProxy: !conf.IKnowWhatIAmDoingNotUsingAReverseProxy,
}
errorutil.MustSucceed(httpServer.Start(), "server died")
}
func buildAuthOptions(conf config.Config) auth.Options {
if len(conf.RegisteredUserEmail) == 0 || len(conf.RegisteredUserName) == 0 || len(conf.RegisteredUserPassword) == 0 {
return auth.Options{AllowMultipleUsers: false, PlainAuthOptions: nil}
}
log.Info().Msgf("Using user information from environment/command-line. This is VERY experimental: %v -> %v",
conf.RegisteredUserEmail, conf.RegisteredUserName)
return auth.Options{
AllowMultipleUsers: false,
PlainAuthOptions: &auth.PlainAuthOptions{
Email: conf.RegisteredUserEmail,
Name: conf.RegisteredUserName,
Password: conf.RegisteredUserPassword,
},
}
}
func buildWorkspaceAndLogReader(conf config.Config) (*workspace.Workspace, logsource.Reader, error) {
nodeTypeHandler, err := tracking.BuildNodeTypeHandler(conf.MultiNodeType)
if err != nil {
return nil, logsource.Reader{}, errorutil.Wrap(err)
}
options := &workspace.Options{
IsUsingRsyncedLogs: conf.RsyncedDir,
DefaultSettings: conf.DefaultSettings,
AuthOptions: buildAuthOptions(conf),
NodeTypeHandler: nodeTypeHandler,
DataRetentionDuration: conf.DataRetentionDuration,
}
ws, err := workspace.NewWorkspace(conf.WorkspaceDirectory, options)
if err != nil {
return nil, logsource.Reader{}, errorutil.Wrap(err)
}
logSource, err := buildLogSource(ws, conf)
if err != nil {
return nil, logsource.Reader{}, errorutil.Wrap(err)
}
logReader := logsource.NewReader(logSource, ws.NewPublisher())
return ws, logReader, nil
}
func buildLogSource(ws *workspace.Workspace, conf config.Config) (logsource.Source, error) {
clock := &timeutil.RealClock{}
firstAnnouncer, err := ws.ImportAnnouncer()
if err != nil {
return nil, errorutil.Wrap(err)
}
announcerUsed := false
nextAnnouncer := func() announcer.ImportAnnouncer {
// only the first used source can notify progress.
// All the others use a fake one.
if !announcerUsed {
announcerUsed = true
return firstAnnouncer
}
return &announcer.EmptyImportAnnouncer{}
}
patterns := func(patterns []string) dirwatcher.LogPatterns {
if len(patterns) == 0 {
return dirwatcher.DefaultLogPatterns
}
return dirwatcher.BuildLogPatterns(patterns)
}(conf.LogPatterns)
var sources logsource.ComposedSource
sum, err := ws.MostRecentLogTimeAndSum()
if err != nil {
return nil, errorutil.Wrap(err)
}
for _, dir := range conf.DirsToWatch {
s, err := dirlogsource.New(dir, sum, nextAnnouncer(), !conf.ImportOnly, conf.RsyncedDir, conf.LogFormat, patterns, clock)
if err != nil {
return nil, errorutil.Wrap(err)
}
sources = append(sources, s)
}
builder, err := transform.Get(conf.LogFormat, clock, conf.LogYear)
if err != nil {
return nil, errorutil.Wrap(err)
}
if conf.ShouldWatchFromStdin {
s, err := filelogsource.New(os.Stdin, builder, nextAnnouncer())
if err != nil {
return nil, errorutil.Wrap(err)
}
sources = append(sources, s)
}
if len(conf.Socket) > 0 {
s, err := socketsource.New(conf.Socket, builder, nextAnnouncer())
if err != nil {
return nil, errorutil.Wrap(err)
}
sources = append(sources, s)
}
if len(sources) == 0 {
errorutil.Dief(nil, "No logs sources specified or import flag provided! Use -help to more info.")
}
return sources, nil
}