From: Michael Tremer Date: Fri, 26 May 2023 14:50:20 +0000 (+0000) Subject: repos: Implement deleting repositories X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fe17d32f1e9c92ed4db58da017fae380c54ae2be;p=pbs.git repos: Implement deleting repositories Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/__init__.py b/src/buildservice/__init__.py index e6045f39..fd248bb8 100644 --- a/src/buildservice/__init__.py +++ b/src/buildservice/__init__.py @@ -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 diff --git a/src/buildservice/repository.py b/src/buildservice/repository.py index 77942a9f..351188d8 100644 --- a/src/buildservice/repository.py +++ b/src/buildservice/repository.py @@ -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) diff --git a/src/database.sql b/src/database.sql index 7f814a0d..c4b5c94e 100644 --- a/src/database.sql +++ b/src/database.sql @@ -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: - --