Skip to content
This repository was archived by the owner on Aug 12, 2024. It is now read-only.

Commit 6e2f0d6

Browse files
committed
hackathon beacons work
Signed-off-by: Gaetan de Villele <[email protected]>
1 parent 1d621e7 commit 6e2f0d6

File tree

5 files changed

+438
-111
lines changed

5 files changed

+438
-111
lines changed

go/src/goproxy/main.go

+132-94
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ import (
2828
// remote API
2929
var 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
3232
var dockerDaemonVersion string
3333

34+
//
3435
type CPUStats struct {
3536
TotalUsage uint64
3637
SystemUsage uint64
@@ -40,6 +41,7 @@ type CPUStats struct {
4041
// docker daemon through the docker remote API
4142
var previousCPUStats map[string]*CPUStats = make(map[string]*CPUStats)
4243

44+
// main function of goproxy
4345
func 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
9296
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)
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
190229
func 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"
245287
func 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 {

start.sh

+5
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@ goproxy &> /srv/world/goproxy_out &
66

77
# start Minecraft C++ server
88
cd /srv/world
9+
10+
# GOPROXY DEVELOPMENT: start cuberite as a daemon
11+
#../cuberite_server/Cuberite -d
912
../cuberite_server/Cuberite
13+
14+
/bin/bash

world/Plugins/Docker/container.lua

+36-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CONTAINER_STOPPED = 2
1111
-- the Minecraft world
1212
function NewContainer()
1313
c = {
14+
-- attributes
1415
displayed = false,
1516
x = 0,
1617
z = 0,
@@ -19,18 +20,31 @@ function NewContainer()
1920
imageRepo="",
2021
imageTag="",
2122
running=false,
23+
networks={}, -- array of network objects
24+
-- methods
2225
init=Container.init,
2326
setInfos=Container.setInfos,
2427
destroy=Container.destroy,
2528
display=Container.display,
2629
updateMemSign=Container.updateMemSign,
2730
updateCPUSign=Container.updateCPUSign,
28-
addGround=Container.addGround
31+
addGround=Container.addGround,
32+
connectNetwork=Container.connectNetwork
2933
}
3034
return c
3135
end
3236

33-
Container = {displayed = false, x = 0, z = 0, name="",id="",imageRepo="",imageTag="",running=false}
37+
Container = {
38+
displayed = false,
39+
x = 0,
40+
z = 0,
41+
name="",
42+
id="",
43+
imageRepo="",
44+
imageTag="",
45+
running=false,
46+
networks={}
47+
}
3448

3549
-- Container:init sets Container's position
3650
function Container:init(x,z)
@@ -152,20 +166,17 @@ function Container:display(running)
152166
setBlock(UpdateQueue,self.x+1,GROUND_LEVEL+3,self.z+1,E_BLOCK_LEVER,9)
153167
end
154168

155-
156169
-- remove button
157170

158171
setBlock(UpdateQueue,self.x+2,GROUND_LEVEL + 3,self.z + 2,E_BLOCK_WALLSIGN,E_META_CHEST_FACING_XM)
159172
updateSign(UpdateQueue,self.x+2,GROUND_LEVEL + 3,self.z + 2,"","REMOVE","---->","",2)
160173

161174
setBlock(UpdateQueue,self.x+2,GROUND_LEVEL+3,self.z+3,E_BLOCK_STONE_BUTTON,E_BLOCK_BUTTON_XM)
162175

163-
164176
-- door
165177
-- Cuberite bug with Minecraft 1.8 apparently, doors are not displayed correctly
166178
-- setBlock(UpdateQueue,self.x+2,GROUND_LEVEL+2,self.z,E_BLOCK_WOODEN_DOOR,E_META_CHEST_FACING_ZM)
167179

168-
169180
for px=self.x, self.x+3
170181
do
171182
for pz=self.z, self.z+4
@@ -213,3 +224,23 @@ function Container:addGround()
213224
end
214225
end
215226
end
227+
228+
-- Container:connectNetwork
229+
function Container:connectNetwork(network)
230+
-- check if the container is already connected to this network
231+
found = false
232+
for i=1, table.getn(self.networks) do
233+
if self.networks[i].id == network.id then
234+
found = true
235+
break
236+
end
237+
end
238+
if found == false then
239+
-- TODO: gdevillele: support multiple networks per container
240+
LOG("[CONTAINER] [connectNetwork] container is connected to a new network")
241+
table.insert(self.networks, network)
242+
setBlock(UpdateQueue, self.x+3, GROUND_LEVEL+5, self.z, E_BLOCK_WOOL, E_META_WOOL_RED)
243+
else
244+
-- container already know it is connected to this network, we do noting
245+
end
246+
end

0 commit comments

Comments
 (0)