-
Notifications
You must be signed in to change notification settings - Fork 48
/
abstract.py
390 lines (329 loc) · 12.7 KB
/
abstract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# Copyright Contributors to the Packit project.
# SPDX-License-Identifier: MIT
"""
This file defines generic job handler
"""
import enum
import logging
import shutil
from collections import defaultdict
from datetime import datetime
from os import getenv
from pathlib import Path
from typing import Dict, Optional, Set, Type, Tuple
from celery import Task
from celery import signature
from celery.canvas import Signature
from ogr.abstract import GitProject
from packit.config import JobConfig, JobType, PackageConfig
from packit.constants import DATETIME_FORMAT
from packit_service.config import ServiceConfig
from packit_service.models import (
AbstractProjectObjectDbType,
)
from packit_service.sentry_integration import push_scope_to_sentry
from packit_service.utils import dump_job_config, dump_package_config
from packit_service.worker.celery_task import CeleryTask
from packit_service.worker.events import Event, EventData
from packit_service.worker.monitoring import Pushgateway
from packit_service.worker.result import TaskResults
from packit_service.worker.checker.abstract import Checker
from packit_service.worker.mixin import (
Config,
PackitAPIProtocol,
)
logger = logging.getLogger(__name__)
MAP_JOB_TYPE_TO_HANDLER: Dict[JobType, Set[Type["JobHandler"]]] = defaultdict(set)
MAP_REQUIRED_JOB_TYPE_TO_HANDLER: Dict[JobType, Set[Type["JobHandler"]]] = defaultdict(
set
)
SUPPORTED_EVENTS_FOR_HANDLER: Dict[Type["JobHandler"], Set[Type["Event"]]] = (
defaultdict(set)
)
MAP_COMMENT_TO_HANDLER: Dict[str, Set[Type["JobHandler"]]] = defaultdict(set)
MAP_CHECK_PREFIX_TO_HANDLER: Dict[str, Set[Type["JobHandler"]]] = defaultdict(set)
def configured_as(job_type: JobType):
"""
[class decorator]
Specify a job_type which we want to use this handler for.
In other words, what job-config in the configuration file
is compatible with this handler.
Example:
```
@configured_as(job_type=JobType.propose_downstream)
class ProposeDownstreamHandler(JobHandler):
```
Multiple handlers can match one job_type.
(e.g. CoprBuildHandler and CoprBuildEndHandler both uses copr_build)
The handler needs to match the event type by using @reacts_to decorator.
Multiple decorators can be applied.
E.g. CoprBuildHandler uses both copr_build and build:
```
@configured_as(job_type=JobType.copr_build)
@configured_as(job_type=JobType.build)
class CoprBuildHandler(JobHandler):
```
"""
def _add_to_mapping(kls: Type["JobHandler"]):
MAP_JOB_TYPE_TO_HANDLER[job_type].add(kls)
return kls
return _add_to_mapping
def reacts_to(event: Type["Event"]):
"""
[class decorator]
Specify an event for which we want to use this handler.
Matching is done via isinstance so you can use some abstract class as well.
Multiple decorators are allowed.
Example:
```
@reacts_to(ReleaseEvent)
@reacts_to(PullRequestGithubEvent)
@reacts_to(PushGitHubEvent)
class CoprBuildHandler(JobHandler):
```
"""
def _add_to_mapping(kls: Type["JobHandler"]):
SUPPORTED_EVENTS_FOR_HANDLER[kls].add(event)
return kls
return _add_to_mapping
def run_for_comment(command: str):
"""
[class decorator]
Specify a command for which we want to run a handler.
e.g. for `/packit command` we need to add `command`
Multiple decorators are allowed.
Don't forget to specify valid comment events
using @reacts_to decorator.
Example:
```
@configured_as(job_type=JobType.propose_downstream)
@run_for_comment(command="propose-downstream")
@run_for_comment(command="propose-update")
@reacts_to(event=ReleaseEvent)
@reacts_to(event=IssueCommentEvent)
class ProposeDownstreamHandler(JobHandler):
```
"""
def _add_to_mapping(kls: Type["JobHandler"]):
MAP_COMMENT_TO_HANDLER[command].add(kls)
return kls
return _add_to_mapping
def run_for_check_rerun(prefix: str):
"""
[class decorator]
Specify a check prefix for which we want to run a handler.
Multiple decorators are allowed.
Don't forget to specify valid check rerun events
using @reacts_to decorator.
Example:
```
@configured_as(job_type=JobType.copr_build)
@configured_as(job_type=JobType.build)
@run_for_check_rerun(prefix="rpm-build")
@reacts_to(CheckRerunPullRequestEvent)
@reacts_to(CheckRerunCommitEvent)
@reacts_to(CheckRerunReleaseEvent)
```
"""
def _add_to_mapping(kls: Type["JobHandler"]):
MAP_CHECK_PREFIX_TO_HANDLER[prefix].add(kls)
return kls
return _add_to_mapping
class TaskName(str, enum.Enum):
copr_build_start = "task.run_copr_build_start_handler"
copr_build_end = "task.run_copr_build_end_handler"
copr_build = "task.run_copr_build_handler"
installation = "task.run_installation_handler"
testing_farm = "task.run_testing_farm_handler"
testing_farm_results = "task.run_testing_farm_results_handler"
propose_downstream = "task.run_propose_downstream_handler"
upstream_koji_build = "task.run_koji_build_handler"
upstream_koji_build_report = "task.run_koji_build_report_handler"
downstream_koji_build = "task.run_downstream_koji_build_handler"
retrigger_downstream_koji_build = "task.run_retrigger_downstream_koji_build_handler"
downstream_koji_build_report = "task.run_downstream_koji_build_report_handler"
# Fedora notification is ok for now
# downstream_koji_build_report = "task.run_downstream_koji_build_report_handler"
sync_from_downstream = "task.run_sync_from_downstream_handler"
bodhi_update = "task.bodhi_update"
bodhi_update_from_sidetag = "task.bodhi_update_from_sidetag"
retrigger_bodhi_update = "task.retrigger_bodhi_update"
issue_comment_retrigger_bodhi_update = "task.issue_comment_retrigger_bodhi_update"
github_fas_verification = "task.github_fas_verification"
vm_image_build = "task.run_vm_image_build_handler"
vm_image_build_result = "task.run_vm_image_build_result_handler"
pull_from_upstream = "task.pull_from_upstream"
check_onboarded_projects = "task.check_onboarded_projects"
koji_build_tag = "task.koji_build_tag"
tag_into_sidetag = "task.tag_into_sidetag"
class Handler(PackitAPIProtocol, Config):
def run(self) -> TaskResults:
raise NotImplementedError("This should have been implemented.")
def get_tag_info(self) -> dict:
tags = {"handler": self.__class__.__name__}
# repository info for easier filtering events that were grouped based on event type
if self.project:
tags.update(
{
"repository": self.project.repo,
"namespace": self.project.namespace,
}
)
if "package_name" in self.data.event_dict:
tags.update({"package_name": self.data.event_dict["package_name"]})
return tags
def run_n_clean(self) -> TaskResults:
try:
with push_scope_to_sentry() as scope:
for k, v in self.get_tag_info().items():
scope.set_tag(k, v)
return self.run()
finally:
self.clean()
def _clean_workplace(self):
# clean only when we are in k8s for sure
if not getenv("KUBERNETES_SERVICE_HOST"):
logger.debug("This is not a kubernetes pod, won't clean.")
return
logger.debug("Removing contents of the PV.")
p = Path(self.service_config.command_handler_work_dir)
# Do not clean dir if does not exist
if not p.is_dir():
logger.debug(
f"Directory {self.service_config.command_handler_work_dir!r} does not exist."
)
return
# remove everything in the volume, but not the volume dir
dir_items = list(p.iterdir())
if dir_items:
logger.info("Volume is not empty.")
logger.debug(f"Content: {[g.name for g in dir_items]}")
for item in dir_items:
# symlink pointing to a dir is also a dir and a symlink
if item.is_symlink() or item.is_file():
item.unlink()
else:
shutil.rmtree(item)
@staticmethod
def get_checkers() -> Tuple[Type[Checker], ...]:
return ()
@classmethod
def pre_check(
cls,
package_config: PackageConfig,
job_config: JobConfig,
event: dict,
) -> bool:
"""
Returns
bool: False if we have to skip the job execution.
"""
checks_pass = True
for checker_cls in cls.get_checkers():
checker = checker_cls(
package_config=package_config,
job_config=job_config,
event=event,
)
checks_pass = checks_pass and checker.pre_check()
return checks_pass
@staticmethod
def get_handler_specific_task_accepted_message(
service_config: ServiceConfig,
) -> str:
"""
Get a message specific to a particular handler that will be appended
to the 'Task was accepted' message posted once Packit picks up a job.
"""
return ""
def clean(self):
"""clean up the mess once we're done"""
logger.info("Cleaning up the mess.")
self.clean_api()
self._clean_workplace()
class JobHandler(Handler):
"""Generic interface to handle different type of inputs"""
task_name: TaskName
def __init__(
self,
package_config: PackageConfig,
job_config: JobConfig,
event: dict,
):
# build helper needs package_config to resolve dependencies b/w tests and build jobs
self.package_config = package_config
# always use job_config to pick up values, use package_config only for package_config.jobs
self.job_config = job_config
self.data = EventData.from_event_dict(event)
self.pushgateway = Pushgateway()
self._db_project_object: Optional[AbstractProjectObjectDbType] = None
self._project: Optional[GitProject] = None
self._clean_workplace()
def get_package_name(self) -> Optional[str]:
"""If the package_config is just for one package,
returns the package name. Otherwise None.
Handlers should always have PackageConfigView(s)
references which hold just a single package.
"""
if len(self.package_config.packages) == 1:
return list(self.package_config.packages.keys())[0]
else:
return None
def run_job(self):
"""
If pre-check succeeds, run the job for the specific handler.
:return: Dict [str, TaskResults]
"""
job_type = (
self.job_config.type.value if self.job_config else self.task_name.value
)
logger.debug(f"Running handler {str(self)} for {job_type}")
job_results: Dict[str, TaskResults] = {}
current_time = datetime.now().strftime(DATETIME_FORMAT)
result_key = f"{job_type}-{current_time}"
job_results[result_key] = self.run_n_clean()
logger.debug("Job finished!")
for result in job_results.values():
if not (result and result["success"]):
if msg := result["details"].get("msg"):
logger.error(msg)
# push the metrics from job
self.pushgateway.push()
return job_results
@classmethod
def get_signature(cls, event: Event, job: Optional[JobConfig]) -> Signature:
"""
Get the signature of a Celery task which will run the handler.
https://docs.celeryq.dev/en/stable/userguide/canvas.html#signatures
:param event: event which triggered the task
:param job: job to process
"""
logger.debug(f"Getting signature of a Celery task {cls.task_name}.")
return signature(
cls.task_name.value,
kwargs={
"package_config": dump_package_config(
event.packages_config.get_package_config_for(job)
if event.packages_config
else None
),
"job_config": dump_job_config(job),
"event": event.get_dict(),
},
)
def run(self) -> TaskResults:
raise NotImplementedError("This should have been implemented.")
class RetriableJobHandler(JobHandler):
def __init__(
self,
package_config: PackageConfig,
job_config: JobConfig,
event: dict,
celery_task: Task,
):
super().__init__(
package_config=package_config, job_config=job_config, event=event
)
self.celery_task = CeleryTask(celery_task)
def run(self) -> TaskResults:
raise NotImplementedError("This should have been implemented.")