xmlrpc.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. #
  2. # BitBake XMLRPC Server
  3. #
  4. # Copyright (C) 2006 - 2007 Michael 'Mickey' Lauer
  5. # Copyright (C) 2006 - 2008 Richard Purdie
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License version 2 as
  9. # published by the Free Software Foundation.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License along
  17. # with this program; if not, write to the Free Software Foundation, Inc.,
  18. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. """
  20. This module implements an xmlrpc server for BitBake.
  21. Use this by deriving a class from BitBakeXMLRPCServer and then adding
  22. methods which you want to "export" via XMLRPC. If the methods have the
  23. prefix xmlrpc_, then registering those function will happen automatically,
  24. if not, you need to call register_function.
  25. Use register_idle_function() to add a function which the xmlrpc server
  26. calls from within server_forever when no requests are pending. Make sure
  27. that those functions are non-blocking or else you will introduce latency
  28. in the server's main loop.
  29. """
  30. import bb
  31. import xmlrpclib, sys
  32. from bb import daemonize
  33. from bb.ui import uievent
  34. import hashlib, time
  35. import socket
  36. import os, signal
  37. import threading
  38. try:
  39. import cPickle as pickle
  40. except ImportError:
  41. import pickle
  42. DEBUG = False
  43. from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
  44. import inspect, select, httplib
  45. from . import BitBakeBaseServer, BitBakeBaseServerConnection, BaseImplServer
  46. class BBTransport(xmlrpclib.Transport):
  47. def __init__(self, timeout):
  48. self.timeout = timeout
  49. self.connection_token = None
  50. xmlrpclib.Transport.__init__(self)
  51. # Modified from default to pass timeout to HTTPConnection
  52. def make_connection(self, host):
  53. #return an existing connection if possible. This allows
  54. #HTTP/1.1 keep-alive.
  55. if self._connection and host == self._connection[0]:
  56. return self._connection[1]
  57. # create a HTTP connection object from a host descriptor
  58. chost, self._extra_headers, x509 = self.get_host_info(host)
  59. #store the host argument along with the connection object
  60. self._connection = host, httplib.HTTPConnection(chost, timeout=self.timeout)
  61. return self._connection[1]
  62. def set_connection_token(self, token):
  63. self.connection_token = token
  64. def send_content(self, h, body):
  65. if self.connection_token:
  66. h.putheader("Bitbake-token", self.connection_token)
  67. xmlrpclib.Transport.send_content(self, h, body)
  68. def _create_server(host, port, timeout = 60):
  69. t = BBTransport(timeout)
  70. s = xmlrpclib.ServerProxy("http://%s:%d/" % (host, port), transport=t, allow_none=True)
  71. return s, t
  72. class BitBakeServerCommands():
  73. def __init__(self, server):
  74. self.server = server
  75. self.has_client = False
  76. def registerEventHandler(self, host, port):
  77. """
  78. Register a remote UI Event Handler
  79. """
  80. s, t = _create_server(host, port)
  81. # we don't allow connections if the cooker is running
  82. if (self.cooker.state in [bb.cooker.state.parsing, bb.cooker.state.running]):
  83. return None
  84. self.event_handle = bb.event.register_UIHhandler(s)
  85. return self.event_handle
  86. def unregisterEventHandler(self, handlerNum):
  87. """
  88. Unregister a remote UI Event Handler
  89. """
  90. return bb.event.unregister_UIHhandler(handlerNum)
  91. def runCommand(self, command):
  92. """
  93. Run a cooker command on the server
  94. """
  95. return self.cooker.command.runCommand(command, self.server.readonly)
  96. def getEventHandle(self):
  97. return self.event_handle
  98. def terminateServer(self):
  99. """
  100. Trigger the server to quit
  101. """
  102. self.server.quit = True
  103. print("Server (cooker) exiting")
  104. return
  105. def addClient(self):
  106. if self.has_client:
  107. return None
  108. token = hashlib.md5(str(time.time())).hexdigest()
  109. self.server.set_connection_token(token)
  110. self.has_client = True
  111. return token
  112. def removeClient(self):
  113. if self.has_client:
  114. self.server.set_connection_token(None)
  115. self.has_client = False
  116. if self.server.single_use:
  117. self.server.quit = True
  118. # This request handler checks if the request has a "Bitbake-token" header
  119. # field (this comes from the client side) and compares it with its internal
  120. # "Bitbake-token" field (this comes from the server). If the two are not
  121. # equal, it is assumed that a client is trying to connect to the server
  122. # while another client is connected to the server. In this case, a 503 error
  123. # ("service unavailable") is returned to the client.
  124. class BitBakeXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
  125. def __init__(self, request, client_address, server):
  126. self.server = server
  127. SimpleXMLRPCRequestHandler.__init__(self, request, client_address, server)
  128. def do_POST(self):
  129. try:
  130. remote_token = self.headers["Bitbake-token"]
  131. except:
  132. remote_token = None
  133. if remote_token != self.server.connection_token and remote_token != "observer":
  134. self.report_503()
  135. else:
  136. if remote_token == "observer":
  137. self.server.readonly = True
  138. else:
  139. self.server.readonly = False
  140. SimpleXMLRPCRequestHandler.do_POST(self)
  141. def report_503(self):
  142. self.send_response(503)
  143. response = 'No more client allowed'
  144. self.send_header("Content-type", "text/plain")
  145. self.send_header("Content-length", str(len(response)))
  146. self.end_headers()
  147. self.wfile.write(response)
  148. class XMLRPCProxyServer(BaseImplServer):
  149. """ not a real working server, but a stub for a proxy server connection
  150. """
  151. def __init__(self, host, port):
  152. self.host = host
  153. self.port = port
  154. class XMLRPCServer(SimpleXMLRPCServer, BaseImplServer):
  155. # remove this when you're done with debugging
  156. # allow_reuse_address = True
  157. def __init__(self, interface):
  158. """
  159. Constructor
  160. """
  161. BaseImplServer.__init__(self)
  162. if (interface[1] == 0): # anonymous port, not getting reused
  163. self.single_use = True
  164. # Use auto port configuration
  165. if (interface[1] == -1):
  166. interface = (interface[0], 0)
  167. SimpleXMLRPCServer.__init__(self, interface,
  168. requestHandler=BitBakeXMLRPCRequestHandler,
  169. logRequests=False, allow_none=True)
  170. self.host, self.port = self.socket.getsockname()
  171. self.connection_token = None
  172. #self.register_introspection_functions()
  173. self.commands = BitBakeServerCommands(self)
  174. self.autoregister_all_functions(self.commands, "")
  175. self.interface = interface
  176. self.single_use = False
  177. def addcooker(self, cooker):
  178. BaseImplServer.addcooker(self, cooker)
  179. self.commands.cooker = cooker
  180. def autoregister_all_functions(self, context, prefix):
  181. """
  182. Convenience method for registering all functions in the scope
  183. of this class that start with a common prefix
  184. """
  185. methodlist = inspect.getmembers(context, inspect.ismethod)
  186. for name, method in methodlist:
  187. if name.startswith(prefix):
  188. self.register_function(method, name[len(prefix):])
  189. def serve_forever(self):
  190. # Start the actual XMLRPC server
  191. bb.cooker.server_main(self.cooker, self._serve_forever)
  192. def _serve_forever(self):
  193. """
  194. Serve Requests. Overloaded to honor a quit command
  195. """
  196. self.quit = False
  197. while not self.quit:
  198. fds = [self]
  199. nextsleep = 0.1
  200. for function, data in self._idlefuns.items():
  201. retval = None
  202. try:
  203. retval = function(self, data, False)
  204. if retval is False:
  205. del self._idlefuns[function]
  206. elif retval is True:
  207. nextsleep = 0
  208. elif isinstance(retval, float):
  209. if (retval < nextsleep):
  210. nextsleep = retval
  211. else:
  212. fds = fds + retval
  213. except SystemExit:
  214. raise
  215. except:
  216. import traceback
  217. traceback.print_exc()
  218. if retval == None:
  219. # the function execute failed; delete it
  220. del self._idlefuns[function]
  221. pass
  222. socktimeout = self.socket.gettimeout() or nextsleep
  223. socktimeout = min(socktimeout, nextsleep)
  224. # Mirror what BaseServer handle_request would do
  225. try:
  226. fd_sets = select.select(fds, [], [], socktimeout)
  227. if fd_sets[0] and self in fd_sets[0]:
  228. self._handle_request_noblock()
  229. except IOError:
  230. # we ignore interrupted calls
  231. pass
  232. # Tell idle functions we're exiting
  233. for function, data in self._idlefuns.items():
  234. try:
  235. retval = function(self, data, True)
  236. except:
  237. pass
  238. self.server_close()
  239. return
  240. def set_connection_token(self, token):
  241. self.connection_token = token
  242. class BitBakeXMLRPCServerConnection(BitBakeBaseServerConnection):
  243. def __init__(self, serverImpl, clientinfo=("localhost", 0), observer_only = False, featureset = None):
  244. self.connection, self.transport = _create_server(serverImpl.host, serverImpl.port)
  245. self.clientinfo = clientinfo
  246. self.serverImpl = serverImpl
  247. self.observer_only = observer_only
  248. if featureset:
  249. self.featureset = featureset
  250. else:
  251. self.featureset = []
  252. def connect(self, token = None):
  253. if token is None:
  254. if self.observer_only:
  255. token = "observer"
  256. else:
  257. token = self.connection.addClient()
  258. if token is None:
  259. return None
  260. self.transport.set_connection_token(token)
  261. self.events = uievent.BBUIEventQueue(self.connection, self.clientinfo)
  262. for event in bb.event.ui_queue:
  263. self.events.queue_event(event)
  264. _, error = self.connection.runCommand(["setFeatures", self.featureset])
  265. if error:
  266. # disconnect the client, we can't make the setFeature work
  267. self.connection.removeClient()
  268. # no need to log it here, the error shall be sent to the client
  269. raise BaseException(error)
  270. return self
  271. def removeClient(self):
  272. if not self.observer_only:
  273. self.connection.removeClient()
  274. def terminate(self):
  275. # Don't wait for server indefinitely
  276. import socket
  277. socket.setdefaulttimeout(2)
  278. try:
  279. self.events.system_quit()
  280. except:
  281. pass
  282. try:
  283. self.connection.removeClient()
  284. except:
  285. pass
  286. class BitBakeServer(BitBakeBaseServer):
  287. def initServer(self, interface = ("localhost", 0)):
  288. self.interface = interface
  289. self.serverImpl = XMLRPCServer(interface)
  290. def detach(self):
  291. daemonize.createDaemon(self.serverImpl.serve_forever, "bitbake-cookerdaemon.log")
  292. del self.cooker
  293. def establishConnection(self, featureset):
  294. self.connection = BitBakeXMLRPCServerConnection(self.serverImpl, self.interface, False, featureset)
  295. return self.connection.connect()
  296. def set_connection_token(self, token):
  297. self.connection.transport.set_connection_token(token)
  298. class BitBakeXMLRPCClient(BitBakeBaseServer):
  299. def __init__(self, observer_only = False, token = None):
  300. self.token = token
  301. self.observer_only = observer_only
  302. # if we need extra caches, just tell the server to load them all
  303. pass
  304. def saveConnectionDetails(self, remote):
  305. self.remote = remote
  306. def establishConnection(self, featureset):
  307. # The format of "remote" must be "server:port"
  308. try:
  309. [host, port] = self.remote.split(":")
  310. port = int(port)
  311. except Exception as e:
  312. bb.warn("Failed to read remote definition (%s)" % str(e))
  313. raise e
  314. # We need our IP for the server connection. We get the IP
  315. # by trying to connect with the server
  316. try:
  317. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  318. s.connect((host, port))
  319. ip = s.getsockname()[0]
  320. s.close()
  321. except Exception as e:
  322. bb.warn("Could not create socket for %s:%s (%s)" % (host, port, str(e)))
  323. raise e
  324. try:
  325. self.serverImpl = XMLRPCProxyServer(host, port)
  326. self.connection = BitBakeXMLRPCServerConnection(self.serverImpl, (ip, 0), self.observer_only, featureset)
  327. return self.connection.connect(self.token)
  328. except Exception as e:
  329. bb.warn("Could not connect to server at %s:%s (%s)" % (host, port, str(e)))
  330. raise e
  331. def endSession(self):
  332. self.connection.removeClient()