This repository has been archived by the owner on Aug 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 391
/
daemon.go
565 lines (481 loc) · 14 KB
/
daemon.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
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net"
"os/exec"
"strconv"
"strings"
"sync"
"time"
log "github.com/Sirupsen/logrus"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/events"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
)
// TCPMessage defines what a message that can be
// sent or received to/from LUA scripts
type TCPMessage struct {
Cmd string `json:"cmd,omitempty"`
Args []string `json:"args,omitempty"`
// Id is used to associate requests & responses
ID int `json:"id,omitempty"`
Data interface{} `json:"data,omitempty"`
}
// StatsOptionsEntry is used to collect stats from
// the Docker daemon
type StatsOptionsEntry struct {
statsChan chan *types.StatsJSON
doneChan chan bool
}
// ContainerEvent is one kind of Data that can
// be transported by a TCPMessage in the Data field.
// It describes a Docker container event. (start, stop, destroy...)
type ContainerEvent struct {
Action string `json:"action,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
ImageRepo string `json:"imageRepo,omitempty"`
ImageTag string `json:"imageTag,omitempty"`
CPU string `json:"cpu,omitempty"`
RAM string `json:"ram,omitempty"`
Running bool `json:"running,omitempty"`
}
// Daemon maintains state when the dockercraft daemon is running
type Daemon struct {
// Client is an instance of the DockerClient
Client *client.Client
// Version is the version of the Docker Daemon
Version string
// BinaryName is the name of the Docker Binary
BinaryName string
// previouscpustats is a map containing the previous cpu stats we got from the
// docker daemon through the docker remote api
previousCPUStats map[string]*CPUStats
// tcpMessages can be used to send bytes to the Lua
// plugin from any go routine.
tcpMessages chan []byte
// statsOptionsStore references docker.StatsOptions
// of monitored containers.
statsOptionsStore map[string]StatsOptionsEntry
sync.Mutex
}
// NewDaemon returns a new instance of Daemon
func NewDaemon() *Daemon {
return &Daemon{
previousCPUStats: make(map[string]*CPUStats),
}
}
// CPUStats contains the Total and System CPU stats
type CPUStats struct {
TotalUsage uint64
SystemUsage uint64
}
// Init initializes a Daemon
func (d *Daemon) Init() error {
var err error
d.Client, err = client.NewEnvClient()
if err != nil {
return err
}
// get the version of the remote docker
info, err := d.Client.Info(context.Background())
if err != nil {
log.Fatal(err.Error())
}
d.Version = info.ServerVersion
d.statsOptionsStore = make(map[string]StatsOptionsEntry)
d.tcpMessages = make(chan []byte)
return nil
}
// Serve exposes a TCP server on port 25566 to handle
// connections from the LUA scripts
func (d *Daemon) Serve() {
tcpAddr, err := net.ResolveTCPAddr("tcp", ":25566")
ln, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
log.Fatalln("listen tcp error:", err)
}
for {
conn, err := ln.Accept()
if err != nil {
log.Fatalln("tcp conn accept error:", err)
}
// no need to handle connection in a go routine
// goproxy is used as support for one single Lua plugin.
d.handleConn(conn)
}
}
// StartMonitoringEvents listens for events from the
// Docker daemon and uses callback to transmit them
// to LUA scripts.
func (d *Daemon) StartMonitoringEvents() {
log.Info("Monitoring Docker Events")
filters := filters.NewArgs()
filters.Add("type", events.ContainerEventType)
opts := types.EventsOptions{
Filters: filters,
}
//context.TODO, cancel := context.WithCancel(d.context.TODO)
//defer cancel()
events, errs := d.Client.Events(context.Background(), opts)
for {
select {
case event := <-events:
log.Info("New Event Received")
d.eventCallback(event)
case err := <-errs:
log.Fatal(err.Error())
}
}
}
// handleConn handles a TCP connection
// with a Dockercraft Lua plugin.
func (d *Daemon) handleConn(conn net.Conn) {
go func() {
separator := []byte(string('\n'))
buf := make([]byte, 256)
cursor := 0
for {
// resize buf if needed
if len(buf)-cursor < 256 {
buf = append(buf, make([]byte, 256-(len(buf)-cursor))...)
}
n, err := conn.Read(buf[cursor:])
if err != nil && err != io.EOF {
log.Fatalln("conn read error: ", err)
}
cursor += n
// TODO(aduermael): check cNetwork plugin implementation
// conn.Read doesn't seem to be blocking if there's nothing
// to read. Maybe the broken pipe is due to an implementation
// problem on cNetwork plugin side
if cursor == 0 {
<-time.After(500 * time.Millisecond)
continue
}
// log.Println("TCP data read:", string(buf[:cursor]), "cursor:", cursor)
// see if there's a complete json message in buf.
// messages are separated with \n characters
messages := bytes.Split(buf[:cursor], separator)
// if one complete message and seperator is found
// then we should have len(messages) > 1, the
// last entry being an incomplete message or empty array.
if len(messages) > 1 {
shiftLen := 0
for i := 0; i < len(messages)-1; i++ {
// log.Println(string(messages[i]))
msgCopy := make([]byte, len(messages[i]))
copy(msgCopy, messages[i])
go d.handleMessage(msgCopy)
shiftLen += len(messages[i]) + 1
}
copy(buf, buf[shiftLen:])
cursor -= shiftLen
}
}
}()
for {
tcpMessage := <-d.tcpMessages
log.Debug("tcpMessage:", string(tcpMessage))
_, err := conn.Write(tcpMessage)
if err != nil {
log.Fatal("conn write error:", err)
}
}
}
// handleMessage handles a message read
// from TCP connection
func (d *Daemon) handleMessage(message []byte) {
var tcpMsg TCPMessage
err := json.Unmarshal(message, &tcpMsg)
if err != nil {
log.Println("json unmarshal error:", err)
return
}
log.Debugf("handleMessage: %#v \n", tcpMsg)
switch tcpMsg.Cmd {
case "docker":
d.execDockerCmd(tcpMsg.Args)
case "info":
if len(tcpMsg.Args) > 0 {
switch tcpMsg.Args[0] {
case "containers":
d.listContainers()
}
}
}
}
// eventCallback receives and handles the docker events
func (d *Daemon) eventCallback(event events.Message) {
containerEvent, err := d.apiEventToContainerEvent(event)
if err != nil {
log.Println("apiEventToContainerEvent error:", err)
return
}
switch event.Status {
case "create":
log.Infof("Container Create Event received for %s", containerEvent.ID)
containerEvent.Action = "createContainer"
data, err := containerEventToTCPMsg(containerEvent)
if err != nil {
log.Println(err)
return
}
d.tcpMessages <- append(data, '\n')
log.Info("DONE")
case "start":
log.Infof("Container Start Event received for %s", containerEvent.ID)
containerEvent.Action = "startContainer"
data, err := containerEventToTCPMsg(containerEvent)
if err != nil {
log.Println(err)
return
}
d.tcpMessages <- append(data, '\n')
d.startStatsMonitoring(containerEvent.ID)
log.Info("DONE")
case "die":
log.Infof("Container Die Event received for %s", containerEvent.ID)
containerEvent.Action = "stopContainer"
data, err := containerEventToTCPMsg(containerEvent)
if err != nil {
log.Println(err)
return
}
d.tcpMessages <- append(data, '\n')
d.Lock()
log.Info("Removing Container")
statsOptionsEntry, found := d.statsOptionsStore[containerEvent.ID]
if found {
log.Info("Sending Done on channel")
close(statsOptionsEntry.doneChan)
log.Info("Deleting the entry from the list")
delete(d.statsOptionsStore, containerEvent.ID)
}
d.Unlock()
// enforce 0% display (Cpu & Ram)
d.statCallback(containerEvent.ID, nil)
log.Info("DONE")
case "destroy":
log.Infof("Container Destroy Event received for %s", containerEvent.ID)
containerEvent.Action = "destroyContainer"
data, err := containerEventToTCPMsg(containerEvent)
if err != nil {
log.Println(err)
return
}
d.tcpMessages <- append(data, '\n')
log.Info("DONE")
default:
// Ignoring
log.Debug("Ignoring event: %s", event.Status)
}
}
// statCallback receives the stats (cpu & ram) from containers and send them to
// the cuberite server
func (d *Daemon) statCallback(id string, stats *types.StatsJSON, args ...interface{}) {
containerEvent := ContainerEvent{}
containerEvent.ID = id
containerEvent.Action = "stats"
if stats != nil {
memPercent := float64(stats.MemoryStats.Usage) / float64(stats.MemoryStats.Limit) * 100.0
var cpuPercent float64
if preCPUStats, exists := d.previousCPUStats[id]; exists {
cpuPercent = calculateCPUPercent(preCPUStats, &stats.CPUStats)
}
d.previousCPUStats[id] = &CPUStats{TotalUsage: stats.CPUStats.CPUUsage.TotalUsage, SystemUsage: stats.CPUStats.SystemUsage}
containerEvent.CPU = strconv.FormatFloat(cpuPercent, 'f', 2, 64) + "%"
containerEvent.RAM = strconv.FormatFloat(memPercent, 'f', 2, 64) + "%"
} else {
// if stats == nil set Cpu and Ram to 0%
// it's a way to enforce these values
// when stopping a container
containerEvent.CPU = "0.00%"
containerEvent.RAM = "0.00%"
}
tcpMsg := TCPMessage{}
tcpMsg.Cmd = "event"
tcpMsg.Args = []string{"containers"}
tcpMsg.ID = 0
tcpMsg.Data = &containerEvent
data, err := json.Marshal(&tcpMsg)
if err != nil {
log.Println("statCallback error:", err)
return
}
separator := []byte(string('\n'))
d.tcpMessages <- append(data, separator...)
}
// execDockerCmd handles Docker commands
func (d *Daemon) execDockerCmd(args []string) {
if len(args) > 0 {
log.Debugln("execDockerCmd:", d.BinaryName, args)
cmd := exec.Command(d.BinaryName, args...)
err := cmd.Run() // will wait for command to return
if err != nil {
log.Println("Error:", err.Error())
}
}
}
// listContainers handles and reply to http requests having the path "/containers"
func (d *Daemon) listContainers() {
go func() {
containers, err := d.Client.ContainerList(context.Background(), types.ContainerListOptions{All: true})
if err != nil {
log.Println(err.Error())
return
}
for _, container := range containers {
id := container.ID
// get container name:
// use first name in array
// and remove leading '/'
// if necessary
name := ""
if len(container.Names) > 0 {
name = container.Names[0]
if len(name) > 0 && name[0] == '/' {
name = name[1:]
}
}
imageRepo, imageTag := splitRepoAndTag(container.Image)
if imageTag == "" {
imageTag = "latest"
}
containerEvent := ContainerEvent{}
containerEvent.ID = id
containerEvent.Action = "containerInfos"
containerEvent.ImageRepo = imageRepo
containerEvent.ImageTag = imageTag
containerEvent.Name = name
containerEvent.Running = container.State == "running"
data, err := containerEventToTCPMsg(containerEvent)
if err != nil {
log.Println(err)
return
}
d.tcpMessages <- append(data, '\n')
if containerEvent.Running {
// Monitor stats
d.startStatsMonitoring(containerEvent.ID)
} else {
// enforce 0% display (Cpu & Ram)
d.statCallback(containerEvent.ID, nil)
}
}
}()
}
func (d *Daemon) startStatsMonitoring(containerID string) {
d.Lock()
statsOptionsEntry, found := d.statsOptionsStore[containerID]
if !found {
statsOptionsEntry = StatsOptionsEntry{
make(chan *types.StatsJSON),
make(chan bool, 1),
}
d.statsOptionsStore[containerID] = statsOptionsEntry
}
d.Unlock()
go func() {
log.Infof("Start monitoring stats: %s", containerID)
resp, err := d.Client.ContainerStats(context.Background(), containerID, true)
if err != nil {
log.Printf("dClient.Stats err: %#v", err)
}
defer resp.Body.Close()
dec := json.NewDecoder(resp.Body)
for {
select {
case <-statsOptionsEntry.doneChan:
log.Infof("Stopping collecting stats for %s", containerID)
return
default:
v := types.StatsJSON{}
if err := dec.Decode(&v); err != nil {
dec = json.NewDecoder(io.MultiReader(dec.Buffered(), resp.Body))
if err != io.EOF {
break
}
time.Sleep(100 * time.Millisecond)
continue
}
statsOptionsEntry.statsChan <- &v
}
}
}()
go func() {
for {
select {
case stats := <-statsOptionsEntry.statsChan:
if stats != nil {
d.statCallback(containerID, stats)
}
case <-statsOptionsEntry.doneChan:
log.Println("Go routine END")
return
}
}
}()
}
// Utility functions
func calculateCPUPercent(previousCPUStats *CPUStats, newCPUStats *types.CPUStats) float64 {
var (
cpuPercent = 0.0
// calculate the change for the cpu usage of the container in between readings
cpuDelta = float64(newCPUStats.CPUUsage.TotalUsage - previousCPUStats.TotalUsage)
// calculate the change for the entire system between readings
systemDelta = float64(newCPUStats.SystemUsage - previousCPUStats.SystemUsage)
)
if systemDelta > 0.0 && cpuDelta > 0.0 {
cpuPercent = (cpuDelta / systemDelta) * float64(len(newCPUStats.CPUUsage.PercpuUsage)) * 100.0
}
return cpuPercent
}
func splitRepoAndTag(repoTag string) (string, string) {
repo := ""
tag := ""
repoAndTag := strings.Split(repoTag, ":")
if len(repoAndTag) > 0 {
repo = repoAndTag[0]
}
if len(repoAndTag) > 1 {
tag = repoAndTag[1]
}
return repo, tag
}
func containerEventToTCPMsg(containerEvent ContainerEvent) ([]byte, error) {
tcpMsg := TCPMessage{}
tcpMsg.Cmd = "event"
tcpMsg.Args = []string{"containers"}
tcpMsg.ID = 0
tcpMsg.Data = &containerEvent
data, err := json.Marshal(&tcpMsg)
if err != nil {
return nil, errors.New("containerEventToTCPMsg error: " + err.Error())
}
return data, nil
}
func (d *Daemon) apiEventToContainerEvent(event events.Message) (ContainerEvent, error) {
containerEvent := ContainerEvent{}
containerEvent.ID = event.Actor.ID
// don't try to inspect container in that case, it's already gone!
if event.Action == "destroy" {
return containerEvent, nil
}
log.Debugf("apiEventToContainerEvent: %#v\n", event)
container, err := d.Client.ContainerInspect(context.Background(), containerEvent.ID)
if err != nil {
return containerEvent, err
}
containerEvent.ImageRepo, containerEvent.ImageTag = splitRepoAndTag(event.From)
if containerEvent.ImageTag == "" {
containerEvent.ImageTag = "latest"
}
containerEvent.Name = container.Name
return containerEvent, nil
}