Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 2 | # Copyright BitBake Contributors |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: GPL-2.0-only |
| 5 | # |
| 6 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | import os,sys,logging |
| 8 | import signal, time |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 9 | import socket |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 10 | import io |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 11 | import sqlite3 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 12 | import prserv |
| 13 | import prserv.db |
| 14 | import errno |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 15 | import bb.asyncrpc |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 16 | |
| 17 | logger = logging.getLogger("BitBake.PRserv") |
| 18 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 19 | PIDPREFIX = "/tmp/PRServer_%s_%s.pid" |
| 20 | singleton = None |
| 21 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 22 | class PRServerClient(bb.asyncrpc.AsyncServerConnection): |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 23 | def __init__(self, socket, server): |
| 24 | super().__init__(socket, "PRSERVICE", server.logger) |
| 25 | self.server = server |
| 26 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 27 | self.handlers.update({ |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 28 | "get-pr": self.handle_get_pr, |
| 29 | "test-pr": self.handle_test_pr, |
| 30 | "test-package": self.handle_test_package, |
| 31 | "max-package-pr": self.handle_max_package_pr, |
| 32 | "import-one": self.handle_import_one, |
| 33 | "export": self.handle_export, |
| 34 | "is-readonly": self.handle_is_readonly, |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 35 | }) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 36 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 37 | def validate_proto_version(self): |
| 38 | return (self.proto_version == (1, 0)) |
| 39 | |
| 40 | async def dispatch_message(self, msg): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 41 | try: |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 42 | return await super().dispatch_message(msg) |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 43 | except: |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 44 | self.server.table.sync() |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 45 | raise |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 46 | else: |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 47 | self.server.table.sync_if_dirty() |
| 48 | |
| 49 | async def handle_test_pr(self, request): |
| 50 | '''Finds the PR value corresponding to the request. If not found, returns None and doesn't insert a new value''' |
| 51 | version = request["version"] |
| 52 | pkgarch = request["pkgarch"] |
| 53 | checksum = request["checksum"] |
| 54 | |
| 55 | value = self.server.table.find_value(version, pkgarch, checksum) |
| 56 | return {"value": value} |
| 57 | |
| 58 | async def handle_test_package(self, request): |
| 59 | '''Tells whether there are entries for (version, pkgarch) in the db. Returns True or False''' |
| 60 | version = request["version"] |
| 61 | pkgarch = request["pkgarch"] |
| 62 | |
| 63 | value = self.server.table.test_package(version, pkgarch) |
| 64 | return {"value": value} |
| 65 | |
| 66 | async def handle_max_package_pr(self, request): |
| 67 | '''Finds the greatest PR value for (version, pkgarch) in the db. Returns None if no entry was found''' |
| 68 | version = request["version"] |
| 69 | pkgarch = request["pkgarch"] |
| 70 | |
| 71 | value = self.server.table.find_max_value(version, pkgarch) |
| 72 | return {"value": value} |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 73 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 74 | async def handle_get_pr(self, request): |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 75 | version = request["version"] |
| 76 | pkgarch = request["pkgarch"] |
| 77 | checksum = request["checksum"] |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 78 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 79 | response = None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 80 | try: |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 81 | value = self.server.table.get_value(version, pkgarch, checksum) |
| 82 | response = {"value": value} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 83 | except prserv.NotFoundError: |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 84 | self.logger.error("failure storing value in database for (%s, %s)",version, checksum) |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 85 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 86 | return response |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 87 | |
| 88 | async def handle_import_one(self, request): |
| 89 | response = None |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 90 | if not self.server.read_only: |
| 91 | version = request["version"] |
| 92 | pkgarch = request["pkgarch"] |
| 93 | checksum = request["checksum"] |
| 94 | value = request["value"] |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 95 | |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 96 | value = self.server.table.importone(version, pkgarch, checksum, value) |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 97 | if value is not None: |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 98 | response = {"value": value} |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 99 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 100 | return response |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 101 | |
| 102 | async def handle_export(self, request): |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 103 | version = request["version"] |
| 104 | pkgarch = request["pkgarch"] |
| 105 | checksum = request["checksum"] |
| 106 | colinfo = request["colinfo"] |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 107 | |
| 108 | try: |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 109 | (metainfo, datainfo) = self.server.table.export(version, pkgarch, checksum, colinfo) |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 110 | except sqlite3.Error as exc: |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 111 | self.logger.error(str(exc)) |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 112 | metainfo = datainfo = None |
| 113 | |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 114 | return {"metainfo": metainfo, "datainfo": datainfo} |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 115 | |
| 116 | async def handle_is_readonly(self, request): |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 117 | return {"readonly": self.server.read_only} |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 118 | |
| 119 | class PRServer(bb.asyncrpc.AsyncServer): |
| 120 | def __init__(self, dbfile, read_only=False): |
| 121 | super().__init__(logger) |
| 122 | self.dbfile = dbfile |
| 123 | self.table = None |
| 124 | self.read_only = read_only |
| 125 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 126 | def accept_client(self, socket): |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 127 | return PRServerClient(socket, self) |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 128 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 129 | def start(self): |
| 130 | tasks = super().start() |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 131 | self.db = prserv.db.PRData(self.dbfile, read_only=self.read_only) |
| 132 | self.table = self.db["PRMAIN"] |
| 133 | |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 134 | self.logger.info("Started PRServer with DBfile: %s, Address: %s, PID: %s" % |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 135 | (self.dbfile, self.address, str(os.getpid()))) |
| 136 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 137 | return tasks |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 138 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 139 | async def stop(self): |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 140 | self.table.sync_if_dirty() |
| 141 | self.db.disconnect() |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 142 | await super().stop() |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 143 | |
| 144 | def signal_handler(self): |
| 145 | super().signal_handler() |
| 146 | if self.table: |
| 147 | self.table.sync() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 148 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 149 | class PRServSingleton(object): |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 150 | def __init__(self, dbfile, logfile, host, port): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 151 | self.dbfile = dbfile |
| 152 | self.logfile = logfile |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 153 | self.host = host |
| 154 | self.port = port |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 155 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 156 | def start(self): |
| 157 | self.prserv = PRServer(self.dbfile) |
| 158 | self.prserv.start_tcp_server(socket.gethostbyname(self.host), self.port) |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 159 | self.process = self.prserv.serve_as_process(log_level=logging.WARNING) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 160 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 161 | if not self.prserv.address: |
| 162 | raise PRServiceConfigError |
| 163 | if not self.port: |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 164 | self.port = int(self.prserv.address.rsplit(":", 1)[1]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 165 | |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 166 | def run_as_daemon(func, pidfile, logfile): |
| 167 | """ |
| 168 | See Advanced Programming in the UNIX, Sec 13.3 |
| 169 | """ |
| 170 | try: |
| 171 | pid = os.fork() |
| 172 | if pid > 0: |
| 173 | os.waitpid(pid, 0) |
| 174 | #parent return instead of exit to give control |
| 175 | return pid |
| 176 | except OSError as e: |
| 177 | raise Exception("%s [%d]" % (e.strerror, e.errno)) |
| 178 | |
| 179 | os.setsid() |
| 180 | """ |
| 181 | fork again to make sure the daemon is not session leader, |
| 182 | which prevents it from acquiring controlling terminal |
| 183 | """ |
| 184 | try: |
| 185 | pid = os.fork() |
| 186 | if pid > 0: #parent |
| 187 | os._exit(0) |
| 188 | except OSError as e: |
| 189 | raise Exception("%s [%d]" % (e.strerror, e.errno)) |
| 190 | |
| 191 | os.chdir("/") |
| 192 | |
| 193 | sys.stdout.flush() |
| 194 | sys.stderr.flush() |
| 195 | |
| 196 | # We could be called from a python thread with io.StringIO as |
| 197 | # stdout/stderr or it could be 'real' unix fd forking where we need |
| 198 | # to physically close the fds to prevent the program launching us from |
| 199 | # potentially hanging on a pipe. Handle both cases. |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 200 | si = open("/dev/null", "r") |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 201 | try: |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 202 | os.dup2(si.fileno(), sys.stdin.fileno()) |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 203 | except (AttributeError, io.UnsupportedOperation): |
| 204 | sys.stdin = si |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 205 | so = open(logfile, "a+") |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 206 | try: |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 207 | os.dup2(so.fileno(), sys.stdout.fileno()) |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 208 | except (AttributeError, io.UnsupportedOperation): |
| 209 | sys.stdout = so |
| 210 | try: |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 211 | os.dup2(so.fileno(), sys.stderr.fileno()) |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 212 | except (AttributeError, io.UnsupportedOperation): |
| 213 | sys.stderr = so |
| 214 | |
| 215 | # Clear out all log handlers prior to the fork() to avoid calling |
| 216 | # event handlers not part of the PRserver |
| 217 | for logger_iter in logging.Logger.manager.loggerDict.keys(): |
| 218 | logging.getLogger(logger_iter).handlers = [] |
| 219 | |
| 220 | # Ensure logging makes it to the logfile |
| 221 | streamhandler = logging.StreamHandler() |
| 222 | streamhandler.setLevel(logging.DEBUG) |
| 223 | formatter = bb.msg.BBLogFormatter("%(levelname)s: %(message)s") |
| 224 | streamhandler.setFormatter(formatter) |
| 225 | logger.addHandler(streamhandler) |
| 226 | |
| 227 | # write pidfile |
| 228 | pid = str(os.getpid()) |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 229 | with open(pidfile, "w") as pf: |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 230 | pf.write("%s\n" % pid) |
| 231 | |
| 232 | func() |
| 233 | os.remove(pidfile) |
| 234 | os._exit(0) |
| 235 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 236 | def start_daemon(dbfile, host, port, logfile, read_only=False): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 237 | ip = socket.gethostbyname(host) |
| 238 | pidfile = PIDPREFIX % (ip, port) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 239 | try: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 240 | with open(pidfile) as pf: |
| 241 | pid = int(pf.readline().strip()) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 242 | except IOError: |
| 243 | pid = None |
| 244 | |
| 245 | if pid: |
| 246 | sys.stderr.write("pidfile %s already exist. Daemon already running?\n" |
| 247 | % pidfile) |
| 248 | return 1 |
| 249 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 250 | dbfile = os.path.abspath(dbfile) |
| 251 | def daemon_main(): |
| 252 | server = PRServer(dbfile, read_only=read_only) |
| 253 | server.start_tcp_server(ip, port) |
| 254 | server.serve_forever() |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 255 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 256 | run_as_daemon(daemon_main, pidfile, os.path.abspath(logfile)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 257 | return 0 |
| 258 | |
| 259 | def stop_daemon(host, port): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 260 | import glob |
| 261 | ip = socket.gethostbyname(host) |
| 262 | pidfile = PIDPREFIX % (ip, port) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 263 | try: |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 264 | with open(pidfile) as pf: |
| 265 | pid = int(pf.readline().strip()) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 266 | except IOError: |
| 267 | pid = None |
| 268 | |
| 269 | if not pid: |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 270 | # when server starts at port=0 (i.e. localhost:0), server actually takes another port, |
| 271 | # so at least advise the user which ports the corresponding server is listening |
| 272 | ports = [] |
| 273 | portstr = "" |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 274 | for pf in glob.glob(PIDPREFIX % (ip, "*")): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 275 | bn = os.path.basename(pf) |
| 276 | root, _ = os.path.splitext(bn) |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 277 | ports.append(root.split("_")[-1]) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 278 | if len(ports): |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 279 | portstr = "Wrong port? Other ports listening at %s: %s" % (host, " ".join(ports)) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 280 | |
| 281 | sys.stderr.write("pidfile %s does not exist. Daemon not running? %s\n" |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 282 | % (pidfile, portstr)) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 283 | return 1 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 284 | |
| 285 | try: |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 286 | if is_running(pid): |
| 287 | print("Sending SIGTERM to pr-server.") |
| 288 | os.kill(pid, signal.SIGTERM) |
| 289 | time.sleep(0.1) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 290 | |
Patrick Williams | b58112e | 2024-03-07 11:16:36 -0600 | [diff] [blame] | 291 | try: |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 292 | os.remove(pidfile) |
Patrick Williams | b58112e | 2024-03-07 11:16:36 -0600 | [diff] [blame] | 293 | except FileNotFoundError: |
| 294 | # The PID file might have been removed by the exiting process |
| 295 | pass |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 296 | |
| 297 | except OSError as e: |
| 298 | err = str(e) |
| 299 | if err.find("No such process") <= 0: |
| 300 | raise e |
| 301 | |
| 302 | return 0 |
| 303 | |
| 304 | def is_running(pid): |
| 305 | try: |
| 306 | os.kill(pid, 0) |
| 307 | except OSError as err: |
| 308 | if err.errno == errno.ESRCH: |
| 309 | return False |
| 310 | return True |
| 311 | |
| 312 | def is_local_special(host, port): |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 313 | if (host == "localhost" or host == "127.0.0.1") and not port: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 314 | return True |
| 315 | else: |
| 316 | return False |
| 317 | |
| 318 | class PRServiceConfigError(Exception): |
| 319 | pass |
| 320 | |
| 321 | def auto_start(d): |
| 322 | global singleton |
| 323 | |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 324 | host_params = list(filter(None, (d.getVar("PRSERV_HOST") or "").split(":"))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 325 | if not host_params: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 326 | # Shutdown any existing PR Server |
| 327 | auto_shutdown() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 328 | return None |
| 329 | |
| 330 | if len(host_params) != 2: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 331 | # Shutdown any existing PR Server |
| 332 | auto_shutdown() |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 333 | logger.critical("\n".join(["PRSERV_HOST: incorrect format", |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 334 | 'Usage: PRSERV_HOST = "<hostname>:<port>"'])) |
| 335 | raise PRServiceConfigError |
| 336 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 337 | host = host_params[0].strip().lower() |
| 338 | port = int(host_params[1]) |
| 339 | if is_local_special(host, port): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 340 | import bb.utils |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 341 | cachedir = (d.getVar("PERSISTENT_DIR") or d.getVar("CACHE")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 342 | if not cachedir: |
| 343 | logger.critical("Please set the 'PERSISTENT_DIR' or 'CACHE' variable") |
| 344 | raise PRServiceConfigError |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 345 | dbfile = os.path.join(cachedir, "prserv.sqlite3") |
| 346 | logfile = os.path.join(cachedir, "prserv.log") |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 347 | if singleton: |
| 348 | if singleton.dbfile != dbfile: |
| 349 | # Shutdown any existing PR Server as doesn't match config |
| 350 | auto_shutdown() |
| 351 | if not singleton: |
| 352 | bb.utils.mkdirhier(cachedir) |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 353 | singleton = PRServSingleton(os.path.abspath(dbfile), os.path.abspath(logfile), host, port) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 354 | singleton.start() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 355 | if singleton: |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 356 | host = singleton.host |
| 357 | port = singleton.port |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 358 | |
| 359 | try: |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 360 | ping(host, port) |
| 361 | return str(host) + ":" + str(port) |
| 362 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 363 | except Exception: |
| 364 | logger.critical("PRservice %s:%d not available" % (host, port)) |
| 365 | raise PRServiceConfigError |
| 366 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 367 | def auto_shutdown(): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 368 | global singleton |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 369 | if singleton and singleton.process: |
| 370 | singleton.process.terminate() |
| 371 | singleton.process.join() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 372 | singleton = None |
| 373 | |
| 374 | def ping(host, port): |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 375 | from . import client |
| 376 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 377 | with client.PRClient() as conn: |
| 378 | conn.connect_tcp(host, port) |
| 379 | return conn.ping() |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 380 | |
| 381 | def connect(host, port): |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 382 | from . import client |
| 383 | |
| 384 | global singleton |
| 385 | |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 386 | if host.strip().lower() == "localhost" and not port: |
| 387 | host = "localhost" |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 388 | port = singleton.port |
| 389 | |
| 390 | conn = client.PRClient() |
| 391 | conn.connect_tcp(host, port) |
| 392 | return conn |