-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgonjacserver.py
339 lines (299 loc) · 11.6 KB
/
gonjacserver.py
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import SocketServer
import socket
import os
import sys
import ast
import re
import threading
import signal
import copy
import subprocess
import gonjaccore
import gonjacreqs
import gonjacutils
import gonjacerrors
import gonjacbuilder
try:
import psutil
except Exception:
sys.stderr.write(gonjacerrors.INSTALL_PSUTIL)
sys.exit(-1)
GONJAC_PORT = 6666
LOCALHOST = "127.0.0.1"
class GonjacBaseRequest(object):
"""Base class container for requests.
After setting key-value pairs, use validate_req()
to make sure request is consistent, so as to avoid
server-side issues.
req_type : The type of the request
a_dict : Optional initializer
"""
def __init__(self, req_type, a_dict=None):
self._dict = dict()
self._dict["type"] = req_type
self.type = req_type
if a_dict:
for k, v in a_dict.iteritems():
self._dict[k] = v
def __getitem__(self, key):
if self._dict.has_key(key):
return self._dict[key]
else:
return None
def __setitem__(self, key, item):
self._dict[key] = item
def __str__(self):
return self._dict.__str__()
def __getattribute__(self, name):
if not hasattr(self, name):
return None
else:
return object.__getattribute__(self, name)
def __str__(self):
ret_dict = copy.copy(self._dict)
if ret_dict.has_key("_dict"):
del ret_dict["_dict"]
return "%s" % ret_dict
def iteritems(self):
for k, v in self._dict.iteritems():
yield k, v
def to_str(self):
return self.__str__()
def get_dict(self):
ret_dict = copy.copy(self.__dict__)
if ret_dict.has_key("_dict"):
del ret_dict["_dict"]
return ret_dict
class GonjacLocalRequest(GonjacBaseRequest):
def __init__(self, req_type, req=None):
GonjacBaseRequest.__init__(self, req_type, req)
class GonjacRemoteRequest(GonjacBaseRequest):
def __init__(self, req_type, req=None):
GonjacBaseRequest.__init__(self, req_type, req)
class GonjacHandler(SocketServer.BaseRequestHandler):
def handle(self):
data = "dummy"
while data:
data = self.request.recv(2048)
if not data:
return
try:
req = self.parse_request(data)
except gonjaccore.GonjacException as e:
print e
self.request.send(gonjacerrors.INVALID_REQUEST)
continue
request_ip, port = self.client_address
if request_ip == LOCALHOST:
#Pass the parsed dict to replicate the request as
#a GonjacLocalRequest
local_req = GonjacLocalRequest(req["type"], req)
validate_req(local_req)
print "Request from localhost"
for k, v in local_req.iteritems():
print "\t%s : %s" % (k ,v)
self.execute_local_req(local_req)
elif request_ip == gonjacutils.GonjacConfig.get_remote_ip():
remote_req = GonjacRemoteRequest(req["type"], req)
validate_req(remote_req, self.request)
print "Request from master server"
for k, v in remote_req.iteritems():
print "\t%s : %s" % (k, v)
self.execute_remote_request(remote_req)
else:
self.request.send("Permission denied for host %s" % request_ip)
def parse_request(self, reqstr):
"""Parse data from request, and attempt to parse as a dict."""
try:
obj = ast.literal_eval(reqstr)
except (SyntaxError, ValueError):
raise gonjaccore.GonjacException(gonjacerrors.UNRECOGNIZED_REQ)
if type(obj) is dict:
return obj
else:
raise gonjaccore.GonjacException(gonjacerrors.UNRECOGNIZED_REQ)
def execute_local_req(self, local_request):
if local_request.type == gonjacreqs.LOCAL_REGISTER:
gonjacutils.GonjacConfig.set_remote_ip(
local_request[gonjacreqs.GENERIC_KEY_IP]
)
sock = get_simple_client()
sock.connect((local_request[gonjacreqs.GENERIC_KEY_IP],
GONJAC_PORT))
sock.send(local_request.to_str())
reqdict = self.parse_request(sock.recv(2048))
remote_req = GonjacRemoteRequest(
reqdict[gonjacreqs.GENERIC_KEY_TYPE],
reqdict)
if not gonjacutils.is_req_regack(remote_req):
sock.send(gonjacerrors.INVALID_RESPONSE)
do_unregister()
self.request.send("fail")
else:
sock.close()
#Set
gonjacutils.GonjacConfig.set_remote_ip(
local_request[gonjacreqs.GENERIC_KEY_IP]
)
self.request.send("pass")
elif local_request.type == gonjacreqs.LOCAL_UNREGISTER:
sock = get_simple_client()
sock.connect((local_request[gonjacreqs.GENERIC_KEY_IP],
GONJAC_PORT))
sock.send(local_request.to_str())
sock.close()
elif local_request.type == gonjacreqs.LOCAL_TESTCONN:
sock = get_simple_client()
sock.connect((local_request[gonjacreqs.GENERIC_KEY_IP],
GONJAC_PORT))
sock.send(local_request.to_str())
reqdict = self.parse_request(sock.recv(2024))
req = GonjacRemoteRequest(
reqdict[gonjacreqs.GENERIC_KEY_TYPE],
reqdict)
if req.type == gonjacreqs.REMOTE_TESTCONNACK:
self.request.send("pass")
elif req.type == gonjacreqs.REMOTE_TESTCONNDENY:
self.request.send("fail")
def execute_remote_request(self, req):
if req.type == gonjacreqs.REMOTE_NEWJOB:
builder = gonjacbuilder.GonjacBuilder(
req[gonjacreqs.GENERIC_REPO],
req[gonjacreqs.GENERIC_BRANCH]
)
jobstart_req = GonjacRemoteRequest(
gonjacreqs.REMOTE_JOBSTART
)
jobstart_req[gonjacreqs.GENERIC_BUILD_ID] = builder.build_id
self.request.send(jobstart_req.to_str())
if builder.start_job():
#Build succeeded
req.type = gonjacreqs.GENERIC_JOBDONE
req[gonjacreqs.GENERIC_KEY_TYPE] = \
gonjacreqs.GENERIC_JOBDONE
req[gonjacreqs.GENERIC_URL] = \
"%s" % builder.tar_name
#Open new socket to send jobdone to master
sock = get_simple_client()
sock.connect((
self.client_address[0],
GONJAC_PORT))
sock.send(req.to_str())
sock.close()
else:
print "Build failed"
class GonjacServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
def __init__(self, server_info, handler, autobind=True):
SocketServer.TCPServer.__init__(self, server_info, handler, autobind)
self.allow_reuse_address = True
#Check if a remote ip has been configured before
remote_ip = gonjacutils.GonjacConfig().get_remote_ip()
if not gonjacutils.is_valid_ip(remote_ip):
sys.stderr.write(gonjacerrors.DO_REGISTER)
def get_simple_client():
return socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def init_gonjac_server():
child_pid = os.fork()
if child_pid == 0:
try:
subprocess.Popen("./start-web.sh")
local_server_info = ("0.0.0.0", GONJAC_PORT)
local_server = GonjacServer(local_server_info,
GonjacHandler, False)
local_server.allow_reuse_address = True
local_server.timeout = 5
local_server.server_bind()
local_server.server_activate()
print "Server listening on port %d" % GONJAC_PORT
local_server.serve_forever()
except socket.error, (value, message):
sys.stderr.write(gonjacerrors.SERVER_ALREADY_RUNNING)
sys.exit(-1)
os.wait()
def stop_gonjac_server():
for proc in psutil.process_iter():
if proc.cmdline:
cmd = reduce(lambda string, item: string + item, proc.cmdline, " ")
if "gonjac" in cmd and "--startserver" in cmd:
proc.kill()
if "SimpleHTTPServer" in cmd:
proc.kill()
sys.stderr.write(gonjacerrors.SERVER_NOT_RUNNING)
sys.exit(0)
def is_gonjac_server_running():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.bind((LOCALHOST,
GONJAC_PORT))
sock.close()
return False
except socket.error:
return True
def do_register(server_ip):
if not gonjacutils.is_valid_ip(server_ip):
raise ValueError("Invalid IP")
sock = get_simple_client()
req = GonjacLocalRequest(gonjacreqs.LOCAL_REGISTER)
req[gonjacreqs.GENERIC_KEY_IP] = server_ip
sock.connect((LOCALHOST, GONJAC_PORT))
sock.send(req.to_str())
data = sock.recv(2048)
if data == "pass":
return True
elif data == "fail":
return False
else:
return False
sock.close()
def do_unregister():
old_ip = gonjacutils.GonjacConfig.get_remote_ip()
if gonjacutils.is_valid_ip(old_ip):
req = GonjacLocalRequest(gonjacreqs.LOCAL_UNREGISTER)
sock = get_simple_client()
sock.connect((LOCALHOST, GONJAC_PORT))
sock.send(req.to_str())
sock.close()
else:
sys.stderr.write(gonjacerrors.DO_REGISTER)
gonjacutils.GonjacConfig.set_remote_ip("None")
def do_testconn():
config = gonjacutils.GonjacConfig()
config.read()
remote_ip = config.get_remote_ip()
if gonjacutils.is_valid_ip(remote_ip):
req = GonjacLocalRequest(gonjacreqs.GENERIC_KEY_TESTCONN)
sock = get_simple_client()
sock.connect((LOCALHOST, GONJAC_PORT))
sock.send(req.to_str())
data = sock.recv(2048)
if data == "pass":
return True
elif data == "fail":
return False
else:
return False
sock.close()
else:
sys.stderr.write(gonjacerrors.DO_REGISTER)
sys.exit(-1)
def validate_req(gjreq, remote_client=None):
"""Validate consistency of a GonjacRequest instance."""
if isinstance(gjreq, GonjacLocalRequest):
#Check if all keys are permitted
gonjacutils.validate_local_keys(gjreq)
gonjacutils.validate_local_type(gjreq)
if gjreq.type == gonjacreqs.LOCAL_REGISTER:
gonjacutils.validate_register(gjreq)
elif gjreq.type == gonjacreqs.LOCAL_UNREGISTER:
gonjacutils.validate_unregister(gjreq)
elif gjreq.type == gonjacreqs.GENERIC_KEY_TESTCONN:
gonjacutils.validate_testconn(gjreq)
#if gjreq.type == gonjacreqs.LOCAL_TESTCONN
elif isinstance(gjreq, GonjacRemoteRequest):
gonjacutils.validate_remote_keys(gjreq, remote_client)
gonjacutils.validate_remote_type(gjreq, remote_client)
# If request type is regack, nothing to be done
if gjreq.type == gonjacreqs.REMOTE_REGACK:
return
if gjreq.type == gonjacreqs.REMOTE_NEWJOB:
gonjacutils.validate_remote_newjob(gjreq, remote_client)