Skip to content

Commit

Permalink
Serve previously published artifacts for 3 days
Browse files Browse the repository at this point in the history
fixes pulp#911
  • Loading branch information
daviddavis committed Nov 6, 2023
1 parent 9e09522 commit 85d20ea
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES/911.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Added feature to serve published artifacts from previous publications for 3 days.
This fulfills the apt-by-hash/acquire-by-hash spec by allowing by-hash files to be cached for a
period of time.
48 changes: 48 additions & 0 deletions pulp_deb/app/migrations/0029_distributedpublication.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Generated by Django 4.2.2 on 2023-10-25 17:07

from django.db import migrations, models
import django.db.models.deletion
import django_lifecycle.mixins
import pulpcore.app.models.base


class Migration(migrations.Migration):

dependencies = [
("deb", "0028_sourcepackage_sourcepackagereleasecomponent_and_more"),
]

operations = [
migrations.CreateModel(
name="DistributedPublication",
fields=[
(
"pulp_id",
models.UUIDField(
default=pulpcore.app.models.base.pulp_uuid,
editable=False,
primary_key=True,
serialize=False,
),
),
("pulp_created", models.DateTimeField(auto_now_add=True)),
("pulp_last_updated", models.DateTimeField(auto_now=True, null=True)),
(
"distribution",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="deb.aptdistribution"
),
),
(
"publication",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="deb.aptpublication"
),
),
],
options={
"abstract": False,
},
bases=(django_lifecycle.mixins.LifecycleModelMixin, models.Model),
),
]
53 changes: 52 additions & 1 deletion pulp_deb/app/models/publication.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from datetime import timedelta

from django.db import models
from django.utils import timezone
from django_lifecycle import hook, AFTER_CREATE, AFTER_SAVE

from pulpcore.plugin.models import Publication, Distribution
from pulpcore.plugin.models import BaseModel, Distribution, Publication, PublishedArtifact

from pulp_deb.app.models.signing_service import AptReleaseSigningService

Expand All @@ -17,6 +21,11 @@ class VerbatimPublication(Publication):

TYPE = "verbatim-publication"

@hook(AFTER_CREATE)
def set_distributed_publication(self):
for distro in AptDistribution.objects.filter(repository__pk=self.repository.pk):
DistributedPublication(distribution=distro, publication=self).save()

class Meta:
default_related_name = "%(app_label)s_%(model_name)s"

Expand All @@ -36,6 +45,11 @@ class AptPublication(Publication):
AptReleaseSigningService, on_delete=models.PROTECT, null=True
)

@hook(AFTER_CREATE)
def set_distributed_publication(self):
for distro in AptDistribution.objects.filter(repository__pk=self.repository.pk):
DistributedPublication(distribution=distro, publication=self).save()

class Meta:
default_related_name = "%(app_label)s_%(model_name)s"

Expand All @@ -47,5 +61,42 @@ class AptDistribution(Distribution):

TYPE = "apt-distribution"

PUBLICATION_CACHE_DURATION = timedelta(days=3)

@hook(AFTER_SAVE, when="publication", has_changed=True)
def set_distributed_publication(self):
if self.publication:
DistributedPublication(distribution=self, publication=self.publication).save()

def content_handler(self, path):
recent_publications = (
self.distributedpublication_set.filter(
pulp_created__gte=timezone.now() - self.PUBLICATION_CACHE_DURATION
)
.order_by("pulp_created")
.values_list("publication", flat=True)
)
for publication in recent_publications:
pa = (
PublishedArtifact.objects.filter(relative_path=path, publication__pk=publication)
.select_related(
"content_artifact",
"content_artifact__artifact",
)
.first()
)
if pa:
return pa.content_artifact
return

class Meta:
default_related_name = "%(app_label)s_%(model_name)s"


class DistributedPublication(BaseModel):
"""
A history of previously distributed publications.
"""

distribution = models.ForeignKey(AptDistribution, on_delete=models.CASCADE)
publication = models.ForeignKey(AptPublication, on_delete=models.CASCADE)

0 comments on commit 85d20ea

Please sign in to comment.