]> git.ipfire.org Git - pbs.git/commitdiff
hub: Add a new handler for communication with build workers
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 29 May 2022 12:11:03 +0000 (12:11 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 29 May 2022 12:11:03 +0000 (12:11 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/hub/__init__.py
src/hub/jobs.py [new file with mode: 0644]
src/hub/queue.py

index 068be00b78af8a951791565dea4aa84514d3b9ae..b0f9225351a97ac4291c09ce555c32eb2dc7c54c 100644 (file)
@@ -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
index f8073ce90b11b351836b39dfe63a48b16e7fd1a9..95ed5f0320aecbb87ce888a0bcac428ce02231d2 100644 (file)
@@ -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 (file)
index 0000000..dd41779
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+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)
index 05e89a14c4763a223671aa4570e1fff4ace59670..6879089852f334a4dcf868a416cb457bddd255e5 100644 (file)
@@ -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)