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 9 10 -class ActionsLogic(object):
11 12 @classmethod
13 - def get(cls, action_id):
14 """ 15 Return single action identified by `action_id` 16 """ 17 18 query = models.Action.query.filter(models.Action.id == action_id) 19 return query
20 21 @classmethod
22 - def get_many(cls, action_type=None, result=None):
23 query = models.Action.query 24 if action_type is not None: 25 query = query.filter(models.Action.action_type == 26 int(action_type)) 27 if result is not None: 28 query = query.filter(models.Action.result == 29 int(result)) 30 31 return query
32 33 @classmethod
34 - def get_waiting(cls):
35 """ 36 Return actions that aren't finished 37 """ 38 39 query = (models.Action.query 40 .filter(models.Action.result == 41 BackendResultEnum("waiting")) 42 .filter(models.Action.action_type != 43 ActionTypeEnum("legal-flag")) 44 .order_by(models.Action.created_on.asc())) 45 46 return query
47 48 @classmethod
49 - def get_by_ids(cls, ids):
50 """ 51 Return actions matching passed `ids` 52 """ 53 54 return models.Action.query.filter(models.Action.id.in_(ids))
55 56 @classmethod
57 - def update_state_from_dict(cls, action, upd_dict):
58 """ 59 Update `action` object with `upd_dict` data 60 61 Updates result, message and ended_on parameters. 62 """ 63 64 for attr in ["result", "message", "ended_on"]: 65 value = upd_dict.get(attr, None) 66 if value: 67 setattr(action, attr, value) 68 69 db.session.add(action)
70 71 @classmethod
72 - def send_createrepo(cls, copr):
73 data_dict = { 74 "ownername": copr.owner_name, 75 "projectname": copr.name, 76 "project_dirnames": [copr_dir.name for copr_dir in copr.dirs], 77 "chroots": [chroot.name for chroot in copr.active_chroots], 78 } 79 action = models.Action( 80 action_type=ActionTypeEnum("createrepo"), 81 object_type="repository", 82 object_id=0, 83 data=json.dumps(data_dict), 84 created_on=int(time.time()), 85 ) 86 db.session.add(action)
87 88 @classmethod
89 - def send_delete_copr(cls, copr):
90 data_dict = { 91 "ownername": copr.owner_name, 92 "project_dirnames": [copr_dir.name for copr_dir in copr.dirs], 93 } 94 action = models.Action(action_type=ActionTypeEnum("delete"), 95 object_type="copr", 96 object_id=copr.id, 97 data=json.dumps(data_dict), 98 created_on=int(time.time())) 99 db.session.add(action)
100 101 @classmethod
102 - def get_chroot_builddirs(cls, build):
103 """ 104 Creates a dictionary of chroot builddirs for build delete action 105 :type build: models.build 106 """ 107 chroot_builddirs = {'srpm-builds': build.result_dir} 108 109 for build_chroot in build.build_chroots: 110 chroot_builddirs[build_chroot.name] = build_chroot.result_dir 111 112 return chroot_builddirs
113 114 @classmethod
115 - def get_build_delete_data(cls, build):
116 """ 117 Creates data needed for build delete action 118 :type build: models.build 119 """ 120 data = { 121 "ownername": build.copr.owner_name, 122 "projectname": build.copr_name, 123 } 124 125 if build.copr_dir: 126 data["project_dirname"] = build.copr_dirname 127 else: 128 data["project_dirname"] = build.copr_name 129 130 return data
131 132 @classmethod
133 - def send_delete_build(cls, build):
134 """ 135 Schedules build delete action 136 :type build: models.Build 137 """ 138 data = cls.get_build_delete_data(build) 139 data["chroot_builddirs"] = cls.get_chroot_builddirs(build) 140 141 action = models.Action( 142 action_type=ActionTypeEnum("delete"), 143 object_type="build", 144 object_id=build.id, 145 data=json.dumps(data), 146 created_on=int(time.time()) 147 ) 148 db.session.add(action)
149 150 @classmethod
151 - def send_delete_multiple_builds(cls, builds):
152 """ 153 Schedules builds delete action for builds belonging to the same project 154 :type build: list of models.Build 155 """ 156 data = cls.get_build_delete_data(builds[0]) 157 data["builds"] = [] 158 for build in builds: 159 chroot_builddirs = cls.get_chroot_builddirs(build) 160 data["builds"].append(chroot_builddirs) 161 162 action = models.Action( 163 action_type=ActionTypeEnum("delete"), 164 object_type="builds", 165 data=json.dumps(data), 166 created_on=int(time.time()) 167 ) 168 db.session.add(action)
169 170 @classmethod
171 - def send_cancel_build(cls, build):
172 """ Schedules build cancel action 173 :type build: models.Build 174 """ 175 for chroot in build.build_chroots: 176 if chroot.state != "running": 177 continue 178 179 data_dict = { 180 "task_id": chroot.task_id, 181 } 182 183 action = models.Action( 184 action_type=ActionTypeEnum("cancel_build"), 185 data=json.dumps(data_dict), 186 created_on=int(time.time()) 187 ) 188 db.session.add(action)
189 190 @classmethod
191 - def send_update_comps(cls, chroot):
192 """ Schedules update comps.xml action 193 194 :type copr_chroot: models.CoprChroot 195 """ 196 197 url_path = helpers.copr_url("coprs_ns.chroot_view_comps", chroot.copr, chrootname=chroot.name) 198 data_dict = { 199 "ownername": chroot.copr.owner_name, 200 "projectname": chroot.copr.name, 201 "chroot": chroot.name, 202 "comps_present": chroot.comps_zlib is not None, 203 "url_path": url_path, 204 } 205 206 action = models.Action( 207 action_type=ActionTypeEnum("update_comps"), 208 object_type="copr_chroot", 209 data=json.dumps(data_dict), 210 created_on=int(time.time()) 211 ) 212 db.session.add(action)
213 214 @classmethod
215 - def send_create_gpg_key(cls, copr):
216 """ 217 :type copr: models.Copr 218 """ 219 220 data_dict = { 221 "ownername": copr.owner_name, 222 "projectname": copr.name, 223 } 224 225 action = models.Action( 226 action_type=ActionTypeEnum("gen_gpg_key"), 227 object_type="copr", 228 data=json.dumps(data_dict), 229 created_on=int(time.time()), 230 ) 231 db.session.add(action)
232 233 @classmethod
234 - def send_rawhide_to_release(cls, data):
235 action = models.Action( 236 action_type=ActionTypeEnum("rawhide_to_release"), 237 object_type="None", 238 data=json.dumps(data), 239 created_on=int(time.time()), 240 ) 241 db.session.add(action)
242 243 @classmethod
244 - def send_fork_copr(cls, src, dst, builds_map):
245 """ 246 :type src: models.Copr 247 :type dst: models.Copr 248 :type builds_map: dict where keys are forked builds IDs and values are IDs from the original builds. 249 """ 250 251 action = models.Action( 252 action_type=ActionTypeEnum("fork"), 253 object_type="copr", 254 old_value="{0}".format(src.full_name), 255 new_value="{0}".format(dst.full_name), 256 data=json.dumps({"user": dst.owner_name, "copr": dst.name, "builds_map": builds_map}), 257 created_on=int(time.time()), 258 ) 259 db.session.add(action)
260 261 @classmethod
262 - def send_build_module(cls, copr, module):
263 """ 264 :type copr: models.Copr 265 :type modulemd: str content of module yaml file 266 """ 267 268 mock_chroots = set.intersection(*[set(b.chroots) for b in module.builds]) 269 data = { 270 "chroots": [ch.name for ch in mock_chroots], 271 "builds": [b.id for b in module.builds], 272 } 273 274 action = models.Action( 275 action_type=ActionTypeEnum("build_module"), 276 object_type="module", 277 object_id=module.id, 278 old_value="", 279 new_value="", 280 data=json.dumps(data), 281 created_on=int(time.time()), 282 ) 283 db.session.add(action)
284 285 @classmethod
286 - def send_delete_chroot(cls, copr_chroot):
287 """ 288 Schedules deletion of a chroot directory from project 289 Useful to remove outdated chroots 290 :type build: models.CoprChroot 291 """ 292 data_dict = { 293 "ownername": copr_chroot.copr.owner_name, 294 "projectname": copr_chroot.copr.name, 295 "chrootname": copr_chroot.name, 296 } 297 298 action = models.Action( 299 action_type=ActionTypeEnum("delete"), 300 object_type="chroot", 301 object_id=None, 302 data=json.dumps(data_dict), 303 created_on=int(time.time()) 304 ) 305 db.session.add(action)
306