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 send_delete_build(cls, build):
103 """ 104 Schedules 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 data_dict = { 113 "ownername": build.copr.owner_name, 114 "projectname": build.copr_name, 115 "project_dirname": build.copr_dirname, 116 "chroot_builddirs": chroot_builddirs, 117 } 118 119 action = models.Action( 120 action_type=ActionTypeEnum("delete"), 121 object_type="build", 122 object_id=build.id, 123 data=json.dumps(data_dict), 124 created_on=int(time.time()) 125 ) 126 db.session.add(action)
127 128 @classmethod
129 - def send_cancel_build(cls, build):
130 """ Schedules build cancel action 131 :type build: models.Build 132 """ 133 for chroot in build.build_chroots: 134 if chroot.state != "running": 135 continue 136 137 data_dict = { 138 "task_id": chroot.task_id, 139 } 140 141 action = models.Action( 142 action_type=ActionTypeEnum("cancel_build"), 143 data=json.dumps(data_dict), 144 created_on=int(time.time()) 145 ) 146 db.session.add(action)
147 148 @classmethod
149 - def send_update_comps(cls, chroot):
150 """ Schedules update comps.xml action 151 152 :type copr_chroot: models.CoprChroot 153 """ 154 155 url_path = helpers.copr_url("coprs_ns.chroot_view_comps", chroot.copr, chrootname=chroot.name) 156 data_dict = { 157 "ownername": chroot.copr.owner_name, 158 "projectname": chroot.copr.name, 159 "chroot": chroot.name, 160 "comps_present": chroot.comps_zlib is not None, 161 "url_path": url_path, 162 } 163 164 action = models.Action( 165 action_type=ActionTypeEnum("update_comps"), 166 object_type="copr_chroot", 167 data=json.dumps(data_dict), 168 created_on=int(time.time()) 169 ) 170 db.session.add(action)
171 172 @classmethod
173 - def send_update_module_md(cls, chroot):
174 """ Schedules update module_md.yaml action 175 176 :type copr_chroot: models.CoprChroot 177 """ 178 url_path = helpers.copr_url("coprs_ns.chroot_view_module_md", chroot.copr, chrootname=chroot.name) 179 data_dict = { 180 "ownername": chroot.copr.owner_name, 181 "projectname": chroot.copr.name, 182 "chroot": chroot.name, 183 "module_md_present": chroot.module_md_zlib is not None, 184 "url_path": url_path, 185 } 186 187 action = models.Action( 188 action_type=ActionTypeEnum("update_module_md"), 189 object_type="copr_chroot", 190 data=json.dumps(data_dict), 191 created_on=int(time.time()) 192 ) 193 db.session.add(action)
194 195 @classmethod
196 - def send_create_gpg_key(cls, copr):
197 """ 198 :type copr: models.Copr 199 """ 200 201 data_dict = { 202 "ownername": copr.owner_name, 203 "projectname": copr.name, 204 } 205 206 action = models.Action( 207 action_type=ActionTypeEnum("gen_gpg_key"), 208 object_type="copr", 209 data=json.dumps(data_dict), 210 created_on=int(time.time()), 211 ) 212 db.session.add(action)
213 214 @classmethod
215 - def send_rawhide_to_release(cls, data):
216 action = models.Action( 217 action_type=ActionTypeEnum("rawhide_to_release"), 218 object_type="None", 219 data=json.dumps(data), 220 created_on=int(time.time()), 221 ) 222 db.session.add(action)
223 224 @classmethod
225 - def send_fork_copr(cls, src, dst, builds_map):
226 """ 227 :type src: models.Copr 228 :type dst: models.Copr 229 :type builds_map: dict where keys are forked builds IDs and values are IDs from the original builds. 230 """ 231 232 action = models.Action( 233 action_type=ActionTypeEnum("fork"), 234 object_type="copr", 235 old_value="{0}".format(src.full_name), 236 new_value="{0}".format(dst.full_name), 237 data=json.dumps({"user": dst.owner_name, "copr": dst.name, "builds_map": builds_map}), 238 created_on=int(time.time()), 239 ) 240 db.session.add(action)
241 242 @classmethod
243 - def send_build_module(cls, copr, module):
244 """ 245 :type copr: models.Copr 246 :type modulemd: str content of module yaml file 247 """ 248 249 data = { 250 "chroots": [c.name for c in copr.active_chroots], 251 "builds": [b.id for b in module.builds], 252 } 253 254 action = models.Action( 255 action_type=ActionTypeEnum("build_module"), 256 object_type="module", 257 object_id=module.id, 258 old_value="", 259 new_value="", 260 data=json.dumps(data), 261 created_on=int(time.time()), 262 ) 263 db.session.add(action)
264 265 @classmethod
266 - def send_delete_chroot(cls, copr_chroot):
267 """ 268 Schedules deletion of a chroot directory from project 269 Useful to remove outdated chroots 270 :type build: models.CoprChroot 271 """ 272 data_dict = { 273 "ownername": copr_chroot.copr.owner_name, 274 "projectname": copr_chroot.copr.name, 275 "chrootname": copr_chroot.name, 276 } 277 278 action = models.Action( 279 action_type=ActionTypeEnum("delete"), 280 object_type="chroot", 281 object_id=None, 282 data=json.dumps(data_dict), 283 created_on=int(time.time()) 284 ) 285 db.session.add(action)
286