-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslave.py
More file actions
executable file
·228 lines (193 loc) · 7.47 KB
/
slave.py
File metadata and controls
executable file
·228 lines (193 loc) · 7.47 KB
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
#!/usr/bin/env python
from inspect import getmembers, ismethod, getargspec
from threading import Thread, Event, current_thread
from SocketServer import ThreadingMixIn
from os import path as os_path
from re import compile as re_compile
from DocXMLRPCServer import DocXMLRPCServer
from optparse import OptionParser
from time import sleep
from common import *
cwd = re_compile(".*\/").search(os_path.realpath(__file__)).group(0)
def unwrap_kwargs(func, spec):
def wrapper(*args, **kwargs):
if args and isinstance(args[-1], list) and len(args[-1]) == 2 and "kwargs" == args[-1][0]:
return func(*args[:-1], **args[-1][1])
else:
return func(*args, **kwargs)
wrapper.__doc__ = str(spec)
if func.__doc__ is not None :
wrapper.__doc__ += "\n\n" + func.__doc__
return wrapper
class MLLSlave():
def __init__(self, port, debug) :
self.debug = debug
self.port = port
def register(self, address) :
if address not in services :
service = MLLSlaveService(self.debug, \
self.port, \
address)
append_service(address, service)
msg = "Success, bound to hostname: " + address
result = True
else :
msg = "Failed. already bound to hostname: " + address
result = False
return {"status" : 0, "msg" : msg, "result" : result}
def unregister(self, address) :
service = remove_service(address)
if service :
msg = "Success, unbound from hostname: " + address
result = True
else :
msg = "Failed. was never bound to hostname: " + address
result = False
return {"status" : 0, "msg" : msg, "result" : result}
def success(self, result, msg = "") :
mdebug(msg)
return {"status" : 0, "msg" : msg, "result" : result }
def error(self, status, msg, result) :
merr(msg)
return {"status" : status, "msg" : msg, "result" : result }
def get_functions(self):
'''
List the names of all the available MLL Slave functions
'''
return self.success(self.signatures, "success")
def get_signature(self, name):
'''
Get the list of arguments of a specific MLL Slave function
'''
return self.success(self.signatures[name], "signature")
def foo(self, bar) :
return self.success(bar)
class AsyncDocXMLRPCServer(SocketServer.ThreadingMixIn,DocXMLRPCServer): pass
services = {}
def append_service(hostname, service) :
if hostname not in services :
services[hostname] = service
service.abort = False
service.start()
return True
return False
def remove_service(hostname):
if hostname in services :
service = services[hostname]
del services[hostname]
service.stop()
service.join()
return service
return False
class MLLSlaveService (Thread):
def __init__(self, debug, port, hostname) :
super(MLLSlaveService, self).__init__()
self._stop = Event()
self.abort = False
self.aborted = False
self.port = port
self.hostname = hostname
self.slave = MLLSlave(port, debug)
mdebug("Initializing MLL Slave Service on " + hostname + ":" + str(port))
if debug is None :
self.server = AsyncDocXMLRPCServer((self.hostname, int(self.port)), allow_none = True)
else :
self.server = DocXMLRPCServer((self.hostname, int(self.port)), allow_none = True)
self.server.abort = False
self.server.aborted = False
self.server.set_server_title("MLL Slave Service (xmlrpc)")
self.server.set_server_name("MLL Slave Service (xmlrpc)")
#self.server.register_introspection_functions()
self.slave.signatures = {}
for methodtuple in getmembers(self.slave, predicate=ismethod) :
name = methodtuple[0]
if name in ["__init__", "success", "error" ] :
continue
func = getattr(self.slave, name)
argspec = getargspec(func)
spec = argspec[0]
defaults = [] if argspec[3] is None else argspec[3]
num_spec = len(spec)
num_defaults = len(defaults)
diff = num_spec - num_defaults
named = diff - 1
doc = "Usage: "
for x in range(1, diff) :
doc += spec[x] + ", "
for x in range(diff, num_spec) :
doc += spec[x] + " = " + str(defaults[x - diff]) + ", "
doc = doc[:-2]
self.slave.signatures[name] = {"args" : spec[1:], "named" : named }
self.server.register_function(unwrap_kwargs(func, doc), name)
mdebug("MLL Slave Service started")
def run(self):
mdebug("MLL Slave Service waiting for requests...")
self.server.serve_forever()
mdebug("MLL Slave Service shutting down...")
def stop (self) :
mdebug("Calling MLL Slave Service shutdown....")
self._stop.set()
self.server.shutdown()
parser = OptionParser()
parser.add_option("-p", "--port", dest = "port", default = "5050", help ="port")
parser.add_option("-H", "--host", dest = "host", default = "0.0.0.0", help
="Comma-separated list of hostnames to bind to")
parser.add_option("-D", "--daemon", dest = "daemon", action = "store_true", \
default = False, help ="Daemonize the service.")
parser.add_option("-d", "--debug_host", dest = "debug_host", \
default = False, help ="Hostname for remote debugging")
parser.add_option("-l", "--log", dest = "logfile", default = cwd +
"logs/slave.log", help ="MLL Slave Service log file.")
parser.set_defaults()
options, args = parser.parse_args()
def main() :
mica_init_logging(options.logfile)
apiservices = []
debug = True if options.debug_host else False
hostnames = options.host.split(",")
abort = True
try :
for hostname in hostnames :
wait_for_port_ready(hostname, options.port)
apiservice = MLLSlaveService(debug, \
options.port, \
hostname)
apiservices.append(apiservice)
apiservice.daemon = True
for apiservice in apiservices :
if debug and not len(apiservices) > 1:
apiservice.run()
# only debug the first hostname
break
else :
append_service(hostname, apiservice)
abort = False
# If this process is monitoring itself, then it could
# call itself and loop here while it is alive.
# So, we don't need this one:
#for apiservice in apiservices :
# remove_service(hostname)
# Instead, just join to them all.
while True :
sleep(5)
if abort :
break
except KeyboardInterrupt:
merr("CTRL-C Exiting...")
except Exception, e :
merr("Failed to startup MLL Slave Service: " + str(e))
finally :
if not abort :
minfo("Tearing down services...")
for hostname in hostnames :
remove_service(hostname)
MainThread = current_thread()
MainThread.abort = False
if options.daemon :
with DaemonContext(
working_directory=cwd,
pidfile=None,
) :
main()
else :
main()