From: Michael Tremer Date: Sun, 29 May 2022 12:11:03 +0000 (+0000) Subject: hub: Add a new handler for communication with build workers X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6b78d51de799d8e81665bffd9e18da2d47cc3f97;p=pbs.git hub: Add a new handler for communication with build workers Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 068be00b..b0f92253 100644 --- a/Makefile.am +++ b/Makefile.am @@ -117,6 +117,7 @@ CLEANFILES += \ hub_PYTHON = \ src/hub/__init__.py \ src/hub/handlers.py \ + src/hub/jobs.py \ src/hub/queue.py hubdir = $(buildservicedir)/hub diff --git a/src/hub/__init__.py b/src/hub/__init__.py index f8073ce9..95ed5f03 100644 --- a/src/hub/__init__.py +++ b/src/hub/__init__.py @@ -7,6 +7,7 @@ import tornado.web from .. import Backend from . import handlers +from . import jobs from . import queue class Application(tornado.web.Application): @@ -35,6 +36,9 @@ class Application(tornado.web.Application): (r"/builders/jobs/(.*)/state/(.*)", handlers.BuildersJobsStateHandler), # Jobs + (r"/jobs/([0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12})/builder", + jobs.BuilderHandler), + (r"/jobs/active", handlers.JobsGetActiveHandler), (r"/jobs/latest", handlers.JobsGetLatestHandler), (r"/jobs/queue", handlers.JobsGetQueueHandler), diff --git a/src/hub/jobs.py b/src/hub/jobs.py new file mode 100644 index 00000000..dd41779d --- /dev/null +++ b/src/hub/jobs.py @@ -0,0 +1,85 @@ +#!/usr/bin/python3 +############################################################################### +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2011 Pakfire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +import json +import logging +import tornado.websocket + +from .handlers import BackendMixin, HTTPBasicAuthMixin + +log = logging.getLogger("pakfire.hub.jobs") + +class BuilderHandler(BackendMixin, HTTPBasicAuthMixin, tornado.websocket.WebSocketHandler): + """ + Builders connect to this handler when they are running a build. + + We can pass information about this build around in real time. + """ + def get_current_user(self): + name, password = self.get_basic_auth_credentials() + if name is None: + return + + return self.backend.builders.auth(name, password) + + @property + def builder(self): + return self.current_user + + @tornado.web.authenticated + def open(self, job_id): + self.job = self.backend.jobs.get_by_uuid(job_id) + if not self.job: + raise tornado.web.HTTPError(404, "Could not find job %s" % job_id) + + # Check if the builder matches + if not self.builder == self.job.builder: + raise tornado.web.HTTPError(403, "Job %s belongs to %s, not %s" % \ + (self.job, self.job.builder, self.builder)) + + log.debug("Connection opened for %s by %s" % (self.job, self.builder)) + + async def on_message(self, message): + # Decode JSON message + try: + message = json.loads(message) + except json.DecodeError as e: + log.error("Could not decode JSON message", exc_info=True) + return + + # Log message + log.debug("Received message:") + log.debug("%s" % json.dumps(message, indent=4)) + + # Get message type + t = message.get("message") + + # Handle status messages + if t == "status": + pass + + # Handle log messages + elif t == "log": + pass + + # Unknown message + else: + log.warning("Received a message of an unknown type: %s" % t) diff --git a/src/hub/queue.py b/src/hub/queue.py index 05e89a14..68790898 100644 --- a/src/hub/queue.py +++ b/src/hub/queue.py @@ -58,6 +58,8 @@ def dispatch_jobs(backend): log.debug(" No jobs waiting") continue + job.builder = builder + # Send the job to the builder connection.assign_job(job)