forked from remind101/empire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processes.go
205 lines (166 loc) · 4.91 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
package empire
import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/remind101/empire/internal/shellwords"
"github.com/remind101/empire/pkg/constraints"
"github.com/remind101/empire/procfile"
)
// DefaultQuantities maps a process type to the default number of instances to
// run.
var DefaultQuantities = map[string]int{
"web": 1,
}
// Command represents a command and it's arguments. For example:
type Command []string
// ParseCommand parses a string into a Command, taking quotes and other shell
// words into account. For example:
func ParseCommand(command string) (Command, error) {
return shellwords.Parse(command)
}
// MustParseCommand parses the string into a Command, panicing if there's an
// error. This method should only be used in tests for convenience.
func MustParseCommand(command string) Command {
c, err := ParseCommand(command)
if err != nil {
panic(err)
}
return c
}
// Scan implements the sql.Scanner interface.
func (c *Command) Scan(src interface{}) error {
bytes, ok := src.([]byte)
if !ok {
return error(errors.New("Scan source was not []bytes"))
}
var cmd Command
if err := json.Unmarshal(bytes, &cmd); err != nil {
return err
}
*c = cmd
return nil
}
// Value implements the driver.Value interface.
func (c Command) Value() (driver.Value, error) {
raw, err := json.Marshal(c)
if err != nil {
return nil, err
}
return driver.Value(raw), nil
}
// String returns the string reprsentation of the command.
func (c Command) String() string {
return strings.Join([]string(c), " ")
}
// Process holds configuration information about a Process.
type Process struct {
// Command is the command to run.
Command Command `json:"Command,omitempty"`
// Signifies that this is a named one off command and not a long lived
// service.
NoService bool `json:"Run,omitempty"`
// Quantity is the desired number of instances of this process.
Quantity int `json:"Quantity,omitempty"`
// The memory constraints, in bytes.
Memory constraints.Memory `json:"Memory,omitempty"`
// The amount of CPU share to give.
CPUShare constraints.CPUShare `json:"CPUShare,omitempty"`
// The allow number of unix processes within the container.
Nproc constraints.Nproc `json:"Nproc,omitempty"`
// A cron expression. If provided, the process will be run as a
// scheduled task.
Cron *string `json:"cron,omitempty"`
// Port mappings from container to load balancer.
Ports []Port `json:"Ports,omitempty"`
// An process specific environment variables.
Environment map[string]string `json:"Environment,omitempty"`
// ECS specific parameters.
ECS *procfile.ECS `json:"ECS,omitempty"`
}
type Port struct {
Host int `json:"Host"`
Container int `json:"Container"`
Protocol string `json:"Protocol"`
}
// IsValid returns nil if the Process is valid.
func (p *Process) IsValid() error {
// Ensure that processes marked as NoService can't be scaled up.
if p.NoService {
if p.Quantity != 0 {
return errors.New("non-service processes cannot be scaled up")
}
}
return nil
}
// Constraints returns a constraints.Constraints from this Process definition.
func (p *Process) Constraints() Constraints {
return Constraints{
Memory: p.Memory,
CPUShare: p.CPUShare,
Nproc: p.Nproc,
}
}
// SetConstraints sets the memory/cpu/nproc for this Process to the given
// constraints.
func (p *Process) SetConstraints(c Constraints) {
p.Memory = c.Memory
p.CPUShare = c.CPUShare
p.Nproc = c.Nproc
}
// Formation represents a collection of named processes and their configuration.
type Formation map[string]Process
// IsValid returns nil if all of the Processes are valid.
func (f Formation) IsValid() error {
for n, p := range f {
if err := p.IsValid(); err != nil {
return fmt.Errorf("process %s is not valid: %v", n, err)
}
}
return nil
}
// Scan implements the sql.Scanner interface.
func (f *Formation) Scan(src interface{}) error {
bytes, ok := src.([]byte)
if !ok {
return error(errors.New("Scan source was not []bytes"))
}
formation := make(Formation)
if err := json.Unmarshal(bytes, &formation); err != nil {
return err
}
*f = formation
return nil
}
// Value implements the driver.Value interface.
func (f Formation) Value() (driver.Value, error) {
if f == nil {
return nil, nil
}
raw, err := json.Marshal(f)
if err != nil {
return nil, err
}
return driver.Value(raw), nil
}
// Merge merges in the existing quantity and constraints from the old Formation
// into this Formation.
func (f Formation) Merge(other Formation) Formation {
new := make(Formation)
for name, p := range f {
if existing, found := other[name]; found {
// If the existing Formation already had a process
// configuration for this process type, copy over the
// instance count.
p.Quantity = existing.Quantity
p.SetConstraints(existing.Constraints())
} else {
p.Quantity = DefaultQuantities[name]
p.SetConstraints(DefaultConstraints)
}
new[name] = p
}
return new
}