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