forked from ovh/cds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swarm.go
422 lines (357 loc) · 10.7 KB
/
swarm.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
package main
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/docker/docker/pkg/namesgenerator"
"github.com/fsouza/go-dockerclient"
"github.com/ovh/cds/engine/api/hatchery"
"github.com/ovh/cds/engine/log"
"github.com/ovh/cds/sdk"
)
//HatcherySwarm is a hatchery which can be connected to a remote to a docker remote api
type HatcherySwarm struct {
sync sync.Mutex
hatch *hatchery.Hatchery
dockerClient *docker.Client
onlyWithServiceReq bool
maxContainers int
}
//ParseConfig do nothing
func (h *HatcherySwarm) ParseConfig() {}
//Init connect the hatchery to the docker api
func (h *HatcherySwarm) Init() error {
var err error
if os.Getenv("DOCKER_HOST") == "" {
return errors.New("Please export docker client env variables DOCKER_HOST, DOCKER_TLS_VERIFY, DOCKER_CERT_PATH")
}
if os.Getenv("ONLY_WITH_SERVICE_REQ") == "true" {
h.onlyWithServiceReq = true
}
if os.Getenv("MAX_CONTAINER") == "" {
h.maxContainers = 10
} else {
h.maxContainers, err = strconv.Atoi(os.Getenv("MAX_CONTAINER"))
if err != nil {
log.Critical("Invalid MAX_CONTAINER")
return err
}
}
h.dockerClient, err = docker.NewClientFromEnv()
if err != nil {
log.Critical("Unable to connect to a docker client")
return err
}
if err := h.dockerClient.Ping(); err != nil {
log.Critical("Unable to ping docker host")
return err
}
// Register without declaring model
name, err := os.Hostname()
if err != nil {
log.Warning("Cannot retrieve hostname: %s\n", err)
name = "cds-hatchery"
}
name += "-dockerapi"
h.hatch = &hatchery.Hatchery{
Name: name,
}
if err := register(h.hatch); err != nil {
log.Warning("Cannot register hatchery: %s\n", err)
return err
}
log.Notice("Swarm Hatchery ready to run !")
go h.killAwolWorker()
return nil
}
// Refresh doesn't do anything
func (h *HatcherySwarm) Refresh() error {
return nil
}
// KillWorker kill the worker
func (h *HatcherySwarm) KillWorker(worker sdk.Worker) error {
log.Warning("killing container %s", worker.Name)
containers, err := h.dockerClient.ListContainers(docker.ListContainersOptions{
All: true,
})
if err != nil {
return err
}
for i := range containers {
if strings.Replace(containers[i].Names[0], "/", "", 1) == strings.Replace(worker.Name, "/", "", 1) {
//Kill the container, and all linked containers
h.killAndRemove(containers[i].ID)
}
}
return nil
}
func (h *HatcherySwarm) getContainer(name string) (*docker.APIContainers, error) {
containers, err := h.dockerClient.ListContainers(docker.ListContainersOptions{
All: true,
})
if err != nil {
return nil, err
}
for i := range containers {
if strings.Replace(containers[i].Names[0], "/", "", 1) == strings.Replace(name, "/", "", 1) {
return &containers[i], nil
}
}
return nil, nil
}
func (h *HatcherySwarm) killAndRemove(ID string) error {
log.Debug("Remove container %s", ID)
container, err := h.dockerClient.InspectContainer(ID)
if err != nil {
return err
}
links := container.HostConfig.Links
for _, l := range links {
log.Debug("Remove linked containers : %s", l)
if strings.Contains(l, ":") {
c, _ := h.getContainer(strings.Split(l, ":")[0])
if c != nil {
defer h.killAndRemove(c.ID)
}
} else {
log.Warning("I dont know what to do with %s", l)
}
}
h.dockerClient.KillContainer(docker.KillContainerOptions{
ID: ID,
Signal: docker.SIGKILL,
})
err = h.dockerClient.RemoveContainer(docker.RemoveContainerOptions{
ID: ID,
})
return err
}
//SpawnWorker start a new docker container
func (h *HatcherySwarm) SpawnWorker(model *sdk.Model, req []sdk.Requirement) error {
//uk is the worker key for worker auth
uk, err := sdk.GenerateWorkerKey(sdk.FirstUseExpire)
if err != nil {
return fmt.Errorf("SpawnWorker> Cannot generate worker key: %s", err)
}
//name is the name of the worker and the name of the container
name := fmt.Sprintf("%s-%s", strings.ToLower(model.Name), strings.Replace(namesgenerator.GetRandomName(0), "_", "-", -1))
log.Debug("Spawning worker %s with requirements %v", name, req)
//Prepare worker services from requirements
//docker legacy links (https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks/) are <name or id>:alias
links := []string{}
services := []string{}
for _, r := range req {
if r.Type == sdk.ServiceRequirement {
//name= <alias> => the name of the host put in /etc/hosts of the worker
//value= "postgres:latest env_1=blabla env_2=blabla"" => we can add env variables in requirement name
tuple := strings.Split(r.Value, " ")
img := tuple[0]
env := []string{}
if len(tuple) > 1 {
env = append(env, tuple[1:]...)
}
serviceName := r.Name + "-" + name
//labels are used to make container cleanup easier. We "link" the service to its worker this way.
labels := map[string]string{
"service_worker": name,
"service_name": serviceName,
}
//Start the services
if err := h.createAndStartContainer(serviceName, img, []string{}, env, []string{}, labels); err != nil {
log.Warning("SpawnWorker>Unable to start required container: %s\n", err)
return err
}
services = append(services, serviceName)
links = append(links, serviceName+":"+r.Name)
}
}
//cmd is the command to start the worker (we need curl to download current version of the worker binary)
cmd := []string{"sh", "-c", fmt.Sprintf("curl %s/download/worker/`uname -m` -o worker && echo chmod worker && chmod +x worker && echo starting worker && ./worker", sdk.Host)}
//CDS env needed by the worker binary
env := []string{
"CDS_API" + "=" + sdk.Host,
"CDS_NAME" + "=" + name,
"CDS_KEY" + "=" + uk,
"CDS_MODEL" + "=" + strconv.FormatInt(model.ID, 10),
"CDS_HATCHERY" + "=" + strconv.FormatInt(h.hatch.ID, 10),
}
//labels are used to make container cleanup easier
labels := map[string]string{
"worker_model": strconv.FormatInt(model.ID, 10),
"worker_name": name,
"worker_requirements": strings.Join(services, ","),
}
//start the worker
if err := h.createAndStartContainer(name, model.Image, cmd, env, links, labels); err != nil {
log.Warning("SpawnWorker> Unable to start container %s\n", err)
}
return nil
}
//shortcut to create+start(=run) a container
func (h *HatcherySwarm) createAndStartContainer(name, image string, cmd, env, links []string, labels map[string]string) error {
log.Debug("createAndStartContainer> Create container %s from %s\n", name, image)
opts := docker.CreateContainerOptions{
Name: name,
Config: &docker.Config{
Image: image,
Cmd: cmd,
Env: env,
Labels: labels,
},
HostConfig: &docker.HostConfig{
Links: links,
},
}
c, err := h.dockerClient.CreateContainer(opts)
if err != nil {
log.Warning("startAndCreateContainer> Unable to create container %s\n", err)
return err
}
if err := h.dockerClient.StartContainer(c.ID, nil); err != nil {
log.Warning("startAndCreateContainer> Unable to start container %s\n", err)
return err
}
return nil
}
// CanSpawn checks if the model can be spawned by this hatchery
func (h *HatcherySwarm) CanSpawn(model *sdk.Model, req []sdk.Requirement) bool {
log.Debug("CanSpawn> Checking %s %v", model.Name, req)
if model.Type != sdk.Docker {
return false
}
//List all containers to check if we can spawn a new one
if cs, _ := h.dockerClient.ListContainers(docker.ListContainersOptions{}); len(cs) > h.maxContainers {
return false
}
//Get links from requirements
var atLeastOneLink bool
links := map[string]string{}
for _, r := range req {
if r.Type == sdk.ServiceRequirement {
atLeastOneLink = true
links[r.Name] = strings.Split(r.Value, " ")[0]
}
}
//This hatchery may only manage container with links
if (!atLeastOneLink) && h.onlyWithServiceReq {
return false
}
log.Notice("CanSpawn> %s need %v", model.Name, links)
//Pull the worker image
opts := docker.PullImageOptions{
Repository: model.Image,
OutputStream: nil,
}
auth := docker.AuthConfiguration{}
log.Notice("CanSpawn> pulling image %s", model.Image)
if err := h.dockerClient.PullImage(opts, auth); err != nil {
log.Warning("Unable to pull image %s : %s", model.Image, err)
return false
}
//Pull the service image
for _, i := range links {
opts := docker.PullImageOptions{
Repository: i,
OutputStream: nil,
}
log.Notice("CanSpawn> pulling image %s", i)
if err := h.dockerClient.PullImage(opts, auth); err != nil {
log.Warning("Unable to pull image %s : %s", i, err)
return false
}
}
//Ready to spawn
return true
}
// WorkerStarted returns the number of started workers
func (h *HatcherySwarm) WorkerStarted(model *sdk.Model) int {
if model.Type != sdk.Docker {
return 0
}
containers, err := h.dockerClient.ListContainers(docker.ListContainersOptions{})
if err != nil {
log.Warning("WorkerStarted> error listing containers : %s", err)
}
list := []string{}
for _, c := range containers {
log.Info("Container : %s %s [%s]", c.ID, c.Image, c.Status)
if c.Image == model.Image {
list = append(list, c.ID)
}
}
log.Notice("WorkerStarted> %s \t %d", model.Name, len(list))
return len(list)
}
// SetWorkerModelID does nothing
func (h *HatcherySwarm) SetWorkerModelID(int64) {}
// Hatchery returns Hatchery instances
func (h *HatcherySwarm) Hatchery() *hatchery.Hatchery {
return h.hatch
}
// ID returns ID of the Hatchery
func (h *HatcherySwarm) ID() int64 {
if h.hatch == nil {
return 0
}
return h.hatch.ID
}
//Mode returns DockerAPIMode value
func (h *HatcherySwarm) Mode() string {
return SwarmMode
}
func (h *HatcherySwarm) killAwolWorkerRoutine() {
for {
time.Sleep(30 * time.Second)
h.killAwolWorker()
}
}
func (h *HatcherySwarm) killAwolWorker() {
apiworkers, err := sdk.GetWorkers()
if err != nil {
log.Warning("Cannot get workers: %s", err)
return
}
containers, err := h.dockerClient.ListContainers(docker.ListContainersOptions{
All: true,
})
oldContainers := []docker.APIContainers{}
//Checking workers
for _, c := range containers {
if c.Labels["worker_name"] == "" {
continue
}
var found bool
for _, n := range apiworkers {
// If worker is disabled, kill it
if n.Name == c.Names[0] {
if n.Status == sdk.StatusDisabled {
log.Info("Worker %s is disabled. Kill it with fire !\n", c.Names[0])
oldContainers = append(oldContainers, c)
continue
}
found = true
}
}
if !found {
oldContainers = append(oldContainers, c)
}
}
//Checking services
for _, c := range containers {
if c.Labels["service_worker"] == "" {
continue
}
if w, _ := h.getContainer(c.Labels["service_worker"]); w == nil {
oldContainers = append(oldContainers, c)
continue
}
}
for _, c := range oldContainers {
h.killAndRemove(c.ID)
log.Notice("HatcherySwarm.killAwolWorker> Delete worker %s\n", c.Names[0])
}
}