Package coprs :: Package logic :: Module actions_logic
[hide private]
[frames] | no frames]

Source Code for Module coprs.logic.actions_logic

  1  import json 
  2  import time 
  3  import base64 
  4  from coprs import db 
  5  from coprs import models 
  6  from coprs import helpers 
  7  from coprs import exceptions 
  8  from flask import url_for 
9 10 11 -class ActionsLogic(object):
12 13 @classmethod
14 - def get(cls, action_id):
15 """ 16 Return single action identified by `action_id` 17 """ 18 19 query = models.Action.query.filter(models.Action.id == action_id) 20 return query
21 22 @classmethod
23 - def get_many(cls, action_type=None, result=None):
24 query = models.Action.query 25 if action_type is not None: 26 query = query.filter(models.Action.action_type == 27 int(action_type)) 28 if result is not None: 29 query = query.filter(models.Action.result == 30 int(result)) 31 32 return query
33 34 @classmethod
35 - def get_waiting(cls):
36 """ 37 Return actions that aren't finished 38 """ 39 40 query = (models.Action.query 41 .filter(models.Action.result == 42 helpers.BackendResultEnum("waiting")) 43 .filter(models.Action.action_type != 44 helpers.ActionTypeEnum("legal-flag")) 45 .order_by(models.Action.created_on.asc())) 46 47 return query
48 49 @classmethod
50 - def get_by_ids(cls, ids):
51 """ 52 Return actions matching passed `ids` 53 """ 54 55 return models.Action.query.filter(models.Action.id.in_(ids))
56 57 @classmethod
58 - def update_state_from_dict(cls, action, upd_dict):
59 """ 60 Update `action` object with `upd_dict` data 61 62 Updates result, message and ended_on parameters. 63 """ 64 65 for attr in ["result", "message", "ended_on"]: 66 value = upd_dict.get(attr, None) 67 if value: 68 setattr(action, attr, value) 69 70 db.session.add(action)
71 72 @classmethod
73 - def send_createrepo(cls, username, coprname, chroots):
74 data_dict = { 75 "username": username, 76 "projectname": coprname, 77 "chroots": chroots 78 } 79 action = models.Action( 80 action_type=helpers.ActionTypeEnum("createrepo"), 81 object_type="None", 82 object_id=0, 83 old_value="", 84 data=json.dumps(data_dict), 85 created_on=int(time.time()), 86 ) 87 db.session.add(action)
88 89 @classmethod
90 - def send_delete_build(cls, build):
91 """ Schedules build delete action 92 :type build: models.Build 93 """ 94 # don't delete skipped chroots 95 chroots_to_delete = [ 96 chroot.name for chroot in build.build_chroots 97 if chroot.state not in ["skipped"] 98 ] 99 if len(chroots_to_delete) == 0: 100 return 101 102 data_dict = { 103 "username": build.copr.owner_name, 104 "projectname": build.copr.name, 105 "chroots": chroots_to_delete 106 } 107 108 if build.is_older_results_naming_used: 109 if build.src_pkg_name is None or build.src_pkg_name == "": 110 return 111 data_dict["src_pkg_name"] = build.src_pkg_name 112 else: 113 data_dict["result_dir_name"] = build.result_dir_name 114 115 action = models.Action( 116 action_type=helpers.ActionTypeEnum("delete"), 117 object_type="build", 118 object_id=build.id, 119 old_value=build.copr.full_name, 120 data=json.dumps(data_dict), 121 created_on=int(time.time()) 122 ) 123 db.session.add(action)
124 125 @classmethod
126 - def send_cancel_build(cls, build):
127 """ Schedules build cancel action 128 :type build: models.Build 129 """ 130 for chroot in build.build_chroots: 131 if chroot.state != "running": 132 continue 133 134 data_dict = { 135 "task_id": chroot.task_id, 136 } 137 138 action = models.Action( 139 action_type=helpers.ActionTypeEnum("cancel_build"), 140 data=json.dumps(data_dict), 141 created_on=int(time.time()) 142 ) 143 db.session.add(action)
144 145 @classmethod
146 - def send_update_comps(cls, chroot):
147 """ Schedules update comps.xml action 148 149 :type copr_chroot: models.CoprChroot 150 """ 151 152 url_path = helpers.copr_url("coprs_ns.chroot_view_comps", chroot.copr, chrootname=chroot.name) 153 data_dict = { 154 "ownername": chroot.copr.owner_name, 155 "projectname": chroot.copr.name, 156 "chroot": chroot.name, 157 "comps_present": chroot.comps_zlib is not None, 158 "url_path": url_path, 159 } 160 161 action = models.Action( 162 action_type=helpers.ActionTypeEnum("update_comps"), 163 object_type="copr_chroot", 164 data=json.dumps(data_dict), 165 created_on=int(time.time()) 166 ) 167 db.session.add(action)
168 169 @classmethod
170 - def send_update_module_md(cls, chroot):
171 """ Schedules update module_md.yaml action 172 173 :type copr_chroot: models.CoprChroot 174 """ 175 url_path = helpers.copr_url("coprs_ns.chroot_view_module_md", chroot.copr, chrootname=chroot.name) 176 data_dict = { 177 "ownername": chroot.copr.owner_name, 178 "projectname": chroot.copr.name, 179 "chroot": chroot.name, 180 "module_md_present": chroot.module_md_zlib is not None, 181 "url_path": url_path, 182 } 183 184 action = models.Action( 185 action_type=helpers.ActionTypeEnum("update_module_md"), 186 object_type="copr_chroot", 187 data=json.dumps(data_dict), 188 created_on=int(time.time()) 189 ) 190 db.session.add(action)
191 192 @classmethod
193 - def send_create_gpg_key(cls, copr):
194 """ 195 :type copr: models.Copr 196 """ 197 198 data_dict = { 199 "username": copr.owner_name, 200 "projectname": copr.name, 201 } 202 203 action = models.Action( 204 action_type=helpers.ActionTypeEnum("gen_gpg_key"), 205 object_type="copr", 206 data=json.dumps(data_dict), 207 created_on=int(time.time()), 208 ) 209 db.session.add(action)
210 211 @classmethod
212 - def send_rawhide_to_release(cls, data):
213 action = models.Action( 214 action_type=helpers.ActionTypeEnum("rawhide_to_release"), 215 object_type="None", 216 data=json.dumps(data), 217 created_on=int(time.time()), 218 ) 219 db.session.add(action)
220 221 @classmethod
222 - def send_fork_copr(cls, src, dst, builds_map):
223 """ 224 :type src: models.Copr 225 :type dst: models.Copr 226 :type builds_map: dict where keys are forked builds IDs and values are IDs from the original builds. 227 """ 228 229 action = models.Action( 230 action_type=helpers.ActionTypeEnum("fork"), 231 object_type="copr", 232 old_value="{0}".format(src.full_name), 233 new_value="{0}".format(dst.full_name), 234 data=json.dumps({"user": dst.owner_name, "copr": dst.name, "builds_map": builds_map}), 235 created_on=int(time.time()), 236 ) 237 db.session.add(action)
238 239 @classmethod
240 - def send_build_module(cls, copr, module):
241 """ 242 :type copr: models.Copr 243 :type modulemd: str content of module yaml file 244 """ 245 246 data = { 247 "chroots": [c.name for c in copr.active_chroots], 248 "builds": [b.id for b in module.builds], 249 } 250 251 action = models.Action( 252 action_type=helpers.ActionTypeEnum("build_module"), 253 object_type="module", 254 object_id=module.id, 255 old_value="", 256 new_value="", 257 data=json.dumps(data), 258 created_on=int(time.time()), 259 ) 260 db.session.add(action)
261