forked from remind101/empire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
391 lines (324 loc) · 7.86 KB
/
events.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
package empire
import (
"fmt"
"log"
"strings"
)
type multiError struct {
Errors []error
}
func (e *multiError) Error() string {
points := make([]string, len(e.Errors))
for i, err := range e.Errors {
points[i] = fmt.Sprintf("* %s", err)
}
return fmt.Sprintf(
"%d error(s) occurred:\n\n%s",
len(e.Errors), strings.Join(points, "\n"))
}
func appendCommitMessage(main, commit string) string {
output := main
if commit != "" {
output = fmt.Sprintf("%s: '%s'", main, commit)
}
return output
}
// RunEvent is triggered when a user starts or stops a one off process.
type RunEvent struct {
User string
App string
Command Command
URL string
Attached bool
Message string
Finished bool
app *App
}
func (e RunEvent) Event() string {
return "run"
}
func (e *RunEvent) Finish() {
e.Finished = true
}
func (e RunEvent) String() string {
attachment := "detached"
if e.Attached {
attachment = "attached"
}
action := "started running"
if e.Finished {
action = "ran"
}
msg := fmt.Sprintf("%s %s `%s` (%s) on %s", e.User, action, e.Command.String(), attachment, e.App)
if e.URL != "" {
msg = fmt.Sprintf("%s (<%s|logs>)", msg, e.URL)
}
return appendCommitMessage(msg, e.Message)
}
func (e RunEvent) GetApp() *App {
return e.app
}
// RestartEvent is triggered when a user restarts an application.
type RestartEvent struct {
User string
App string
PID string
Message string
app *App
}
func (e RestartEvent) Event() string {
return "restart"
}
func (e RestartEvent) String() string {
msg := ""
if e.PID == "" {
msg = fmt.Sprintf("%s restarted %s", e.User, e.App)
} else {
msg = fmt.Sprintf("%s restarted `%s` on %s", e.User, e.PID, e.App)
}
return appendCommitMessage(msg, e.Message)
}
func (e RestartEvent) GetApp() *App {
return e.app
}
type MaintenanceEvent struct {
User string
App string
Maintenance bool
Message string
app *App
}
func (e MaintenanceEvent) Event() string {
return "maintenance"
}
func (e MaintenanceEvent) String() string {
state := "disabled"
if e.Maintenance {
state = "enabled"
}
msg := fmt.Sprintf("%s %s maintenance mode on %s", e.User, state, e.App)
return appendCommitMessage(msg, e.Message)
}
func (e MaintenanceEvent) GetApp() *App {
return e.app
}
type ScaleEventUpdate struct {
Process string
Quantity int
PreviousQuantity int
Constraints Constraints
PreviousConstraints Constraints
}
// ScaleEvent is triggered when a manual scaling event happens.
type ScaleEvent struct {
User string
App string
Updates []*ScaleEventUpdate
Message string
app *App
}
func (e ScaleEvent) Event() string {
return "scale"
}
func (e ScaleEvent) String() string {
var msg, sep string
for _, up := range e.Updates {
// Deal with no new constraints by copying previous constraint settings.
newConstraints := up.Constraints
previousConstraints := up.PreviousConstraints
if newConstraints.CPUShare == 0 {
newConstraints.CPUShare = previousConstraints.CPUShare
}
if newConstraints.Memory == 0 {
newConstraints.Memory = previousConstraints.Memory
}
if newConstraints.Nproc == 0 {
newConstraints.Nproc = previousConstraints.Nproc
}
msg += fmt.Sprintf(
"%s%s scaled `%s` on %s from %d(%s) to %d(%s)",
sep,
e.User,
up.Process,
e.App,
up.PreviousQuantity,
up.PreviousConstraints,
up.Quantity,
newConstraints,
)
sep = "\n"
}
return appendCommitMessage(msg, e.Message)
}
func (e ScaleEvent) GetApp() *App {
return e.app
}
// DeployEvent is triggered when a user deploys a new image to an app.
type DeployEvent struct {
User string
App string
Image string
Environment string
Release int
Message string
app *App
}
func (e DeployEvent) Event() string {
return "deploy"
}
func (e DeployEvent) String() string {
msg := ""
if e.App == "" {
msg = fmt.Sprintf("%s deployed %s", e.User, e.Image)
} else {
msg = fmt.Sprintf("%s deployed %s to %s %s (v%d)", e.User, e.Image, e.App, e.Environment, e.Release)
}
return appendCommitMessage(msg, e.Message)
}
func (e DeployEvent) GetApp() *App {
return e.app
}
// RollbackEvent is triggered when a user rolls back to an old version.
type RollbackEvent struct {
User string
App string
Version int
Message string
app *App
}
func (e RollbackEvent) Event() string {
return "rollback"
}
func (e RollbackEvent) String() string {
msg := fmt.Sprintf("%s rolled back %s to v%d", e.User, e.App, e.Version)
return appendCommitMessage(msg, e.Message)
}
func (e RollbackEvent) GetApp() *App {
return e.app
}
// SetEvent is triggered when environment variables are changed on an
// application.
type SetEvent struct {
User string
App string
Changed []string
Message string
app *App
}
func (e SetEvent) Event() string {
return "set"
}
func (e SetEvent) String() string {
msg := fmt.Sprintf("%s changed environment variables on %s (%s)", e.User, e.App, strings.Join(e.Changed, ", "))
return appendCommitMessage(msg, e.Message)
}
func (e SetEvent) GetApp() *App {
return e.app
}
// CreateEvent is triggered when a user creates a new application.
type CreateEvent struct {
User string
Name string
Message string
}
func (e CreateEvent) Event() string {
return "create"
}
func (e CreateEvent) String() string {
msg := fmt.Sprintf("%s created %s", e.User, e.Name)
return appendCommitMessage(msg, e.Message)
}
// DestroyEvent is triggered when a user destroys an application.
type DestroyEvent struct {
User string
App string
Message string
}
func (e DestroyEvent) Event() string {
return "destroy"
}
func (e DestroyEvent) String() string {
msg := fmt.Sprintf("%s destroyed %s", e.User, e.App)
return appendCommitMessage(msg, e.Message)
}
// Event represents an event triggered within Empire.
type Event interface {
// Returns the name of the event.
Event() string
// Returns a human readable string about the event.
String() string
}
// AppEvent is an Event that relates to a specific App.
type AppEvent interface {
Event
GetApp() *App
}
// EventStream is an interface for publishing events that happen within
// Empire.
type EventStream interface {
PublishEvent(Event) error
}
// EventStreamFunc is a function that implements the Events interface.
type EventStreamFunc func(Event) error
func (fn EventStreamFunc) PublishEvent(event Event) error {
return fn(event)
}
// NullEventStream an events service that does nothing.
var NullEventStream = EventStreamFunc(func(event Event) error {
return nil
})
// MultiEventStream is an EventStream implementation that publishes the event to multiple EventStreams, returning any errors after publishing to all streams.
type MultiEventStream []EventStream
func (streams MultiEventStream) PublishEvent(e Event) error {
result := new(multiError)
for _, s := range streams {
if err := s.PublishEvent(e); err != nil {
result.Errors = append(result.Errors, err)
}
}
if len(result.Errors) == 0 {
return nil
}
return result
}
// asyncEventStream wraps an array of EventStreams to publish events
// asynchronously in a goroutine
type asyncEventStream struct {
e EventStream
events chan Event
}
// AsyncEvents returns a new AsyncEventStream that will buffer upto 100 events
// before applying backpressure.
func AsyncEvents(e EventStream) EventStream {
s := &asyncEventStream{
e: e,
events: make(chan Event, 100),
}
go s.start()
return s
}
func (e *asyncEventStream) PublishEvent(event Event) error {
e.events <- event
return nil
}
func (e *asyncEventStream) start() {
for event := range e.events {
err := e.publishEvent(event)
if err != nil {
log.Printf("event stream error: %v\n", err)
}
}
}
func (e *asyncEventStream) publishEvent(event Event) (err error) {
defer func() {
if v := recover(); v != nil {
var ok bool
if err, ok = v.(error); ok {
return
}
err = fmt.Errorf("panic: %v", v)
}
}()
err = e.e.PublishEvent(event)
return
}