def f_actions(self): # if using actions, we need to flush coprs into db, so that we can get # their ids self.f_db() self.a1 = models.Action( action_type=helpers.ActionTypeEnum("rename"), object_type="copr", object_id=self.c1.id, old_value="{0}/{1}".format(self.c1.user.name, self.c1.name), new_value="{0}/new_name".format(self.c1.user.name), created_on=int(time.time())) self.a2 = models.Action( action_type=helpers.ActionTypeEnum("rename"), object_type="copr", object_id=self.c2.id, old_value="{0}/{1}".format(self.c2.user.name, self.c2.name), new_value="{0}/new_name2".format(self.c2.user.name), created_on=int(time.time())) self.a3 = models.Action(action_type=helpers.ActionTypeEnum("delete"), object_type="copr", object_id=100, old_value="asd/qwe", new_value=None, result=helpers.BackendResultEnum("success"), created_on=int(time.time())) self.db.session.add_all([self.a1, self.a2, self.a3])
def __unicode__(self): if self.action_type == helpers.ActionTypeEnum("delete"): return "Deleting {0} {1}".format(self.object_type, self.old_value) elif self.action_type == helpers.ActionTypeEnum("rename"): return "Renaming {0} from {1} to {2}.".format( self.object_type, self.old_value, self.new_value) elif self.action_type == helpers.ActionTypeEnum("legal-flag"): return "Legal flag on copr {0}.".format(self.old_value) return "Action {0} on {1}, old value: {2}, new value: {3}.".format( self.action_type, self.object_type, self.old_value, self.new_value)
def unfinished_blocking_actions_for(cls, copr): blocking_actions = [helpers.ActionTypeEnum("rename"), helpers.ActionTypeEnum("delete")] actions = (models.Action.query .filter(models.Action.object_type == "copr") .filter(models.Action.object_id == copr.id) .filter(models.Action.result == helpers.BackendResultEnum("waiting")) .filter(models.Action.action_type.in_(blocking_actions))) return actions
def update(cls, user, copr, check_for_duplicates=True): cls.raise_if_unfinished_blocking_action( user, copr, "Can't change this project name," " another operation is in progress: {action}") users_logic.UsersLogic.raise_if_cant_update_copr( user, copr, "Only owners and admins may update their projects.") existing = cls.exists_for_user(copr.owner, copr.name).first() if existing: if check_for_duplicates and existing.id != copr.id: raise exceptions.DuplicateException( "Project: '{0}' already exists".format(copr.name)) else: # we're renaming # if we fire a models.Copr.query, it will use the modified copr in session # -> workaround this by just getting the name old_copr_name = (db.session.query(models.Copr.name) .filter(models.Copr.id == copr.id) .filter(models.Copr.deleted == False) .first()[0]) action = models.Action(action_type=helpers.ActionTypeEnum("rename"), object_type="copr", object_id=copr.id, old_value="{0}/{1}".format(copr.owner.name, old_copr_name), new_value="{0}/{1}".format(copr.owner.name, copr.name), created_on=int(time.time())) db.session.add(action) db.session.add(copr)
def send_update_module_md(cls, chroot): """ Schedules update module_md.yaml action :type copr_chroot: models.CoprChroot """ if chroot.copr.is_a_group_project: url_path = url_for('coprs_ns.group_chroot_view_module_md', group_name=chroot.copr.group.name, coprname=chroot.copr.name, chrootname=chroot.name) else: url_path = url_for('coprs_ns.chroot_view_module_md', username=chroot.copr.user.username, coprname=chroot.copr.name, chrootname=chroot.name) data_dict = { "ownername": chroot.copr.owner_name, "projectname": chroot.copr.name, "chroot": chroot.name, "module_md_present": chroot.module_md_zlib is not None, "url_path": url_path, } action = models.Action( action_type=helpers.ActionTypeEnum("update_module_md"), object_type="copr_chroot", data=json.dumps(data_dict), created_on=int(time.time())) db.session.add(action)
def send_delete_build(cls, build): """ Schedules build delete action :type build: models.Build """ # don't delete skipped chroots chroots_to_delete = [ chroot.name for chroot in build.build_chroots if chroot.state not in ["skipped"] ] if len(chroots_to_delete) == 0: return data_dict = { "username": build.copr.owner.name, "projectname": build.copr.name, "chroots": chroots_to_delete } if build.is_older_results_naming_used: if build.src_pkg_name is None or build.src_pkg_name == "": return data_dict["src_pkg_name"] = build.src_pkg_name else: data_dict["result_dir_name"] = build.result_dir_name action = models.Action(action_type=helpers.ActionTypeEnum("delete"), object_type="build", object_id=build.id, old_value="{0}/{1}".format( build.copr.owner.name, build.copr.name), data=json.dumps(data_dict), created_on=int(time.time())) db.session.add(action)
def send_build_module(cls, user, copr, modulemd): """ :type copr: models.Copr :type modulemd: str content of module yaml file """ if not user.can_build_in(copr): raise exceptions.InsufficientRightsException( "You don't have permissions to build in this copr.") modulemd_b64 = base64.b64encode(modulemd) data = { "ownername": copr.owner_name, "projectname": copr.name, "chroots": [c.name for c in copr.active_chroots], "modulemd_b64": modulemd_b64, } action = models.Action( action_type=helpers.ActionTypeEnum("build_module"), object_type="copr", old_value="", new_value="", data=json.dumps(data), created_on=int(time.time()), ) db.session.add(action)
def send_rawhide_to_release(cls, data): action = models.Action( action_type=helpers.ActionTypeEnum("rawhide_to_release"), object_type="None", data=json.dumps(data), created_on=int(time.time()), ) db.session.add(action)
def create_delete_action(cls, copr): action = models.Action(action_type=helpers.ActionTypeEnum("delete"), object_type="copr", object_id=copr.id, old_value=copr.full_name, new_value="", created_on=int(time.time())) db.session.add(action) return action
def get_waiting(cls): """ Return actions that aren't finished """ query = (models.Action.query.filter( models.Action.result == helpers.BackendResultEnum("waiting") ).filter( models.Action.action_type != helpers.ActionTypeEnum("legal-flag")). order_by(models.Action.created_on.asc())) return query
def send_createrepo(cls, username, coprname, chroots): data_dict = { "username": username, "projectname": coprname, "chroots": chroots } action = models.Action( action_type=helpers.ActionTypeEnum("createrepo"), object_type="None", object_id=0, old_value="", data=json.dumps(data_dict), created_on=int(time.time()), ) db.session.add(action)
def send_fork_copr(cls, src, dst, builds_map): """ :type src: models.Copr :type dst: models.Copr :type builds_map: dict where keys are forked builds IDs and values are IDs from the original builds. """ action = models.Action( action_type=helpers.ActionTypeEnum("fork"), object_type="copr", old_value="{0}".format(src.full_name), new_value="{0}".format(dst.full_name), data=json.dumps({"user": dst.owner_name, "copr": dst.name, "builds_map": builds_map}), created_on=int(time.time()), ) db.session.add(action)
def send_create_gpg_key(cls, copr): """ :type copr: models.Copr """ data_dict = { "username": copr.owner.name, "projectname": copr.name, } action = models.Action( action_type=helpers.ActionTypeEnum("gen_gpg_key"), object_type="copr", data=json.dumps(data_dict), created_on=int(time.time()), ) db.session.add(action)
def send_update_comps(cls, chroot): """ Schedules update comps.xml action :type copr_chroot: models.CoprChroot """ data_dict = { "username": chroot.copr.owner.name, "projectname": chroot.copr.name, "chroot": chroot.name, "comps_present": chroot.comps_zlib is not None, } action = models.Action( action_type=helpers.ActionTypeEnum("update_comps"), object_type="copr_chroot", data=json.dumps(data_dict), created_on=int(time.time())) db.session.add(action)
def delete(cls, user, copr, check_for_duplicates=True): cls.raise_if_cant_delete(user, copr) # TODO: do we want to dump the information somewhere, so that we can # search it in future? cls.raise_if_unfinished_blocking_action( user, copr, "Can't delete this project," " another operation is in progress: {action}") action = models.Action(action_type=helpers.ActionTypeEnum("delete"), object_type="copr", object_id=copr.id, old_value="{0}/{1}".format(copr.owner.name, copr.name), new_value="", created_on=int(time.time())) copr.deleted = True db.session.add(action) return copr
def delete_build(cls, user, build): if not user.can_build_in(build.copr): raise exceptions.InsufficientRightsException( "You are not allowed to delete this build.") if not build.deletable: raise exceptions.ActionInProgressException( "You can not delete build which is not finished.", "Unfinished build") # Only failed, finished, succeeded get here. if build.state not in ["cancelled" ]: # has nothing in backend to delete # don't delete skipped chroots chroots_to_delete = [ chroot.name for chroot in build.build_chroots if chroot.state not in ["skipped"] ] if chroots_to_delete: data_dict = { "pkgs": build.pkgs, "username": build.copr.owner.name, "projectname": build.copr.name, "chroots": chroots_to_delete } action = models.Action( action_type=helpers.ActionTypeEnum("delete"), object_type="build", object_id=build.id, old_value="{0}/{1}".format(build.copr.owner.name, build.copr.name), data=json.dumps(data_dict), created_on=int(time.time())) db.session.add(action) for build_chroot in build.build_chroots: db.session.delete(build_chroot) db.session.delete(build)
def send_build_module(cls, copr, modulemd): """ :type copr: models.Copr :type modulemd: str content of module yaml file """ modulemd_b64 = base64.b64encode(modulemd) data = { "ownername": copr.owner_name, "projectname": copr.name, "chroots": [c.name for c in copr.active_chroots], "modulemd_b64": modulemd_b64, } action = models.Action( action_type=helpers.ActionTypeEnum("build_module"), object_type="copr", old_value="", new_value="", data=json.dumps(data), created_on=int(time.time()), ) db.session.add(action)
def still_forking(self): return bool( Action.query.filter( Action.result == helpers.BackendResultEnum("waiting")).filter( Action.action_type == helpers.ActionTypeEnum("fork")). filter(Action.new_value == self.full_name).all())
from coprs import models from coprs import helpers ACTION_TYPE = helpers.ActionTypeEnum("build_module") class ModulesLogic(object): """ @FIXME ? Module builds are currently stored as Actions, so we are querying subset of Action objects """ @classmethod def get(cls, module_id): """ Return single module identified by `module_id` """ return models.Module.query.filter(models.Action.id == module_id) @classmethod def get_multiple(cls): return models.Module.query.filter(models.Module.action_type == ACTION_TYPE).order_by(models.Module.id.desc()) @classmethod def get_multiple_by_copr(cls, copr): return filter(lambda m: m.ownername == copr.owner_name and m.projectname == copr.name, cls.get_multiple())