-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
539 lines (495 loc) · 17.8 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
package main
import (
"context"
"flag"
"fmt"
"log/slog"
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/gofri/go-github-ratelimit/github_ratelimit"
"github.com/veertuinc/anklet/internal/anka"
"github.com/veertuinc/anklet/internal/config"
"github.com/veertuinc/anklet/internal/database"
"github.com/veertuinc/anklet/internal/logging"
"github.com/veertuinc/anklet/internal/metrics"
"github.com/veertuinc/anklet/internal/run"
)
var (
version = "dev"
runOnce = "false"
versionFlag = flag.Bool("version", false, "Print the version")
configFlag = flag.String("c", "", "Path to the config file (defaults to ~/.config/anklet/config.yml)")
// signalFlag = flag.String("s", "", `Send signal to the daemon:
// drain — graceful shutdown, will wait until all jobs finish before exiting
// stop — best effort graceful shutdown, interrupting the job as soon as possible`)
// attachFlag = flag.Bool("attach", false, "Attach to the anklet and don't background it (useful for containers)")
// stop = make(chan struct{})
// done = make(chan struct{})
shutDownMessage = "anklet plugin shut down"
)
// func termHandler(ctx context.Context, logger *slog.Logger) daemon.SignalHandlerFunc {
// return func(sig os.Signal) error {
// logger.WarnContext(ctx, "terminating anklet, please do not interrupt...")
// stop <- struct{}{}
// if sig == syscall.SIGQUIT {
// <-done
// }
// return daemon.ErrStop
// }
// }
func main() {
parentLogger := logging.New()
parentCtx := context.Background()
if version == "" {
version = "dev" // Default version if not set by go build
}
flag.Parse()
if *versionFlag {
fmt.Println(version)
os.Exit(0)
}
// daemon.AddCommand(daemon.StringFlag(signalFlag, "drain"), syscall.SIGQUIT, termHandler(parentCtx, logger))
// daemon.AddCommand(daemon.StringFlag(signalFlag, "stop"), syscall.SIGTERM, termHandler(parentCtx, logger))
homeDir, err := os.UserHomeDir()
if err != nil {
parentLogger.ErrorContext(parentCtx, "unable to get user home directory", "error", err)
os.Exit(1)
}
var configPath string
var configFileName string
if *configFlag != "" {
configPath = *configFlag
} else {
envConfigFileName := os.Getenv("ANKLET_CONFIG_FILE_NAME")
if envConfigFileName != "" {
configFileName = envConfigFileName
} else {
configFileName = "config.yml"
}
configPath = filepath.Join(homeDir, ".config", "anklet", configFileName)
}
parentCtx = context.WithValue(parentCtx, config.ContextKey("configFileName"), configFileName)
// obtain config
loadedConfig, err := config.LoadConfig(configPath)
if err != nil {
parentLogger.ErrorContext(parentCtx, "unable to load config.yml (is it in the work_dir, or are you using an absolute path?)", "error", err)
os.Exit(1)
}
loadedConfig, err = config.LoadInEnvs(loadedConfig)
if err != nil {
parentLogger.ErrorContext(parentCtx, "unable to load config.yml from environment variables", "error", err)
os.Exit(1)
}
parentLogger.InfoContext(parentCtx, "loaded config", slog.Any("config", loadedConfig))
parentCtx = logging.AppendCtx(parentCtx, slog.String("ankletVersion", version))
var suffix string
if loadedConfig.Metrics.Aggregator {
suffix = "-aggregator"
}
parentCtx = context.WithValue(parentCtx, config.ContextKey("suffix"), suffix)
if loadedConfig.Log.FileDir != "" {
if !strings.HasSuffix(loadedConfig.Log.FileDir, "/") {
loadedConfig.Log.FileDir += "/"
}
if _, err := os.Stat(loadedConfig.Log.FileDir); os.IsNotExist(err) {
parentLogger.ErrorContext(parentCtx, "log directory does not exist", "directory", loadedConfig.Log.FileDir)
os.Exit(1)
}
// logger, fileLocation, err = logging.UpdateLoggerToFile(logger, logFileDir, suffix)
// if err != nil {
// fmt.Printf("{\"time\":\"%s\",\"level\":\"ERROR\",\"msg\":\"%s\"}\n", time.Now().Format(time.RFC3339), err)
// os.Exit(1)
// }
// logger.InfoContext(parentCtx, "writing logs to file", slog.String("fileLocation", fileLocation))
}
// must come after the log config is handled
parentCtx = context.WithValue(parentCtx, config.ContextKey("logger"), parentLogger)
// if loadedConfig.PidFileDir == "" {
// loadedConfig.PidFileDir = "./"
// }
if loadedConfig.WorkDir == "" {
loadedConfig.WorkDir = "./"
}
// Handle setting defaults for receiver plugins
for index, plugin := range loadedConfig.Plugins {
if strings.Contains(plugin.Plugin, "_receiver") {
if plugin.RedeliverHours == 0 {
loadedConfig.Plugins[index].RedeliverHours = 24
}
if loadedConfig.GlobalReceiverSecret != "" {
loadedConfig.Plugins[index].Secret = loadedConfig.GlobalReceiverSecret
}
}
}
parentLogger.DebugContext(parentCtx, "loaded config", slog.Any("config", loadedConfig))
parentCtx = context.WithValue(parentCtx, config.ContextKey("config"), &loadedConfig)
// daemonContext := &daemon.Context{
// PidFileName: loadedConfig.PidFileDir + "anklet" + suffix + ".pid",
// PidFilePerm: 0644,
// LogFileName: loadedConfig.Log.FileDir + "anklet" + suffix + ".log",
// LogFilePerm: 0640,
// WorkDir: loadedConfig.WorkDir,
// Umask: 027,
// Args: []string{"anklet", "-c", configPath},
// }
// if len(daemon.ActiveFlags()) > 0 {
// d, err := daemonContext.Search()
// if err != nil {
// log.Fatalf("Unable send signal to the daemon: %s", err.Error())
// }
// err = daemon.SendCommands(d)
// if err != nil {
// log.Fatalln(err.Error())
// }
// return
// }
var pluginsPath string
if loadedConfig.PluginsPath != "" {
pluginsPath = loadedConfig.PluginsPath
} else {
pluginsPath = filepath.Join(homeDir, ".config", "anklet", "plugins")
}
parentLogger.InfoContext(parentCtx, "plugins path", slog.String("pluginsPath", pluginsPath))
parentCtx = context.WithValue(parentCtx, config.ContextKey("globals"), config.Globals{
RunOnce: runOnce,
PullLock: &sync.Mutex{},
PluginsPath: pluginsPath,
DebugEnabled: logging.IsDebugEnabled(),
})
httpTransport := http.DefaultTransport
parentCtx = context.WithValue(parentCtx, config.ContextKey("httpTransport"), httpTransport)
githubPluginExists := false
for _, plugin := range loadedConfig.Plugins {
if plugin.Plugin == "github" || plugin.Plugin == "github_receiver" {
githubPluginExists = true
}
}
if githubPluginExists {
rateLimiter, err := github_ratelimit.NewRateLimitWaiterClient(httpTransport)
if err != nil {
parentLogger.ErrorContext(parentCtx, "error creating github_ratelimit.NewRateLimitWaiterClient", "err", err)
os.Exit(1)
}
parentCtx = context.WithValue(parentCtx, config.ContextKey("rateLimiter"), rateLimiter)
}
// if !*attachFlag {
// d, err := daemonContext.Reborn()
// if err != nil {
// log.Fatalln(err)
// }
// if d != nil {
// return
// }
// defer daemonContext.Release()
// }
// Capture ctrl+c and handle sending cancellation
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
//go
worker(parentCtx, parentLogger, loadedConfig, sigChan)
// err = daemon.ServeSignals()
// if err != nil {
// log.Printf("Error: %s", err.Error())
// }
}
func worker(
parentCtx context.Context,
parentLogger *slog.Logger,
loadedConfig config.Config,
sigChan chan os.Signal,
) {
globals, err := config.GetGlobalsFromContext(parentCtx)
if err != nil {
parentLogger.ErrorContext(parentCtx, "unable to get globals from context", "error", err)
os.Exit(1)
}
toRunOnce := globals.RunOnce
workerCtx, workerCancel := context.WithCancel(parentCtx)
suffix := parentCtx.Value(config.ContextKey("suffix")).(string)
parentLogger.InfoContext(workerCtx, "starting anklet"+suffix)
returnToMainQueue := make(chan bool, 1)
jobFailureChannel := make(chan bool, 1)
workerCtx = context.WithValue(workerCtx, config.ContextKey("returnToMainQueue"), returnToMainQueue)
workerCtx = context.WithValue(workerCtx, config.ContextKey("jobFailureChannel"), jobFailureChannel)
var wg sync.WaitGroup
go func() {
defer signal.Stop(sigChan)
defer close(sigChan)
for sig := range sigChan {
switch sig {
// case syscall.SIGTERM:
// logger.WarnContext(workerCtx, "best effort graceful shutdown, interrupting the job as soon as possible...")
// workerCancel()
case syscall.SIGQUIT: // doesn't work for receivers since they don't loop
parentLogger.WarnContext(workerCtx, "graceful shutdown, waiting for jobs to finish...")
toRunOnce = "true"
default:
parentLogger.WarnContext(workerCtx, "best effort graceful shutdown, interrupting the job as soon as possible...")
workerCancel()
returnToMainQueue <- true
}
}
}()
// set global database variables
var databaseURL string
var databasePort int
var databaseUser string
var databasePassword string
var databaseDatabase int
if loadedConfig.GlobalDatabaseURL != "" {
databaseURL = loadedConfig.GlobalDatabaseURL
databasePort = loadedConfig.GlobalDatabasePort
databaseUser = loadedConfig.GlobalDatabaseUser
databasePassword = loadedConfig.GlobalDatabasePassword
databaseDatabase = loadedConfig.GlobalDatabaseDatabase
}
// TODO: move this into a function/different file
// Setup Metrics Server and context
metricsPort := "8080"
if loadedConfig.Metrics.Port != "" {
metricsPort = loadedConfig.Metrics.Port
} else {
for {
ln, err := net.Listen("tcp", ":"+metricsPort)
if err == nil {
ln.Close()
break
}
port, _ := strconv.Atoi(metricsPort)
port++
metricsPort = strconv.Itoa(port)
}
}
ln, err := net.Listen("tcp", ":"+metricsPort)
if err != nil {
parentLogger.ErrorContext(workerCtx, "metrics port already in use", "port", metricsPort, "error", err)
os.Exit(1)
}
ln.Close()
metricsService := metrics.NewServer(metricsPort)
if loadedConfig.Metrics.Aggregator {
if databaseURL == "" { // if no global database URL is set, use the metrics database URL
databaseURL = loadedConfig.Metrics.Database.URL
databasePort = loadedConfig.Metrics.Database.Port
databaseUser = loadedConfig.Metrics.Database.User
databasePassword = loadedConfig.Metrics.Database.Password
databaseDatabase = loadedConfig.Metrics.Database.Database
}
databaseContainer, err := database.NewClient(workerCtx, config.Database{
URL: databaseURL,
Port: databasePort,
User: databaseUser,
Password: databasePassword,
Database: databaseDatabase,
})
if err != nil {
parentLogger.ErrorContext(workerCtx, "unable to access database", "error", err)
os.Exit(1)
}
workerCtx = context.WithValue(workerCtx, config.ContextKey("database"), databaseContainer)
go metricsService.StartAggregatorServer(workerCtx, parentLogger, false)
parentLogger.InfoContext(workerCtx, "metrics aggregator started on port "+metricsPort)
wg.Add(1)
defer wg.Done()
pluginCtx, pluginCancel := context.WithCancel(workerCtx) // Inherit from parent context
for {
select {
case <-workerCtx.Done():
pluginCancel()
parentLogger.WarnContext(pluginCtx, shutDownMessage)
return
default:
if workerCtx.Err() != nil || toRunOnce == "true" {
pluginCancel()
break
}
select {
case <-time.After(time.Duration(loadedConfig.Metrics.SleepInterval) * time.Second):
case <-pluginCtx.Done():
break
}
}
}
} else {
// firstPluginStarted: always make sure the first plugin in the config starts first before any others.
// this allows users to mix a receiver with multiple other plugins,
// and let the receiver do its thing to prepare the db first.
firstPluginStarted := make(chan bool, 1)
metricsData := &metrics.MetricsDataLock{}
workerCtx = context.WithValue(workerCtx, config.ContextKey("metrics"), metricsData)
parentLogger.InfoContext(workerCtx, "metrics server started on port "+metricsPort)
metrics.UpdateSystemMetrics(workerCtx, parentLogger, metricsData)
/////////////
// Plugins
soloReceiver := false
for index, plugin := range loadedConfig.Plugins {
wg.Add(1)
if index != 0 {
waitLoop:
for {
select {
case <-firstPluginStarted:
break waitLoop
case <-workerCtx.Done():
return
default:
time.Sleep(100 * time.Millisecond)
}
}
}
go func(plugin config.Plugin) {
defer wg.Done()
pluginCtx, pluginCancel := context.WithCancel(workerCtx) // Inherit from parent context
if plugin.Name == "" {
parentLogger.ErrorContext(pluginCtx, "name is required for plugins")
pluginCancel()
workerCancel()
return
}
if strings.Contains(plugin.Name, " ") {
parentLogger.ErrorContext(pluginCtx, "plugin name cannot contain spaces")
pluginCancel()
workerCancel()
return
}
// support plugin specific log files
var pluginLogger = parentLogger
if loadedConfig.Log.SplitByPlugin {
parentLogger.InfoContext(parentCtx, "writing "+plugin.Name+" logs to "+loadedConfig.Log.FileDir+"anklet"+"-"+plugin.Name+".log")
pluginLogger, _, err = logging.UpdateLoggerToFile(parentLogger, loadedConfig.Log.FileDir, "-"+plugin.Name)
if err != nil {
parentLogger.ErrorContext(pluginCtx, "unable to update logger to file", "error", err)
pluginCancel()
workerCancel()
return
}
}
// must come after the log config is handled
pluginCtx = context.WithValue(pluginCtx, config.ContextKey("logger"), pluginLogger)
pluginCtx = logging.AppendCtx(pluginCtx, slog.String("pluginName", plugin.Name))
if plugin.Repo == "" {
pluginLogger.InfoContext(pluginCtx, "no repo set for plugin; assuming it's an organization level plugin")
pluginCtx = context.WithValue(pluginCtx, config.ContextKey("isRepoSet"), false)
logging.DevContext(pluginCtx, "set isRepoSet to false")
} else {
pluginCtx = context.WithValue(pluginCtx, config.ContextKey("isRepoSet"), true)
logging.DevContext(pluginCtx, "set isRepoSet to true")
}
if plugin.PrivateKey == "" && loadedConfig.GlobalPrivateKey != "" {
logging.DevContext(pluginCtx, "using global private key")
plugin.PrivateKey = loadedConfig.GlobalPrivateKey
}
// keep this here or the changes to plugin don't get set in the pluginCtx
pluginCtx = context.WithValue(pluginCtx, config.ContextKey("plugin"), plugin)
if !strings.Contains(plugin.Plugin, "_receiver") {
logging.DevContext(pluginCtx, "plugin is not a receiver; loading the anka CLI")
ankaCLI, err := anka.NewCLI(pluginCtx)
if err != nil {
pluginLogger.ErrorContext(pluginCtx, "unable to create anka cli", "error", err)
pluginCancel()
workerCancel()
return
}
pluginCtx = context.WithValue(pluginCtx, config.ContextKey("ankacli"), ankaCLI)
logging.DevContext(pluginCtx, "loaded the anka CLI")
}
if databaseURL != "" || plugin.Database.URL != "" {
if databaseURL == "" {
databaseURL = plugin.Database.URL
databasePort = plugin.Database.Port
databaseUser = plugin.Database.User
databasePassword = plugin.Database.Password
databaseDatabase = plugin.Database.Database
}
logging.DevContext(pluginCtx, "connecting to database")
databaseClient, err := database.NewClient(pluginCtx, config.Database{
URL: databaseURL,
Port: databasePort,
User: databaseUser,
Password: databasePassword,
Database: databaseDatabase,
})
if err != nil {
pluginLogger.ErrorContext(pluginCtx, "unable to access database", "error", err)
pluginCancel()
workerCancel()
return
}
pluginCtx = context.WithValue(pluginCtx, config.ContextKey("database"), databaseClient)
// cleanup metrics data when the plugin is stopped (otherwise it's orphaned in the aggregator)
if index == 0 { // only cleanup the first plugin's metrics data (they're aggregated by the first plugin's name)
defer metrics.Cleanup(pluginCtx, pluginLogger, plugin.Owner, plugin.Name)
}
logging.DevContext(pluginCtx, "connected to database")
}
pluginLogger.InfoContext(pluginCtx, "starting plugin")
for {
select {
case <-pluginCtx.Done():
logging.DevContext(pluginCtx, "plugin for loop::pluginCtx.Done()")
metricsData.SetStatus(pluginCtx, pluginLogger, "stopped")
pluginLogger.WarnContext(pluginCtx, shutDownMessage)
pluginCancel()
return
default:
updatedPluginCtx, err := run.Plugin(
workerCtx,
pluginCtx,
pluginCancel,
pluginLogger,
firstPluginStarted,
metricsData,
)
if err != nil {
pluginLogger.ErrorContext(updatedPluginCtx, "error running plugin", "error", err)
pluginCancel()
workerCancel()
// Send SIGQUIT to the main pid
p, err := os.FindProcess(os.Getpid())
if err != nil {
pluginLogger.ErrorContext(updatedPluginCtx, "error finding process", "error", err)
} else {
err = p.Signal(syscall.SIGQUIT)
if err != nil {
pluginLogger.ErrorContext(updatedPluginCtx, "error sending SIGQUIT signal", "error", err)
}
}
return
}
if workerCtx.Err() != nil || toRunOnce == "true" {
pluginLogger.WarnContext(updatedPluginCtx, shutDownMessage)
pluginCancel()
return
}
metricsData.SetStatus(updatedPluginCtx, pluginLogger, "idle")
select {
case <-time.After(time.Duration(plugin.SleepInterval) * time.Second):
case <-pluginCtx.Done():
logging.DevContext(pluginCtx, "plugin for loop::default::pluginCtx.Done()")
break
}
}
}
}(plugin)
// if the only service is a receiver, set the soloReceiver flag to true
if strings.Contains(plugin.Plugin, "_receiver") {
soloReceiver = true
} else { // otherwise disable it if other plugins exist
soloReceiver = false
}
}
go metricsService.Start(workerCtx, parentLogger, soloReceiver)
}
wg.Wait()
time.Sleep(time.Second) // prevents exiting before the logger has a chance to write the final log entry (from panics)
parentLogger.WarnContext(parentCtx, "anklet (and all plugins) shut down")
os.Exit(0)
}