Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #5391 https://pulp.plan.io/issues/5391
- Loading branch information
Matthias Dellweg
committed
Sep 4, 2019
1 parent
ec1038f
commit 20cd60d
Showing
7 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add oneshot upload functionality for deb type packages. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ | |
) | ||
|
||
from .remote_serializers import DebRemoteSerializer | ||
from .upload_serializers import OneShotUploadSerializer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from gettext import gettext as _ | ||
|
||
from rest_framework.serializers import FileField, HyperlinkedRelatedField, Serializer | ||
from pulpcore.plugin.models import Artifact, Repository | ||
|
||
|
||
class OneShotUploadSerializer(Serializer): | ||
""" | ||
A serializer for the One Shot Upload API. | ||
""" | ||
|
||
repository = HyperlinkedRelatedField( | ||
help_text=_("A URI of the repository."), | ||
required=False, | ||
queryset=Repository.objects.all(), | ||
view_name="repositories-detail", | ||
) | ||
file = FileField(help_text=_("The deb file."), required=True) | ||
|
||
def validate(self, data): | ||
""" | ||
Validate uploaded file, prepare artifact and collect shared resources. | ||
""" | ||
data = super().validate(data) | ||
data["filename"] = data["file"].name | ||
data["artifact"] = Artifact.init_and_validate(data["file"]) | ||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# flake8: noqa | ||
from .publishing import publish, publish_verbatim | ||
from .synchronizing import synchronize | ||
from .upload import one_shot_upload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import logging | ||
from gettext import gettext as _ | ||
|
||
from debian import debfile | ||
|
||
from django.db import transaction | ||
from django.core.exceptions import ObjectDoesNotExist | ||
|
||
from pulpcore.plugin.models import Artifact, ContentArtifact, Repository, RepositoryVersion | ||
from pulpcore.app.models.task import CreatedResource | ||
|
||
from pulp_deb.app.models import Package | ||
|
||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def one_shot_upload(artifact_pk, repository_pk=None): | ||
""" | ||
Create a Package from an uploaded file and attach it to a Repository if provided. | ||
Args: | ||
artifact_pk (str): Create a Package from this artifact. | ||
Optional: | ||
repository_pk (str): Insert the Package into this Repository. | ||
""" | ||
artifact = Artifact.objects.get(pk=artifact_pk) | ||
repository = Repository.objects.get(pk=repository_pk) if repository_pk else None | ||
log.debug( | ||
_("Uploading deb package: artifact={artifact}, repository={repo}").format( | ||
artifact=artifact, repo=repository | ||
) | ||
) | ||
|
||
package_paragraph = debfile.DebFile(fileobj=artifact.file).debcontrol() | ||
package_dict = Package.from822(package_paragraph) | ||
package_dict["sha256"] = artifact.sha256 | ||
package = Package(**package_dict) | ||
package.relative_path = package.filename | ||
try: | ||
package = Package.objects.get(sha256=artifact.sha256, relative_path=package.relative_path) | ||
except ObjectDoesNotExist: | ||
with transaction.atomic(): | ||
package.save() | ||
|
||
ContentArtifact.objects.create( | ||
artifact=artifact, content=package, relative_path=package.relative_path | ||
) | ||
|
||
resource = CreatedResource(content_object=package) | ||
resource.save() | ||
|
||
if repository: | ||
content_to_add = Package.objects.filter(pk=package.pk) | ||
|
||
# create new repo version with uploaded package | ||
with RepositoryVersion.create(repository) as new_version: | ||
new_version.add_content(content_to_add) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.conf.urls import url | ||
|
||
from .viewsets import OneShotUploadViewSet | ||
|
||
|
||
urlpatterns = [url(r"deb/upload/$", OneShotUploadViewSet.as_view({"post": "create"}))] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters