forked from remind101/empire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processes.go
246 lines (193 loc) · 5.21 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
package empire
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"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/procfile"
)
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 {
ReleaseID string
ID string
Type ProcessType
Quantity int
Command Command
Port int `sql:"-"`
Constraints
}
// 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
func commandMapFromProcfile(p procfile.Procfile) CommandMap {
cm := make(CommandMap)
for n, c := range p {
cm[ProcessType(n)] = Command(c)
}
return cm
}
// 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
}
// processesUpdate updates an existing process into the database.
func processesUpdate(db *gorm.DB, process *Process) error {
return db.Save(process).Error
}