forked from cyverse-de/interapps-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcompose.go
577 lines (504 loc) · 17.5 KB
/
dcompose.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
package main
import (
"crypto/sha256"
"fmt"
"net/url"
"os"
"path"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/spf13/viper"
"gopkg.in/cyverse-de/model.v3"
)
// WORKDIR is the path to the working directory inside all of the containers
// that are run as part of a job.
const WORKDIR = "/de-app-work"
// CONFIGDIR is the path to the local configs inside the containers that are
// used to transfer files into and out of the job.
const CONFIGDIR = "/configs"
// VOLUMEDIR is the name of the directory that is used for the working directory
// volume.
const VOLUMEDIR = "workingvolume"
// TMPDIR is the name of the directory that will be mounted into the container
// as the /tmp directory.
const TMPDIR = "tmpfiles"
const (
// UploadExcludesFilename is the filename for the list of files that should
// not be uploaded when the app is finished executing.
UploadExcludesFilename string = "porklock-upload-exclusions.txt"
// TypeLabel is the label key applied to every container.
TypeLabel = "org.iplantc.containertype"
// InputContainer is the value used in the TypeLabel for input containers.
InputContainer = iota
// DataContainer is the value used in the TypeLabel for data containers.
DataContainer
// StepContainer is the value used in the TypeLabel for step containers.
StepContainer
// OutputContainer is the value used in the TypeLabel for output containers.
OutputContainer
)
var (
logdriver string
hostworkingdir string
)
// JobComposition is the top-level type for what will become a job's docker-compose
// file.
type JobComposition struct {
Version string `yaml:"version"`
Volumes map[string]*Volume
Networks map[string]*Network `yaml:",omitempty"`
Services map[string]*Service
}
// NewJobComposition returns a newly instantiated *JobComposition instance.
func NewJobComposition(ld string, pathprefix string) (*JobComposition, error) {
wd, err := os.Getwd()
if err != nil {
return nil, errors.Wrap(err, "failed to get host working directory")
}
logdriver = ld
hostworkingdir = strings.TrimPrefix(wd, pathprefix)
if strings.HasPrefix(hostworkingdir, "/") {
hostworkingdir = strings.TrimPrefix(hostworkingdir, "/")
}
return &JobComposition{
Version: "2.2",
Volumes: make(map[string]*Volume),
Networks: make(map[string]*Network),
Services: make(map[string]*Service),
}, nil
}
// Composer orchestrates the creation of the docker-compose file for a job.
type Composer struct {
job *model.Job
composition *JobComposition
ingressID string // This shouldn't be accessed directly. Use IngressID().
porklockImage string
porklockTag string
memoryLimit int64
maxCPUCores float64
frontendBaseURL string
}
// NewComposer returns a new *Composer.
func NewComposer(job *model.Job, cfg *viper.Viper, logDriver, pathPrefix string) (*Composer, error) {
c, err := NewJobComposition(logDriver, pathPrefix)
if err != nil {
return nil, err
}
return &Composer{
job: job,
composition: c,
porklockImage: cfg.GetString(ConfigPorklockImageKey),
porklockTag: cfg.GetString(ConfigPorklockTagKey),
memoryLimit: cfg.GetInt64(ConfigMemoryLimitKey),
maxCPUCores: cfg.GetFloat64(ConfigMaxCPUCoresKey),
frontendBaseURL: cfg.GetString(ConfigFrontendBaseKey),
}, nil
}
// IngressID returns the name/identifier for the ingress in k8s.
func (c *Composer) IngressID() string {
if c.ingressID == "" {
c.ingressID = fmt.Sprintf("a%x", sha256.Sum256([]byte(fmt.Sprintf("%s%s", c.job.UserID, c.job.InvocationID))))[0:9]
}
return c.ingressID
}
// FrontendURL generates the full URL to to the running app, as shown to the
// user and accessed through a browser.
func (c *Composer) FrontendURL(step *model.Step) (string, error) {
var (
unmodifiedURL string
fURL *url.URL
err error
)
if step.Component.Container.InteractiveApps.FrontendURL != "" {
unmodifiedURL = step.Component.Container.InteractiveApps.FrontendURL
} else {
unmodifiedURL = c.frontendBaseURL
}
fURL, err = url.Parse(unmodifiedURL)
if err != nil {
return "", errors.Wrapf(err, "error parsing URL %s", unmodifiedURL)
}
fURLPort := fURL.Port()
fURLHost := fmt.Sprintf("%s.%s", c.IngressID(), fURL.Hostname())
if fURLPort != "" {
fURL.Host = fmt.Sprintf("%s:%s", fURLHost, fURLPort)
} else {
fURL.Host = fURLHost
}
return fURL.String(), nil
}
// Volume is a Docker volume definition in the Docker compose file.
type Volume struct {
Driver string
Options map[string]string `yaml:"driver_opts"`
}
// Network is a Docker network definition in the docker-compose file.
type Network struct {
Driver string
// EnableIPv6 bool `yaml:"enable_ipv6"`
DriverOpts map[string]string `yaml:"driver_opts"`
}
// LoggingConfig configures the logging for a docker-compose service.
type LoggingConfig struct {
Driver string
Options map[string]string `yaml:"options,omitempty"`
}
// ServiceNetworkConfig configures a docker-compose service to use a Docker
// Network.
type ServiceNetworkConfig struct {
Aliases []string `yaml:",omitempty"`
}
// Service configures a docker-compose service.
type Service struct {
CapAdd []string `yaml:"cap_add,flow"`
CapDrop []string `yaml:"cap_drop,flow"`
Command []string `yaml:",omitempty"`
ContainerName string `yaml:"container_name,omitempty"`
CPUs string `yaml:"cpus,omitempty"`
CPUSet string `yaml:"cpuset,omitempty"`
CPUShares int64 `yaml:"cpu_shares,omitempty"`
CPUQuota string `yaml:"cpu_quota,omitempty"`
DependsOn []string `yaml:"depends_on,omitempty"`
Devices []string `yaml:",omitempty"`
DNS []string `yaml:",omitempty"`
DNSSearch []string `yaml:"dns_search,omitempty"`
TMPFS []string `yaml:",omitempty"`
EntryPoint string `yaml:",omitempty"`
Environment map[string]string `yaml:",omitempty"`
Expose []string `yaml:",omitempty"`
Image string
Labels map[string]string `yaml:",omitempty"`
Logging *LoggingConfig `yaml:",omitempty"`
MemLimit string `yaml:"mem_limit,omitempty"`
MemSwapLimit string `yaml:"memswap_limit,omitempty"`
MemSwappiness string `yaml:"mem_swappiness,omitempty"`
NetworkMode string `yaml:"network_mode,omitempty"`
Networks map[string]*ServiceNetworkConfig `yaml:",omitempty"`
PIDsLimit int64 `yaml:"pids_limit,omitempty"`
Ports []string `yaml:",omitempty"`
Volumes []string `yaml:",omitempty"`
VolumesFrom []string `yaml:"volumes_from,omitempty"`
WorkingDir string `yaml:"working_dir,omitempty"`
}
// InitFromJob fills out values as appropriate for running in the DE's Condor
// Cluster.
func (c *Composer) InitFromJob(workingdir string, availablePort int) error {
var err error
workingVolumeHostPath := path.Join(workingdir, VOLUMEDIR)
// The volume containing the local working directory
porklockImageName := fmt.Sprintf("%s:%s", c.porklockImage, c.porklockTag)
for index, dc := range job.DataContainers() {
svcKey := fmt.Sprintf("data_%d", index)
c.composition.Services[svcKey] = &Service{
Image: fmt.Sprintf("%s:%s", dc.Name, dc.Tag),
ContainerName: fmt.Sprintf("%s-%s", dc.NamePrefix, job.InvocationID),
EntryPoint: "/bin/true",
Logging: &LoggingConfig{Driver: "none"},
Labels: map[string]string{
model.DockerLabelKey: strconv.Itoa(DataContainer),
},
}
svc := c.composition.Services[svcKey]
if dc.HostPath != "" || dc.ContainerPath != "" {
var rw string
if dc.ReadOnly {
rw = "ro"
} else {
rw = "rw"
}
svc.Volumes = []string{
fmt.Sprintf("%s:%s:%s", dc.HostPath, dc.ContainerPath, rw),
}
}
}
for index, input := range job.Inputs() {
c.composition.Services[fmt.Sprintf("input_%d", index)] = &Service{
CapAdd: []string{"IPC_LOCK"},
Image: porklockImageName,
Command: input.Arguments(job.Submitter, job.FileMetadata),
Environment: map[string]string{
"VAULT_ADDR": "${VAULT_ADDR}",
"VAULT_TOKEN": "${VAULT_TOKEN}",
"JOB_UUID": job.InvocationID,
},
WorkingDir: WORKDIR,
Volumes: []string{
strings.Join([]string{workingVolumeHostPath, WORKDIR, "rw"}, ":"),
},
Labels: map[string]string{
model.DockerLabelKey: strconv.Itoa(InputContainer),
},
}
}
// Add the steps to the docker-compose file.
for index, step := range job.Steps {
stepcfg := &ConvertStepParams{
Step: &step,
Index: index,
User: job.Submitter,
UserID: job.UserID,
InvID: job.InvocationID,
WorkingDirHostPath: workingVolumeHostPath,
AvailablePort: availablePort,
}
if err = c.ConvertStep(stepcfg); err != nil {
return err
}
}
// Add the final output job
excludesPath := path.Join(workingdir, UploadExcludesFilename)
excludesMount := path.Join(CONFIGDIR, UploadExcludesFilename)
c.composition.Services["upload_outputs"] = &Service{
CapAdd: []string{"IPC_LOCK"},
Image: porklockImageName,
Command: job.FinalOutputArguments(excludesMount),
Environment: map[string]string{
"VAULT_ADDR": "${VAULT_ADDR}",
"VAULT_TOKEN": "${VAULT_TOKEN}",
"JOB_UUID": job.InvocationID,
},
WorkingDir: WORKDIR,
Volumes: []string{
strings.Join([]string{workingVolumeHostPath, WORKDIR, "rw"}, ":"),
strings.Join([]string{excludesPath, excludesMount, "ro"}, ":"),
},
Labels: map[string]string{
model.DockerLabelKey: job.InvocationID,
TypeLabel: strconv.Itoa(OutputContainer),
},
}
return nil
}
func websocketURL(step *model.Step, backendURL string) (string, error) {
var websocketURL string
// if step.Component.Container.InteractiveApps.WebsocketPath != "" ||
// step.Component.Container.InteractiveApps.WebsocketProto != "" ||
// step.Component.Container.InteractiveApps.WebsocketPort != "" {
burl, err := url.Parse(backendURL)
if err != nil {
return "", errors.Wrapf(err, "couldn't parse URL %s", backendURL)
}
var wsPath, wsProto, wsPort string
if step.Component.Container.InteractiveApps.WebsocketPath != "" {
wsPath = step.Component.Container.InteractiveApps.WebsocketPath
} else {
wsPath = burl.Path
}
if step.Component.Container.InteractiveApps.WebsocketProto != "" {
wsProto = step.Component.Container.InteractiveApps.WebsocketProto
} else {
wsProto = burl.Scheme
}
if step.Component.Container.InteractiveApps.WebsocketPort != "" {
wsPort = step.Component.Container.InteractiveApps.WebsocketPort
} else {
wsPort = burl.Port()
}
burl.Path = wsPath
burl.Scheme = wsProto
if wsPort != "" {
burl.Host = fmt.Sprintf("%s:%s", burl.Hostname(), wsPort)
}
websocketURL = burl.String()
// }
return websocketURL, nil
}
// ConvertStepParams contains the info needed to call ConvertStep()
type ConvertStepParams struct {
Step *model.Step
Index int
User string
UserID string
InvID string
WorkingDirHostPath string
AvailablePort int
}
// ConvertStep will add the job step to the JobComposition services along with a
// proxy service.
func (c *Composer) ConvertStep(s *ConvertStepParams) error {
var (
step = s.Step
index = s.Index
user = s.User
workingDirHostPath = s.WorkingDirHostPath
availablePort = s.AvailablePort
)
// Construct the name of the image
// Set the name of the image for the container.
var imageName string
if step.Component.Container.Image.Tag != "" {
imageName = fmt.Sprintf(
"%s:%s",
step.Component.Container.Image.Name,
step.Component.Container.Image.Tag,
)
} else {
imageName = step.Component.Container.Image.Name
}
redirectURL, err := c.FrontendURL(step)
if err != nil {
return err
}
step.Environment["IPLANT_USER"] = user
step.Environment["IPLANT_EXECUTION_ID"] = c.job.InvocationID
step.Environment["REDIRECT_URL"] = redirectURL
var containername string
if step.Component.Container.Name != "" {
containername = step.Component.Container.Name
} else {
containername = fmt.Sprintf("step_%d_%s", index, c.job.InvocationID)
}
indexstr := strconv.Itoa(index)
c.composition.Services[fmt.Sprintf("step_%d", index)] = &Service{
Image: imageName,
Command: step.Arguments(),
WorkingDir: step.Component.Container.WorkingDirectory(),
Labels: map[string]string{
model.DockerLabelKey: strconv.Itoa(StepContainer),
},
Logging: &LoggingConfig{
Driver: logdriver,
Options: map[string]string{
"stderr": path.Join(hostworkingdir, VOLUMEDIR, step.Stderr(indexstr)),
"stdout": path.Join(hostworkingdir, VOLUMEDIR, step.Stdout(indexstr)),
},
},
ContainerName: containername,
Environment: step.Environment,
VolumesFrom: []string{},
Volumes: []string{},
Devices: []string{},
}
svc := c.composition.Services[fmt.Sprintf("step_%d", index)]
stepContainer := step.Component.Container
for _, cp := range step.Component.Container.Ports {
portstr := fmt.Sprintf("%d", cp.ContainerPort)
svc.Ports = append(svc.Ports, portstr)
svc.Expose = append(svc.Expose, portstr)
}
if stepContainer.EntryPoint != "" {
svc.EntryPoint = stepContainer.EntryPoint
}
// If a memory limit is set, use it. Otherwise default to allocating 4GB of
// RAM for the container. For now we won't worry about the swap limit.
if stepContainer.MemoryLimit > 0 {
svc.MemLimit = fmt.Sprintf("%db", stepContainer.MemoryLimit)
} else {
svc.MemLimit = fmt.Sprintf("%db", c.memoryLimit)
}
if stepContainer.CPUShares > 0 {
svc.CPUShares = stepContainer.CPUShares
}
if stepContainer.PIDsLimit > 0 {
svc.PIDsLimit = stepContainer.PIDsLimit
}
// If the MaxCPUCores setting is provided, use it. Otherwise, default to
// limiting the container to 2.0 cores.
if stepContainer.MaxCPUCores > 0.0 {
svc.CPUs = fmt.Sprintf("%f", stepContainer.MaxCPUCores)
} else {
svc.CPUs = fmt.Sprintf("%f", c.maxCPUCores)
}
// Handles volumes created by other containers.
for _, vf := range stepContainer.VolumesFrom {
containerName := fmt.Sprintf("%s-%s", vf.NamePrefix, c.job.InvocationID)
var foundService string
for svckey, svc := range c.composition.Services { // svckey is the docker-compose service name.
if svc.ContainerName == containerName {
foundService = svckey
}
}
svc.VolumesFrom = append(svc.VolumesFrom, foundService)
}
// The working directory needs to be mounted as a volume.
svc.Volumes = append(svc.Volumes, strings.Join([]string{workingDirHostPath, stepContainer.WorkingDirectory(), "rw"}, ":"))
// The TMPDIR needs to be mounted as a volume
if !step.Component.Container.SkipTmpMount {
svc.Volumes = append(svc.Volumes, fmt.Sprintf("./%s:/tmp:rw", TMPDIR))
}
for _, v := range stepContainer.Volumes {
var rw string
if v.ReadOnly {
rw = "ro"
} else {
rw = "rw"
}
if v.HostPath == "" {
svc.Volumes = append(svc.Volumes, fmt.Sprintf("%s:%s", v.ContainerPath, rw))
} else {
svc.Volumes = append(svc.Volumes, fmt.Sprintf("%s:%s:%s", v.HostPath, v.ContainerPath, rw))
}
}
for _, device := range stepContainer.Devices {
svc.Devices = append(svc.Devices,
fmt.Sprintf("%s:%s:%s",
device.HostPath,
device.ContainerPath,
device.CgroupPermissions,
),
)
}
/////////// Proxy
//Only supporting a single port for now.
var containerPort int
if step.Component.Container.Ports[0].ContainerPort != 0 {
containerPort = step.Component.Container.Ports[0].ContainerPort
}
var backendURL string
if step.Component.Container.InteractiveApps.BackendURL != "" {
backendURL = step.Component.Container.InteractiveApps.BackendURL
} else {
if containerPort != 0 {
backendURL = fmt.Sprintf("http://step_%d_%s:%d", index, c.job.InvocationID, containerPort)
} else {
backendURL = fmt.Sprintf("http://step_%d_%s", index, c.job.InvocationID)
}
}
websocketURL, err := websocketURL(step, backendURL)
if err != nil {
return err
}
frontendURL, err := c.FrontendURL(step)
if err != nil {
return err
}
// Add a service for the proxy container. Each step has a corresponding proxy.
proxyName := c.ProxyName(index)
c.composition.Services[ProxyServiceName(index)] = &Service{
Image: stepContainer.InteractiveApps.ProxyImage,
Command: []string{
"--backend-url", backendURL,
"--ws-backend-url", websocketURL,
"--frontend-url", frontendURL,
"--cas-base-url", stepContainer.InteractiveApps.CASURL,
"--cas-validate", stepContainer.InteractiveApps.CASValidate,
},
Labels: map[string]string{
model.DockerLabelKey: strconv.Itoa(StepContainer),
},
Logging: &LoggingConfig{
Driver: logdriver,
Options: map[string]string{
"stderr": path.Join(hostworkingdir, VOLUMEDIR, step.Stderr(indexstr)),
"stdout": path.Join(hostworkingdir, VOLUMEDIR, step.Stdout(indexstr)),
},
},
ContainerName: proxyName,
Environment: step.Environment,
Ports: []string{
fmt.Sprintf("%s:8080", strconv.Itoa(availablePort)),
},
}
return nil
}
// ProxyName returns the name of the cas-proxy container based on the step index and the
// job's invocationID.
func (c *Composer) ProxyName(index int) string {
return fmt.Sprintf("step_proxy_%d_%s", index, c.job.InvocationID)
}
// ProxyServiceName returns the docker-compose service name for the cas-proxy,
// based on the step index passed in.
func ProxyServiceName(index int) string {
return fmt.Sprintf("proxy_%d", index)
}