-
Notifications
You must be signed in to change notification settings - Fork 9
/
pulp.py
104 lines (79 loc) · 3.19 KB
/
pulp.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
import logging
import os
import sys
from threading import Lock
import attr
from pubtools.pluggy import hookimpl, pm # pylint: disable=wrong-import-order
from ..gateway import ExodusGatewaySession
LOG = logging.getLogger("pubtools-exodus")
# pylint: disable=unused-argument
class ExodusPulpHandler(ExodusGatewaySession):
def __init__(self):
super(ExodusPulpHandler, self).__init__()
self.lock = Lock()
self.publish_committed = False
@hookimpl
def pulp_repository_pre_publish(self, repository, options):
"""Invoked as the first step in publishing a Pulp repository.
This implementation adds to each config the --exodus-publish argument,
attaching the repository to an exodus-gw publish.
Args:
repository (:class:`~pubtools.pulplib.Repository`):
The repository to be published.
options (:class:`~pubtools.pulplib.PublishOptions`):
The options to use in publishing.
Returns:
options (:class:`~pubtools.pulplib.PublishOptions`):
The adjusted options used for this publish.
"""
with self.lock:
if not self.publish:
self.publish = self.new_publish()
if not self.publish:
return None
args = (
list(options.rsync_extra_args) if options.rsync_extra_args else []
)
args.append("--exodus-publish=%s" % self.publish["id"])
return attr.evolve(options, rsync_extra_args=args)
@property
def exodus_enabled(self):
# If this hook-specific env var is set, then it solely determines
# whether the hook is enabled.
enabled = os.getenv("EXODUS_PULP_HOOK_ENABLED")
if enabled is not None:
return enabled.lower() in ["true", "t", "1", "yes", "y"]
# Otherwise, we let super decide, which will check a more generic env var.
return super().exodus_enabled
def ensure_publish_committed(self):
"""Commit the current publish, if and only if there is a current publish
and it was not already committed.
"""
if not self.publish:
LOG.debug("No exodus-gw publish to commit")
return
if self.publish_committed:
LOG.debug("Publish is already committed")
return
self.publish_committed = True
self.commit_publish(self.publish)
@hookimpl
def task_pulp_flush(self):
"""Invoked during task execution after successful completion of all
Pulp publishes.
This implementation commits the active exodus-gw publish, making
the content visible on the target CDN environment.
"""
self.ensure_publish_committed()
@hookimpl
def task_stop(self, failed):
pm.unregister(self)
if not failed:
# If a task is finishing up successfully and there's still an uncommitted
# publish, make sure to commit it. This is not expected to do anything
# if the commit happened earlier via task_pulp_flush.
self.ensure_publish_committed()
@hookimpl
def task_start():
pm.register(ExodusPulpHandler())
pm.register(sys.modules[__name__])