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