-
Notifications
You must be signed in to change notification settings - Fork 179
/
manifest.go
448 lines (367 loc) · 10.6 KB
/
manifest.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
package manifest1
import (
"bufio"
"bytes"
"fmt"
"io"
"net"
"os"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/docker/go-units"
"github.com/twmb/algoimpl/go/graph"
"gopkg.in/yaml.v2"
)
var interpolationBracketRegex = regexp.MustCompile("\\$\\{([0-9A-Za-z_]*)\\}")
var interpolationDollarRegex = regexp.MustCompile("\\$([0-9A-Za-z_]+)")
type Manifest struct {
Version string `yaml:"version"`
Networks Networks `yaml:"networks,omitempty"`
Services map[string]Service `yaml:"services"`
}
// Load a Manifest from raw data
func Load(data []byte) (*Manifest, error) {
data, err := parseEnvVars(data)
if err != nil {
return nil, err
}
v, err := manifestVersion(data)
if err != nil {
return nil, err
}
m := &Manifest{Version: v}
switch v {
case "1":
if err := yaml.Unmarshal(data, &m.Services); err != nil {
return nil, fmt.Errorf("error loading manifest: %s", err)
}
case "2", "2.0", "2.1", "2.2":
if err := yaml.Unmarshal(data, m); err != nil {
return nil, fmt.Errorf("error loading manifest: %s", err)
}
default:
return nil, fmt.Errorf("unknown manifest version: %s", v)
}
for name, service := range m.Services {
service.Name = name
// there are two places in a docker-compose.yml to specify a dockerfile
// normalize (for caching) and complain if both are set
if service.Dockerfile != "" {
if service.Build.Dockerfile != "" {
return nil, fmt.Errorf("dockerfile specified twice for %s", name)
}
service.Build.Dockerfile = service.Dockerfile
service.Dockerfile = ""
}
// denormalize a bit
service.Networks = m.Networks
m.Services[name] = service
}
return m, nil
}
// Load a Manifest from a file
func LoadFile(path string) (*Manifest, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return Load(data)
}
func (m Manifest) Service(name string) (*Service, error) {
for _, s := range m.Services {
if s.Name == name {
return &s, nil
}
}
return nil, fmt.Errorf("no such service: %s", name)
}
func (m Manifest) Validate() []error {
regexValidCronLabel := regexp.MustCompile(`\A[a-zA-Z][-a-zA-Z0-9]{3,29}\z`)
errors := []error{}
for _, entry := range m.Services {
if strings.Contains(entry.Name, "_") {
errors = append(errors, fmt.Errorf("service name cannot contain an underscore: %s", entry.Name))
}
labels := entry.LabelsByPrefix("convox.cron")
for k := range labels {
parts := strings.Split(k, ".")
if len(parts) != 3 {
errors = append(errors, fmt.Errorf("Cron task is not valid (must be in format convox.cron.myjob)"))
}
name := parts[2]
if !regexValidCronLabel.MatchString(name) {
errors = append(errors, fmt.Errorf(
"Cron task %s is not valid (cron names can contain only alphanumeric characters, dashes and must be between 4 and 30 characters)",
name,
))
}
}
labels = entry.LabelsByPrefix("convox.health.interval")
for _, v := range labels {
i, err := strconv.Atoi(v)
if err != nil || i < 5 || i > 300 {
errors = append(errors, fmt.Errorf("convox.health.interval is invalid for %s, must be a number between 5 and 300", entry.Name))
}
}
labels = entry.LabelsByPrefix("convox.health.timeout")
for _, v := range labels {
i, err := strconv.Atoi(v)
if err != nil || i < 0 || i > 60 {
errors = append(errors, fmt.Errorf("convox.health.timeout is invalid for %s, must be a number between 0 and 60", entry.Name))
}
}
labels = entry.LabelsByPrefix("convox.health.threshold.healthy")
for _, v := range labels {
i, err := strconv.Atoi(v)
if err != nil || i < 2 || i > 10 {
errors = append(errors, fmt.Errorf("convox.health.threshold.healthy is invalid for %s, must be a number between 2 and 10", entry.Name))
}
}
labels = entry.LabelsByPrefix("convox.health.threshold.unhealthy")
for _, v := range labels {
i, err := strconv.Atoi(v)
if err != nil || i < 2 || i > 10 {
errors = append(errors, fmt.Errorf("convox.health.threshold.unhealthy is invalid for %s, must be a number between 2 and 10", entry.Name))
}
}
labels = entry.LabelsByPrefix("convox.draining.timeout")
for _, v := range labels {
i, err := strconv.Atoi(v)
if err != nil || i < 1 || i > 3600 {
errors = append(errors, fmt.Errorf("convox.draining.timeout for %s must be between 1 and 3600", entry.Name))
}
}
for _, l := range entry.Links {
ls, ok := m.Services[l]
if !ok {
errors = append(errors, fmt.Errorf("%s links to service: %s which does not exist", entry.Name, l))
}
if len(ls.Ports) == 0 {
errors = append(errors, fmt.Errorf("%s links to service: %s which does not expose any ports", entry.Name, l))
}
}
// test mem_limit: Docker requires a mem_limit of at least 4mb (or 0)
mem_min := Memory(units.MB * 4)
mem := entry.Memory
if mem < mem_min && mem != 0 { //Memory(0) {
e := fmt.Errorf("%s service has invalid mem_limit %#v bytes (%#v MB): should be either 0, or at least %#vMB",
entry.Name,
mem,
mem/units.MB,
mem_min/units.MB)
errors = append(errors, e)
}
// check that health check port is valid
if port, ok := entry.Labels["convox.health.port"]; ok {
pi, err := strconv.Atoi(port)
if err != nil {
errors = append(errors, err)
} else {
found := false
for _, p := range entry.Ports {
if p.Container == pi {
found = true
break
}
}
if !found {
errors = append(errors, fmt.Errorf("%s service has convox.health.port set to a port it does not declare", entry.Name))
}
}
}
}
return errors
}
// ExternalPorts returns a collection of ints of the Manifest's Services' external ports
func (m *Manifest) ExternalPorts() []int {
ports := []int{}
for _, service := range m.Services {
for _, port := range service.ExternalPorts() {
ports = append(ports, port.Balancer)
}
}
return ports
}
// InternalPorts returns a collection of ints of the Manifest's Services' internal ports
func (m *Manifest) InternalPorts() []int {
ports := []int{}
for _, service := range m.Services {
for _, port := range service.InternalPorts() {
ports = append(ports, port.Container)
}
}
return ports
}
// UDPPorts returns a collection of ints of the Manifest's Services' UDP ports
func (m *Manifest) UDPPorts() []int {
ports := []int{}
for _, service := range m.Services {
for _, port := range service.UDPPorts() {
ports = append(ports, port.Container)
}
}
return ports
}
// Find any port conflits that would prevent this manifest from running
// TODO - Doesn't find UDP port conflicts
func (m *Manifest) PortConflicts() ([]int, error) {
ext := m.ExternalPorts()
conflicts := make([]int, 0)
host := dockerHost()
for _, p := range ext {
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", host, p), 200*time.Millisecond)
if err == nil {
conflicts = append(conflicts, p)
defer conn.Close()
}
}
sort.Ints(conflicts)
return conflicts, nil
}
// Run Instantiate a Run object based on this manifest to be run via 'convox start'
func (m *Manifest) Run(dir, app string, opts RunOptions) Run {
return NewRun(*m, dir, app, opts)
}
func (m *Manifest) getDeps(root, dep string, deps map[string]bool) error {
deps[dep] = true
targetService, ok := m.Services[dep]
if !ok {
return fmt.Errorf("Dependency %s of %s not found in manifest", dep, root)
}
for _, x := range targetService.Links {
_, ok := deps[x]
if !ok {
deps[dep] = true
err := m.getDeps(root, x, deps)
if err != nil {
return err
}
}
}
return nil
}
// Return the Services of this Manifest in the order you should run them
func (m *Manifest) runOrder(target string) (Services, error) {
deps := make(map[string]bool)
if target != "" {
err := m.getDeps(target, target, deps)
if err != nil {
return nil, err
}
}
services := Services{}
// Make a directed graph
serviceGraph := graph.New(graph.Directed)
// Make a map of service names to graph nodes
nodes := make(map[string]graph.Node, 0)
// Make a map of service names to services
serviceMap := make(map[string]Service, 0)
// Sort the service names alphabetically
sortedNames := make([]string, 0, len(m.Services))
for key := range m.Services {
sortedNames = append(sortedNames, key)
}
sort.Strings(sortedNames)
// Map the service names to services
for _, key := range sortedNames {
service := m.Services[key]
serviceMap[service.Name] = service
nodes[service.Name] = serviceGraph.MakeNode()
}
// Make references from nodes back to the service names
for key, node := range nodes {
*node.Value = key
}
// Connect the nodes
for serviceName, node := range nodes {
service := serviceMap[serviceName]
for _, link := range service.Links {
err := serviceGraph.MakeEdge(nodes[link], node)
if err != nil {
return nil, err
}
}
}
// Do a topological sort on the DAG
sorted := serviceGraph.TopologicalSort()
// Populate services from the service name
for _, node := range sorted {
name := (*node.Value).(string)
// Only include the target and its dependencies if a target was specified
if target != "" {
if deps[name] || (name == target) {
services = append(services, serviceMap[name])
}
} else {
services = append(services, serviceMap[name])
}
}
return services, nil
}
// Shift all external ports and labels in this Manifest by the given amount and their shift labels
func (m *Manifest) Shift(shift int) error {
for _, service := range m.Services {
service.Labels.Shift(shift)
service.Ports.Shift(shift)
if ss, ok := service.Labels["convox.start.shift"]; ok {
shift, err := strconv.Atoi(ss)
if err != nil {
return fmt.Errorf("invalid shift: %s", ss)
}
service.Labels.Shift(shift)
service.Ports.Shift(shift)
}
}
return nil
}
func manifestVersion(data []byte) (string, error) {
var check struct {
Version string
}
if err := yaml.Unmarshal(data, &check); err != nil {
return "", fmt.Errorf("could not parse manifest: %s", err)
}
if check.Version != "" {
return check.Version, nil
}
return "1", nil
}
func parseEnvVars(data []byte) ([]byte, error) {
r := bytes.NewReader(data)
result := []byte{}
reader := bufio.NewReader(r)
for {
line, err := reader.ReadString('\n')
if err != nil && err != io.EOF {
return result, err
}
result = append(result, []byte(parseLine(line))...)
if err == io.EOF {
break
}
}
return result, nil
}
func (m *Manifest) Raw() ([]byte, error) {
return yaml.Marshal(m)
}
func (m Manifest) EntryNames() []string {
names := make([]string, len(m.Services))
x := 0
for k := range m.Services {
names[x] = k
x += 1
}
return names
}
func (m Manifest) BalancerResourceName(process string) string {
for _, b := range m.Balancers() {
if b.Entry.Name == process {
return b.ResourceName()
}
}
return ""
}