Skip to content

Commit

Permalink
Merge pull request #3100 from jortel/issue-2893
Browse files Browse the repository at this point in the history
Add publication models.
  • Loading branch information
jortel committed Sep 7, 2017
2 parents 93e64e0 + 2193fdd commit a0764f9
Show file tree
Hide file tree
Showing 10 changed files with 363 additions and 17 deletions.
1 change: 1 addition & 0 deletions platform/pulpcore/app/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from .consumer import Consumer, ConsumerContent # noqa
from .content import Content, Artifact, ContentArtifact, RemoteArtifact # noqa
from .publication import Distribution, Publication, PublishedArtifact, PublishedMetadata # noqa
from .repository import Repository, Importer, Publisher, RepositoryContent # noqa
from .storage import FileContent # noqa
from .task import ReservedResource, Worker, Task, TaskTag, TaskLock # noqa
Expand Down
102 changes: 102 additions & 0 deletions platform/pulpcore/app/models/publication.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
from django.db import models
from django.core.files.storage import DefaultStorage

from pulpcore.app.models import Model


class Publication(Model):
"""
Fields:
created (models.DatetimeField): When the publication was created UTC.
Relations:
publisher (models.ForeignKey): The publisher that created the publication.
"""

created = models.DateTimeField(auto_now_add=True)

publisher = models.ForeignKey('Publisher', on_delete=models.CASCADE)


class PublishedFile(Model):
"""
A file included in Publication.
Fields:
relative_path (models.CharField): The (relative) path component of the published url.
Relations:
publication (models.ForeignKey): The publication in which the artifact is included.
"""
relative_path = models.CharField(max_length=255)

publication = models.ForeignKey(Publication, on_delete=models.CASCADE)

class Meta:
abstract = True


class PublishedArtifact(PublishedFile):
"""
An artifact that is part of a publication.
Relations:
content_artifact (models.ForeignKey): The referenced content artifact.
"""
content_artifact = models.ForeignKey('ContentArtifact', on_delete=models.CASCADE)

class Meta:
unique_together = ('publication', 'content_artifact')
default_related_name = 'published_artifact'


class PublishedMetadata(PublishedFile):
"""
Metadata file that is part of a publication.
Fields:
file (models.FileField): The stored file.
"""

def _storage_path(self, name):
return DefaultStorage().published_metadata_path(self, name)

file = models.FileField(upload_to=_storage_path, max_length=255)

class Meta:
unique_together = ('publication', 'file')
default_related_name = 'published_metadata'


class Distribution(Model):
"""
A distribution defines how a publication is distributed by pulp.
Fields:
name (models.CharField): The name of the distribution.
Examples: "rawhide" and "stable".
base_path (models.CharField): The base (relative) path component of the published url.
auto_updated (models.BooleanField): The publication is updated automatically
whenever the publisher has created a new publication.
http (models.BooleanField): The publication is distributed using HTTP.
https (models.BooleanField): The publication is distributed using HTTPS.
Relations:
publisher (models.ForeignKey): The associated publisher.
publication (models.ForeignKey): The current publication associated with
the distribution. This is the publication being served by Pulp through
this relative URL path and settings.
"""

name = models.CharField(max_length=255)
base_path = models.CharField(max_length=255, unique=True)
auto_updated = models.BooleanField(default=True)
http = models.BooleanField(default=True)
https = models.BooleanField(default=True)

publication = models.ForeignKey(Publication, null=True, on_delete=models.SET_NULL)
publisher = models.ForeignKey('Publisher', on_delete=models.CASCADE)

class Meta:
unique_together = ('publisher', 'name')
2 changes: 0 additions & 2 deletions platform/pulpcore/app/models/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ class Publisher(ContentAdaptor):
auto_publish (models.BooleanField): Indicates that the adaptor may publish automatically
when the associated repository's content has changed.
relative_path (models.TextField): The (relative) path component of the published url.
last_published (models.DatetimeField): When the last successful publish occurred.
Relations:
Expand All @@ -190,7 +189,6 @@ class Publisher(ContentAdaptor):
repository = models.ForeignKey(Repository, on_delete=models.CASCADE)

auto_publish = models.BooleanField(default=True)
relative_path = models.TextField(blank=True)
last_published = models.DateTimeField(blank=True, null=True)

