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   
  4  from copr_common.enums import ActionTypeEnum, BackendResultEnum 
  5  from coprs import db 
  6  from coprs import models 
  7  from coprs import helpers 
  8  from coprs import exceptions 
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 BackendResultEnum("waiting")) 43 .filter(models.Action.action_type != 44 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, copr, dirnames=None):
74 possible_dirnames = [copr_dir.name for copr_dir in copr.dirs] 75 if not dirnames: 76 # by default we createrepo for all of them 77 dirnames = possible_dirnames 78 else: 79 missing = set(dirnames) - set(possible_dirnames) 80 if missing: 81 raise exceptions.NotFoundException( 82 "Can't createrepo for {} dirnames in {} project".format( 83 missing, copr.full_name)) 84 data_dict = { 85 "ownername": copr.owner_name, 86 "projectname": copr.name, 87 "project_dirnames": dirnames, 88 "chroots": [chroot.name for chroot in copr.active_chroots], 89 } 90 action = models.Action( 91 action_type=ActionTypeEnum("createrepo"), 92 object_type="repository", 93 object_id=0, 94 data=json.dumps(data_dict), 95 created_on=int(time.time()), 96 ) 97 db.session.add(action)
98 99 @classmethod
100 - def send_delete_copr(cls, copr):
101 data_dict = { 102 "ownername": copr.owner_name, 103 "project_dirnames": [copr_dir.name for copr_dir in copr.dirs], 104 } 105 action = models.Action(action_type=ActionTypeEnum("delete"), 106 object_type="copr", 107 object_id=copr.id, 108 data=json.dumps(data_dict), 109 created_on=int(time.time())) 110 db.session.add(action)
111 112 @classmethod
113 - def get_chroot_builddirs(cls, build):
114 """ 115 Creates a dictionary of chroot builddirs for build delete action 116 :type build: models.build 117 """ 118 chroot_builddirs = {'srpm-builds': build.result_dir} 119 120 for build_chroot in build.build_chroots: 121 chroot_builddirs[build_chroot.name] = build_chroot.result_dir 122 123 return chroot_builddirs
124 125 @classmethod
126 - def get_build_delete_data(cls, build):
127 """ 128 Creates data needed for build delete action 129 :type build: models.build 130 """ 131 data = { 132 "ownername": build.copr.owner_name, 133 "projectname": build.copr_name, 134 } 135 136 if build.copr_dir: 137 data["project_dirname"] = build.copr_dirname 138 else: 139 data["project_dirname"] = build.copr_name 140 141 return data
142 143 @classmethod
144 - def send_delete_build(cls, build):
145 """ 146 Schedules build delete action 147 :type build: models.Build 148 """ 149 data = cls.get_build_delete_data(build) 150 data["chroot_builddirs"] = cls.get_chroot_builddirs(build) 151 152 action = models.Action( 153 action_type=ActionTypeEnum("delete"), 154 object_type="build", 155 object_id=build.id, 156 data=json.dumps(data), 157 created_on=int(time.time()) 158 ) 159 db.session.add(action)
160 161 @classmethod
162 - def send_delete_multiple_builds(cls, builds):
163 """ 164 Schedules builds delete action for builds belonging to the same project 165 :type build: list of models.Build 166 """ 167 data = cls.get_build_delete_data(builds[0]) 168 data["builds"] = [] 169 for build in builds: 170 chroot_builddirs = cls.get_chroot_builddirs(build) 171 data["builds"].append(chroot_builddirs) 172 173 action = models.Action( 174 action_type=ActionTypeEnum("delete"), 175 object_type="builds", 176 data=json.dumps(data), 177 created_on=int(time.time()) 178 ) 179 db.session.add(action)
180 181 @classmethod
182 - def send_cancel_build(cls, build):
183 """ Schedules build cancel action 184 :type build: models.Build 185 """ 186 for chroot in build.build_chroots: 187 if chroot.state != "running": 188 continue 189 190 data_dict = { 191 "task_id": chroot.task_id, 192 } 193 194 action = models.Action( 195 action_type=ActionTypeEnum("cancel_build"), 196 data=json.dumps(data_dict), 197 created_on=int(time.time()) 198 ) 199 db.session.add(action)
200 201 @classmethod
202 - def send_update_comps(cls, chroot):
203 """ Schedules update comps.xml action 204 205 :type copr_chroot: models.CoprChroot 206 """ 207 208 url_path = helpers.copr_url("coprs_ns.chroot_view_comps", chroot.copr, chrootname=chroot.name) 209 data_dict = { 210 "ownername": chroot.copr.owner_name, 211 "projectname": chroot.copr.name, 212 "chroot": chroot.name, 213 "comps_present": chroot.comps_zlib is not None, 214 "url_path": url_path, 215 } 216 217 action = models.Action( 218 action_type=ActionTypeEnum("update_comps"), 219 object_type="copr_chroot", 220 data=json.dumps(data_dict), 221 created_on=int(time.time()) 222 ) 223 db.session.add(action)
224 225 @classmethod
226 - def send_create_gpg_key(cls, copr):
227 """ 228 :type copr: models.Copr 229 """ 230 231 data_dict = { 232 "ownername": copr.owner_name, 233 "projectname": copr.name, 234 } 235 236 action = models.Action( 237 action_type=ActionTypeEnum("gen_gpg_key"), 238 object_type="copr", 239 data=json.dumps(data_dict), 240 created_on=int(time.time()), 241 ) 242 db.session.add(action)
243 244 @classmethod
245 - def send_rawhide_to_release(cls, data):
246 action = models.Action( 247 action_type=ActionTypeEnum("rawhide_to_release"), 248 object_type="None", 249 data=json.dumps(data), 250 created_on=int(time.time()), 251 ) 252 db.session.add(action)
253 254 @classmethod
255 - def send_fork_copr(cls, src, dst, builds_map):
256 """ 257 :type src: models.Copr 258 :type dst: models.Copr 259 :type builds_map: dict where keys are forked builds IDs and values are IDs from the original builds. 260 """ 261 262 action = models.Action( 263 action_type=ActionTypeEnum("fork"), 264 object_type="copr", 265 old_value="{0}".format(src.full_name), 266 new_value="{0}".format(dst.full_name), 267 data=json.dumps({"user": dst.owner_name, "copr": dst.name, "builds_map": builds_map}), 268 created_on=int(time.time()), 269 ) 270 db.session.add(action)
271 272 @classmethod
273 - def send_build_module(cls, copr, module):
274 """ 275 :type copr: models.Copr 276 :type modulemd: str content of module yaml file 277 """ 278 279 mock_chroots = set.intersection(*[set(b.chroots) for b in module.builds]) 280 data = { 281 "chroots": [ch.name for ch in mock_chroots], 282 "builds": [b.id for b in module.builds], 283 } 284 285 action = models.Action( 286 action_type=ActionTypeEnum("build_module"), 287 object_type="module", 288 object_id=module.id, 289 old_value="", 290 new_value="", 291 data=json.dumps(data), 292 created_on=int(time.time()), 293 ) 294 db.session.add(action)
295 296 @classmethod
297 - def send_delete_chroot(cls, copr_chroot):
298 """ 299 Schedules deletion of a chroot directory from project 300 Useful to remove outdated chroots 301 :type build: models.CoprChroot 302 """ 303 data_dict = { 304 "ownername": copr_chroot.copr.owner_name, 305 "projectname": copr_chroot.copr.name, 306 "chrootname": copr_chroot.name, 307 } 308 309 action = models.Action( 310 action_type=ActionTypeEnum("delete"), 311 object_type="chroot", 312 object_id=None, 313 data=json.dumps(data_dict), 314 created_on=int(time.time()) 315 ) 316 db.session.add(action)
317