forked from pulp/pulp_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.py
More file actions
242 lines (210 loc) · 9.08 KB
/
Copy pathsync.py
File metadata and controls
242 lines (210 loc) · 9.08 KB
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
import logging
from gettext import gettext as _
from os import environ
from rest_framework import serializers
from pulpcore.plugin.models import Artifact, ProgressReport, Remote, Repository
from pulpcore.plugin.stages import (
DeclarativeArtifact,
DeclarativeContent,
DeclarativeVersion,
Stage,
)
from pulp_python.app.models import (
PythonPackageContent,
PythonRemote,
)
from pulp_python.app.utils import parse_metadata
from bandersnatch.mirror import Mirror
from bandersnatch.master import Master
from bandersnatch.configuration import BandersnatchConfig
from packaging.requirements import Requirement
logger = logging.getLogger(__name__)
def sync(remote_pk, repository_pk, mirror):
"""
Sync content from the remote repository.
Create a new version of the repository that is synchronized with the remote.
Args:
remote_pk (str): The remote PK.
repository_pk (str): The repository PK.
mirror (boolean): True for mirror mode, False for additive mode.
Raises:
serializers: ValidationError
"""
remote = PythonRemote.objects.get(pk=remote_pk)
repository = Repository.objects.get(pk=repository_pk)
if not remote.url:
raise serializers.ValidationError(
detail=_("A remote must have a url attribute to sync.")
)
first_stage = PythonBanderStage(remote)
DeclarativeVersion(first_stage, repository, mirror).create()
def create_bandersnatch_config(remote):
"""Modifies the global Bandersnatch config state for this sync"""
config = BandersnatchConfig().config
config["mirror"]["master"] = remote.url
config["mirror"]["workers"] = str(remote.download_concurrency)
if not config.has_section("plugins"):
config.add_section("plugins")
config["plugins"]["enabled"] = "blocklist_release\n"
if remote.includes:
if not config.has_section("allowlist"):
config.add_section("allowlist")
config["plugins"]["enabled"] += "allowlist_release\nallowlist_project\n"
config["allowlist"]["packages"] = "\n".join(remote.includes)
if remote.excludes:
if not config.has_section("blocklist"):
config.add_section("blocklist")
config["plugins"]["enabled"] += "blocklist_project\n"
config["blocklist"]["packages"] = "\n".join(remote.excludes)
if not remote.prereleases:
config["plugins"]["enabled"] += "prerelease_release\n"
if remote.package_types:
rrfm = "regex_release_file_metadata"
config["plugins"]["enabled"] += rrfm
if not config.has_section(rrfm):
config.add_section(rrfm)
config[rrfm]["any:release_file.packagetype"] = "\n".join(remote.package_types)
if remote.keep_latest_packages:
config["plugins"]["enabled"] += "latest_release\n"
if not config.has_section("latest_release"):
config.add_section("latest_release")
config["latest_release"]["keep"] = str(remote.keep_latest_packages)
if remote.exclude_platforms:
config["plugins"]["enabled"] += "exclude_platform\n"
if not config.has_section("blocklist"):
config.add_section("blocklist")
config["blocklist"]["platforms"] = "\n".join(remote.exclude_platforms)
class PythonBanderStage(Stage):
"""
Python Package Syncing Stage using Bandersnatch
"""
def __init__(self, remote):
"""Initialize the stage and Bandersnatch config"""
super().__init__()
self.remote = remote
create_bandersnatch_config(remote)
async def run(self):
"""
If includes is specified, then only sync those,else try to sync all other packages
"""
# TODO Change Bandersnatch internal API to take proxy settings in from config parameters
if self.remote.proxy_url:
environ['http_proxy'] = self.remote.proxy_url
# local & global timeouts defaults to 10secs and 5 hours
async with Master(self.remote.url) as master:
if self.remote.proxy_url:
environ.pop('http_proxy')
deferred_download = self.remote.policy != Remote.IMMEDIATE
workers = self.remote.download_concurrency or self.remote.DEFAULT_DOWNLOAD_CONCURRENCY
with ProgressReport(
message="Fetching Project Metadata", code="sync.fetching.project"
) as p:
pmirror = PulpMirror(
serial=0, # Serial currently isn't supported by Pulp
master=master,
workers=workers,
deferred_download=deferred_download,
python_stage=self,
progress_report=p,
)
packages_to_sync = None
if self.remote.includes:
packages_to_sync = [
Requirement(pkg).name for pkg in self.remote.includes
]
await pmirror.synchronize(packages_to_sync)
class PulpMirror(Mirror):
"""
Pulp Mirror Class to perform syncing using Bandersnatch
"""
def __init__(
self, serial, master, workers, deferred_download, python_stage, progress_report
):
"""Initialize Bandersnatch Mirror"""
super().__init__(master=master, workers=workers)
self.synced_serial = serial
self.python_stage = python_stage
self.progress_report = progress_report
self.deferred_download = deferred_download
async def determine_packages_to_sync(self):
"""
Calling this means that includes wasn't specified,
so try to get all of the packages from Mirror (hopefully PyPi)
"""
number_xmlrpc_attempts = 3
for attempt in range(number_xmlrpc_attempts):
logger.info(
"Attempt {} to get package list from {}".format(
attempt, self.master.url
)
)
try:
if not self.synced_serial:
logger.info("Syncing all packages.")
# First get the current serial, then start to sync.
all_packages = await self.master.all_packages()
self.packages_to_sync.update(all_packages)
self.target_serial = max(
[self.synced_serial] + [int(v) for v in self.packages_to_sync.values()]
)
else:
logger.info("Syncing based on changelog.")
changed_packages = await self.master.changed_packages(
self.synced_serial
)
self.packages_to_sync.update(changed_packages)
self.target_serial = max(
[self.synced_serial] + [int(v) for v in self.packages_to_sync.values()]
)
self._filter_packages()
logger.info(f"Trying to reach serial: {self.target_serial}")
pkg_count = len(self.packages_to_sync)
logger.info(f"{pkg_count} packages to sync.")
return
except Exception as e:
"""Handle different exceptions if it is XMLRPC error or Mirror error"""
logger.info("Encountered an error in Master {}".format(e))
pass
"""
If we reach here, then the Mirror most likely doesn't support XMLRPC.
Could raise an exception or try to manually find all the packages from the index page,
Or just keep packages_to_sync empty and have the sync do no work
"""
async def process_package(self, package):
"""Filters the package and creates content from it"""
# Don't save anything if our metadata filters all fail.
self.progress_report.increment()
if not package.filter_metadata(self.filters.filter_metadata_plugins()):
return None
package.filter_all_releases_files(self.filters.filter_release_file_plugins())
package.filter_all_releases(self.filters.filter_release_plugins())
await self.create_content(package)
async def create_content(self, pkg):
"""
Take the filtered package, separate into releases and
create a Content Unit to put into the pipeline
"""
for version, dists in pkg.releases.items():
for package in dists:
entry = parse_metadata(pkg.info, version, package)
url = entry.pop("url")
artifact = Artifact(sha256=entry["sha256"])
package = PythonPackageContent(**entry)
da = DeclarativeArtifact(
artifact,
url,
entry["filename"],
self.python_stage.remote,
deferred_download=self.deferred_download,
)
dc = DeclarativeContent(content=package, d_artifacts=[da])
await self.python_stage.put(dc)
def finalize_sync(self):
"""No work to be done currently"""
pass
def on_error(self, exception, **kwargs):
"""
TODO
This should have some error checking
"""
pass