Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | import os,sys,logging |
| 2 | import signal, time |
| 3 | from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler |
| 4 | import threading |
| 5 | import Queue |
| 6 | |
| 7 | try: |
| 8 | import sqlite3 |
| 9 | except ImportError: |
| 10 | from pysqlite2 import dbapi2 as sqlite3 |
| 11 | |
| 12 | import bb.server.xmlrpc |
| 13 | import prserv |
| 14 | import prserv.db |
| 15 | import errno |
| 16 | |
| 17 | logger = logging.getLogger("BitBake.PRserv") |
| 18 | |
| 19 | if sys.hexversion < 0x020600F0: |
| 20 | print("Sorry, python 2.6 or later is required.") |
| 21 | sys.exit(1) |
| 22 | |
| 23 | class Handler(SimpleXMLRPCRequestHandler): |
| 24 | def _dispatch(self,method,params): |
| 25 | try: |
| 26 | value=self.server.funcs[method](*params) |
| 27 | except: |
| 28 | import traceback |
| 29 | traceback.print_exc() |
| 30 | raise |
| 31 | return value |
| 32 | |
| 33 | PIDPREFIX = "/tmp/PRServer_%s_%s.pid" |
| 34 | singleton = None |
| 35 | |
| 36 | |
| 37 | class PRServer(SimpleXMLRPCServer): |
| 38 | def __init__(self, dbfile, logfile, interface, daemon=True): |
| 39 | ''' constructor ''' |
| 40 | import socket |
| 41 | try: |
| 42 | SimpleXMLRPCServer.__init__(self, interface, |
| 43 | logRequests=False, allow_none=True) |
| 44 | except socket.error: |
| 45 | ip=socket.gethostbyname(interface[0]) |
| 46 | port=interface[1] |
| 47 | msg="PR Server unable to bind to %s:%s\n" % (ip, port) |
| 48 | sys.stderr.write(msg) |
| 49 | raise PRServiceConfigError |
| 50 | |
| 51 | self.dbfile=dbfile |
| 52 | self.daemon=daemon |
| 53 | self.logfile=logfile |
| 54 | self.working_thread=None |
| 55 | self.host, self.port = self.socket.getsockname() |
| 56 | self.pidfile=PIDPREFIX % (self.host, self.port) |
| 57 | |
| 58 | self.register_function(self.getPR, "getPR") |
| 59 | self.register_function(self.quit, "quit") |
| 60 | self.register_function(self.ping, "ping") |
| 61 | self.register_function(self.export, "export") |
| 62 | self.register_function(self.importone, "importone") |
| 63 | self.register_introspection_functions() |
| 64 | |
| 65 | self.db = prserv.db.PRData(self.dbfile) |
| 66 | self.table = self.db["PRMAIN"] |
| 67 | |
| 68 | self.requestqueue = Queue.Queue() |
| 69 | self.handlerthread = threading.Thread(target = self.process_request_thread) |
| 70 | self.handlerthread.daemon = False |
| 71 | |
| 72 | def process_request_thread(self): |
| 73 | """Same as in BaseServer but as a thread. |
| 74 | |
| 75 | In addition, exception handling is done here. |
| 76 | |
| 77 | """ |
| 78 | iter_count = 1 |
| 79 | # 60 iterations between syncs or sync if dirty every ~30 seconds |
| 80 | iterations_between_sync = 60 |
| 81 | |
| 82 | while not self.quit: |
| 83 | try: |
| 84 | (request, client_address) = self.requestqueue.get(True, 30) |
| 85 | except Queue.Empty: |
| 86 | self.table.sync_if_dirty() |
| 87 | continue |
| 88 | try: |
| 89 | self.finish_request(request, client_address) |
| 90 | self.shutdown_request(request) |
| 91 | iter_count = (iter_count + 1) % iterations_between_sync |
| 92 | if iter_count == 0: |
| 93 | self.table.sync_if_dirty() |
| 94 | except: |
| 95 | self.handle_error(request, client_address) |
| 96 | self.shutdown_request(request) |
| 97 | self.table.sync() |
| 98 | self.table.sync_if_dirty() |
| 99 | |
| 100 | def sigint_handler(self, signum, stack): |
| 101 | self.table.sync() |
| 102 | |
| 103 | def sigterm_handler(self, signum, stack): |
| 104 | self.table.sync() |
| 105 | raise SystemExit |
| 106 | |
| 107 | def process_request(self, request, client_address): |
| 108 | self.requestqueue.put((request, client_address)) |
| 109 | |
| 110 | def export(self, version=None, pkgarch=None, checksum=None, colinfo=True): |
| 111 | try: |
| 112 | return self.table.export(version, pkgarch, checksum, colinfo) |
| 113 | except sqlite3.Error as exc: |
| 114 | logger.error(str(exc)) |
| 115 | return None |
| 116 | |
| 117 | def importone(self, version, pkgarch, checksum, value): |
| 118 | return self.table.importone(version, pkgarch, checksum, value) |
| 119 | |
| 120 | def ping(self): |
| 121 | return not self.quit |
| 122 | |
| 123 | def getinfo(self): |
| 124 | return (self.host, self.port) |
| 125 | |
| 126 | def getPR(self, version, pkgarch, checksum): |
| 127 | try: |
| 128 | return self.table.getValue(version, pkgarch, checksum) |
| 129 | except prserv.NotFoundError: |
| 130 | logger.error("can not find value for (%s, %s)",version, checksum) |
| 131 | return None |
| 132 | except sqlite3.Error as exc: |
| 133 | logger.error(str(exc)) |
| 134 | return None |
| 135 | |
| 136 | def quit(self): |
| 137 | self.quit=True |
| 138 | return |
| 139 | |
| 140 | def work_forever(self,): |
| 141 | self.quit = False |
| 142 | self.timeout = 0.5 |
| 143 | |
| 144 | logger.info("Started PRServer with DBfile: %s, IP: %s, PORT: %s, PID: %s" % |
| 145 | (self.dbfile, self.host, self.port, str(os.getpid()))) |
| 146 | |
| 147 | self.handlerthread.start() |
| 148 | while not self.quit: |
| 149 | self.handle_request() |
| 150 | self.handlerthread.join() |
| 151 | self.table.sync() |
| 152 | logger.info("PRServer: stopping...") |
| 153 | self.server_close() |
| 154 | return |
| 155 | |
| 156 | def start(self): |
| 157 | if self.daemon: |
| 158 | pid = self.daemonize() |
| 159 | else: |
| 160 | pid = self.fork() |
| 161 | |
| 162 | # Ensure both the parent sees this and the child from the work_forever log entry above |
| 163 | logger.info("Started PRServer with DBfile: %s, IP: %s, PORT: %s, PID: %s" % |
| 164 | (self.dbfile, self.host, self.port, str(pid))) |
| 165 | |
| 166 | def delpid(self): |
| 167 | os.remove(self.pidfile) |
| 168 | |
| 169 | def daemonize(self): |
| 170 | """ |
| 171 | See Advanced Programming in the UNIX, Sec 13.3 |
| 172 | """ |
| 173 | try: |
| 174 | pid = os.fork() |
| 175 | if pid > 0: |
| 176 | os.waitpid(pid, 0) |
| 177 | #parent return instead of exit to give control |
| 178 | return pid |
| 179 | except OSError as e: |
| 180 | raise Exception("%s [%d]" % (e.strerror, e.errno)) |
| 181 | |
| 182 | os.setsid() |
| 183 | """ |
| 184 | fork again to make sure the daemon is not session leader, |
| 185 | which prevents it from acquiring controlling terminal |
| 186 | """ |
| 187 | try: |
| 188 | pid = os.fork() |
| 189 | if pid > 0: #parent |
| 190 | os._exit(0) |
| 191 | except OSError as e: |
| 192 | raise Exception("%s [%d]" % (e.strerror, e.errno)) |
| 193 | |
| 194 | self.cleanup_handles() |
| 195 | os._exit(0) |
| 196 | |
| 197 | def fork(self): |
| 198 | try: |
| 199 | pid = os.fork() |
| 200 | if pid > 0: |
| 201 | return pid |
| 202 | except OSError as e: |
| 203 | raise Exception("%s [%d]" % (e.strerror, e.errno)) |
| 204 | |
| 205 | bb.utils.signal_on_parent_exit("SIGTERM") |
| 206 | self.cleanup_handles() |
| 207 | os._exit(0) |
| 208 | |
| 209 | def cleanup_handles(self): |
| 210 | signal.signal(signal.SIGINT, self.sigint_handler) |
| 211 | signal.signal(signal.SIGTERM, self.sigterm_handler) |
| 212 | os.umask(0) |
| 213 | os.chdir("/") |
| 214 | |
| 215 | sys.stdout.flush() |
| 216 | sys.stderr.flush() |
| 217 | si = file('/dev/null', 'r') |
| 218 | so = file(self.logfile, 'a+') |
| 219 | se = so |
| 220 | os.dup2(si.fileno(),sys.stdin.fileno()) |
| 221 | os.dup2(so.fileno(),sys.stdout.fileno()) |
| 222 | os.dup2(se.fileno(),sys.stderr.fileno()) |
| 223 | |
| 224 | # Clear out all log handlers prior to the fork() to avoid calling |
| 225 | # event handlers not part of the PRserver |
| 226 | for logger_iter in logging.Logger.manager.loggerDict.keys(): |
| 227 | logging.getLogger(logger_iter).handlers = [] |
| 228 | |
| 229 | # Ensure logging makes it to the logfile |
| 230 | streamhandler = logging.StreamHandler() |
| 231 | streamhandler.setLevel(logging.DEBUG) |
| 232 | formatter = bb.msg.BBLogFormatter("%(levelname)s: %(message)s") |
| 233 | streamhandler.setFormatter(formatter) |
| 234 | logger.addHandler(streamhandler) |
| 235 | |
| 236 | # write pidfile |
| 237 | pid = str(os.getpid()) |
| 238 | pf = file(self.pidfile, 'w') |
| 239 | pf.write("%s\n" % pid) |
| 240 | pf.close() |
| 241 | |
| 242 | self.work_forever() |
| 243 | self.delpid() |
| 244 | |
| 245 | class PRServSingleton(object): |
| 246 | def __init__(self, dbfile, logfile, interface): |
| 247 | self.dbfile = dbfile |
| 248 | self.logfile = logfile |
| 249 | self.interface = interface |
| 250 | self.host = None |
| 251 | self.port = None |
| 252 | |
| 253 | def start(self): |
| 254 | self.prserv = PRServer(self.dbfile, self.logfile, self.interface, daemon=False) |
| 255 | self.prserv.start() |
| 256 | self.host, self.port = self.prserv.getinfo() |
| 257 | |
| 258 | def getinfo(self): |
| 259 | return (self.host, self.port) |
| 260 | |
| 261 | class PRServerConnection(object): |
| 262 | def __init__(self, host, port): |
| 263 | if is_local_special(host, port): |
| 264 | host, port = singleton.getinfo() |
| 265 | self.host = host |
| 266 | self.port = port |
| 267 | self.connection, self.transport = bb.server.xmlrpc._create_server(self.host, self.port) |
| 268 | |
| 269 | def terminate(self): |
| 270 | try: |
| 271 | logger.info("Terminating PRServer...") |
| 272 | self.connection.quit() |
| 273 | except Exception as exc: |
| 274 | sys.stderr.write("%s\n" % str(exc)) |
| 275 | |
| 276 | def getPR(self, version, pkgarch, checksum): |
| 277 | return self.connection.getPR(version, pkgarch, checksum) |
| 278 | |
| 279 | def ping(self): |
| 280 | return self.connection.ping() |
| 281 | |
| 282 | def export(self,version=None, pkgarch=None, checksum=None, colinfo=True): |
| 283 | return self.connection.export(version, pkgarch, checksum, colinfo) |
| 284 | |
| 285 | def importone(self, version, pkgarch, checksum, value): |
| 286 | return self.connection.importone(version, pkgarch, checksum, value) |
| 287 | |
| 288 | def getinfo(self): |
| 289 | return self.host, self.port |
| 290 | |
| 291 | def start_daemon(dbfile, host, port, logfile): |
| 292 | pidfile = PIDPREFIX % (host, port) |
| 293 | try: |
| 294 | pf = file(pidfile,'r') |
| 295 | pid = int(pf.readline().strip()) |
| 296 | pf.close() |
| 297 | except IOError: |
| 298 | pid = None |
| 299 | |
| 300 | if pid: |
| 301 | sys.stderr.write("pidfile %s already exist. Daemon already running?\n" |
| 302 | % pidfile) |
| 303 | return 1 |
| 304 | |
| 305 | server = PRServer(os.path.abspath(dbfile), os.path.abspath(logfile), (host,port)) |
| 306 | server.start() |
| 307 | return 0 |
| 308 | |
| 309 | def stop_daemon(host, port): |
| 310 | pidfile = PIDPREFIX % (host, port) |
| 311 | try: |
| 312 | pf = file(pidfile,'r') |
| 313 | pid = int(pf.readline().strip()) |
| 314 | pf.close() |
| 315 | except IOError: |
| 316 | pid = None |
| 317 | |
| 318 | if not pid: |
| 319 | sys.stderr.write("pidfile %s does not exist. Daemon not running?\n" |
| 320 | % pidfile) |
| 321 | |
| 322 | try: |
| 323 | PRServerConnection(host, port).terminate() |
| 324 | except: |
| 325 | logger.critical("Stop PRService %s:%d failed" % (host,port)) |
| 326 | |
| 327 | try: |
| 328 | if pid: |
| 329 | wait_timeout = 0 |
| 330 | print("Waiting for pr-server to exit.") |
| 331 | while is_running(pid) and wait_timeout < 50: |
| 332 | time.sleep(0.1) |
| 333 | wait_timeout += 1 |
| 334 | |
| 335 | if is_running(pid): |
| 336 | print("Sending SIGTERM to pr-server.") |
| 337 | os.kill(pid,signal.SIGTERM) |
| 338 | time.sleep(0.1) |
| 339 | |
| 340 | if os.path.exists(pidfile): |
| 341 | os.remove(pidfile) |
| 342 | |
| 343 | except OSError as e: |
| 344 | err = str(e) |
| 345 | if err.find("No such process") <= 0: |
| 346 | raise e |
| 347 | |
| 348 | return 0 |
| 349 | |
| 350 | def is_running(pid): |
| 351 | try: |
| 352 | os.kill(pid, 0) |
| 353 | except OSError as err: |
| 354 | if err.errno == errno.ESRCH: |
| 355 | return False |
| 356 | return True |
| 357 | |
| 358 | def is_local_special(host, port): |
| 359 | if host.strip().upper() == 'localhost'.upper() and (not port): |
| 360 | return True |
| 361 | else: |
| 362 | return False |
| 363 | |
| 364 | class PRServiceConfigError(Exception): |
| 365 | pass |
| 366 | |
| 367 | def auto_start(d): |
| 368 | global singleton |
| 369 | |
| 370 | host_params = filter(None, (d.getVar('PRSERV_HOST', True) or '').split(':')) |
| 371 | if not host_params: |
| 372 | return None |
| 373 | |
| 374 | if len(host_params) != 2: |
| 375 | logger.critical('\n'.join(['PRSERV_HOST: incorrect format', |
| 376 | 'Usage: PRSERV_HOST = "<hostname>:<port>"'])) |
| 377 | raise PRServiceConfigError |
| 378 | |
| 379 | if is_local_special(host_params[0], int(host_params[1])) and not singleton: |
| 380 | import bb.utils |
| 381 | cachedir = (d.getVar("PERSISTENT_DIR", True) or d.getVar("CACHE", True)) |
| 382 | if not cachedir: |
| 383 | logger.critical("Please set the 'PERSISTENT_DIR' or 'CACHE' variable") |
| 384 | raise PRServiceConfigError |
| 385 | bb.utils.mkdirhier(cachedir) |
| 386 | dbfile = os.path.join(cachedir, "prserv.sqlite3") |
| 387 | logfile = os.path.join(cachedir, "prserv.log") |
| 388 | singleton = PRServSingleton(os.path.abspath(dbfile), os.path.abspath(logfile), ("localhost",0)) |
| 389 | singleton.start() |
| 390 | if singleton: |
| 391 | host, port = singleton.getinfo() |
| 392 | else: |
| 393 | host = host_params[0] |
| 394 | port = int(host_params[1]) |
| 395 | |
| 396 | try: |
| 397 | connection = PRServerConnection(host,port) |
| 398 | connection.ping() |
| 399 | realhost, realport = connection.getinfo() |
| 400 | return str(realhost) + ":" + str(realport) |
| 401 | |
| 402 | except Exception: |
| 403 | logger.critical("PRservice %s:%d not available" % (host, port)) |
| 404 | raise PRServiceConfigError |
| 405 | |
| 406 | def auto_shutdown(d=None): |
| 407 | global singleton |
| 408 | if singleton: |
| 409 | host, port = singleton.getinfo() |
| 410 | try: |
| 411 | PRServerConnection(host, port).terminate() |
| 412 | except: |
| 413 | logger.critical("Stop PRService %s:%d failed" % (host,port)) |
| 414 | singleton = None |
| 415 | |
| 416 | def ping(host, port): |
| 417 | conn=PRServerConnection(host, port) |
| 418 | return conn.ping() |