reaction/app/conf.go

257 lines
5.9 KiB
Go
Raw Normal View History

2023-03-25 18:27:01 +01:00
package app
2023-03-23 21:14:53 +01:00
import (
2023-04-26 17:18:55 +02:00
"encoding/gob"
2023-03-24 00:27:51 +01:00
"fmt"
2023-04-26 17:18:55 +02:00
"io"
2023-03-23 21:14:53 +01:00
"log"
"os"
2023-03-24 00:27:51 +01:00
"regexp"
2023-03-24 17:36:41 +01:00
"strings"
2023-03-24 00:49:59 +01:00
"time"
2023-03-23 21:14:53 +01:00
"gopkg.in/yaml.v3"
)
type Conf struct {
Patterns map[string]string `yaml:"patterns"`
Streams map[string]*Stream `yaml:"streams"`
2023-03-24 00:27:51 +01:00
}
type Stream struct {
name string `yaml:"-"`
2023-03-24 17:36:41 +01:00
Cmd []string `yaml:"cmd"`
Filters map[string]*Filter `yaml:"filters"`
2023-03-24 00:27:51 +01:00
}
type Filter struct {
stream *Stream `yaml:"-"`
name string `yaml:"-"`
2023-03-24 17:36:41 +01:00
Regex []string `yaml:"regex"`
compiledRegex []regexp.Regexp `yaml:"-"`
patternName string `yaml:"-"`
patternWithBraces string `yaml:"-"`
2023-03-24 17:36:41 +01:00
Retry int `yaml:"retry"`
RetryPeriod string `yaml:"retry-period"`
retryDuration time.Duration `yaml:"-"`
2023-03-24 17:36:41 +01:00
2023-04-26 17:18:55 +02:00
Actions map[string]*Action `yaml:"actions"`
longuestActionDuration *time.Duration
matches map[string][]time.Time `yaml:"-"`
2023-03-24 00:27:51 +01:00
}
type Action struct {
filter *Filter `yaml:"-"`
name string `yaml:"-"`
2023-03-24 17:36:41 +01:00
Cmd []string `yaml:"cmd"`
2023-03-24 17:36:41 +01:00
After string `yaml:"after"`
afterDuration time.Duration `yaml:"-"`
2023-03-24 00:27:51 +01:00
}
2023-04-26 17:18:55 +02:00
type LogEntry struct {
t time.Time
pattern string
stream, filter string
exec bool
}
2023-03-24 00:27:51 +01:00
func (c *Conf) setup() {
2023-03-24 17:36:41 +01:00
for patternName, pattern := range c.Patterns {
c.Patterns[patternName] = fmt.Sprintf("(?P<%s>%s)", patternName, pattern)
}
if len(c.Streams) == 0 {
log.Fatalln("Bad configuration: no streams configured!")
}
2023-03-24 17:36:41 +01:00
for streamName := range c.Streams {
stream := c.Streams[streamName]
stream.name = streamName
if len(stream.Filters) == 0 {
log.Fatalln("Bad configuration: no filters configured in '%s'!", stream.name)
}
2023-03-24 17:36:41 +01:00
for filterName := range stream.Filters {
filter := stream.Filters[filterName]
filter.stream = stream
filter.name = filterName
2023-03-25 19:12:11 +01:00
filter.matches = make(map[string][]time.Time)
2023-03-24 00:49:59 +01:00
// Parse Duration
retryDuration, err := time.ParseDuration(filter.RetryPeriod)
if err != nil {
log.Fatalln("Failed to parse time in configuration file:", err)
}
filter.retryDuration = retryDuration
if len(filter.Regex) == 0 {
log.Fatalln("Bad configuration: no regexes configured in '%s.%s'!", stream.name, filter.name)
}
2023-03-24 00:27:51 +01:00
// Compute Regexes
2023-03-24 17:36:41 +01:00
// Look for Patterns inside Regexes
2023-03-24 00:27:51 +01:00
for _, regex := range filter.Regex {
2023-03-24 17:36:41 +01:00
for patternName, pattern := range c.Patterns {
if strings.Contains(regex, patternName) {
switch filter.patternName {
case "":
filter.patternName = patternName
2023-03-24 18:06:57 +01:00
filter.patternWithBraces = fmt.Sprintf("<%s>", patternName)
2023-03-24 17:36:41 +01:00
case patternName:
// no op
default:
log.Fatalf(
"ERROR Can't mix different patterns (%s, %s) in same filter (%s.%s)\n",
filter.patternName, patternName, streamName, filterName,
)
}
regex = strings.Replace(regex, fmt.Sprintf("<%s>", patternName), pattern, 1)
}
}
2023-03-24 00:27:51 +01:00
filter.compiledRegex = append(filter.compiledRegex, *regexp.MustCompile(regex))
}
2023-03-24 00:49:59 +01:00
if len(filter.Actions) == 0 {
log.Fatalln("Bad configuration: no actions configured in '%s.%s'!", stream.name, filter.name)
}
2023-03-24 17:36:41 +01:00
for actionName := range filter.Actions {
2023-03-24 00:49:59 +01:00
2023-03-24 17:36:41 +01:00
action := filter.Actions[actionName]
action.filter = filter
2023-03-24 00:27:51 +01:00
action.name = actionName
2023-03-24 00:49:59 +01:00
// Parse Duration
if action.After != "" {
afterDuration, err := time.ParseDuration(action.After)
if err != nil {
log.Fatalln("Failed to parse time in configuration file:", err)
}
action.afterDuration = afterDuration
}
2023-03-23 21:14:53 +01:00
}
}
}
}
2023-04-26 17:18:55 +02:00
var DBname = "./reaction.db"
var DBnewName = "./reaction.new.db"
func (c *Conf) updateFromDB() {
file, err := os.Open(DBname)
if err != nil {
if err == os.ErrNotExist {
log.Printf("WARN: No DB found at %s\n", DBname)
return
}
log.Fatalln("Failed to open DB:", err)
}
dec := gob.NewDecoder(&file)
newfile, err := os.Create(DBnewName)
if err != nil {
log.Fatalln("Failed to open DB:", err)
}
enc := gob.NewEncoder(&newfile)
// This extra code is made to warn only one time for each non-existant filter
type SF struct{ s, f string }
discardedEntries := make(map[SF]bool)
malformedEntries := 0
defer func() {
for sf, t := range discardedEntries {
if t {
log.Printf("WARN: info discarded from the DB: stream/filter not found: %s.%s\n", sf.s, sf.f)
}
}
if malformedEntries > 0 {
log.Printf("WARN: %v malformed entry discarded from the DB\n", malformedEntries)
}
}()
encodeOrFatal := func(entry LogEntry) {
err = enc.Encode(entry)
if err != nil {
log.Fatalln("ERRO: couldn't write to new DB:", err)
}
}
now := time.Now()
for {
var entry LogEntry
var filter *Filter
// decode entry
err = dec.Decode(&entry)
if err != nil {
if err == io.EOF {
return
}
malformedEntries++
continue
}
// retrieve related filter
if s := c.Streams[entry.stream]; stream != nil {
filter = stream.Filters[entry.filter]
}
if filter == nil {
discardedEntries[SF{entry.stream, entry.filter}] = true
continue
}
// store matches
if !entry.exec && entry.t+filter.retryDuration > now {
filter.matches[entry.pattern] = append(f.matches[entry.pattern], entry.t)
encodeOrFatal(entry)
}
// replay executions
if entry.exec && entry.t+filter.longuestActionDuration > now {
delete(filter.matches, match)
filter.execActions(match, now-entry.t)
encodeOrFatal(entry)
}
}
err = os.Rename(DBnewName, DBname)
if err != nil {
log.Fatalln("ERRO: Failed to replace old DB with new one:", err)
}
}
func openDB() {
f, err := os.OpenFile(DBname, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
log.Fatalln("Failed to open DB:", err)
}
return gob.NewEncoder(&f)
}
2023-03-23 21:14:53 +01:00
func parseConf(filename string) *Conf {
data, err := os.ReadFile(filename)
if err != nil {
log.Fatalln("Failed to read configuration file:", err)
}
var conf Conf
err = yaml.Unmarshal(data, &conf)
if err != nil {
log.Fatalln("Failed to parse configuration file:", err)
}
2023-03-24 00:27:51 +01:00
conf.setup()
2023-03-23 21:14:53 +01:00
return &conf
}