@@ -28,9 +28,10 @@ import (
28
28
// remote API
29
29
var dockerClient * dockerclient.DockerClient
30
30
31
- // version of the docker daemon which is exposing the remote API
31
+ // version of the docker daemon goproxy is connected to
32
32
var dockerDaemonVersion string
33
33
34
+ //
34
35
type CPUStats struct {
35
36
TotalUsage uint64
36
37
SystemUsage uint64
@@ -40,6 +41,7 @@ type CPUStats struct {
40
41
// docker daemon through the docker remote API
41
42
var previousCPUStats map [string ]* CPUStats = make (map [string ]* CPUStats )
42
43
44
+ // main function of goproxy
43
45
func main () {
44
46
45
47
// goproxy is executed as a short lived process to send a request to the
@@ -60,6 +62,8 @@ func main() {
60
62
return
61
63
}
62
64
65
+ logrus .Println ("[goproxy] starting dockercraft goproxy daemon..." )
66
+
63
67
// init docker client object
64
68
var err error
65
69
dockerClient , err = dockerclient .NewDockerClient ("unix:///var/run/docker.sock" , nil )
@@ -90,105 +94,143 @@ func main() {
90
94
91
95
// eventCallback receives and handles the docker events
92
96
func eventCallback (event * dockerclient.Event , ec chan error , args ... interface {}) {
93
- logrus .Debugln ("--\n %+v" , * event )
94
-
95
- id := event .ID
96
-
97
- switch event .Status {
98
- case "create" :
99
- logrus .Debugln ("create event" )
100
-
101
- repo , tag := splitRepoAndTag (event .From )
102
- containerName := "<name>"
103
- containerInfo , err := dockerClient .InspectContainer (id )
104
- if err != nil {
105
- logrus .Print ("InspectContainer error:" , err .Error ())
106
- } else {
107
- containerName = containerInfo .Name
108
- }
109
-
110
- data := url.Values {
111
- "action" : {"createContainer" },
112
- "id" : {id },
113
- "name" : {containerName },
114
- "imageRepo" : {repo },
115
- "imageTag" : {tag }}
116
-
117
- CuberiteServerRequest (data )
118
-
119
- case "start" :
120
- logrus .Debugln ("start event" )
121
-
122
- repo , tag := splitRepoAndTag (event .From )
123
- containerName := "<name>"
124
- containerInfo , err := dockerClient .InspectContainer (id )
125
- if err != nil {
126
- logrus .Print ("InspectContainer error:" , err .Error ())
127
- } else {
128
- containerName = containerInfo .Name
129
- }
130
-
131
- data := url.Values {
132
- "action" : {"startContainer" },
133
- "id" : {id },
134
- "name" : {containerName },
135
- "imageRepo" : {repo },
136
- "imageTag" : {tag }}
137
-
138
- // Monitor stats
139
- dockerClient .StartMonitorStats (id , statCallback , nil )
140
- CuberiteServerRequest (data )
141
-
142
- case "stop" :
143
- // die event is enough
144
- // http://docs.docker.com/reference/api/docker_remote_api/#docker-events
145
-
146
- case "restart" :
147
- // start event is enough
148
- // http://docs.docker.com/reference/api/docker_remote_api/#docker-events
149
-
150
- case "kill" :
151
- // die event is enough
152
- // http://docs.docker.com/reference/api/docker_remote_api/#docker-events
153
-
154
- case "die" :
155
- logrus .Debugln ("die event" )
156
-
157
- // same as stop event
158
- repo , tag := splitRepoAndTag (event .From )
159
- containerName := "<name>"
160
- containerInfo , err := dockerClient .InspectContainer (id )
161
- if err != nil {
162
- logrus .Print ("InspectContainer error:" , err .Error ())
163
- } else {
164
- containerName = containerInfo .Name
97
+ // logrus.Println("[goproxy] [event] ----- event -----")
98
+ // logrus.Println("[goproxy] [event] | type :", event.Type)
99
+ // logrus.Println("[goproxy] [event] | action:", event.Action)
100
+
101
+ // handle different kind of events
102
+ switch event .Type {
103
+ case "container" :
104
+ // TODO: gdevillele: maybe check for "event.Action" instead of
105
+ // "event.Status" for event of type "container"
106
+ switch event .Status {
107
+ case "create" :
108
+ // logrus.Println("[goproxy] [event received] create")
109
+ // get container ID
110
+ containerId := event .ID
111
+ repo , tag := splitRepoAndTag (event .From )
112
+ containerName := "<name>"
113
+ containerInfo , err := dockerClient .InspectContainer (containerId )
114
+ if err != nil {
115
+ logrus .Print ("InspectContainer error:" , err .Error ())
116
+ } else {
117
+ containerName = containerInfo .Name
118
+ }
119
+ data := url.Values {
120
+ "action" : {"createContainer" },
121
+ "id" : {containerId },
122
+ "name" : {containerName },
123
+ "imageRepo" : {repo },
124
+ "imageTag" : {tag }}
125
+ CuberiteServerRequest (data )
126
+ case "start" :
127
+ // logrus.Println("[goproxy] [event received] start")
128
+ // get container ID
129
+ containerId := event .ID
130
+ repo , tag := splitRepoAndTag (event .From )
131
+ containerName := "<name>"
132
+ containerInfo , err := dockerClient .InspectContainer (containerId )
133
+ if err != nil {
134
+ logrus .Print ("InspectContainer error:" , err .Error ())
135
+ } else {
136
+ containerName = containerInfo .Name
137
+ }
138
+ data := url.Values {
139
+ "action" : {"startContainer" },
140
+ "id" : {containerId },
141
+ "name" : {containerName },
142
+ "imageRepo" : {repo },
143
+ "imageTag" : {tag }}
144
+ // Monitor stats
145
+ dockerClient .StartMonitorStats (containerId , statCallback , nil )
146
+ CuberiteServerRequest (data )
147
+ case "stop" :
148
+ // die event is enough
149
+ // http://docs.docker.com/reference/api/docker_remote_api/#docker-events
150
+ case "restart" :
151
+ // start event is enough
152
+ // http://docs.docker.com/reference/api/docker_remote_api/#docker-events
153
+ case "kill" :
154
+ // die event is enough
155
+ // http://docs.docker.com/reference/api/docker_remote_api/#docker-events
156
+ case "die" :
157
+ // logrus.Println("[goproxy] [event received] die")
158
+ // same as stop event
159
+ // get container ID
160
+ containerId := event .ID
161
+ repo , tag := splitRepoAndTag (event .From )
162
+ containerName := "<name>"
163
+ containerInfo , err := dockerClient .InspectContainer (containerId )
164
+ if err != nil {
165
+ logrus .Print ("InspectContainer error:" , err .Error ())
166
+ } else {
167
+ containerName = containerInfo .Name
168
+ }
169
+ data := url.Values {
170
+ "action" : {"stopContainer" },
171
+ "id" : {containerId },
172
+ "name" : {containerName },
173
+ "imageRepo" : {repo },
174
+ "imageTag" : {tag }}
175
+ CuberiteServerRequest (data )
176
+ case "destroy" :
177
+ // logrus.Println("[goproxy] [event received] destroy")
178
+ // get container ID
179
+ containerId := event .ID
180
+ data := url.Values {
181
+ "action" : {"destroyContainer" },
182
+ "id" : {containerId },
183
+ }
184
+ CuberiteServerRequest (data )
165
185
}
186
+ // TODO: gdevillele: disable network events for now
187
+ case "network" :
188
+ switch event .Action {
189
+ case "connect" :
190
+ // a container has been connected to a network
191
+
192
+ // id of the network
193
+ networkID := event .Actor .ID
194
+ // name of network
195
+ networkName := event .Actor .Attributes ["name" ]
196
+ // type of network
197
+ networkType := event .Actor .Attributes ["type" ]
198
+ // id of container affected
199
+ containerID := event .Actor .Attributes ["container" ]
200
+
201
+ // TODO: gdevillele: clean this
202
+ if networkName == "bridge" || networkName == "none" || networkName == "host" {
203
+ // those are default network values
204
+ // we do nothing for now
205
+ return
206
+ }
166
207
167
- data := url.Values {
168
- "action" : {"stopContainer" },
169
- "id" : {id },
170
- "name" : {containerName },
171
- "imageRepo" : {repo },
172
- "imageTag" : {tag }}
173
-
174
- CuberiteServerRequest (data )
175
-
176
- case "destroy" :
177
- logrus .Debugln ("destroy event" )
208
+ logrus .Println ("[goproxy] [event] ----- custom network connect -----" )
209
+ logrus .Println ("[goproxy] [event] | networkID:" , networkID )
210
+ logrus .Println ("[goproxy] [event] | networkName:" , networkName )
211
+ logrus .Println ("[goproxy] [event] | networkType:" , networkType )
212
+ logrus .Println ("[goproxy] [event] | containerID:" , containerID )
178
213
179
- data := url.Values {
180
- "action" : {"destroyContainer" },
181
- "id" : {id },
214
+ // send a HTTP request to the Cuberite server
215
+ data := url.Values {
216
+ "action" : {"network_connect" },
217
+ "networkId" : {networkID },
218
+ "networkName" : {networkName },
219
+ "networkType" : {networkType },
220
+ "containerId" : {containerID },
221
+ }
222
+ CuberiteServerRequest (data )
182
223
}
183
-
184
- CuberiteServerRequest (data )
185
224
}
186
225
}
187
226
188
227
// statCallback receives the stats (cpu & ram) from containers and send them to
189
228
// the cuberite server
190
229
func statCallback (id string , stat * dockerclient.Stats , ec chan error , args ... interface {}) {
191
230
231
+ // TODO: gdevillele: re-activate stats later
232
+ return
233
+
192
234
// logrus.Debugln("STATS", id, stat)
193
235
// logrus.Debugln("---")
194
236
// logrus.Debugln("cpu :", float64(stat.CpuStats.CpuUsage.TotalUsage)/float64(stat.CpuStats.SystemUsage))
@@ -244,32 +286,28 @@ func execCmd(w http.ResponseWriter, r *http.Request) {
244
286
// listContainers handles and reply to http requests having the path "/containers"
245
287
func listContainers (w http.ResponseWriter , r * http.Request ) {
246
288
247
- // answer right away to avoid dead locks in LUA
289
+ // answer right away to avoid deadlocks in LUA
248
290
io .WriteString (w , "OK" )
249
291
250
292
go func () {
251
293
containers , err := dockerClient .ListContainers (true , false , "" )
252
-
253
294
if err != nil {
254
295
logrus .Println (err .Error ())
255
296
return
256
297
}
257
298
258
299
images , err := dockerClient .ListImages (true )
259
-
260
300
if err != nil {
261
301
logrus .Println (err .Error ())
262
302
return
263
303
}
264
304
265
305
for i := 0 ; i < len (containers ); i ++ {
266
-
267
306
id := containers [i ].Id
268
307
info , _ := dockerClient .InspectContainer (id )
269
308
name := info .Name [1 :]
270
309
imageRepo := ""
271
310
imageTag := ""
272
-
273
311
for _ , image := range images {
274
312
if image .Id == info .Image {
275
313
if len (image .RepoTags ) > 0 {
0 commit comments