forked from release-engineering/pubtools-pulplib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyum.py
More file actions
586 lines (458 loc) · 21.5 KB
/
yum.py
File metadata and controls
586 lines (458 loc) · 21.5 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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
import re
from frozenlist2 import frozenlist
from more_executors.futures import f_map, f_proxy, f_return, f_zip, f_flat_map
from .base import Repository, SyncOptions, repo_type, Importer
from ..attr import pulp_attrib
from ..common import DetachedException
from ...model.unit import RpmUnit
from ... import compat_attr as attr, comps
from ...criteria import Criteria, Matcher
@attr.s(kw_only=True, frozen=True)
class YumImporter(Importer):
type_id = pulp_attrib(
default="yum_importer", type=str, pulp_field="importer_type_id"
)
"""
Specific importer_type_id for Yum repositories.
"""
@attr.s(kw_only=True, frozen=True)
class YumSyncOptions(SyncOptions):
"""Options controlling a yum repository
:meth:`~pubtools.pulplib.Repository.sync`.
"""
query_auth_token = pulp_attrib(default=None, type=str)
"""An authorization token that will be added to every request made to the feed URL's server
"""
max_downloads = pulp_attrib(default=None, type=int)
"""Number of threads used when synchronizing the repository.
"""
remove_missing = pulp_attrib(default=None, type=bool)
"""If true, as the repository is synchronized, old rpms will be removed.
"""
retain_old_count = pulp_attrib(default=None, type=int)
"""Count indicating how many old rpm versions to retain.
"""
skip = pulp_attrib(default=None, type=list, pulp_field="type_skip_list")
"""List of content types to be skipped during the repository synchronization
"""
checksum_type = pulp_attrib(default=None, type=str)
"""checksum type to use for metadata generation.
Defaults to source checksum type of sha256
"""
num_retries = pulp_attrib(default=None, type=int)
"""Number of times to retry before declaring an error during repository synchronization
Default is 2.
"""
download_policy = pulp_attrib(default=None, type=str)
"""Set the download policy for a repository.
Supported options are immediate,on_demand,background
"""
force_full = pulp_attrib(default=None, type=bool)
"""Boolean flag. If true, full re-sync is triggered.
"""
require_signature = pulp_attrib(default=None, type=bool)
"""Requires that imported packages like RPM/DRPM/SRPM should be signed
"""
allowed_keys = pulp_attrib(default=None, type=list)
"""List of allowed signature key IDs that imported packages can be signed with
"""
@repo_type("rpm-repo")
@attr.s(kw_only=True, frozen=True)
class YumRepository(Repository):
"""A :class:`~pubtools.pulplib.Repository` for RPMs, errata and related content."""
# this class only overrides some defaults for attributes defined in super
type = pulp_attrib(default="rpm-repo", type=str, pulp_field="notes._repo-type")
population_sources = pulp_attrib(
default=attr.Factory(frozenlist),
type=list,
converter=frozenlist,
pulp_field="notes.population_sources",
mutable=True,
)
"""List of repository IDs used to populate this repository
"""
ubi_population = pulp_attrib(
default=False,
type=bool,
pulp_field="notes.ubi_population",
mutable=True,
)
"""Flag indicating whether repo should be populated from population_sources for the purposes of UBI
"""
mutable_urls = attr.ib(
default=attr.Factory(lambda: frozenlist(["repodata/repomd.xml"])),
type=list,
converter=frozenlist,
)
ubi_config_version = pulp_attrib(
default="",
type=str,
pulp_field="notes.ubi_config_version",
mutable=True,
)
"""Version of UBI config that should be used for population of this repository."""
importer = pulp_attrib(
default=YumImporter(),
type=YumImporter,
pulp_field="importers",
pulp_py_converter=YumImporter._from_data,
py_pulp_converter=YumImporter._to_data,
)
"""
An object of :class:`~pubtools.pulplib.YumImporter` that is associated with the repository.
.. versionadded:: 2.39.0
"""
def get_binary_repository(self):
"""Find and return the binary repository relating to this repository.
Yum repositories usually come in triplets of
(binary RPMs, debuginfo RPMs, source RPMs). For example:
.. list-table::
:widths: 75 25
* - ``rhel-7-server-rpms__7Server__x86_64``
- binary
* - ``rhel-7-server-debug-rpms__7Server__x86_64``
- debug
* - ``rhel-7-server-source-rpms__7Server__x86_64``
- source
This method along with :meth:`get_debug_repository` and :meth:`get_source_repository` allow locating other repositories
from within this group.
Returns:
``Future[YumRepository]``
Binary repository relating to this repository.
``Future[None]``
If there is no related repository.
"""
return self._get_related_repository(repo_t="binary")
def get_debug_repository(self):
"""Find and return the debug repository relating to this repository.
Returns:
``Future[YumRepository]``
Debug repository relating to this repository.
``Future[None]``
If there is no related repository.
"""
return self._get_related_repository(repo_t="debug")
def get_source_repository(self):
"""Find and return the source repository relating to this repository.
Returns:
``Future[YumRepository]``
Source repository relating to this repository.
``Future[None]``
If there is no related repository.
"""
return self._get_related_repository(repo_t="source")
def _get_related_repository(self, repo_t):
if not self._client:
raise DetachedException()
suffixes_mapping = {
"binary": "/os",
"debug": "/debug",
"source": "/source/SRPMS",
}
regex = r"(/os|/source/SRPMS|/debug)$"
def unpack_page(page):
if len(page.data) != 1:
return None
return page.data[0]
suffix = suffixes_mapping[repo_t]
if str(self.relative_url).endswith(suffix):
return f_proxy(f_return(self))
base_url = re.sub(regex, "", self.relative_url)
relative_url = base_url + suffix
criteria = Criteria.and_(
Criteria.with_field("relative_url", relative_url),
Criteria.with_field("type_id", "yum_distributor"),
)
page_f = self._client.search_distributor(criteria)
distributor_f = f_proxy(f_map(page_f, unpack_page))
if distributor_f.result() is None:
out = f_return()
else:
out = self._client.get_repository(distributor_f.repo_id)
return out
def upload_rpm(self, file_obj, **kwargs):
"""Upload an RPM to this repository.
.. warning::
For RPMs belonging to a module, it's strongly advised to upload
the module metadata first (using :meth:`upload_modules`) and only
proceed with uploading RPMs once module upload has completed.
This reduces the risk of accidentally publishing a repository with
modular RPMs without the corresponding metadata (which has a much
worse impact than publishing metadata without the corresponding RPMs).
Args:
file_obj (str, file object)
If it's a string, then it's the path of an RPM to upload.
Otherwise, it should be a
`file-like object <https://docs.python.org/3/glossary.html#term-file-object>`_
pointing at the bytes to upload.
The client takes ownership of this file object; it should
not be modified elsewhere, and will be closed when upload
completes.
kwargs
Additional field values to set on the uploaded unit.
Any :class:`~pubtools.pulplib.RpmUnit` fields documented as
*mutable* may be included here (for example, ``cdn_path``).
An error will occur if attempting to set other fields.
Returns:
Future[list of :class:`~pubtools.pulplib.Task`]
A future which is resolved after content has been imported
to this repo.
Raises:
DetachedException
If this instance is not attached to a Pulp client.
.. versionadded:: 2.16.0
.. versionadded:: 2.20.0
Added ability to set mutable fields on upload.
"""
# We want some name of what we're uploading for logging purposes, but the
# input could be a plain string, or a file object with 'name' attribute, or
# a file object without 'name' ... make sure we do something reasonable in
# all cases.
if isinstance(file_obj, str):
name = file_obj
else:
# If we don't know what we're uploading we just say it's "an RPM"...
name = getattr(file_obj, "name", "an RPM")
unit_metadata_fn = None
usermeta = RpmUnit._usermeta_from_kwargs(**kwargs)
if usermeta:
unit_metadata_fn = lambda _: usermeta
return self._upload_then_import(
file_obj, name, "rpm", unit_metadata_fn=unit_metadata_fn
)
def upload_metadata(self, file_obj, metadata_type):
"""Upload a metadata file to this repository.
A metadata file is any additional file which will be published alongside,
and referenced from, the repodata ``.xml`` and ``.sqlite`` files when this
repo is published.
Args:
file_obj (str, file object)
If it's a string, then it's the path of a file to upload.
Otherwise, it should be a
`file-like object <https://docs.python.org/3/glossary.html#term-file-object>`_
pointing at the bytes to upload.
The client takes ownership of this file object; it should
not be modified elsewhere, and will be closed when upload
completes.
metadata_type (str)
Identifies the type of metadata being uploaded.
This is an arbitrary string which will be reproduced in the yum
repo metadata on publish. The appropriate value depends on the
type of data being uploaded. For example, ``"productid"`` should
be used when uploading an RHSM-style product certificate.
A repository may only contain a single metadata file of each type.
If a file of this type is already present in the repo, it will be
overwritten by the upload.
Returns:
Future[list of :class:`~pubtools.pulplib.Task`]
A future which is resolved after content has been imported
to this repo.
Raises:
DetachedException
If this instance is not attached to a Pulp client.
.. versionadded:: 2.17.0
"""
if isinstance(file_obj, str):
name = "%s (%s)" % (file_obj, metadata_type)
else:
# If we don't know what we're uploading we just say "<type> metadata"...
name = getattr(file_obj, "name", "%s metadata" % metadata_type)
return self._upload_then_import(
file_obj,
name,
"yum_repo_metadata_file",
# Requirements around unit key and metadata can be found at:
# https://github.com/pulp/pulp_rpm/blob/5c5a7dcc058b29d89b3a913d29cfcab41db96686/plugins/pulp_rpm/plugins/importers/yum/upload.py#L246
unit_key_fn=lambda _: {"data_type": metadata_type, "repo_id": self.id},
unit_metadata_fn=lambda upload: {
"checksum": upload[0],
"checksum_type": "sha256",
},
)
def upload_modules(self, file_obj):
"""Upload a modulemd stream to this repository.
All supported documents in the given stream will be imported to this
repository. On current versions of Pulp 2.x, this means only:
* `modulemd v2 <https://github.com/fedora-modularity/libmodulemd/blob/main/yaml_specs/modulemd_stream_v2.yaml>`_
* `modulemd-defaults v1 <https://github.com/fedora-modularity/libmodulemd/blob/main/yaml_specs/modulemd_defaults_v1.yaml>`_
Attempting to use other document types may result in an error.
Args:
file_obj (str, file object)
If it's a string, then it's the path of a modulemd YAML
file to upload.
Otherwise, it should be a
`file-like object <https://docs.python.org/3/glossary.html#term-file-object>`_
pointing at the text to upload.
The client takes ownership of this file object; it should
not be modified elsewhere, and will be closed when upload
completes.
Returns:
Future[list of :class:`~pubtools.pulplib.Task`]
A future which is resolved after content has been imported
to this repo.
Raises:
DetachedException
If this instance is not attached to a Pulp client.
.. versionadded:: 2.17.0
"""
if isinstance(file_obj, str):
name = file_obj
else:
name = getattr(file_obj, "name", "modulemds")
return self._upload_then_import(file_obj, name, "modulemd")
def upload_comps_xml(self, file_obj):
"""Upload a comps.xml file to this repository.
.. warning::
Beware of the following quirks with respect to the upload of comps.xml:
* Pulp does not directly store the uploaded XML. Instead, this library
parses the XML and uses the content to store various units. The comps
XML rendered as a yum repository is published is therefore not
guaranteed to be bytewise-identical to the uploaded content.
* The uploaded XML must contain all comps data for the repo, as
any existing comps data will be removed from the repo.
* The XML parser is not secure against maliciously constructed data.
* The process of parsing the XML and storing units consists of multiple
steps which cannot be executed atomically. That means *if this
operation is interrupted, the repository may be left with incomplete
data*. It's recommended to avoid publishing a repository in this state.
Args:
file_obj (str, file object)
If it's a string, then it's the path of a comps XML
file to upload.
Otherwise, it should be a
`file-like object <https://docs.python.org/3/glossary.html#term-file-object>`_
pointing at the bytes of a valid comps.xml file.
The client takes ownership of this file object; it should
not be modified elsewhere, and will be closed when upload
completes.
Returns:
Future[list of :class:`~pubtools.pulplib.Task`]
A future which is resolved after content has been imported
to this repo.
Raises:
DetachedException
If this instance is not attached to a Pulp client.
.. versionadded:: 2.17.0
"""
if isinstance(file_obj, str):
file_name = file_obj
file_obj = open(file_obj, "rb")
else:
file_name = getattr(file_obj, "name", "comps.xml")
# Parse the provided XML. We will crash here if the given XML is not
# valid.
with file_obj:
unit_dicts = comps.units_for_xml(file_obj)
# Every comps-related unit type has a repo_id which should reference the repo
# we're uploading to.
for unit in unit_dicts:
unit["repo_id"] = self.id
comps_type_ids = [
"package_group",
"package_category",
"package_environment",
"package_langpacks",
]
# Remove former units of comps-related types so that the end result is only
# those units included in the current XML.
crit = Criteria.with_field("content_type_id", Matcher.in_(comps_type_ids))
out = self.remove_content(crit)
# Once removal is done we can upload each unit.
upload_f = []
for unit_dict in unit_dicts:
type_id = unit_dict["_content_type_id"]
# For one comps.xml we are doing multiple upload operations, each of
# which would be logged independently. Come up with some reasonable name
# for each unit to put into the logs.
#
# Example: if uploading my-comps.xml and processing a package_group
# with id kde-desktop-environment, the name for logging purposes would
# be: "my-comps.xml [group.kde-desktop-environment]".
#
unit_name = type_id.replace("package_", "")
if unit_dict.get("id"):
unit_name = "%s.%s" % (unit_name, unit_dict["id"])
unit_name = "%s [%s]" % (file_name, unit_name)
upload_f.append(
f_flat_map(
out, self._comps_unit_uploader(unit_name, type_id, unit_dict)
)
)
# If there were no units to upload then just return the removal.
if not upload_f:
return out
# There were uploads, then we'll wait for all of them to complete and
# return the tasks for all.
out = f_zip(*upload_f)
out = f_map(out, lambda uploads: sum(uploads, []))
return out
def _comps_unit_uploader(self, name, type_id, metadata):
# A helper used from upload_comps_xml.
#
# This helper only exists to eagerly bind arguments for a single
# unit upload, due to confusing behavior around variable scope
# when combining loops and lambdas.
def upload(_unused):
return self._upload_then_import(
file_obj=None,
name=name,
type_id=type_id,
unit_metadata_fn=lambda _: metadata,
)
return upload
def upload_erratum(self, erratum):
"""Upload an erratum/advisory object to this repository.
.. warning::
There are many quirks with respect to advisory upload. Please be aware
of the following before using this API:
* Only one advisory with a given ``id`` may exist in the system.
* When uploading an advisory with an ``id`` equal to one already in the
system, the upload will generally be ignored (i.e. complete successfully
but have no effect), unless either the ``version`` or ``updated`` fields
have a value larger than the existing advisory.
This implies that, if you want to ensure an existing advisory is updated,
you must first search for the existing object and mutate one of these
fields before uploading a modified object. *The library will not take
care of this for you.*
* When overwriting an existing advisory, all fields will be overwritten.
The sole exception is the ``pkglist`` field which will be merged with
existing data when applicable.
* If an advisory with the same ``id`` is present in multiple published yum
repositories with inconsistent fields, yum/dnf client errors or warnings
may occur. It's therefore recommended that, whenever an existing
advisory is modified, every repository containing that advisory should
be republished. *The library will not take care of this for you.*
* The ``repository_memberships`` field on the provided object has no effect
(it cannot be used to upload an advisory to multiple repos at once).
Args:
erratum (:class:`~pubtools.pulplib.ErratumUnit`)
An erratum object.
Unlike most other uploaded content, errata are not backed by any
file; any arbitrarily constructed ErratumUnit may be uploaded.
Returns:
Future[list of :class:`~pubtools.pulplib.Task`]
A future which is resolved after content has been imported
to this repo.
Raises:
DetachedException
If this instance is not attached to a Pulp client.
.. versionadded:: 2.17.0
"""
# Convert from ErratumUnit to a raw Pulp-style dict, recursively.
erratum_dict = erratum._to_data()
# Drop this one field because the _content_type_id, though embedded
# in unit dicts on read, is passed as a separate parameter on write.
type_id = erratum_dict.pop("_content_type_id")
# Drop this because the caller cannot influence the _id (unit id)
# for uploaded units.
del erratum_dict["_id"]
# And drop this one because repository_memberships is synthesized when
# Pulp renders units, and can't be set during import.
del erratum_dict["repository_memberships"]
return self._upload_then_import(
file_obj=None,
name=erratum_dict["id"],
type_id=type_id,
unit_key_fn=lambda _: {"id": erratum_dict["id"]},
unit_metadata_fn=lambda _: erratum_dict,
)