class Meta(ContentAdaptor.Meta):
Expand Down
19 changes: 19 additions & 0 deletions platform/pulpcore/app/models/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,25 @@ def get_artifact_path(sha256digest):
"""
return os.path.join(settings.MEDIA_ROOT, 'artifact', sha256digest[0:2], sha256digest[2:])

@staticmethod
def published_metadata_path(model, name):
"""
Get the storage path for published metadata.
Args:
model (pulpcore.app.models.PublishedMetadata): A model instance.
name (str): The file name.
Returns:
str: The absolute storage path.
"""
return os.path.join(
settings.MEDIA_ROOT,
'published',
'metadata',
str(model.pk),
name)

def get_available_name(self, name, max_length=None):
"""
Get the available absolute path based on the name requested.
Expand Down
7 changes: 1 addition & 6 deletions platform/pulpcore/app/serializers/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,6 @@ class PublisherSerializer(MasterModelSerializer, NestedHyperlinkedModelSerialize
' the repository content has changed.'),
required=False
)
relative_path = serializers.CharField(
help_text=_('The (relative) path component of the published url'),
required=False,
allow_blank=True
)
last_published = serializers.DateTimeField(
help_text=_('Timestamp of the most recent successful publish.'),
read_only=True
Expand All @@ -179,7 +174,7 @@ class Meta:
abstract = True
model = models.Publisher
fields = MasterModelSerializer.Meta.fields + (
'name', 'last_updated', 'repository', 'auto_publish', 'relative_path', 'last_published'
'name', 'last_updated', 'repository', 'auto_publish', 'last_published'
)


Expand Down
41 changes: 36 additions & 5 deletions platform/pulpcore/app/tasks/publisher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from datetime import datetime
from gettext import gettext as _
from logging import getLogger

from celery import shared_task
from django.db import transaction
from django.http import QueryDict

from pulpcore.app import models
Expand All @@ -7,6 +12,9 @@
from pulpcore.tasking.tasks import UserFacingTask


log = getLogger(__name__)


@shared_task(base=UserFacingTask)
def update(publisher_id, app_label, serializer_name, data=None, partial=False):
"""
Expand Down Expand Up @@ -59,9 +67,32 @@ def publish(repo_name, publisher_name):
repo_name (str): unique name to specify the repository.
publisher_name (str): name to specify the Publisher.
"""
publisher = models.Publisher.objects.get(name=publisher_name,
repository__name=repo_name).cast()
publisher = models.Publisher.objects.get(
name=publisher_name,
repository__name=repo_name).cast()
log.warn(
_('Publishing: repository=%(r)s, publisher=%(p)s'),
{
'r': repo_name,
'p': publisher_name
})
with transaction.atomic():
publication = models.Publication(publisher=publisher)
publisher.publication = publication
publication.save()
with storage.working_dir_context() as working_dir:
publisher.working_dir = working_dir
publisher.publish()
publisher.last_published = datetime.utcnow()
publisher.save()
distributions = models.Distribution.objects.filter(
publisher=publisher,
auto_updated=True)
distributions.update(publication=publication)
log.warn(
_('Publication: %(p)s created'),
{
'p': publication.pk
})

with storage.working_dir_context() as working_dir:
publisher.working_dir = working_dir
publisher.publish()
# TODO: Replace WARN w/ INFO or DEBUG.
3 changes: 2 additions & 1 deletion platform/pulpcore/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from rest_framework_nested import routers

from pulpcore.app.apps import pulp_plugin_configs
from pulpcore.app.views.status import StatusView
from pulpcore.app.views import ContentView, StatusView

root_router = routers.DefaultRouter(
schema_title='Pulp API',
Expand Down Expand Up @@ -63,6 +63,7 @@ def router_for_nested_viewset(viewset):


urlpatterns = [
url(r'^{}/'.format(ContentView.BASE_PATH), ContentView.as_view()),
url(r'^api/v3/', include(root_router.urls)),
url(r'^api/v3/status/', StatusView.as_view()),
]
Expand Down
3 changes: 2 additions & 1 deletion platform/pulpcore/app/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from pulpcore.app.views.status import StatusView # noqa
from .content import ContentView # noqa
from .status import StatusView # noqa
Loading

0 comments on commit a0764f9

Please sign in to comment.