-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtasks.go
91 lines (76 loc) · 1.91 KB
/
tasks.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
package mesoslib
import (
"crypto/rand"
"encoding/hex"
"fmt"
"github.com/golang/protobuf/proto"
"github.com/jimenez/go-mesoslib/mesosproto"
)
type Volume struct {
ContainerPath string `json:"container_path,omitempty"`
HostPath string `json:"host_path,omitempty"`
Mode string `json:"mode,omitempty"`
}
type Task struct {
ID string
Command []string
Image string
Volumes []*Volume
}
func NewTask(image string, command []string) *Task {
id := make([]byte, 6)
n, err := rand.Read(id)
if n != len(id) || err != nil {
return nil
}
return &Task{
ID: hex.EncodeToString(id),
Command: command,
Image: image,
}
}
// Helper for task info object creation
func CreateTaskInfo(offer *mesosproto.Offer, resources []*mesosproto.Resource, task *Task) *mesosproto.TaskInfo {
taskInfo := mesosproto.TaskInfo{
Name: proto.String(fmt.Sprintf("mesoscon-demo-task-%s", task.ID)),
TaskId: &mesosproto.TaskID{
Value: &task.ID,
},
AgentId: offer.AgentId,
Resources: resources,
Command: &mesosproto.CommandInfo{},
}
// Set value only if provided
if task.Command[0] != "" {
taskInfo.Command.Value = &task.Command[0]
}
// Set args only if they exist
if len(task.Command) > 1 {
taskInfo.Command.Arguments = task.Command[1:]
}
// Set the docker image if specified
if task.Image != "" {
taskInfo.Container = &mesosproto.ContainerInfo{
Type: mesosproto.ContainerInfo_DOCKER.Enum(),
Docker: &mesosproto.ContainerInfo_DockerInfo{
Image: &task.Image,
},
}
for _, v := range task.Volumes {
var (
vv = v
mode = mesosproto.Volume_RW
)
if vv.Mode == "ro" {
mode = mesosproto.Volume_RO
}
taskInfo.Container.Volumes = append(taskInfo.Container.Volumes, &mesosproto.Volume{
ContainerPath: &vv.ContainerPath,
HostPath: &vv.HostPath,
Mode: &mode,
})
}
taskInfo.Command.Shell = proto.Bool(false)
}
return &taskInfo
}