forked from pulp/pulp_ansible
/
collections.py
1207 lines (1032 loc) · 47.8 KB
/
collections.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
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import hashlib
import json
import logging
import tarfile
from collections import defaultdict
from gettext import gettext as _
from operator import attrgetter
from urllib.parse import urljoin
from uuid import uuid4
import yaml
from aiohttp.client_exceptions import ClientError, ClientResponseError
from asgiref.sync import sync_to_async
from async_lru import alru_cache
from django.conf import settings
from django.db import transaction
from django.db.models import F, Q
from django.db.utils import IntegrityError
from django.urls import reverse
from django.utils.dateparse import parse_datetime
from galaxy_importer.collection import CollectionFilename
from galaxy_importer.collection import import_collection as process_collection
from galaxy_importer.collection import sync_collection
from galaxy_importer.exceptions import ImporterError
from git import GitCommandError, Repo
from pulpcore.plugin.models import (
Artifact,
ContentArtifact,
CreatedResource,
ProgressReport,
PulpTemporaryFile,
Remote,
RepositoryContent,
RepositoryVersion,
Task,
)
from pulpcore.plugin.stages import (
ArtifactDownloader,
ArtifactSaver,
ContentSaver,
DeclarativeArtifact,
DeclarativeContent,
DeclarativeVersion,
QueryExistingArtifacts,
QueryExistingContents,
RemoteArtifactSaver,
ResolveContentFutures,
Stage,
)
from rest_framework.serializers import ValidationError
from semantic_version import SimpleSpec, Version
from semantic_version.base import Always
from pulp_ansible.app.constants import PAGE_SIZE
from pulp_ansible.app.models import (
AnsibleCollectionDeprecated,
AnsibleNamespace,
AnsibleNamespaceMetadata,
AnsibleRepository,
Collection,
CollectionImport,
CollectionRemote,
CollectionVersion,
CollectionVersionMark,
CollectionVersionSignature,
Tag,
)
from pulp_ansible.app.serializers import CollectionVersionSerializer
from pulp_ansible.app.tasks.utils import (
RequirementsFileEntry,
get_file_obj_from_tarball,
parse_collections_requirements_file,
parse_metadata,
)
log = logging.getLogger(__name__)
# semantic_version.SimpleSpec interpretes "*" as ">=0.0.0"
class AnsibleSpec(SimpleSpec):
def __init__(self, expression):
super().__init__(expression)
if self.expression == "*":
self.clause = Always()
async def declarative_content_from_git_repo(remote, url, git_ref=None, metadata_only=False):
"""Returns a DeclarativeContent for the Collection in a Git repository."""
if git_ref:
try:
gitrepo = Repo.clone_from(
url, uuid4(), depth=1, branch=git_ref, multi_options=["--recurse-submodules"]
)
except GitCommandError:
gitrepo = Repo.clone_from(url, uuid4(), multi_options=["--recurse-submodules"])
gitrepo.git.checkout(git_ref)
else:
gitrepo = Repo.clone_from(url, uuid4(), depth=1, multi_options=["--recurse-submodules"])
commit_sha = gitrepo.head.commit.hexsha
metadata, artifact_path = sync_collection(gitrepo.working_dir, ".")
if not metadata_only:
artifact = Artifact.init_and_validate(artifact_path)
try:
await sync_to_async(artifact.save)()
except IntegrityError:
artifact = Artifact.objects.get(sha256=artifact.sha256)
metadata["artifact_url"] = reverse("artifacts-detail", args=[artifact.pk])
metadata["artifact"] = artifact
else:
metadata["artifact"] = None
metadata["artifact_url"] = None
metadata["remote_artifact_url"] = "{}/commit/{}".format(url.rstrip("/"), commit_sha)
artifact = metadata["artifact"]
try:
collection_version = await sync_to_async(create_collection_from_importer)(
metadata, metadata_only=metadata_only
)
await sync_to_async(ContentArtifact.objects.get_or_create)(
artifact=artifact,
content=collection_version,
relative_path=collection_version.relative_path,
)
except ValidationError as e:
if e.args[0]["non_field_errors"][0].code == "unique":
namespace = metadata["metadata"]["namespace"]
name = metadata["metadata"]["name"]
version = metadata["metadata"]["version"]
else:
raise e
collection_version = await sync_to_async(CollectionVersion.objects.get)(
namespace=namespace, name=name, version=version
)
if artifact is None:
artifact = Artifact()
d_artifact = DeclarativeArtifact(
artifact=artifact,
url=metadata["remote_artifact_url"],
relative_path=collection_version.relative_path,
remote=remote,
deferred_download=metadata_only,
)
# TODO: docs blob support??
d_content = DeclarativeContent(
content=collection_version,
d_artifacts=[d_artifact],
)
return d_content
def sync(remote_pk, repository_pk, mirror, optimize):
"""
Sync Collections with ``remote_pk``, and save a new RepositoryVersion for ``repository_pk``.
Args:
remote_pk (str): The remote PK.
repository_pk (str): The repository PK.
mirror (bool): True for mirror mode, False for additive.
optimize (boolean): Whether to optimize sync or not.
Raises:
ValueError: If the remote does not specify a URL to sync.
"""
remote = CollectionRemote.objects.get(pk=remote_pk)
repository = AnsibleRepository.objects.get(pk=repository_pk)
is_repo_remote = False
if repository.remote:
is_repo_remote = remote.pk == repository.remote.pk
if not remote.url:
raise ValueError(_("A CollectionRemote must have a 'url' specified to synchronize."))
deprecation_before_sync = set()
for namespace, name in AnsibleCollectionDeprecated.objects.filter(
pk__in=repository.latest_version().content
).values_list("namespace", "name"):
deprecation_before_sync.add(f"{namespace}.{name}")
first_stage = CollectionSyncFirstStage(
remote, repository, is_repo_remote, deprecation_before_sync, optimize
)
if first_stage.should_sync is False:
log.debug(_("no-op: remote wasn't updated since last sync."))
return
d_version = AnsibleDeclarativeVersion(first_stage, repository, mirror=mirror)
repo_version = d_version.create()
if not repo_version:
return
repository.last_synced_metadata_time = first_stage.last_synced_metadata_time
repository.save(update_fields=["last_synced_metadata_time"])
to_undeprecate = Q()
undeprecated = deprecation_before_sync.difference(first_stage.deprecation_after_sync)
if undeprecated:
for collection in undeprecated:
namespace, name = collection.split(".")
to_undeprecate |= Q(namespace=namespace, name=name)
deprecated = AnsibleCollectionDeprecated.objects.filter(to_undeprecate)
RepositoryContent.objects.filter(
repository=repository, content__in=deprecated
).all().update(version_removed=repo_version)
def import_collection(
temp_file_pk,
repository_pk=None,
expected_namespace=None,
expected_name=None,
expected_version=None,
):
"""
Create a Collection from an uploaded artifact and optionally validate its expected metadata.
This task provides optional validation of the `namespace`, `name`, and `version` metadata
attributes. If the Artifact fails validation or parsing, the Artifact is deleted and the
Collection is not created.
This task performs a CollectionImport object get_or_create() to allow import messages to be
logged.
Args:
temp_file_pk (str): The pk of the PulpTemporaryFile to create the Collection from.
Keyword Args:
repository_pk (str): Optional. If specified, a new RepositoryVersion will be created for the
Repository and any new Collection content associated with it.
expected_namespace (str): Optional. The namespace is validated against the namespace
specified in the Collection's metadata. If it does not match a ImporterError is
raised.
expected_name (str): Optional. The name is validated against the name specified in the
Collection's metadata. If it does not match a ImporterError is raised.
expected_version (str): Optional. The version is validated against the version specified in
the Collection's metadata. If it does not match a ImporterError is raised.
Raises:
ImporterError: If the `expected_namespace`, `expected_name`, or `expected_version` do not
match the metadata in the tarball.
"""
CollectionImport.objects.get_or_create(task_id=Task.current().pulp_id)
temp_file = PulpTemporaryFile.objects.get(pk=temp_file_pk)
filename = CollectionFilename(expected_namespace, expected_name, expected_version)
log.info(f"Processing collection from {temp_file.file.name}")
user_facing_logger = logging.getLogger("pulp_ansible.app.tasks.collection.import_collection")
try:
with temp_file.file.open() as artifact_file:
with tarfile.open(fileobj=artifact_file, mode="r") as tar:
manifest_data = json.load(
get_file_obj_from_tarball(tar, "MANIFEST.json", temp_file.file.name)
)
files_data = json.load(
get_file_obj_from_tarball(tar, "FILES.json", temp_file.file.name)
)
url = _get_backend_storage_url(artifact_file)
artifact_file.seek(0)
importer_result = process_collection(
artifact_file, filename=filename, file_url=url, logger=user_facing_logger
)
artifact = Artifact.from_pulp_temporary_file(temp_file)
temp_file = None
importer_result["artifact_url"] = reverse("artifacts-detail", args=[artifact.pk])
collection_version = create_collection_from_importer(importer_result)
collection_version.manifest = manifest_data
collection_version.files = files_data
collection_version.save()
except ImporterError as exc:
log.info(f"Collection processing was not successful: {exc}")
if temp_file:
temp_file.delete()
raise
except Exception as exc:
user_facing_logger.error(f"Collection processing was not successful: {exc}")
if temp_file:
temp_file.delete()
raise
ContentArtifact.objects.create(
artifact=artifact,
content=collection_version,
relative_path=collection_version.relative_path,
)
CreatedResource.objects.create(content_object=collection_version)
if repository_pk:
repository = AnsibleRepository.objects.get(pk=repository_pk)
content_q = CollectionVersion.objects.filter(pk=collection_version.pk)
with repository.new_version() as new_version:
new_version.add_content(content_q)
CreatedResource.objects.create(content_object=repository)
def create_collection_from_importer(importer_result, metadata_only=False):
"""
Process results from importer.
"""
collection_info = importer_result["metadata"]
with transaction.atomic():
collection, created = Collection.objects.get_or_create(
namespace=collection_info["namespace"], name=collection_info["name"]
)
tags = collection_info.pop("tags")
# Remove fields not used by this model
collection_info.pop("license_file")
collection_info.pop("readme")
# the importer returns many None values. We need to let the defaults in the model prevail
for key in ["description", "documentation", "homepage", "issues", "repository"]:
if collection_info[key] is None:
collection_info.pop(key)
collection_version = CollectionVersion(
collection=collection,
**collection_info,
requires_ansible=importer_result.get("requires_ansible"),
contents=importer_result["contents"],
docs_blob=importer_result["docs_blob"],
)
serializer_fields = CollectionVersionSerializer.Meta.fields
data = {k: v for k, v in collection_version.__dict__.items() if k in serializer_fields}
data["id"] = collection_version.pulp_id
if not metadata_only:
data["artifact"] = importer_result["artifact_url"]
serializer = CollectionVersionSerializer(data=data)
serializer.is_valid(raise_exception=True)
collection_version.save()
for name in tags:
tag, created = Tag.objects.get_or_create(name=name)
collection_version.tags.add(tag)
collection_version.save() # Save the FK updates
_update_highest_version(collection_version)
return collection_version
def rebuild_repository_collection_versions_metadata(
repository_version_pk, namespace=None, name=None, version=None
):
"""Rebuild metadata for all collection versions in a repo."""
repov = RepositoryVersion.objects.get(pk=repository_version_pk)
qs = None
if namespace or name or version:
qkwargs = {}
if namespace:
qkwargs["namespace"] = namespace
if name:
qkwargs["name"] = name
if version:
qkwargs["version"] = version
qs = CollectionVersion.objects.filter(**qkwargs)
qs = repov.get_content(content_qs=qs)
with ProgressReport(
message=_("Rebuild collection version metadata (total)"),
code="rebuild_metadata.total",
total=qs.count(),
) as ptotal, ProgressReport(
message=_("Rebuild collection version metadata (failed)"), code="rebuild_metadata.failed"
) as pfailed:
for cv in qs:
try:
_rebuild_collection_version_meta(cv)
except Exception as e:
pfailed.increment()
log.exception(e)
ptotal.increment()
def _rebuild_collection_version_meta(collection_version):
"""Rebuild metadata for a single collection version."""
# where is the artifact?
artifact = collection_version._artifacts.first()
# call the importer to re-generate meta
importer_result = process_collection(
artifact.file, filename=artifact.file.name, file_url=artifact.file.url, logger=None
)
# set the new info and save
collection_version.requires_ansible = importer_result["requires_ansible"]
collection_version.docs_blob = importer_result["docs_blob"]
collection_version.contents = importer_result["contents"]
collection_version.save()
def _get_backend_storage_url(artifact_file):
"""Get artifact url from pulp backend storage."""
if (
settings.DEFAULT_FILE_STORAGE == "pulpcore.app.models.storage.FileSystem"
or not settings.REDIRECT_TO_OBJECT_STORAGE
):
url = None
elif settings.DEFAULT_FILE_STORAGE == "storages.backends.s3boto3.S3Boto3Storage":
parameters = {"ResponseContentDisposition": "attachment%3Bfilename=archive.tar.gz"}
url = artifact_file.storage.url(artifact_file.name, parameters=parameters)
elif settings.DEFAULT_FILE_STORAGE == "storages.backends.azure_storage.AzureStorage":
url = artifact_file.storage.url(artifact_file.name)
else:
raise NotImplementedError(
f"The value settings.DEFAULT_FILE_STORAGE={settings.DEFAULT_FILE_STORAGE} "
"was not expected"
)
return url
def _update_highest_version(collection_version):
"""
Checks if this version is greater than the most highest one.
TODO: This function violates content immutability. It must be deprecated.
"""
qs = collection_version.collection.versions.annotate(prerelease=Q(version_prerelease__ne=""))
highest_subq = qs.order_by("prerelease", "-version")[0:1].values("pk")
# Order them in such a way, that only the latest updated record will recieve true.
# This should prevent hitting the uniquenes constraint.
qs.annotate(new_is_highest=Q(pk=highest_subq)).order_by("-prerelease", "version").update(
is_highest=F("new_is_highest")
)
class AnsibleDeclarativeVersion(DeclarativeVersion):
"""
Subclassed Declarative version creates a custom pipeline for Ansible sync.
"""
def pipeline_stages(self, new_version):
"""
Build a list of stages feeding into the ContentUnitAssociation stage.
This defines the "architecture" of the entire sync.
Args:
new_version (:class:`~pulpcore.plugin.models.RepositoryVersion`): The
new repository version that is going to be built.
Returns:
list: List of :class:`~pulpcore.plugin.stages.Stage` instances
"""
pipeline = [
self.first_stage,
QueryExistingArtifacts(),
ArtifactDownloader(),
ArtifactSaver(),
QueryExistingContents(),
DocsBlobDownloader(),
AnsibleContentSaver(new_version),
RemoteArtifactSaver(),
ResolveContentFutures(),
]
return pipeline
class CollectionSyncFirstStage(Stage):
"""
The first stage of a pulp_ansible sync pipeline.
"""
def __init__(self, remote, repository, is_repo_remote, deprecation_before_sync, optimize):
"""
The first stage of a pulp_ansible sync pipeline.
Args:
remote (CollectionRemote): The remote data to be used when syncing
repository (AnsibleRepository): The repository being syncedself.
is_repo_remote (bool): True if the remote is the repository's remote.
deprecation_before_sync (set): Set of deprecations before the sync.
optimize (boolean): Whether to optimize sync or not.
"""
super().__init__()
self.remote = remote
self.repository = repository
self.deprecation_before_sync = deprecation_before_sync
self.deprecation_after_sync = set()
self.collection_info = parse_collections_requirements_file(remote.requirements_file)
self.exclude_info = {}
self.add_dependents = self.collection_info and self.remote.sync_dependencies
self.signed_only = self.remote.signed_only
self.already_synced = set()
self._unpaginated_collection_metadata = None
self._unpaginated_collection_version_metadata = None
self.optimize = optimize
self.last_synced_metadata_time = None
self.namespaces_seen = set()
self._unpaginated_namespace_metadata = None
# Interpret download policy
self.deferred_download = self.remote.policy != Remote.IMMEDIATE
# Check if we should sync
self.should_sync = not is_repo_remote or asyncio.get_event_loop().run_until_complete(
self._should_we_sync()
)
@alru_cache(maxsize=128)
async def _get_root_api(self, root):
"""
Returns the root api path and api version.
Based on https://git.io/JTMxE.
"""
if root == "https://galaxy.ansible.com" or root == "https://galaxy.ansible.com/":
root = "https://galaxy.ansible.com/api/"
downloader = self.remote.get_downloader(url=root)
try:
api_data = parse_metadata(await downloader.run())
except (json.decoder.JSONDecodeError, ClientResponseError):
if root.endswith("/api/"):
raise
root = urljoin(root, "api/")
downloader = self.remote.get_downloader(url=root)
api_data = parse_metadata(await downloader.run())
if "available_versions" not in api_data:
raise RuntimeError(_("Could not find 'available_versions' at {}").format(root))
if "v3" in api_data.get("available_versions", {}):
api_version = 3
elif "v2" in api_data.get("available_versions", {}):
api_version = 2
else:
raise RuntimeError(_("Unsupported API versions at {}").format(root))
endpoint = f"{root}v{api_version}"
return endpoint, api_version
@alru_cache(maxsize=128)
async def _get_paginated_collection_api(self, root):
"""
Returns the collection api path and api version.
Based on https://git.io/JTMxE.
"""
endpoint, api_version = await self._get_root_api(root)
return f"{endpoint}/collections/", api_version
async def _fetch_collection_version_metadata(self, api_version, collection_version_url):
downloader = self.remote.get_downloader(url=collection_version_url)
metadata = parse_metadata(await downloader.run())
await self._add_collection_version(api_version, collection_version_url, metadata)
async def _add_collection_version(self, api_version, collection_version_url, metadata):
"""Add CollectionVersion to the sync pipeline."""
url = metadata["download_url"]
collection_version = CollectionVersion(
namespace=metadata["namespace"]["name"],
name=metadata["collection"]["name"],
version=metadata["version"],
)
cv_unique = attrgetter("namespace", "name", "version")(collection_version)
fullname, version = f"{cv_unique[0]}.{cv_unique[1]}", cv_unique[2]
if fullname in self.exclude_info and Version(version) in self.exclude_info[fullname]:
return
if cv_unique in self.already_synced:
return
self.already_synced.add(cv_unique)
info = metadata["metadata"]
signatures = metadata.get("signatures", [])
marks = metadata.get("marks", [])
if self.signed_only and not signatures:
return
if self.add_dependents:
dependencies = info["dependencies"]
tasks = []
loop = asyncio.get_event_loop()
for full_name, version in dependencies.items():
namespace, name = full_name.split(".")
req = (namespace, name, version)
new_req = RequirementsFileEntry(full_name, version=version, source=None)
if not any([req in self.already_synced, new_req in self.collection_info]):
self.collection_info.append(new_req)
tasks.append(loop.create_task(self._fetch_collection_metadata(new_req)))
await asyncio.gather(*tasks)
info.pop("tags")
for attr_name, attr_value in info.items():
if attr_value is None or attr_name not in collection_version.__dict__:
continue
setattr(collection_version, attr_name, attr_value)
artifact = metadata["artifact"]
d_artifact = DeclarativeArtifact(
artifact=Artifact(sha256=artifact["sha256"], size=artifact["size"]),
url=url,
relative_path=collection_version.relative_path,
remote=self.remote,
deferred_download=self.deferred_download,
)
extra_data = {}
if api_version != 2: # V2 never implemented the docs-blob requests
extra_data["docs_blob_url"] = f"{collection_version_url}docs-blob/"
d_content = DeclarativeContent(
content=collection_version,
d_artifacts=[d_artifact],
extra_data=extra_data,
)
await self.parsing_metadata_progress_bar.aincrement()
await self.put(d_content)
if signatures or marks:
collection_version = await d_content.resolution()
for signature in signatures:
sig = signature["signature"]
cv_signature = CollectionVersionSignature(
signed_collection=collection_version,
data=sig,
digest=hashlib.sha256(sig.encode("utf-8")).hexdigest(),
pubkey_fingerprint=signature["pubkey_fingerprint"],
)
await self.put(DeclarativeContent(content=cv_signature))
for mark_value in marks:
cv_mark = CollectionVersionMark(
marked_collection=collection_version,
value=mark_value,
)
await self.put(DeclarativeContent(content=cv_mark))
# Process syncing CV Namespace Metadata if present
namespace_sha = metadata["namespace"].get("metadata_sha256")
if namespace_sha:
namespace = collection_version.namespace
if namespace not in self.namespaces_seen:
if await self._add_namespace(namespace, namespace_sha):
self.namespaces_seen.add(namespace)
async def _add_namespace(self, name, namespace_sha):
"""Adds A Namespace metadata content to the pipeline."""
try:
ns = await sync_to_async(AnsibleNamespaceMetadata.objects.get)(
metadata_sha256=namespace_sha
)
await self.put(DeclarativeContent(ns))
return True
except AnsibleNamespaceMetadata.DoesNotExist:
pass
endpoint, api_version = await self._get_root_api(self.remote.url)
namespace_url = f"{endpoint}/namespaces/{name}"
downloader = self.remote.get_downloader(
url=namespace_url, silence_errors_for_response_codes={404}
)
try:
result = await downloader.run()
except FileNotFoundError:
pass
else:
namespace = parse_metadata(result)
links = namespace.get("links", None)
# clean up the galaxy API for pulp
if links:
namespace["links"] = {x["name"]: x["url"] for x in links}
else:
namespace["links"] = dict()
for key in ("pulp_href", "groups", "id", "related_fields"):
namespace.pop(key, None)
url = namespace.pop("avatar_url", None)
da = (
[
DeclarativeFailsafeArtifact(
Artifact(sha256=namespace.get("avatar_sha256")),
url=url,
remote=self.remote,
relative_path=f"{name}-avatar",
deferred_download=False,
)
]
if url
else None
)
namespace = AnsibleNamespaceMetadata(**namespace)
dc = DeclarativeContent(namespace, d_artifacts=da)
await self.put(dc)
return True
return False
async def _add_collection_version_from_git(self, url, gitref, metadata_only):
d_content = await declarative_content_from_git_repo(
self.remote, url, gitref, metadata_only=False
)
await self.put(d_content)
def _collection_versions_list_downloader(
self, api_version, collection_endpoint, namespace, name, page_num, page_size
):
url_without_get_params = f"{collection_endpoint}{namespace}/{name}/versions/"
if api_version == 2:
versions_list_url = f"{url_without_get_params}?page={page_num}&page_size={page_size}"
else:
offset = (page_num - 1) * page_size
versions_list_url = f"{url_without_get_params}?limit={page_size}&offset={offset}"
return self.remote.get_downloader(url=versions_list_url)
async def _fetch_paginated_collection_metadata(self, name, namespace, requirement, source=None):
root = source or self.remote.url
collection_endpoint, api_version = await self._get_paginated_collection_api(root)
collection_url = f"{collection_endpoint}{namespace}/{name}"
collection_metadata_downloader = self.remote.get_downloader(url=collection_url)
collection_metadata = parse_metadata(await collection_metadata_downloader.run())
loop = asyncio.get_event_loop()
tasks = []
page_num = 1
while True:
versions_list_downloader = self._collection_versions_list_downloader(
api_version, collection_endpoint, namespace, name, page_num, PAGE_SIZE
)
collection_versions_list = parse_metadata(await versions_list_downloader.run())
if api_version == 2:
collection_versions = collection_versions_list["results"]
else:
collection_versions = collection_versions_list["data"]
for collection_version in collection_versions:
if Version(collection_version["version"]) in requirement:
version_num = collection_version["version"]
collection_version_detail_url = f"{collection_url}/versions/{version_num}/"
if collection_metadata["deprecated"]:
d_content = DeclarativeContent(
content=AnsibleCollectionDeprecated(namespace=namespace, name=name),
)
self.deprecation_after_sync.add(f"{namespace}.{name}")
await self.put(d_content)
tasks.append(
loop.create_task(
self._fetch_collection_version_metadata(
api_version,
collection_version_detail_url,
)
)
)
next_value = self._get_response_next_value(api_version, collection_versions_list)
if not next_value:
break
page_num = page_num + 1
self.parsing_metadata_progress_bar.total += len(tasks)
await self.parsing_metadata_progress_bar.asave(update_fields=["total"])
await asyncio.gather(*tasks)
async def _read_from_downloaded_metadata(self, name, namespace, requirement):
tasks = []
loop = asyncio.get_event_loop()
if (
namespace not in self._unpaginated_collection_metadata
or name not in self._unpaginated_collection_metadata[namespace]
):
raise FileNotFoundError(
f"Collection {namespace}.{name} does not exist on {self.remote.url}"
)
if self._unpaginated_collection_metadata[namespace][name]["deprecated"]:
d_content = DeclarativeContent(
content=AnsibleCollectionDeprecated(namespace=namespace, name=name),
)
self.deprecation_after_sync.add(f"{namespace}.{name}")
await self.put(d_content)
all_versions_of_collection = self._unpaginated_collection_version_metadata[namespace][name]
for col_version_metadata in all_versions_of_collection:
if Version(col_version_metadata["version"]) in requirement:
if "git_url" in col_version_metadata and col_version_metadata["git_url"]:
tasks.append(
loop.create_task(
self._add_collection_version_from_git(
col_version_metadata["git_url"],
col_version_metadata["git_commit_sha"],
False,
)
)
)
else:
collection_version_url = urljoin(
self.remote.url, f"{col_version_metadata['href']}"
)
tasks.append(
loop.create_task(
self._add_collection_version(
self._api_version, collection_version_url, col_version_metadata
)
)
)
self.parsing_metadata_progress_bar.total += len(tasks)
await self.parsing_metadata_progress_bar.asave(update_fields=["total"])
await asyncio.gather(*tasks)
async def _fetch_collection_metadata(self, requirements_entry):
requirement_version = AnsibleSpec(requirements_entry.version)
namespace, name = requirements_entry.name.split(".")
if self._unpaginated_collection_version_metadata and requirements_entry.source is None:
await self._read_from_downloaded_metadata(name, namespace, requirement_version)
else:
await self._fetch_paginated_collection_metadata(
name, namespace, requirement_version, requirements_entry.source
)
@staticmethod
def _get_response_next_value(api_version, response):
if api_version == 2:
return response["next"]
else:
return response["links"]["next"]
def _collection_list_downloader(self, api_version, collection_endpoint, page_num, page_size):
if api_version == 2:
collection_list_url = f"{collection_endpoint}?page={page_num}&page_size={page_size}"
else:
offset = (page_num - 1) * page_size
collection_list_url = f"{collection_endpoint}?limit={page_size}&offset={offset}"
return self.remote.get_downloader(url=collection_list_url)
async def _download_unpaginated_metadata(self):
root_endpoint, api_version = await self._get_root_api(self.remote.url)
self._api_version = api_version
if api_version > 2:
loop = asyncio.get_event_loop()
collection_endpoint = f"{root_endpoint}/collections/all/"
excludes_endpoint = f"{root_endpoint}/excludes/"
col_downloader = self.remote.get_downloader(
url=collection_endpoint, silence_errors_for_response_status_codes={404}
)
exc_downloader = self.remote.get_downloader(
url=excludes_endpoint, silence_errors_for_response_status_codes={404}
)
tasks = [loop.create_task(col_downloader.run()), loop.create_task(exc_downloader.run())]
col_results, exc_results = await asyncio.gather(*tasks, return_exceptions=True)
if not isinstance(exc_results, FileNotFoundError):
excludes_response = parse_metadata(exc_results)
if excludes_response:
try:
excludes_list = parse_collections_requirements_file(excludes_response)
except ValidationError:
pass
else:
excludes = {r.name: AnsibleSpec(r.version) for r in excludes_list}
self.exclude_info.update(excludes)
if not isinstance(col_results, FileNotFoundError):
collection_metadata_list = parse_metadata(col_results)
self._unpaginated_collection_metadata = defaultdict(dict)
for collection in collection_metadata_list:
namespace = collection["namespace"]
name = collection["name"]
self._unpaginated_collection_metadata[namespace][name] = collection
collection_version_endpoint = f"{root_endpoint}/collection_versions/all/"
downloader = self.remote.get_downloader(url=collection_version_endpoint)
collection_version_metadata_list = parse_metadata(await downloader.run())
self._unpaginated_collection_version_metadata = defaultdict(
lambda: defaultdict(list)
)
for collection_version_metadata in collection_version_metadata_list:
namespace = collection_version_metadata["namespace"]["name"]
name = collection_version_metadata["name"]
self._unpaginated_collection_version_metadata[namespace][name].append(
collection_version_metadata
)
async def _find_all_collections_from_unpaginated_data(self):
tasks = []
loop = asyncio.get_event_loop()
for collection_namespace_dict in self._unpaginated_collection_metadata.values():
for collection in collection_namespace_dict.values():
if collection["deprecated"]:
d_content = DeclarativeContent(
content=AnsibleCollectionDeprecated(
namespace=collection["namespace"], name=collection["name"]
),
)
self.deprecation_after_sync.add(
f"{collection['namespace']}.{collection['name']}"
)
await self.put(d_content)
for collections_in_namespace in self._unpaginated_collection_version_metadata.values():
for collection_versions in collections_in_namespace.values():
for collection_version in collection_versions:
collection_version_url = urljoin(
self.remote.url, f"{collection_version['href']}"
)
tasks.append(
loop.create_task(
self._add_collection_version(
self._api_version, collection_version_url, collection_version
)
)
)
self.parsing_metadata_progress_bar.total = len(tasks)
await self.parsing_metadata_progress_bar.asave(update_fields=["total"])
await asyncio.gather(*tasks)
async def _find_all_collections(self):
if self._unpaginated_collection_version_metadata:
await self._find_all_collections_from_unpaginated_data()
return
collection_endpoint, api_version = await self._get_paginated_collection_api(self.remote.url)
loop = asyncio.get_event_loop()
tasks = []
page_num = 1
while True:
collection_list_downloader = self._collection_list_downloader(
api_version, collection_endpoint, page_num, PAGE_SIZE
)
collection_list = parse_metadata(await collection_list_downloader.run())
if api_version == 2:
collections = collection_list["results"]
else:
collections = collection_list["data"]
for collection in collections:
if api_version == 2:
namespace = collection["namespace"]["name"]
else:
namespace = collection["namespace"]
name = collection["name"]
requirements_file = RequirementsFileEntry(
name=".".join([namespace, name]),
version="*",
source=None,
)
tasks.append(loop.create_task(self._fetch_collection_metadata(requirements_file)))
next_value = self._get_response_next_value(api_version, collection_list)
if not next_value:
break
page_num = page_num + 1
self.parsing_metadata_progress_bar.total = len(tasks)
await self.parsing_metadata_progress_bar.asave(update_fields=["total"])
await asyncio.gather(*tasks)
async def _should_we_sync(self):
"""Check last synced metadata time."""
msg = _("no_change: Checking if remote changed since last sync.")
async with ProgressReport(message=msg, code="sync.no_change") as noop:
root, api_version = await self._get_root_api(self.remote.url)
if api_version == 3:
downloader = self.remote.get_downloader(
url=root, silence_errors_for_response_status_codes={404}
)
try:
metadata = parse_metadata(await downloader.run())
except FileNotFoundError:
return True
try:
self.last_synced_metadata_time = parse_datetime(metadata["published"])
except KeyError:
return True
if not self.optimize:
return True