]> git.ipfire.org Git - pbs.git/commitdiff
repos: Implement deleting repositories
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 26 May 2023 14:50:20 +0000 (14:50 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 26 May 2023 14:50:20 +0000 (14:50 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/buildservice/__init__.py
src/buildservice/repository.py
src/database.sql

index e6045f39b6535bc0e2c6830ef9b9b36773edbf26..fd248bb8720acf8f19e288e10a5414b5be61fb3b 100644 (file)
@@ -389,6 +389,21 @@ class Backend(object):
 
                        log.debug("  Cleaned up %s..." % path)
 
+       async def rmtree(self, path):
+               """
+                       Removes a directory recursively
+               """
+               # Normalize the path
+               path = os.path.abspath(path)
+
+               # Check if the path is within our base directory
+               if not path.startswith(self.basepath):
+                       raise OSError("Cannot delete %s which is outside %s" % (path, self.basepath))
+
+               log.debug("Removing %s..." % path)
+
+               await asyncio.to_thread(shutil.rmtree, path)
+
        def tempfile(self, mode="w+b", delete=True):
                """
                        Returns an open file handle to a new temporary file
index 77942a9fe150686bda236738e7c117ea07ee5919..351188d895a6bc1ea3381a792d12c36acd5395ac 100644 (file)
@@ -1053,3 +1053,21 @@ class Repository(base.DataObject):
 
                                # Move the new repository data to its destination
                                shutil.move(t, path)
+
+       # Delete
+
+       async def delete(self, user=None):
+               """
+                       Deletes this repository
+               """
+               # Mark as deleted
+               self._set_attribute_now("deleted_at")
+               if user:
+                       self._set_attribute("deleted_by", user)
+
+               # Local path
+               path = self.local_path()
+
+               # Lock the repository and remove all files
+               async with self.lock:
+                       await self.backend.rmtree(path)
index 7f814a0d880f78dc7cb74a6dc9a27f93b8dcf414..c4b5c94e3ce9d41391b449d568fd0031dd96cb0e 100644 (file)
@@ -839,7 +839,8 @@ CREATE TABLE public.repositories (
     priority integer,
     owner_id integer,
     created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
-    listed boolean DEFAULT true NOT NULL
+    listed boolean DEFAULT true NOT NULL,
+    deleted_by integer
 );
 
 
@@ -2257,6 +2258,14 @@ ALTER TABLE ONLY public.repo_builds
     ADD CONSTRAINT repo_builds_repo_id FOREIGN KEY (repo_id) REFERENCES public.repositories(id);
 
 
+--
+-- Name: repositories repositories_deleted_by; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY public.repositories
+    ADD CONSTRAINT repositories_deleted_by FOREIGN KEY (deleted_by) REFERENCES public.users(id);
+
+
 --
 -- Name: repositories repositories_distro_id; Type: FK CONSTRAINT; Schema: public; Owner: -
 --