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  import base64 
  4  import os 
  5   
  6  from coprs import db 
  7  from coprs import models 
  8  from coprs import helpers 
  9  from coprs import exceptions 
 10  from flask import url_for 
11 12 13 -class ActionsLogic(object):
14 15 @classmethod
16 - def get(cls, action_id):
17 """ 18 Return single action identified by `action_id` 19 """ 20 21 query = models.Action.query.filter(models.Action.id == action_id) 22 return query
23 24 @classmethod
25 - def get_many(cls, action_type=None, result=None):
26 query = models.Action.query 27 if action_type is not None: 28 query = query.filter(models.Action.action_type == 29 int(action_type)) 30 if result is not None: 31 query = query.filter(models.Action.result == 32 int(result)) 33 34 return query
35 36 @classmethod
37 - def get_waiting(cls):
38 """ 39 Return actions that aren't finished 40 """ 41 42 query = (models.Action.query 43 .filter(models.Action.result == 44 helpers.BackendResultEnum("waiting")) 45 .filter(models.Action.action_type != 46 helpers.ActionTypeEnum("legal-flag")) 47 .order_by(models.Action.created_on.asc())) 48 49 return query
50 51 @classmethod
52 - def get_by_ids(cls, ids):
53 """ 54 Return actions matching passed `ids` 55 """ 56 57 return models.Action.query.filter(models.Action.id.in_(ids))
58 59 @classmethod
60 - def update_state_from_dict(cls, action, upd_dict):
61 """ 62 Update `action` object with `upd_dict` data 63 64 Updates result, message and ended_on parameters. 65 """ 66 67 for attr in ["result", "message", "ended_on"]: 68 value = upd_dict.get(attr, None) 69 if value: 70 setattr(action, attr, value) 71 72 db.session.add(action)
73 74 @classmethod
75 - def send_createrepo(cls, username, coprname, chroots):
76 data_dict = { 77 "username": username, 78 "projectname": coprname, 79 "chroots": chroots 80 } 81 action = models.Action( 82 action_type=helpers.ActionTypeEnum("createrepo"), 83 object_type="None", 84 object_id=0, 85 old_value="", 86 data=json.dumps(data_dict), 87 created_on=int(time.time()), 88 ) 89 db.session.add(action)
90 91 @classmethod
92 - def send_delete_build(cls, build):
93 """ 94 Schedules build delete action 95 :type build: models.Build 96 """ 97 chroot_builddirs = {'srpm-builds': build.result_dir} 98 99 for build_chroot in build.build_chroots: 100 chroot_builddirs[build_chroot.name] = build_chroot.result_dir 101 102 data_dict = { 103 "ownername": build.copr.owner_name, 104 "projectname": build.copr.name, 105 "chroot_builddirs": chroot_builddirs, 106 } 107 108 action = models.Action( 109 action_type=helpers.ActionTypeEnum("delete"), 110 object_type="build", 111 object_id=build.id, 112 old_value=build.copr.full_name, 113 data=json.dumps(data_dict), 114 created_on=int(time.time()) 115 ) 116 db.session.add(action)
117 118 @classmethod
119 - def send_cancel_build(cls, build):
120 """ Schedules build cancel action 121 :type build: models.Build 122 """ 123 for chroot in build.build_chroots: 124 if chroot.state != "running": 125 continue 126 127 data_dict = { 128 "task_id": chroot.task_id, 129 } 130 131 action = models.Action( 132 action_type=helpers.ActionTypeEnum("cancel_build"), 133 data=json.dumps(data_dict), 134 created_on=int(time.time()) 135 ) 136 db.session.add(action)
137 138 @classmethod
139 - def send_update_comps(cls, chroot):
140 """ Schedules update comps.xml action 141 142 :type copr_chroot: models.CoprChroot 143 """ 144 145 url_path = helpers.copr_url("coprs_ns.chroot_view_comps", chroot.copr, chrootname=chroot.name) 146 data_dict = { 147 "ownername": chroot.copr.owner_name, 148 "projectname": chroot.copr.name, 149 "chroot": chroot.name, 150 "comps_present": chroot.comps_zlib is not None, 151 "url_path": url_path, 152 } 153 154 action = models.Action( 155 action_type=helpers.ActionTypeEnum("update_comps"), 156 object_type="copr_chroot", 157 data=json.dumps(data_dict), 158 created_on=int(time.time()) 159 ) 160 db.session.add(action)
161 162 @classmethod
163 - def send_update_module_md(cls, chroot):
164 """ Schedules update module_md.yaml action 165 166 :type copr_chroot: models.CoprChroot 167 """ 168 url_path = helpers.copr_url("coprs_ns.chroot_view_module_md", chroot.copr, chrootname=chroot.name) 169 data_dict = { 170 "ownername": chroot.copr.owner_name, 171 "projectname": chroot.copr.name, 172 "chroot": chroot.name, 173 "module_md_present": chroot.module_md_zlib is not None, 174 "url_path": url_path, 175 } 176 177 action = models.Action( 178 action_type=helpers.ActionTypeEnum("update_module_md"), 179 object_type="copr_chroot", 180 data=json.dumps(data_dict), 181 created_on=int(time.time()) 182 ) 183 db.session.add(action)
184 185 @classmethod
186 - def send_create_gpg_key(cls, copr):
187 """ 188 :type copr: models.Copr 189 """ 190 191 data_dict = { 192 "username": copr.owner_name, 193 "projectname": copr.name, 194 } 195 196 action = models.Action( 197 action_type=helpers.ActionTypeEnum("gen_gpg_key"), 198 object_type="copr", 199 data=json.dumps(data_dict), 200 created_on=int(time.time()), 201 ) 202 db.session.add(action)
203 204 @classmethod
205 - def send_rawhide_to_release(cls, data):
206 action = models.Action( 207 action_type=helpers.ActionTypeEnum("rawhide_to_release"), 208 object_type="None", 209 data=json.dumps(data), 210 created_on=int(time.time()), 211 ) 212 db.session.add(action)
213 214 @classmethod
215 - def send_fork_copr(cls, src, dst, builds_map):
216 """ 217 :type src: models.Copr 218 :type dst: models.Copr 219 :type builds_map: dict where keys are forked builds IDs and values are IDs from the original builds. 220 """ 221 222 action = models.Action( 223 action_type=helpers.ActionTypeEnum("fork"), 224 object_type="copr", 225 old_value="{0}".format(src.full_name), 226 new_value="{0}".format(dst.full_name), 227 data=json.dumps({"user": dst.owner_name, "copr": dst.name, "builds_map": builds_map}), 228 created_on=int(time.time()), 229 ) 230 db.session.add(action)
231 232 @classmethod
233 - def send_build_module(cls, copr, module):
234 """ 235 :type copr: models.Copr 236 :type modulemd: str content of module yaml file 237 """ 238 239 data = { 240 "chroots": [c.name for c in copr.active_chroots], 241 "builds": [b.id for b in module.builds], 242 } 243 244 action = models.Action( 245 action_type=helpers.ActionTypeEnum("build_module"), 246 object_type="module", 247 object_id=module.id, 248 old_value="", 249 new_value="", 250 data=json.dumps(data), 251 created_on=int(time.time()), 252 ) 253 db.session.add(action)
254