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_create_gpg_key(cls, copr):
174 """ 175 :type copr: models.Copr 176 """ 177 178 data_dict = { 179 "ownername": copr.owner_name, 180 "projectname": copr.name, 181 } 182 183 action = models.Action( 184 action_type=ActionTypeEnum("gen_gpg_key"), 185 object_type="copr", 186 data=json.dumps(data_dict), 187 created_on=int(time.time()), 188 ) 189 db.session.add(action)
190 191 @classmethod
192 - def send_rawhide_to_release(cls, data):
193 action = models.Action( 194 action_type=ActionTypeEnum("rawhide_to_release"), 195 object_type="None", 196 data=json.dumps(data), 197 created_on=int(time.time()), 198 ) 199 db.session.add(action)
200 201 @classmethod
202 - def send_fork_copr(cls, src, dst, builds_map):
203 """ 204 :type src: models.Copr 205 :type dst: models.Copr 206 :type builds_map: dict where keys are forked builds IDs and values are IDs from the original builds. 207 """ 208 209 action = models.Action( 210 action_type=ActionTypeEnum("fork"), 211 object_type="copr", 212 old_value="{0}".format(src.full_name), 213 new_value="{0}".format(dst.full_name), 214 data=json.dumps({"user": dst.owner_name, "copr": dst.name, "builds_map": builds_map}), 215 created_on=int(time.time()), 216 ) 217 db.session.add(action)
218 219 @classmethod
220 - def send_build_module(cls, copr, module):
221 """ 222 :type copr: models.Copr 223 :type modulemd: str content of module yaml file 224 """ 225 226 data = { 227 "chroots": [c.name for c in copr.active_chroots], 228 "builds": [b.id for b in module.builds], 229 } 230 231 action = models.Action( 232 action_type=ActionTypeEnum("build_module"), 233 object_type="module", 234 object_id=module.id, 235 old_value="", 236 new_value="", 237 data=json.dumps(data), 238 created_on=int(time.time()), 239 ) 240 db.session.add(action)
241 242 @classmethod
243 - def send_delete_chroot(cls, copr_chroot):
244 """ 245 Schedules deletion of a chroot directory from project 246 Useful to remove outdated chroots 247 :type build: models.CoprChroot 248 """ 249 data_dict = { 250 "ownername": copr_chroot.copr.owner_name, 251 "projectname": copr_chroot.copr.name, 252 "chrootname": copr_chroot.name, 253 } 254 255 action = models.Action( 256 action_type=ActionTypeEnum("delete"), 257 object_type="chroot", 258 object_id=None, 259 data=json.dumps(data_dict), 260 created_on=int(time.time()) 261 ) 262 db.session.add(action)
263