2023-03-25 18:27:01 +01:00
|
|
|
package app
|
2023-03-23 21:14:53 +01:00
|
|
|
|
|
|
|
import (
|
2023-10-04 12:00:00 +02:00
|
|
|
"encoding/json"
|
2023-03-24 00:27:51 +01:00
|
|
|
"fmt"
|
2024-01-04 12:00:00 +01:00
|
|
|
"log"
|
2023-03-23 21:14:53 +01:00
|
|
|
"os"
|
2023-03-24 00:27:51 +01:00
|
|
|
"regexp"
|
2024-01-05 12:00:00 +01:00
|
|
|
"runtime"
|
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
|
|
|
|
2023-10-12 12:00:00 +02:00
|
|
|
"framagit.org/ppom/reaction/logger"
|
|
|
|
|
2023-10-04 12:00:00 +02:00
|
|
|
"github.com/google/go-jsonnet"
|
2024-02-24 11:01:50 +01:00
|
|
|
"golang.org/x/exp/slices"
|
2023-03-23 21:14:53 +01:00
|
|
|
)
|
|
|
|
|
2023-03-24 00:27:51 +01:00
|
|
|
func (c *Conf) setup() {
|
2024-01-05 12:00:00 +01:00
|
|
|
if c.Concurrency == 0 {
|
|
|
|
c.Concurrency = runtime.NumCPU()
|
|
|
|
}
|
2023-08-21 23:33:56 +02:00
|
|
|
|
|
|
|
for patternName := range c.Patterns {
|
|
|
|
pattern := c.Patterns[patternName]
|
|
|
|
pattern.name = patternName
|
|
|
|
pattern.nameWithBraces = fmt.Sprintf("<%s>", pattern.name)
|
|
|
|
|
|
|
|
if pattern.Regex == "" {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Fatalf("Bad configuration: pattern's regex %v is empty!", patternName)
|
2023-08-21 23:33:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
compiled, err := regexp.Compile(fmt.Sprintf("^%v$", pattern.Regex))
|
|
|
|
if err != nil {
|
2024-01-04 12:00:00 +01:00
|
|
|
logger.Fatalf("Bad configuration: pattern %v: %v", patternName, err)
|
2023-08-21 23:33:56 +02:00
|
|
|
}
|
|
|
|
c.Patterns[patternName].Regex = fmt.Sprintf("(?P<%s>%s)", patternName, pattern.Regex)
|
|
|
|
for _, ignore := range pattern.Ignore {
|
|
|
|
if !compiled.MatchString(ignore) {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Fatalf("Bad configuration: pattern ignore '%v' doesn't match pattern %v! It should be fixed or removed.", ignore, pattern.nameWithBraces)
|
2023-08-21 23:33:56 +02:00
|
|
|
}
|
|
|
|
}
|
2024-02-02 15:21:29 +01:00
|
|
|
|
|
|
|
// Compile ignore regexes
|
|
|
|
for _, regex := range pattern.IgnoreRegex {
|
|
|
|
// Enclose the regex to make sure that it matches the whole detected string
|
|
|
|
compiledRegex, err := regexp.Compile("^" + regex + "$")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("%vBad configuration: in ignoreregex of pattern %s: %v", logger.FATAL, pattern.name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pattern.compiledIgnoreRegex = append(pattern.compiledIgnoreRegex, *compiledRegex)
|
|
|
|
}
|
2023-03-24 17:36:41 +01:00
|
|
|
}
|
2023-08-21 23:33:56 +02:00
|
|
|
|
2023-04-12 09:53:44 +02:00
|
|
|
if len(c.Streams) == 0 {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Fatalln("Bad configuration: no streams configured!")
|
2023-04-12 09:53:44 +02:00
|
|
|
}
|
2023-03-24 17:36:41 +01:00
|
|
|
for streamName := range c.Streams {
|
|
|
|
|
|
|
|
stream := c.Streams[streamName]
|
|
|
|
stream.name = streamName
|
|
|
|
|
2023-09-08 15:18:04 +02:00
|
|
|
if strings.Contains(stream.name, ".") {
|
2023-10-22 12:00:00 +02:00
|
|
|
logger.Fatalf("Bad configuration: character '.' is not allowed in stream names: '%v'", stream.name)
|
2023-09-08 15:18:04 +02:00
|
|
|
}
|
|
|
|
|
2023-04-12 09:53:44 +02:00
|
|
|
if len(stream.Filters) == 0 {
|
2023-10-22 12:00:00 +02:00
|
|
|
logger.Fatalf("Bad configuration: no filters configured in %v", stream.name)
|
2023-04-12 09:53:44 +02:00
|
|
|
}
|
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-24 00:49:59 +01:00
|
|
|
|
2023-09-08 15:18:04 +02:00
|
|
|
if strings.Contains(filter.name, ".") {
|
2024-04-08 19:54:02 +02:00
|
|
|
logger.Fatalf("Bad configuration: character '.' is not allowed in filter names: '%v'", filter.name)
|
2023-09-08 15:18:04 +02:00
|
|
|
}
|
2023-03-24 00:49:59 +01:00
|
|
|
// Parse Duration
|
2023-05-05 16:30:35 +02:00
|
|
|
if filter.RetryPeriod == "" {
|
|
|
|
if filter.Retry > 1 {
|
2024-04-08 19:54:02 +02:00
|
|
|
logger.Fatalf("Bad configuration: retry but no retryperiod in %v.%v", stream.name, filter.name)
|
2023-05-05 16:30:35 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
retryDuration, err := time.ParseDuration(filter.RetryPeriod)
|
|
|
|
if err != nil {
|
2024-04-08 19:54:02 +02:00
|
|
|
logger.Fatalf("Bad configuration: Failed to parse retry time in %v.%v: %v", stream.name, filter.name, err)
|
2023-05-05 16:30:35 +02:00
|
|
|
}
|
|
|
|
filter.retryDuration = retryDuration
|
2023-03-24 00:49:59 +01:00
|
|
|
}
|
|
|
|
|
2023-04-12 09:53:44 +02:00
|
|
|
if len(filter.Regex) == 0 {
|
2023-10-22 12:00:00 +02:00
|
|
|
logger.Fatalf("Bad configuration: no regexes configured in %v.%v", stream.name, filter.name)
|
2023-04-12 09:53:44 +02:00
|
|
|
}
|
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 {
|
2024-02-24 11:01:50 +01:00
|
|
|
for _, pattern := range c.Patterns {
|
2023-08-21 23:33:56 +02:00
|
|
|
if strings.Contains(regex, pattern.nameWithBraces) {
|
2024-02-24 11:01:50 +01:00
|
|
|
if !slices.Contains(filter.pattern, pattern) {
|
|
|
|
filter.pattern = append(filter.pattern, pattern)
|
2023-03-24 17:36:41 +01:00
|
|
|
}
|
2023-08-21 23:33:56 +02:00
|
|
|
regex = strings.Replace(regex, pattern.nameWithBraces, pattern.Regex, 1)
|
2023-03-24 17:36:41 +01:00
|
|
|
}
|
|
|
|
}
|
2024-01-04 12:00:00 +01:00
|
|
|
compiledRegex, err := regexp.Compile(regex)
|
|
|
|
if err != nil {
|
2024-04-08 19:54:02 +02:00
|
|
|
log.Fatal("Bad configuration: regex of filter %s.%s: %v", stream.name, filter.name, err)
|
2024-01-04 12:00:00 +01:00
|
|
|
}
|
|
|
|
filter.compiledRegex = append(filter.compiledRegex, *compiledRegex)
|
2023-03-24 00:27:51 +01:00
|
|
|
}
|
2023-03-24 00:49:59 +01:00
|
|
|
|
2023-04-12 09:53:44 +02:00
|
|
|
if len(filter.Actions) == 0 {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Fatalln("Bad configuration: no actions configured in", stream.name, ".", filter.name)
|
2023-04-12 09:53:44 +02:00
|
|
|
}
|
2023-03-24 17:36:41 +01:00
|
|
|
for actionName := range filter.Actions {
|
2023-03-24 00:49:59 +01:00
|
|
|
|
2024-02-24 11:01:50 +01:00
|
|
|
action := filter.Actions[actionName]
|
2023-03-24 17:36:41 +01:00
|
|
|
action.filter = filter
|
2023-03-24 00:27:51 +01:00
|
|
|
action.name = actionName
|
2023-03-24 00:49:59 +01:00
|
|
|
|
2023-09-08 15:18:04 +02:00
|
|
|
if strings.Contains(action.name, ".") {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Fatalln("Bad configuration: character '.' is not allowed in action names", action.name)
|
2023-09-08 15:18:04 +02:00
|
|
|
}
|
2023-03-24 00:49:59 +01:00
|
|
|
// Parse Duration
|
|
|
|
if action.After != "" {
|
|
|
|
afterDuration, err := time.ParseDuration(action.After)
|
|
|
|
if err != nil {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Fatalln("Bad configuration: Failed to parse after time in ", stream.name, ".", filter.name, ".", action.name, ":", err)
|
2023-03-24 00:49:59 +01:00
|
|
|
}
|
|
|
|
action.afterDuration = afterDuration
|
2023-07-12 17:45:16 +02:00
|
|
|
} else if action.OnExit {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Fatalln("Bad configuration: Cannot have `onexit: true` without an `after` directive in", stream.name, ".", filter.name, ".", action.name)
|
2023-03-24 00:49:59 +01:00
|
|
|
}
|
2023-04-27 10:42:19 +02:00
|
|
|
if filter.longuestActionDuration == nil || filter.longuestActionDuration.Milliseconds() < action.afterDuration.Milliseconds() {
|
|
|
|
filter.longuestActionDuration = &action.afterDuration
|
|
|
|
}
|
2023-03-23 21:14:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-03 20:03:22 +02:00
|
|
|
func parseConf(filename string) *Conf {
|
2023-03-23 21:14:53 +01:00
|
|
|
|
2023-10-04 12:00:00 +02:00
|
|
|
data, err := os.Open(filename)
|
2023-03-23 21:14:53 +01:00
|
|
|
if err != nil {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Fatalln("Failed to read configuration file:", err)
|
2023-03-23 21:14:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var conf Conf
|
2023-10-04 12:00:00 +02:00
|
|
|
if filename[len(filename)-4:] == ".yml" || filename[len(filename)-5:] == ".yaml" {
|
|
|
|
err = jsonnet.NewYAMLToJSONDecoder(data).Decode(&conf)
|
2023-10-22 12:00:00 +02:00
|
|
|
if err != nil {
|
|
|
|
logger.Fatalln("Failed to parse yaml configuration file:", err)
|
|
|
|
}
|
2023-10-04 12:00:00 +02:00
|
|
|
} else {
|
|
|
|
var jsondata string
|
|
|
|
jsondata, err = jsonnet.MakeVM().EvaluateFile(filename)
|
|
|
|
if err == nil {
|
|
|
|
err = json.Unmarshal([]byte(jsondata), &conf)
|
|
|
|
}
|
2023-10-22 12:00:00 +02:00
|
|
|
if err != nil {
|
|
|
|
logger.Fatalln("Failed to parse json configuration file:", err)
|
|
|
|
}
|
2023-03-23 21:14:53 +01:00
|
|
|
}
|
|
|
|
|
2023-03-24 00:27:51 +01:00
|
|
|
conf.setup()
|
2023-05-01 18:21:31 +02:00
|
|
|
return &conf
|
2023-03-23 21:14:53 +01:00
|
|
|
}
|