forked from mihirsoni/odfe-monitor-cli
/
push.go
214 lines (198 loc) · 6.17 KB
/
push.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
package commands
import (
mapset "github.com/deckarep/golang-set"
"github.com/mihirsoni/odfe-monitor-cli/destination"
"github.com/mihirsoni/odfe-monitor-cli/monitor"
"github.com/mihirsoni/odfe-monitor-cli/utils"
log "github.com/sirupsen/logrus"
"gopkg.in/cheggaaa/pb.v1"
"github.com/spf13/cobra"
)
var push = &cobra.Command{
Use: "push",
Short: "Publish all changed to remote Elasticsearch",
Long: `This command will push all modification to elasticsearch cluster. Be careful on while passing --delete flag, there is no way to bring them back unless you've snapshot`,
Run: runPush,
}
//Default parallel request for cluster
const (
defaultLimit = 20
)
var deleteUnTracked bool
var dryRun bool
var submit bool
var bar *pb.ProgressBar
func init() {
push.Flags().BoolVar(&deleteUnTracked, "delete", false, "Delete un-tracked monitors from remote es cluster")
push.Flags().BoolVar(&dryRun, "dryRun", true, "Dry run monitor more detailed https://opendistro.github.io/for-elasticsearch-docs/docs/alerting/api/#run-monitor")
push.Flags().BoolVar(&submit, "submit", false, "Publish monitors to remote")
rootCmd.AddCommand(push)
}
func runPush(cmd *cobra.Command, args []string) {
destinations, err := destination.GetRemote(esClient)
check(err)
localMonitors, localMonitorSet, err := monitor.GetAllLocal(rootDir)
if err != nil {
log.Fatal("Unable to parse monitors from yaml files due to ", err)
}
remoteMonitors, remoteMonitorsSet, err := monitor.GetAllRemote(esClient, destinations)
check(err)
unTrackedMonitors := remoteMonitorsSet.Difference(localMonitorSet)
cliNewMonitors := localMonitorSet.Difference(remoteMonitorsSet)
cliManagedMonitors := remoteMonitorsSet.Intersect(localMonitorSet)
modifiedMonitors := mapset.NewSet()
cliManagedMonitorsIt := cliManagedMonitors.Iterator()
for cliManaged := range cliManagedMonitorsIt.C {
if isMonitorChanged(localMonitors[cliManaged.(string)], remoteMonitors[cliManaged.(string)]) == true {
modifiedMonitors.Add(cliManaged)
}
}
monitorsToBeUpdated := cliNewMonitors.Union(modifiedMonitors)
shouldDelete := deleteUnTracked && unTrackedMonitors.Cardinality() > 0
shouldUpdate := modifiedMonitors.Cardinality() > 0
shouldCreate := cliNewMonitors.Cardinality() > 0
if !shouldCreate && !shouldUpdate && !shouldDelete {
log.Info("All monitors are up-to-date with remote monitors")
return
}
var preparedMonitors map[string]monitor.Monitor
preparedMonitors = make(map[string]monitor.Monitor)
for currentMonitor := range monitorsToBeUpdated.Iterator().C {
monitorName := currentMonitor.(string)
localMonitor := localMonitors[monitorName]
err := localMonitor.Prepare(remoteMonitors[monitorName],
destinations,
!cliNewMonitors.Contains(monitorName), esClient.OdVersion)
check(err)
preparedMonitors[monitorName] = localMonitor
}
// RunAll monitor before making update to ensure they're valid
// All of these operations are sequential. Run, Modify, Create, Delete
if shouldCreate || shouldUpdate {
runMonitors(monitorsToBeUpdated, preparedMonitors)
if !submit {
log.Info("Use --submit argument to publish monitors")
return
}
}
if shouldUpdate {
log.Debug("Monitors to be updated in remote ", modifiedMonitors)
updateMonitors(modifiedMonitors, remoteMonitors, preparedMonitors)
}
if shouldCreate {
log.Debug("Monitors to be created in remote ", cliNewMonitors)
createMonitors(cliNewMonitors, preparedMonitors)
}
if shouldDelete {
log.Debug("Monitors to be deleted from remote ", unTrackedMonitors)
deleteMonitors(unTrackedMonitors, remoteMonitors)
}
log.Info("Done")
}
func updateMonitors(
updateMonitors mapset.Set,
remoteMonitors map[string]monitor.Monitor,
preparedMonitors map[string]monitor.Monitor) {
limiter := utils.NewLimiter(defaultLimit)
successfulUpdates := 0
if !Verbose {
bar = pb.StartNew(updateMonitors.Cardinality())
bar.Prefix("Updating monitors")
}
for newMonitor := range updateMonitors.Iterator().C {
monitorName := newMonitor.(string)
limiter.Execute(func() {
log.Debug("Updating monitor: ", monitorName)
currentMonitor := preparedMonitors[monitorName]
err := currentMonitor.Update(esClient)
if err == nil {
successfulUpdates++
} else {
log.Debug(err)
}
if !Verbose {
bar.Increment()
}
})
}
limiter.Wait()
if !Verbose {
bar.Finish()
}
}
func createMonitors(newMonitors mapset.Set, preparedMonitors map[string]monitor.Monitor) {
limiter := utils.NewLimiter(defaultLimit)
successfullCreate := 0
if !Verbose {
bar = pb.StartNew(newMonitors.Cardinality())
bar.Prefix("Creating monitors")
}
for newMonitor := range newMonitors.Iterator().C {
monitorName := newMonitor.(string)
limiter.Execute(func() {
log.Debug("Creating monitor: ", monitorName)
newMonitor := preparedMonitors[monitorName]
err := newMonitor.Create(esClient)
if err == nil {
successfullCreate++
} else {
log.Debug(err)
}
if !Verbose {
bar.Increment()
}
})
}
limiter.Wait()
if !Verbose {
bar.Finish()
}
}
func runMonitors(monitorsToBeUpdated mapset.Set, preparedMonitors map[string]monitor.Monitor) {
limiter := utils.NewLimiter(defaultLimit)
if !Verbose {
bar = pb.StartNew(monitorsToBeUpdated.Cardinality())
bar.Prefix("Running monitors")
}
for currentMonitor := range monitorsToBeUpdated.Iterator().C {
monitorName := currentMonitor.(string)
limiter.Execute(func() {
log.Debug("Running monitor: ", monitorName)
runMonitor := preparedMonitors[monitorName]
err := runMonitor.Run(esClient, dryRun)
check(err)
if !Verbose {
bar.Increment()
}
})
}
limiter.Wait()
if !Verbose {
bar.Finish()
}
}
func deleteMonitors(monitorsToBeDeleted mapset.Set, remoteMonitors map[string]monitor.Monitor) {
limiter := utils.NewLimiter(defaultLimit)
successfulDelete := 0
if !Verbose {
bar = pb.StartNew(monitorsToBeDeleted.Cardinality())
bar.Prefix("Deleting monitors")
}
for currentMonitor := range monitorsToBeDeleted.Iterator().C {
monitorName := currentMonitor.(string)
remoteMonitor := remoteMonitors[monitorName]
limiter.Execute(func() {
err := remoteMonitor.Delete(esClient)
if err == nil {
successfulDelete++
}
if !Verbose {
bar.Increment()
}
})
}
limiter.Wait()
if !Verbose {
bar.Finish()
}
}