@@ -28,9 +28,10 @@ import (
2828// remote API
2929var dockerClient * dockerclient.DockerClient
3030
31- // version of the docker daemon which is exposing the remote API
31+ // version of the docker daemon goproxy is connected to
3232var dockerDaemonVersion string
3333
34+ //
3435type CPUStats struct {
3536 TotalUsage uint64
3637 SystemUsage uint64
@@ -40,6 +41,7 @@ type CPUStats struct {
4041// docker daemon through the docker remote API
4142var previousCPUStats map [string ]* CPUStats = make (map [string ]* CPUStats )
4243
44+ // main function of goproxy
4345func main () {
4446
4547 // goproxy is executed as a short lived process to send a request to the
@@ -60,6 +62,8 @@ func main() {
6062 return
6163 }
6264
65+ logrus .Println ("[goproxy] starting dockercraft goproxy daemon..." )
66+
6367 // init docker client object
6468 var err error
6569 dockerClient , err = dockerclient .NewDockerClient ("unix:///var/run/docker.sock" , nil )
@@ -90,105 +94,143 @@ func main() {
9094
9195// eventCallback receives and handles the docker events
9296func 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 )
165185 }
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+ }
166207
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 )
178213
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 )
182223 }
183-
184- CuberiteServerRequest (data )
185224 }
186225}
187226
188227// statCallback receives the stats (cpu & ram) from containers and send them to
189228// the cuberite server
190229func statCallback (id string , stat * dockerclient.Stats , ec chan error , args ... interface {}) {
191230
231+ // TODO: gdevillele: re-activate stats later
232+ return
233+
192234 // logrus.Debugln("STATS", id, stat)
193235 // logrus.Debugln("---")
194236 // logrus.Debugln("cpu :", float64(stat.CpuStats.CpuUsage.TotalUsage)/float64(stat.CpuStats.SystemUsage))
@@ -244,32 +286,28 @@ func execCmd(w http.ResponseWriter, r *http.Request) {
244286// listContainers handles and reply to http requests having the path "/containers"
245287func listContainers (w http.ResponseWriter , r * http.Request ) {
246288
247- // answer right away to avoid dead locks in LUA
289+ // answer right away to avoid deadlocks in LUA
248290 io .WriteString (w , "OK" )
249291
250292 go func () {
251293 containers , err := dockerClient .ListContainers (true , false , "" )
252-
253294 if err != nil {
254295 logrus .Println (err .Error ())
255296 return
256297 }
257298
258299 images , err := dockerClient .ListImages (true )
259-
260300 if err != nil {
261301 logrus .Println (err .Error ())
262302 return
263303 }
264304
265305 for i := 0 ; i < len (containers ); i ++ {
266-
267306 id := containers [i ].Id
268307 info , _ := dockerClient .InspectContainer (id )
269308 name := info .Name [1 :]
270309 imageRepo := ""
271310 imageTag := ""
272-
273311 for _ , image := range images {
274312 if image .Id == info .Image {
275313 if len (image .RepoTags ) > 0 {
0 commit comments