forked from projecteru/eru-agent
/
status.go
204 lines (182 loc) · 5 KB
/
status.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
package main
import (
"fmt"
"net/http"
"strings"
"./common"
"./defines"
"./logs"
"./metrics"
"./utils"
"github.com/fsouza/go-dockerclient"
"github.com/keimoon/gore"
)
type StatusMoniter struct {
events chan *docker.APIEvents
Apps map[string]*defines.App
}
func NewStatus() *StatusMoniter {
status := &StatusMoniter{}
status.events = make(chan *docker.APIEvents)
status.Apps = map[string]*defines.App{}
logs.Assert(common.Docker.AddEventListener(status.events), "Attacher")
return status
}
func (self *StatusMoniter) Listen() {
logs.Info("Status Monitor Start")
for event := range self.events {
logs.Debug("Status:", event.Status, event.ID, event.From)
switch event.Status {
case common.STATUS_DIE:
// Check if exists
if _, ok := self.Apps[event.ID]; ok {
go metrics.Metrics.Remove(event.ID)
delete(self.Apps, event.ID)
reportContainerDeath(event.ID)
}
case common.STATUS_START:
// if not in watching list, just ignore it
if isInWatchingSet(event.ID) {
container, err := common.Docker.InspectContainer(event.ID)
if err != nil {
logs.Info("Status inspect docker failed", err)
} else {
self.Add(event.ID, container.Name)
logs.Debug(event.ID, "cured, added in watching list")
}
}
}
}
}
func (self *StatusMoniter) getStatus(s string) string {
switch {
case strings.HasPrefix(s, "Up"):
return common.STATUS_START
default:
return common.STATUS_DIE
}
}
func (self *StatusMoniter) Watcher() {
conn, err := common.Rds.Acquire()
if err != nil || conn == nil {
logs.Assert(err, "Get redis conn")
}
defer common.Rds.Release(conn)
subs := gore.NewSubscriptions(conn)
defer subs.Close()
subKey := fmt.Sprintf("eru:agent:%s:watcher", config.HostName)
logs.Debug("Watch New Container", subKey)
subs.Subscribe(subKey)
for message := range subs.Message() {
if message == nil {
break
}
command := string(message.Message)
logs.Debug("Get command", command)
parser := strings.Split(command, "|")
control, containerID := parser[0], parser[1]
switch control {
case "+":
logs.Info("Watch", containerID)
container, err := common.Docker.InspectContainer(containerID)
if err != nil {
logs.Info("Status inspect docker failed", err)
} else {
self.Add(containerID, container.Name)
}
}
}
}
func (self *StatusMoniter) Load() {
containers, err := common.Docker.ListContainers(docker.ListContainersOptions{All: true})
if err != nil {
logs.Assert(err, "List containers")
}
conn, err := common.Rds.Acquire()
if err != nil || conn == nil {
logs.Assert(err, "Get redis conn")
}
defer common.Rds.Release(conn)
containersKey := fmt.Sprintf("eru:agent:%s:containers", config.HostName)
logs.Debug("Get targets from", containersKey)
rep, err := gore.NewCommand("SMEMBERS", containersKey).Run(conn)
if err != nil {
logs.Assert(err, "Get targets")
}
targetContainersList := []string{}
rep.Slice(&targetContainersList)
logs.Debug("Targets:", targetContainersList)
targets := map[string]struct{}{}
for _, target := range targetContainersList {
targets[target] = struct{}{}
}
logs.Info("Load container")
for _, container := range containers {
if _, ok := targets[container.ID]; !ok {
continue
}
status := self.getStatus(container.Status)
if status != common.STATUS_START {
reportContainerDeath(container.ID)
continue
}
self.Add(container.ID, container.Names[0])
}
}
func (self *StatusMoniter) Add(ID, containerName string) {
if _, ok := self.Apps[ID]; ok {
// safe add
return
}
name, entrypoint, ident := utils.GetAppInfo(containerName)
if name == "" {
// ignore
logs.Info("Container name invald", containerName)
return
}
logs.Debug("Container", name, entrypoint, ident)
app := &defines.App{ID, name, entrypoint, ident}
self.Apps[ID] = app
go metrics.Metrics.Add(ID, app)
Lenz.Attacher.Attach(ID, app)
reportContainerCure(ID)
}
func isInWatchingSet(cid string) bool {
conn, err := common.Rds.Acquire()
if err != nil || conn == nil {
logs.Assert(err, "Get redis conn")
}
defer common.Rds.Release(conn)
containersKey := fmt.Sprintf("eru:agent:%s:containers", config.HostName)
rep, err := gore.NewCommand("SISMEMBER", containersKey, cid).Run(conn)
if err != nil {
logs.Assert(err, "Get targets")
}
repInt, _ := rep.Int()
return repInt == 1
}
func reportContainerDeath(cid string) {
conn, err := common.Rds.Acquire()
if err != nil || conn == nil {
logs.Assert(err, "Get redis conn")
}
defer common.Rds.Release(conn)
rep, err := gore.NewCommand("GET", fmt.Sprintf("eru:agent:%s:container:flag", cid)).Run(conn)
if err != nil {
logs.Assert(err, "failed in GET")
}
if !rep.IsNil() {
logs.Debug(cid, "flag set, ignore")
return
}
url := fmt.Sprintf("%s/api/container/%s/kill", config.Eru.Endpoint, cid)
client := &http.Client{}
req, _ := http.NewRequest("PUT", url, nil)
client.Do(req)
}
func reportContainerCure(cid string) {
url := fmt.Sprintf("%s/api/container/%s/cure", config.Eru.Endpoint, cid)
client := &http.Client{}
req, _ := http.NewRequest("PUT", url, nil)
client.Do(req)
}