-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.go
101 lines (76 loc) · 2.43 KB
/
helpers.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
package main
import (
"regexp"
"strings"
log "github.com/sirupsen/logrus"
)
// type instanceState string
// func (py instanceState) Split(str string) (string, string, error) {
// s := strings.Split(string(py), str)
// if len(s) < 2 {
// return "", "", errors.New("Minimum match not found")
// }
// return s[0], s[1], nil
// }
// Used to support multiple --region flags and represent as slice
type regionArrayFromFlags []string
func (i *regionArrayFromFlags) String() string {
return strings.Join(*i, ",")
}
func (i *regionArrayFromFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
func newInstanceEvent(key string, awsRegion string) (bool, error) {
res, err := application.DB.HGetAll([]byte(key)) //, "code")
if err != nil {
return false, err
}
// Key not not exist there for its a new event and must be handled
if len(res) == 0 {
log.WithFields(log.Fields{
"instanceID": string(key),
"awsProfile": application.Config.AWS.Profile,
"awsRegion": awsRegion,
}).Info("Detected instance event")
return true, nil
}
return false, nil
}
func trimEventDescription(event string, awsRegion string, instanceID string) string {
re := regexp.MustCompile("(^The instance\\s)")
// Do we have a match ?
matched := re.MatchString(event)
// Clean up event by removing state prefix
// Eg: 'The instance is running on degraded hardware' -> 'is running on degraded hardware'
afterMatch := re.ReplaceAllString(event, "")
if matched {
log.WithFields(log.Fields{
"instanceID": instanceID,
"awsProfile": application.Config.AWS.Profile,
"awsRegion": awsRegion,
"beforeMatch": event,
"postMatch": afterMatch,
}).Debug("Trimed issue description prefix")
}
return afterMatch
}
func maintenanceComplete(event string, awsRegion string, instanceID string) (bool, string) {
pattern := "(^\\[Completed]\\s)"
re := regexp.MustCompile(pattern)
// Do we have a match ?
matched := re.MatchString(event)
// Clean up event by removing state prefix
// Eg: '[Completed] The instance is running on degraded hardware' --> 'The instance is running on degraded hardware'
afterMatch := re.ReplaceAllString(event, "")
if matched {
log.WithFields(log.Fields{
"instanceID": instanceID,
"awsProfile": application.Config.AWS.Profile,
"awsRegion": awsRegion,
"beforeMatch": event,
"postMatch": afterMatch,
}).Debug("Remove [Completed] from description")
}
return matched, afterMatch
}