Skip to content

Commit

Permalink
Auto-publish doesn't update distributions
Browse files Browse the repository at this point in the history
fixes: #8762
Required PR: pulp/pulpcore#1351
  • Loading branch information
gerrod3 authored and dralley committed May 25, 2021
1 parent 4f96dbc commit 3b510d5
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGES/8762.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Auto-publish no longer modifies distributions.
Auto-distribute now only requires setting a distribution's ``repository`` field.
35 changes: 35 additions & 0 deletions pulp_file/app/migrations/0011_fix_auto_publish.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from django.db import migrations, transaction
from django.core.exceptions import ObjectDoesNotExist

def remove_publications_from_auto_distributed(apps, schema_editor):
with transaction.atomic():
FileDistribution = apps.get_model("file", "FileDistribution")
distributions = FileDistribution.objects.filter(repository__isnull=False, publication__isnull=False)
distributions.update(publication=None)

def add_publications_to_auto_distributed(apps, schema_editor):
with transaction.atomic():
FileDistribution = apps.get_model("file", "FileDistribution")
distributions = list(FileDistribution.objects.filter(repository__isnull=False).select_related("repository"))
for distribution in distributions:
repo_version = distribution.repository.latest_version()
try:
publication = repo_version.publication_set.earliest("pulp_created")
except ObjectDoesNotExist:
publication = None
distribution.publication = publication
FileDistribution.objects.bulk_update(distributions, ['publication'])


class Migration(migrations.Migration):

dependencies = [
('file', '0010_auto_publish'),
]

operations = [
migrations.RunPython(
remove_publications_from_auto_distributed,
reverse_code=add_publications_to_auto_distributed
)
]
9 changes: 1 addition & 8 deletions pulp_file/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,11 @@ def on_new_version(self, version):
from pulp_file.app import tasks

if self.autopublish:
publication = tasks.publish(
tasks.publish(
manifest=self.manifest,
repository_version_pk=version.pk,
)

distributions = self.distributions.all()

if publication and distributions:
for distribution in distributions:
distribution.publication = publication
distribution.save()

def finalize_new_version(self, new_version):
"""
Finalize and validate the new repository version.
Expand Down
22 changes: 22 additions & 0 deletions pulp_file/app/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,28 @@ class FileDistributionSerializer(DistributionSerializer):
allow_null=True,
)

def validate(self, data):
"""
Ensure publication and repository are not set at the same time.
This is needed here till https://pulp.plan.io/issues/8761 is resolved.
"""
data = super().validate(data)
repository_provided = data.get("repository", None)
publication_provided = data.get("publication", None)

if repository_provided and publication_provided:
raise serializers.ValidationError(
_(
"Only one of the attributes 'repository' and 'publication' "
"may be used simultaneously."
)
)
if repository_provided or publication_provided:
data["repository"] = repository_provided
data["publication"] = publication_provided
return data

class Meta:
fields = DistributionSerializer.Meta.fields + ("publication",)
model = FileDistribution
Expand Down
19 changes: 8 additions & 11 deletions pulp_file/tests/functional/api/test_auto_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pulp_smash import config
from pulp_smash.pulp3.bindings import monitor_task
from pulp_smash.pulp3.utils import gen_repo
from pulp_smash.pulp3.utils import gen_repo, download_content_unit

from pulp_file.tests.functional.utils import gen_file_client, gen_file_remote
from pulp_file.tests.functional.utils import set_up_module as setUpModule # noqa:F401
Expand Down Expand Up @@ -63,16 +63,15 @@ def test_01_sync(self):
repository_sync_data = RepositorySyncURL(remote=self.remote.pulp_href)
sync_response = self.repo_api.sync(self.repo.pulp_href, repository_sync_data)
task = monitor_task(sync_response.task)
self.distribution = self.distributions_api.read(self.distribution.pulp_href)

# Check that all the appropriate resources were created
self.assertGreater(len(task.created_resources), 1)
self.assertEqual(self.publications_api.list().count, 1)
self.assertTrue(self.distribution.publication is not None)
self.assertTrue(self.distribution.publication in task.created_resources)
publications = self.publications_api.list()
self.assertEqual(publications.count, 1)
download_content_unit(self.cfg, self.distribution.to_dict(), self.CUSTOM_MANIFEST)

# Check that the publish settings were used
publication = self.publications_api.read(self.distribution.publication)
publication = publications.results[0]
self.assertEqual(publication.manifest, self.CUSTOM_MANIFEST)

# Sync the repository again. Since there should be no new repository version, there
Expand All @@ -94,14 +93,12 @@ def test_02_modify(self):
self.repo.pulp_href, {"add_content_units": [content]}
)
task = monitor_task(modify_response.task)
self.distribution = self.distributions_api.read(self.distribution.pulp_href)

# Check that all the appropriate resources were created
self.assertGreater(len(task.created_resources), 1)
self.assertEqual(self.publications_api.list().count, 1)
self.assertTrue(self.distribution.publication is not None)
self.assertTrue(self.distribution.publication in task.created_resources)
publications = self.publications_api.list()
self.assertEqual(publications.count, 1)

# Check that the publish settings were used
publication = self.publications_api.read(self.distribution.publication)
publication = publications.results[0]
self.assertEqual(publication.manifest, self.CUSTOM_MANIFEST)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pulpcore>=3.12
pulpcore>=3.13.0.dev

0 comments on commit 3b510d5

Please sign in to comment.