From 8dfdf9940001e37c94945a6e831be1aedcb6d55f Mon Sep 17 00:00:00 2001 From: Jakub Kadlčík Date: Jan 11 2019 10:11:48 +0000 Subject: [frontend][backend] remove data from outdated chroots --- diff --git a/backend/backend/actions.py b/backend/backend/actions.py index 404ba36..73f1506 100644 --- a/backend/backend/actions.py +++ b/backend/backend/actions.py @@ -289,6 +289,22 @@ class Action(object): except CreateRepoError: self.log.exception("Error making local repo: %s", full_path) + def handle_delete_chroot(self): + self.log.info("Action delete project chroot.") + + ext_data = json.loads(self.data["data"]) + ownername = ext_data["ownername"] + projectname = ext_data["projectname"] + chrootname = ext_data["chrootname"] + + chroot_path = os.path.join(self.destdir, ownername, projectname, chrootname) + self.log.info("Going to delete: %s", chroot_path) + + if not os.path.isdir(chroot_path): + self.log.error("Directory %s not found", chroot_path) + return + shutil.rmtree(chroot_path) + def handle_generate_gpg_key(self, result): ext_data = json.loads(self.data["data"]) self.log.info("Action generate gpg key: %s", ext_data) @@ -456,6 +472,8 @@ class Action(object): self.handle_delete_project(result) elif self.data["object_type"] == "build": self.handle_delete_build() + elif self.data["object_type"] == "chroot": + self.handle_delete_chroot() result.result = ActionResult.SUCCESS diff --git a/frontend/coprs_frontend/commands/delete_outdated_chroots.py b/frontend/coprs_frontend/commands/delete_outdated_chroots.py new file mode 100644 index 0000000..34881d7 --- /dev/null +++ b/frontend/coprs_frontend/commands/delete_outdated_chroots.py @@ -0,0 +1,29 @@ +from flask_script import Command, Option +from coprs import db +from coprs.logic import coprs_logic, actions_logic + + +class DeleteOutdatedChrootsCommand(Command): + """ + Delete data in all chroots that are considered as outdated. That means, the chroot is EOL + and the preservation period is over because admin of the project didn't extend its duration. + """ + option_list = [ + Option("--dry-run", action="store_true", + help="Do not actually remove any data, but rather print information on stdout"), + ] + + def run(self, dry_run): + self.dry_run = dry_run + + chroots = coprs_logic.CoprChrootsLogic \ + .filter_outdated_to_be_deleted(coprs_logic.CoprChrootsLogic.get_multiple()) + for chroot in chroots: + self.delete(chroot) + db.session.commit() + + def delete(self, chroot): + if self.dry_run: + print("Add delete_chroot action for {} in {}".format(chroot.name, chroot.copr.full_name)) + else: + actions_logic.ActionsLogic.send_delete_chroot(chroot) diff --git a/frontend/coprs_frontend/coprs/logic/actions_logic.py b/frontend/coprs_frontend/coprs/logic/actions_logic.py index 1cebde7..65d75bd 100644 --- a/frontend/coprs_frontend/coprs/logic/actions_logic.py +++ b/frontend/coprs_frontend/coprs/logic/actions_logic.py @@ -261,3 +261,25 @@ class ActionsLogic(object): created_on=int(time.time()), ) db.session.add(action) + + @classmethod + def send_delete_chroot(cls, copr_chroot): + """ + Schedules deletion of a chroot directory from project + Useful to remove outdated chroots + :type build: models.CoprChroot + """ + data_dict = { + "ownername": copr_chroot.copr.owner_name, + "projectname": copr_chroot.copr.name, + "chrootname": copr_chroot.name, + } + + action = models.Action( + action_type=ActionTypeEnum("delete"), + object_type="chroot", + object_id=None, + data=json.dumps(data_dict), + created_on=int(time.time()) + ) + db.session.add(action) diff --git a/frontend/coprs_frontend/coprs/logic/coprs_logic.py b/frontend/coprs_frontend/coprs/logic/coprs_logic.py index 2decfda..841e3d9 100644 --- a/frontend/coprs_frontend/coprs/logic/coprs_logic.py +++ b/frontend/coprs_frontend/coprs/logic/coprs_logic.py @@ -678,6 +678,10 @@ class CoprChrootsLogic(object): def filter_outdated(cls, query): return query.filter(models.CoprChroot.delete_after >= datetime.datetime.now()) + @classmethod + def filter_outdated_to_be_deleted(cls, query): + return query.filter(models.CoprChroot.delete_after < datetime.datetime.now()) + class MockChrootsLogic(object): @classmethod diff --git a/frontend/coprs_frontend/manage.py b/frontend/coprs_frontend/manage.py index b8a61ee..0a7def5 100755 --- a/frontend/coprs_frontend/manage.py +++ b/frontend/coprs_frontend/manage.py @@ -36,6 +36,7 @@ commands = { "update_graphs": "UpdateGraphsDataCommand", "vacuum_graphs": "RemoveGraphsDataCommand", "notify_outdated_chroots": "NotifyOutdatedChrootsCommand", + "delete_outdated_chroots": "DeleteOutdatedChrootsCommand", }