forked from remind101/empire
/
processes.go
344 lines (273 loc) · 7.87 KB
/
processes.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
package empire
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"time"
"github.com/jinzhu/gorm"
"github.com/lib/pq/hstore"
. "github.com/remind101/empire/pkg/bytesize"
"github.com/remind101/empire/pkg/constraints"
"github.com/remind101/empire/pkg/service"
"golang.org/x/net/context"
)
var (
Constraints1X = Constraints{constraints.CPUShare(256), constraints.Memory(512 * MB)}
Constraints2X = Constraints{constraints.CPUShare(512), constraints.Memory(1 * GB)}
ConstraintsPX = Constraints{constraints.CPUShare(1024), constraints.Memory(6 * GB)}
// NamedConstraints maps a heroku dynos size to a Constraints.
NamedConstraints = map[string]Constraints{
"1X": Constraints1X,
"2X": Constraints2X,
"PX": ConstraintsPX,
}
// DefaultConstraints defaults to 1X process size.
DefaultConstraints = Constraints1X
)
// ProcessQuantityMap represents a map of process types to quantities.
type ProcessQuantityMap map[ProcessType]int
// DefaultQuantities maps a process type to the default number of instances to
// run.
var DefaultQuantities = ProcessQuantityMap{
"web": 1,
}
// ProcessType represents the type of a given process/command.
type ProcessType string
// Scan implements the sql.Scanner interface.
func (p *ProcessType) Scan(src interface{}) error {
if src, ok := src.([]byte); ok {
*p = ProcessType(src)
}
return nil
}
// Value implements the driver.Value interface.
func (p ProcessType) Value() (driver.Value, error) {
return driver.Value(string(p)), nil
}
// Command represents the actual shell command that gets executed for a given
// ProcessType.
type Command string
// Scan implements the sql.Scanner interface.
func (c *Command) Scan(src interface{}) error {
if src, ok := src.([]byte); ok {
*c = Command(src)
}
return nil
}
// Value implements the driver.Value interface.
func (c Command) Value() (driver.Value, error) {
return driver.Value(string(c)), nil
}
// Process holds configuration information about a Process Type.
type Process struct {
ID string
Type ProcessType
Quantity int
Command Command
Port int `sql:"-"`
Constraints
ReleaseID string
Release *Release
}
// NewProcess returns a new Process instance.
func NewProcess(t ProcessType, cmd Command) *Process {
return &Process{
Type: t,
Quantity: DefaultQuantities[t],
Command: cmd,
Constraints: DefaultConstraints,
}
}
// CommandMap maps a process ProcessType to a Command.
type CommandMap map[ProcessType]Command
// Scan implements the sql.Scanner interface.
func (cm *CommandMap) Scan(src interface{}) error {
h := hstore.Hstore{}
if err := h.Scan(src); err != nil {
return err
}
m := make(CommandMap)
for k, v := range h.Map {
m[ProcessType(k)] = Command(v.String)
}
*cm = m
return nil
}
// Value implements the driver.Value interface.
func (cm CommandMap) Value() (driver.Value, error) {
m := make(map[string]sql.NullString)
for k, v := range cm {
m[string(k)] = sql.NullString{
Valid: true,
String: string(v),
}
}
h := hstore.Hstore{
Map: m,
}
return h.Value()
}
// Constraints aliases constraints.Constraints to implement the
// sql.Scanner interface.
type Constraints constraints.Constraints
func parseConstraints(con string) (*Constraints, error) {
if con == "" {
return nil, nil
}
if n, ok := NamedConstraints[con]; ok {
return &n, nil
}
c, err := constraints.Parse(con)
if err != nil {
return nil, err
}
r := Constraints(c)
return &r, nil
}
func (c *Constraints) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
cc, err := parseConstraints(s)
if err != nil {
return err
}
if cc != nil {
*c = *cc
}
return nil
}
func (c Constraints) String() string {
for n, constraint := range NamedConstraints {
if c == constraint {
return n
}
}
return fmt.Sprintf("%d:%s", c.CPUShare, c.Memory)
}
// Formation maps a process ProcessType to a Process.
type Formation map[ProcessType]*Process
// NewFormation creates a new Formation based on an existing Formation and
// the available processes from a CommandMap.
func NewFormation(f Formation, cm CommandMap) Formation {
processes := make(Formation)
// Iterate through all of the available process types in the CommandMap.
for t, cmd := range cm {
p := NewProcess(t, cmd)
if existing, found := f[t]; found {
// If the existing Formation already had a process
// configuration for this process type, copy over the
// instance count.
p.Quantity = existing.Quantity
p.Constraints = existing.Constraints
}
processes[t] = p
}
return processes
}
// newFormation takes a slice of processes and returns a Formation.
func newFormation(p []*Process) Formation {
f := make(Formation)
for _, pp := range p {
f[pp.Type] = pp
}
return f
}
// Processes takes a Formation and returns a slice of the processes.
func (f Formation) Processes() []*Process {
var processes []*Process
for _, p := range f {
processes = append(processes, p)
}
return processes
}
// ProcessesQuery is a Scope implementation for common things to filter
// processes by.
type ProcessesQuery struct {
// If provided, finds only processes belonging to the given release.
Release *Release
}
// Scope implements the Scope interface.
func (q ProcessesQuery) Scope(db *gorm.DB) *gorm.DB {
var scope ComposedScope
if q.Release != nil {
scope = append(scope, FieldEquals("release_id", q.Release.ID))
}
return scope.Scope(db)
}
// Processes returns all processes matching the scope.
func (s *store) Processes(scope Scope) ([]*Process, error) {
var processes []*Process
return processes, s.Find(scope, &processes)
}
// Formation returns a Formation for the processes matching the scope.
func (s *store) Formation(scope Scope) (Formation, error) {
p, err := s.Processes(scope)
if err != nil {
return nil, err
}
return newFormation(p), nil
}
// ProcessesCreate persists the process.
func (s *store) ProcessesCreate(process *Process) (*Process, error) {
return processesCreate(s.db, process)
}
// ProcessesUpdate updates the process.
func (s *store) ProcessesUpdate(process *Process) error {
return processesUpdate(s.db, process)
}
// ProcessesCreate inserts a process into the database.
func processesCreate(db *gorm.DB, process *Process) (*Process, error) {
return process, db.Create(process).Error
}
// ProcessesUpdate updates an existing process into the database.
func processesUpdate(db *gorm.DB, process *Process) error {
return db.Save(process).Error
}
// ProcessState represents the state of a Process.
type ProcessState struct {
Name string
Command string
State string
UpdatedAt time.Time
Constraints Constraints
}
type processStatesService struct {
manager service.Manager
}
func (s *processStatesService) JobStatesByApp(ctx context.Context, app *App) ([]*ProcessState, error) {
var states []*ProcessState
instances, err := s.manager.Instances(ctx, app.ID)
if err != nil {
return states, err
}
for _, i := range instances {
states = append(states, processStateFromInstance(i))
}
return states, nil
}
// processStateFromInstance converts a service.Instance into a ProcessState.
// It pulls some of its data from empire specific environment variables if they have been set.
// Once ECS supports this data natively, we can stop doing this.
func processStateFromInstance(i *service.Instance) *ProcessState {
createdAt := i.UpdatedAt
if t, err := time.Parse(time.RFC3339, i.Process.Env["EMPIRE_CREATED_AT"]); err == nil {
createdAt = t
}
version := i.Process.Env["EMPIRE_RELEASE"]
if version == "" {
version = "v0"
}
return &ProcessState{
Name: fmt.Sprintf("%s.%s.%s", version, i.Process.Type, i.ID),
Command: i.Process.Command,
Constraints: Constraints{
CPUShare: constraints.CPUShare(i.Process.CPUShares),
Memory: constraints.Memory(i.Process.MemoryLimit),
},
State: i.State,
UpdatedAt: createdAt, // This is the best data we have, until ECS gives us UpdatedAt
}
}