-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathevent_container.go
More file actions
102 lines (91 loc) · 2.91 KB
/
event_container.go
File metadata and controls
102 lines (91 loc) · 2.91 KB
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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"strings"
"github.com/davecgh/go-spew/spew"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/events"
"github.com/nlopes/slack"
log "github.com/sirupsen/logrus"
)
func (d *daemon) eventContainer(e events.Message) {
if e.Status == "die" || e.Status == "oom" || e.Status == "health_status" {
exitCode := e.Actor.Attributes["exitCode"]
// 137 exit code is if Docker needs to sigkill following entrypoint
// not responding to sigterm (128 + 9 = 137)
// 143 is sigterm (128 + 15 = 143)
if exitCode == "0" || exitCode == "137" || exitCode == "143" {
return
}
// TODO - health status events
if e.Status == "health_status" {
spew.Dump(e)
}
ev := event{
ServiceName: e.Actor.Attributes["com.docker.swarm.service.name"],
ServiceDescription: e.Actor.Attributes["description"],
Repository: e.Actor.Attributes["repository"],
ImageName: e.Actor.Attributes["image"],
Experts: strings.Split(e.Actor.Attributes["experts"], ","),
ExitCode: e.Actor.Attributes["exitCode"],
EventType: e.Status,
}
if len(ev.ServiceName) > 0 {
if len(d.config.logURLFormat) > 0 {
ev.LogsURL = fmt.Sprintf(d.config.logURLFormat, ev.ServiceName)
}
if len(d.config.deployURLFormat) > 0 {
ev.ServiceDeployStatus = fmt.Sprintf(d.config.deployURLFormat, ev.ServiceName)
}
}
logReader, err := d.client.ContainerLogs(d.ctx, e.Actor.ID, types.ContainerLogsOptions{
ShowStderr: true,
ShowStdout: true,
Timestamps: true,
})
if err != nil {
log.Error(err)
} else {
logs, err := ioutil.ReadAll(io.LimitReader(logReader, 1024*1024*100)) // 100M memory
if err == nil {
logLines := strings.Split(string(logs), "\n")
logLen := len(logLines)
numLines := 300
if logLen > numLines {
ev.Logs = strings.Join(logLines[logLen-numLines:], "\n")
} else {
ev.Logs = strings.Join(logLines, "\n")
}
} else {
log.Error(err)
}
}
var text string
if len(ev.ServiceName) > 1 {
text += "*Service Name:* " + ev.ServiceName + "\n"
text += "*Service Description:* " + ev.ServiceDescription + "\n"
text += "*Service Deploy Status:* " + ev.ServiceDeployStatus + "\n"
}
if len(ev.Repository) > 1 {
text += "*Repository:* " + ev.Repository + "\n"
}
text += "*Image Name:* " + ev.ImageName + "\n"
text += "*Event Type:* " + ev.EventType + "\n"
text += "*Exit Code:* " + ev.ExitCode + "\n"
var msgOptions []slack.MsgOption
msgOptions = append(msgOptions, slack.MsgOptionText(text, false))
// this dumb option makes it use the name you give it rather than just "bot"
msgOptions = append(msgOptions, slack.MsgOptionAsUser(true))
evJSON, err := json.Marshal(ev)
if err != nil {
log.Error(err)
}
fmt.Printf("%s\n", evJSON)
if d.slackEnabled {
d.sendSlackMessage(ev.Experts, ev.Logs, msgOptions...)
}
}
}