From 49a00990e6733fb074aa8aa50aa3e9468ca68dd1 Mon Sep 17 00:00:00 2001 From: MichalPysik Date: Tue, 28 May 2024 13:10:23 +0200 Subject: [PATCH] Add support for Replicate feature Pulp Container now supports the Replication feature. Also, Distribution now reports whether the registry is using HTTP (unsecure) or HTTPS (secure) via the new 'secure' field inside ContainerDistributionSerializer. closes #1648 closes #1213 --- CHANGES/1213.feature | 2 ++ CHANGES/1648.feature | 1 + pulp_container/app/replica.py | 56 +++++++++++++++++++++++++++++++ pulp_container/app/serializers.py | 8 +++++ 4 files changed, 67 insertions(+) create mode 100644 CHANGES/1213.feature create mode 100644 CHANGES/1648.feature create mode 100644 pulp_container/app/replica.py diff --git a/CHANGES/1213.feature b/CHANGES/1213.feature new file mode 100644 index 000000000..b8d7de687 --- /dev/null +++ b/CHANGES/1213.feature @@ -0,0 +1,2 @@ +Distribution now reports whether the registry is using HTTP (insecure) or HTTPS (secure) via the +new 'secure' field. diff --git a/CHANGES/1648.feature b/CHANGES/1648.feature new file mode 100644 index 000000000..079bcdc57 --- /dev/null +++ b/CHANGES/1648.feature @@ -0,0 +1 @@ +Pulp Container now supports the Replication feature. diff --git a/pulp_container/app/replica.py b/pulp_container/app/replica.py new file mode 100644 index 000000000..fd3d011f7 --- /dev/null +++ b/pulp_container/app/replica.py @@ -0,0 +1,56 @@ +from pulpcore.plugin.replica import Replicator +from pulpcore.plugin.util import get_url + +from pulp_glue.container.context import ( + PulpContainerDistributionContext, + PulpContainerRepositoryContext, +) + +from pulp_container.app.models import ContainerDistribution, ContainerRemote, ContainerRepository +from pulp_container.app.tasks import synchronize as container_synchronize + + +class ContainerReplicator(Replicator): + distribution_ctx_cls = PulpContainerDistributionContext + repository_ctx_cls = PulpContainerRepositoryContext + publication_ctx_cls = None + remote_model_cls = ContainerRemote + repository_model_cls = ContainerRepository + distribution_model_cls = ContainerDistribution + distribution_serializer_name = "ContainerDistributionSerializer" + repository_serializer_name = "ContainerRepositorySerializer" + remote_serializer_name = "ContainerRemoteSerializer" + app_label = "container" + sync_task = container_synchronize + + def sync_params(self, repository, remote): + """Returns a dictionary where key is a parameter for the sync task.""" + return dict( + remote_pk=str(remote.pk), + repository_pk=str(repository.pk), + signed_only=False, # TODO not sure + mirror=True, + ) + + def url(self, upstream_distribution): + if upstream_distribution["secure"]: + prefix = "https://" + else: + prefix = "http://" + return prefix + upstream_distribution["registry_path"].split("/")[0] + + def remote_extra_fields(self, upstream_distribution): + upstream_name = upstream_distribution["registry_path"].split("/")[1] + return {"upstream_name": upstream_name} + + def distribution_data(self, repository, upstream_distribution): + """ + Return the fields that need to be updated/cleared on distributions for idempotence. + """ + return { + "repository": get_url(repository), + "base_path": upstream_distribution["base_path"], + } + + +REPLICATION_ORDER = [ContainerReplicator] diff --git a/pulp_container/app/serializers.py b/pulp_container/app/serializers.py index 3cac522bd..22589bbf5 100644 --- a/pulp_container/app/serializers.py +++ b/pulp_container/app/serializers.py @@ -351,6 +351,13 @@ class ContainerDistributionSerializer(DistributionSerializer, GetOrCreateSeriali view_name_pattern=r"remotes(-.*/.*)?-detail", read_only=True, ) + secure = serializers.SerializerMethodField( + help_text="Whether the distribution is served over HTTP (insecure) or HTTPS (secure)." + ) + + def get_secure(self, obj): + request = self.context["request"] + return request.is_secure() def validate(self, data): """ @@ -406,6 +413,7 @@ class Meta: "namespace", "private", "description", + "secure", )