-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesos.go
391 lines (336 loc) · 9.75 KB
/
mesos.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
package mesos
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path"
"strconv"
"strings"
"text/template"
"time"
"github.com/docker/docker/pkg/namesgenerator"
"github.com/spf13/viper"
"github.com/ovh/cds/engine/log"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/hatchery"
)
var hatcheryMesos *HatcheryMesos
type marathonPOSTAppParams struct {
DockerImage string
ForcePullImage bool
APIEndpoint string
WorkerKey string
WorkerName string
WorkerModelID int64
HatcheryID int64
MarathonID string
MarathonVHOST string
MarathonLabels string
Memory int
WorkerTTL int
}
const marathonPOSTAppTemplate = `
{
"container": {
"docker": {
"forcePullImage": {{.ForcePullImage}},
"image": "{{.DockerImage}}",
"network": "BRIDGE",
"portMapping": []
},
"type": "DOCKER"
},
"cmd": "rm -f worker && curl ${CDS_API}/download/worker/$(uname -m) -o worker && chmod +x worker && exec ./worker",
"cpus": 0.5,
"env": {
"CDS_API": "{{.APIEndpoint}}",
"CDS_KEY": "{{.WorkerKey}}",
"CDS_NAME": "{{.WorkerName}}",
"CDS_MODEL": "{{.WorkerModelID}}",
"CDS_HATCHERY": "{{.HatcheryID}}",
"CDS_SINGLE_USE": "1",
"CDS_TTL" : "{{.WorkerTTL}}"
},
"id": "{{.MarathonID}}/{{.WorkerName}}",
"instances": 1,
"ports": [],
"mem": {{.Memory}},
"labels": {{.MarathonLabels}}
}
`
// HatcheryMesos implements HatcheryMode interface for mesos mode
type HatcheryMesos struct {
hatch *sdk.Hatchery
token string
marathonHost string
marathonID string
marathonVHOST string
marathonUser string
marathonPassword string
marathonLabelsString string
marathonLabels map[string]string
defaultMemory int
workerTTL int
}
// ID must returns hatchery id
func (m *HatcheryMesos) ID() int64 {
if m.hatch == nil {
return 0
}
return m.hatch.ID
}
//Hatchery returns hatchery instance
func (m *HatcheryMesos) Hatchery() *sdk.Hatchery {
return m.hatch
}
// KillWorker deletes an application on mesos via marathon
func (m *HatcheryMesos) KillWorker(worker sdk.Worker) error {
appID := path.Join(hatcheryMesos.marathonID, worker.Name)
log.Notice("KillWorker> Killing %s\n", appID)
return deleteApp(hatcheryMesos.marathonHost, hatcheryMesos.marathonUser, hatcheryMesos.marathonPassword, appID)
}
// CanSpawn return wether or not hatchery can spawn model
// requirements services are not supported
func (m *HatcheryMesos) CanSpawn(model *sdk.Model, req []sdk.Requirement) bool {
if model.Type != sdk.Docker {
return false
}
//Service requirement are not supported
for _, r := range req {
if r.Type == sdk.ServiceRequirement {
return false
}
}
return true
}
// SpawnWorker creates an application on mesos via marathon
// requirements services are not supported
func (m *HatcheryMesos) SpawnWorker(model *sdk.Model, req []sdk.Requirement) error {
if model.Type != sdk.Docker {
return fmt.Errorf("Model not handled")
}
log.Notice("Spawning worker %s (%s)\n", model.Name, model.Image)
// Do not DOS marathon, if deployment queue is longer than 10, wait
deployments, err := getDeployments(hatcheryMesos.marathonHost, hatcheryMesos.marathonUser, hatcheryMesos.marathonPassword)
if err != nil {
return err
}
if len(deployments) >= 10 {
log.Notice("%d item in deployment queue, waiting\n", len(deployments))
time.Sleep(2 * time.Second)
return nil
}
apps, err := getApps(hatcheryMesos.marathonHost, hatcheryMesos.marathonUser, hatcheryMesos.marathonPassword, hatcheryMesos.marathonID)
if err != nil {
return err
}
if len(apps) >= viper.GetInt("max-worker") {
return fmt.Errorf("max number of containers reached, aborting")
}
mss, err := sdk.GetWorkerModelStatus()
if err != nil {
return err
}
for _, ms := range mss {
if ms.ModelName == model.Name {
// Security against deficient worker model with worker not connecting
// TODO: Should validate worker before running them at scale
if int(ms.CurrentCount) > countOf(model.Name, apps)+10 {
return fmt.Errorf("Over 20 %s workers started on mesos but 0 connected, something is wrong\n", model.Name)
}
break
}
}
return m.spawnMesosDockerWorker(model, m.hatch.ID, req)
}
// WorkerStarted returns the number of instances of given model started but
// not necessarily register on CDS yet
func (m *HatcheryMesos) WorkerStarted(model *sdk.Model) int {
apps, err := getApps(hatcheryMesos.marathonHost, hatcheryMesos.marathonUser, hatcheryMesos.marathonPassword, hatcheryMesos.marathonID)
if err != nil {
return 0
}
var x int
for _, app := range apps {
if strings.Contains(app.ID, strings.ToLower(model.Name)) {
x++
}
}
return x
}
// Init only starts killing routine of worker not registered
func (m *HatcheryMesos) Init() error {
// Register without declaring model
name, err := os.Hostname()
if err != nil {
log.Warning("Cannot retrieve hostname: %s\n", err)
name = "cds-hatchery-mesos"
}
m.hatch = &sdk.Hatchery{
Name: name,
UID: viper.GetString("token"),
}
if err = hatchery.Register(m.hatch, viper.GetString("token")); err != nil {
log.Warning("Cannot register hatchery: %s\n", err)
}
// Start cleaning routines
startKillAwolWorkerRoutine()
return nil
}
func (m *HatcheryMesos) marathonConfig(model *sdk.Model, hatcheryID int64, memory int) (io.Reader, error) {
tmpl, err := template.New("marathonPOST").Parse(marathonPOSTAppTemplate)
if err != nil {
return nil, err
}
m.marathonLabels["hatchery"] = fmt.Sprintf("%d", hatcheryID)
labels, err := json.Marshal(m.marathonLabels)
if err != nil {
log.Critical("spawnMesosDockerWorker> Invalid labels : %s", err)
return nil, err
}
params := marathonPOSTAppParams{
ForcePullImage: strings.HasSuffix(model.Image, ":latest"),
DockerImage: model.Image,
APIEndpoint: sdk.Host,
WorkerKey: m.token,
WorkerName: fmt.Sprintf("%s-%s", strings.ToLower(model.Name), strings.Replace(namesgenerator.GetRandomName(0), "_", "-", -1)),
WorkerModelID: model.ID,
HatcheryID: hatcheryID,
MarathonID: m.marathonID,
MarathonVHOST: m.marathonVHOST,
Memory: memory * 110 / 100,
MarathonLabels: string(labels),
WorkerTTL: m.workerTTL,
}
buffer := &bytes.Buffer{}
if err := tmpl.Execute(buffer, params); err != nil {
log.Critical("Unable to execute marathon template : %s", err)
return nil, err
}
return buffer, nil
}
func (m *HatcheryMesos) spawnMesosDockerWorker(model *sdk.Model, hatcheryID int64, req []sdk.Requirement) error {
// Estimate needed memory, we will set 110% of required memory
memory := m.defaultMemory
//Check if there is a memory requirement
//if there is a service requirement: exit
for _, r := range req {
if r.Name == sdk.ServiceRequirement {
return fmt.Errorf("Service requirement not supported")
}
if r.Type == sdk.MemoryRequirement {
var err error
memory, err = strconv.Atoi(r.Value)
if err != nil {
log.Warning("spawnMesosDockerWorker>Unable to parse memory requirement %s :s\n", memory, err)
return err
}
}
}
buffer, errm := m.marathonConfig(model, hatcheryID, memory)
if errm != nil {
return errm
}
r, errc := http.NewRequest("POST", hatcheryMesos.marathonHost+"/v2/apps", buffer)
if errc != nil {
return errc
}
r.Header.Set("Content-Type", "application/json")
r.Header.Set("User-Agent", "CDS-HATCHERY/1.0")
r.SetBasicAuth(hatcheryMesos.marathonUser, hatcheryMesos.marathonPassword)
resp, err := hatchery.Client.Do(r)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
resp.Body.Close()
return fmt.Errorf("%s", resp.Status)
}
return nil
}
func startKillAwolWorkerRoutine() {
go func() {
for {
time.Sleep(10 * time.Second)
if err := killDisabledWorkers(); err != nil {
log.Warning("Cannot kill awol workers: %s\n", err)
}
}
}()
go func() {
for {
time.Sleep(10 * time.Second)
if err := killAwolWorkers(); err != nil {
log.Warning("Cannot kill awol workers: %s\n", err)
}
}
}()
}
func killDisabledWorkers() error {
workers, err := sdk.GetWorkers()
if err != nil {
return err
}
apps, err := getApps(hatcheryMesos.marathonHost, hatcheryMesos.marathonUser, hatcheryMesos.marathonPassword, hatcheryMesos.marathonID)
if err != nil {
return err
}
for _, w := range workers {
if w.Status != sdk.StatusDisabled {
continue
}
// check that there is a worker matching
for _, app := range apps {
if strings.HasSuffix(app.ID, w.Name) {
log.Notice("killing disabled worker %s\n", app.ID)
if err := deleteApp(hatcheryMesos.marathonHost, hatcheryMesos.marathonUser, hatcheryMesos.marathonPassword, app.ID); err != nil {
return err
}
}
}
}
return nil
}
func killAwolWorkers() error {
workers, err := sdk.GetWorkers()
if err != nil {
return err
}
apps, err := getApps(hatcheryMesos.marathonHost, hatcheryMesos.marathonUser, hatcheryMesos.marathonPassword, hatcheryMesos.marathonID)
if err != nil {
return err
}
var found bool
// then for each RUNNING marathon application
for i := range apps {
// Worker is deploying, leave him alone
if apps[i].TasksRunning == 0 {
continue
}
t, err := time.Parse(time.RFC3339, apps[i].Version)
if err != nil {
log.Warning("Cannot parse last update: %s\n", err)
break
}
// check that there is a worker matching
found = false
for _, w := range workers {
if strings.HasSuffix(apps[i].ID, w.Name) && w.Status != sdk.StatusDisabled {
found = true
break
}
}
// then if it's not found, kill it !
if !found && time.Since(t) > 1*time.Minute {
log.Notice("killing awol worker %s\n", apps[i].ID)
if err := deleteApp(hatcheryMesos.marathonHost, hatcheryMesos.marathonUser, hatcheryMesos.marathonPassword, apps[i].ID); err != nil {
return err
}
}
}
return nil
}