forked from remind101/empire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
236 lines (192 loc) · 4.88 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
package empire
import (
"fmt"
"log"
"strings"
)
// RunEvent is triggered when a user starts a one off process.
type RunEvent struct {
User string
App string
Command string
Attached bool
}
func (e RunEvent) Event() string {
return "run"
}
func (e RunEvent) String() string {
attachment := "detached"
if e.Attached {
attachment = "attached"
}
return fmt.Sprintf("%s ran `%s` (%s) on %s", e.User, e.Command, attachment, e.App)
}
// RestartEvent is triggered when a user restarts an application.
type RestartEvent struct {
User string
App string
PID string
}
func (e RestartEvent) Event() string {
return "restart"
}
func (e RestartEvent) String() string {
if e.PID == "" {
return fmt.Sprintf("%s restarted %s", e.User, e.App)
}
return fmt.Sprintf("%s restarted `%s` on %s", e.User, e.PID, e.App)
}
// ScaleEvent is triggered when a manual scaling event happens.
type ScaleEvent struct {
User string
App string
Process string
Quantity int
PreviousQuantity int
}
func (e ScaleEvent) Event() string {
return "scale"
}
func (e ScaleEvent) String() string {
delta := e.Quantity - e.PreviousQuantity
return fmt.Sprintf("%s scaled `%s` on %s from %d to %d (%s)", e.User, e.Process, e.App, e.PreviousQuantity, e.Quantity, prettyDelta{delta: delta})
}
// DeployEvent is triggered when a user deploys a new image to an app.
type DeployEvent struct {
User string
App string
Image string
}
func (e DeployEvent) Event() string {
return "deploy"
}
func (e DeployEvent) String() string {
if e.App == "" {
return fmt.Sprintf("%s deployed %s", e.User, e.Image)
}
return fmt.Sprintf("%s deployed %s to %s", e.User, e.Image, e.App)
}
// RollbackEvent is triggered when a user rolls back to an old version.
type RollbackEvent struct {
User string
App string
Version int
}
func (e RollbackEvent) Event() string {
return "rollback"
}
func (e RollbackEvent) String() string {
return fmt.Sprintf("%s rolled back %s to v%d", e.User, e.App, e.Version)
}
// SetEvent is triggered when environment variables are changed on an
// application.
type SetEvent struct {
User string
App string
Changed []string
}
func (e SetEvent) Event() string {
return "set"
}
func (e SetEvent) String() string {
return fmt.Sprintf("%s changed environment variables on %s (%s)", e.User, e.App, strings.Join(e.Changed, ", "))
}
// CreateEvent is triggered when a user creates a new application.
type CreateEvent struct {
User string
Name string
}
func (e CreateEvent) Event() string {
return "create"
}
func (e CreateEvent) String() string {
return fmt.Sprintf("%s created %s", e.User, e.Name)
}
// DestroyEvent is triggered when a user destroys an application.
type DestroyEvent struct {
User string
App string
}
func (e DestroyEvent) Event() string {
return "destroy"
}
func (e DestroyEvent) String() string {
return fmt.Sprintf("%s destroyed %s", e.User, e.App)
}
// 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
}
// 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
})
// AsyncEventStream wraps an EventStream to publish events asynchronously in a
// goroutine.
type AsyncEventStream struct {
EventStream
events chan Event
}
// AsyncEvents returns a new AsyncEventStream that will buffer upto 100 events
// before applying backpressure.
func AsyncEvents(e EventStream) *AsyncEventStream {
s := &AsyncEventStream{
EventStream: 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.EventStream.PublishEvent(event)
return
}
// prettyDelta is a fmt.Stringer that formats a delta by prefixing it with a `+`
// or `-`.
type prettyDelta struct {
delta int
}
// String implements the fmt.Stringer interface.
func (d prettyDelta) String() string {
if d.delta > 0 {
return fmt.Sprintf("+%d", d.delta)
}
if d.delta < 0 {
return fmt.Sprintf("-%d", d.delta*-1)
}
return fmt.Sprintf("no change")
}