]> git.ipfire.org Git - pakfire.git/commitdiff
daemon: Write result into a temporary directory and upload the packages
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 1 Nov 2022 12:49:36 +0000 (12:49 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 1 Nov 2022 12:49:36 +0000 (12:49 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/daemon.py
src/pakfire/hub.py

index 73cfa231169a966b61a6253115e286fdcfd7a7c4..bae4301b0df5bfb9120e52602e73e2945ab445a1 100644 (file)
@@ -2,6 +2,7 @@
 
 import asyncio
 import functools
+import glob
 import json
 import logging
 import logging.handlers
@@ -239,30 +240,38 @@ class Worker(multiprocessing.Process):
                # Setup build logger
                logger = BuildLogger(self.log, self.job)
 
-               # Run the build
-               try:
-                       build = self._build(pkg, arch=arch,
-                               logger=logger._log, build_id=job_id)
-
-                       # Wait until the build process is done and stream the log in the meantime
-                       while not build.done():
-                               await logger.stream(timeout=1)
-
-               # Catch any other Exception
-               except Exception as e:
-                       raise e
+               # Create a temporary directory in which the built packages will be copied
+               with tempfile.TemporaryDirectory(prefix="pakfire-packages-") as target:
+                       packages = None
 
-               # The build has finished successfully
-               else:
-                       success = True
-
-               # Notify the hub that the job has finished
-               finally:
-                       await self.hub.finish_job(
-                               job_id,
-                               success=success,
-                               log=logger.logfile.name,
-                       )
+                       # Run the build
+                       try:
+                               build = self._build(pkg, arch=arch, target=target,
+                                       logger=logger._log, build_id=job_id)
+
+                               # Wait until the build process is done and stream the log in the meantime
+                               while not build.done():
+                                       await logger.stream(timeout=1)
+
+                       # Catch any other Exception
+                       except Exception as e:
+                               raise e
+
+                       # The build has finished successfully
+                       else:
+                               success = True
+
+                               # Find any packages
+                               packages = glob.glob("%s/*.pfm" % target)
+
+                       # Notify the hub that the job has finished
+                       finally:
+                               await self.hub.finish_job(
+                                       job_id,
+                                       success=success,
+                                       log=logger.logfile.name,
+                                       packages=packages,
+                               )
 
        def _build(self, pkg, arch=None, logger=None, **kwargs):
                """
index d14ac089e2be4dc6c1115fb7dbff5b84101e4db4..2c5df4fffed463ccb8f50dec4f003769b5ced18c 100644 (file)
@@ -276,6 +276,34 @@ class Hub(object):
        async def delete_upload(self, upload_id):
                await self._request("DELETE", "/uploads", id=upload_id)
 
+       async def upload_multi(self, *paths, show_progress=True):
+               """
+                       Upload multiple files
+
+                       If one file could not be uploaded, any other uploads will be deleted
+               """
+               uploads = []
+
+               # Upload everything
+               try:
+                       for path in paths:
+                               upload = await self.upload(path, show_progress=show_progress)
+
+                               # Store the upload ID
+                               uploads.append(upload)
+
+               except Exception as e:
+                       # Remove any previous uploads
+                       await asyncio.gather(
+                               *(self.delete_upload(upload) for upload in uploads),
+                       )
+
+                       # Raise the exception
+                       raise e
+
+               # Return the IDs of the uploads
+               return uploads
+
        @staticmethod
        def _stream_file(path, size, p, write):
                # Start the progressbar
@@ -438,7 +466,7 @@ class Hub(object):
                # Return a Job proxy
                return Job(self, id, conn)
 
-       async def finish_job(self, job_id, success, log):
+       async def finish_job(self, job_id, success, packages=None, log=None):
                """
                        Will tell the hub that a job has finished
                """
@@ -446,9 +474,13 @@ class Hub(object):
                if log:
                        log = await self.upload(log, filename="%s.log" % job_id)
 
+               # Upload the packages
+               if packages:
+                       packages = await self.upload_multi(*packages)
+
                # Send the request
                response = await self._request("POST", "/jobs/%s/finished" % job_id,
-                       success=success, log=log)
+                       success=success, log=log, packages=packages)
 
                # Handle the response
                # XXX TODO