Skip to content

Commit

Permalink
Use RepositoryVersion to save sync details
Browse files Browse the repository at this point in the history
closes #564
  • Loading branch information
hstct committed Jul 19, 2022
1 parent 667b5d8 commit b02d3c6
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 39 deletions.
18 changes: 0 additions & 18 deletions pulp_deb/app/migrations/0020_aptrepository_last_sync_details.py

This file was deleted.

26 changes: 26 additions & 0 deletions pulp_deb/app/migrations/0020_aptrepositoryversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 3.2.13 on 2022-07-18 14:04

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('core', '0092_alter_upload_options'),
('deb', '0019_immutable_metadata_constraints'),
]

operations = [
migrations.CreateModel(
name='AptRepositoryVersion',
fields=[
('repositoryversion_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='core.repositoryversion')),
('sync_details', models.JSONField(default=dict)),
],
options={
'abstract': False,
},
bases=('core.repositoryversion',),
),
]
2 changes: 1 addition & 1 deletion pulp_deb/app/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@

from .remote import AptRemote

from .repository import AptRepository
from .repository import AptRepository, AptRepositoryVersion

from .signing_service import AptReleaseSigningService
21 changes: 19 additions & 2 deletions pulp_deb/app/models/repository.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.db import models
from pulpcore.plugin.models import Repository
from pulpcore.plugin.models import Repository, RepositoryVersion

from pulpcore.plugin.repo_version_utils import remove_duplicates, validate_repo_version

Expand All @@ -18,6 +18,14 @@
)


class AptRepositoryVersion(RepositoryVersion):
"""
A RepositoryVersion for DebContent.
"""

sync_details = models.JSONField(default=dict)


class AptRepository(Repository):
"""
A Repository for DebContent.
Expand All @@ -40,11 +48,20 @@ class AptRepository(Repository):
AptRemote,
]

last_sync_details = models.JSONField(default=dict)
# last_sync_details = models.JSONField(default=dict)

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

def create_initial_version(self):
"""
Create an initial repository version (version 0) for DebContent.
"""
version = AptRepositoryVersion(repository=self, number=self.next_version, complete=True)
self.next_version += 1
self.save()
version.save()

def initialize_new_version(self, new_version):
"""
Remove old metadata from the repo before performing anything else for the new version. This
Expand Down
20 changes: 19 additions & 1 deletion pulp_deb/app/serializers/repository_serializers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from gettext import gettext as _
from pulpcore.plugin.serializers import (
ModelSerializer,
RepositorySerializer,
RepositorySyncURLSerializer,
validate_unknown_fields,
)

from pulp_deb.app.models import AptRepository
from pulp_deb.app.models import AptRepository, AptRepositoryVersion

from jsonschema import Draft7Validator
from rest_framework import serializers
Expand All @@ -22,6 +23,23 @@ class Meta:
model = AptRepository


class AptRepositoryVersionSerializer(ModelSerializer):
"""
A Serializer for AptRepositoryVersion.
"""

class Meta:
model = AptRepositoryVersion
fields = ModelSerializer.Meta.fields + (
"pulp_href",
"number",
"repository",
"base_version",
"content_summary",
"sync_details",
)


class AptRepositorySyncURLSerializer(RepositorySyncURLSerializer):
"""
A Serializer for AptRepository Sync.
Expand Down
36 changes: 19 additions & 17 deletions pulp_deb/app/tasks/synchronizing.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
InstallerPackage,
AptRemote,
AptRepository,
AptRepositoryVersion,
)

from pulp_deb.app.serializers import (
Expand Down Expand Up @@ -155,11 +156,12 @@ def synchronize(remote_pk, repository_pk, mirror, optimize):
"""
remote = AptRemote.objects.get(pk=remote_pk)
repository = AptRepository.objects.get(pk=repository_pk)
repository_version = AptRepositoryVersion.objects.get(repository=repository)

if not remote.url:
raise ValueError(_("A remote must have a url specified to synchronize."))

first_stage = DebFirstStage(remote, optimize, repository)
first_stage = DebFirstStage(remote, optimize, repository_version)
DebDeclarativeVersion(first_stage, repository, mirror=mirror).create()


Expand Down Expand Up @@ -499,7 +501,7 @@ class DebFirstStage(Stage):
The first stage of a pulp_deb sync pipeline.
"""

def __init__(self, remote, optimize, repository, *args, **kwargs):
def __init__(self, remote, optimize, repository_version, *args, **kwargs):
"""
The first stage of a pulp_deb sync pipeline.
Expand All @@ -511,7 +513,7 @@ def __init__(self, remote, optimize, repository, *args, **kwargs):
super().__init__(*args, **kwargs)
self.remote = remote
self.optimize = optimize
self.repository = repository
self.repository_version = repository_version
self.parsed_url = urlparse(remote.url)

async def run(self):
Expand Down Expand Up @@ -542,7 +544,7 @@ def _to_d_artifact(self, relative_path, data=None):

def _has_remote_changed(self):
ro_compare_dict = self._gen_remote_options()
ro_last_sync = self.repository.last_sync_details["remote_options"]
ro_last_sync = self.repository_version.sync_details["remote_options"]
for key in ro_last_sync:
if ro_last_sync[key] != ro_compare_dict[key]:
return True
Expand Down Expand Up @@ -573,8 +575,8 @@ async def _handle_distribution(self, distribution):
release_file = await self._create_unit(release_file_dc)
if release_file is None:
return
if self.optimize and distribution in self.repository.last_sync_details:
last_sync_dist_details = self.repository.last_sync_details[distribution]
if self.optimize and distribution in self.repository_version.sync_details:
last_sync_dist_details = self.repository_version.sync_details[distribution]
if not self._has_remote_changed():
if (
last_sync_dist_details["artifact_set_sha256"]
Expand All @@ -589,13 +591,13 @@ async def _handle_distribution(self, distribution):
) as pb:
await pb.aincrement()
return
self.repository.last_sync_details["remote_options"] = self._gen_remote_options()
if distribution in self.repository.last_sync_details:
self.repository.last_sync_details[distribution][
self.repository_version.sync_details["remote_options"] = self._gen_remote_options()
if distribution in self.repository_version.sync_details:
self.repository_version.sync_details[distribution][
"artifact_set_sha256"
] = release_file.artifact_set_sha256
else:
self.repository.last_sync_details[distribution] = {
self.repository_version.sync_details[distribution] = {
"artifact_set_sha256": release_file.artifact_set_sha256,
}

Expand Down Expand Up @@ -846,13 +848,13 @@ async def _handle_package_index(

distribution = release_file.distribution
component = release_component.component
if component not in self.repository.last_sync_details[distribution]:
if component not in self.repository_version.sync_details[distribution]:
last_pi_details = {}
else:
if architecture not in self.repository.last_sync_details[distribution][component]:
if architecture not in self.repository_version.sync_details[distribution][component]:
last_pi_details = {}
else:
last_pi_details = self.repository.last_sync_details[distribution][component][
last_pi_details = self.repository_version.sync_details[distribution][component][
architecture
]
if self.optimize and "artifact_set_sha256" in last_pi_details:
Expand All @@ -865,23 +867,23 @@ async def _handle_package_index(
await pb.aincrement()
return
if last_pi_details:
self.repository.last_sync_details[distribution][component][architecture][
self.repository_version.sync_details[distribution][component][architecture][
"artifact_set_sha256"
] = package_index.artifact_set_sha256
else:
if component not in self.repository.last_sync_details[distribution]:
if component not in self.repository_version.sync_details[distribution]:
last_pi_details[component] = {}
last_pi_details[component][architecture] = {
"artifact_set_sha256": package_index.artifact_set_sha256
}
else:
last_pi_details[component] = self.repository.last_sync_details[distribution][
last_pi_details[component] = self.repository_version.sync_details[distribution][
component
]
last_pi_details[component][architecture] = {
"artifact_set_sha256": package_index.artifact_set_sha256
}
self.repository.last_sync_details[distribution] |= last_pi_details
self.repository_version.sync_details[distribution] |= last_pi_details

# Interpret policy to download Artifacts or not
deferred_download = self.remote.policy != Remote.IMMEDIATE
Expand Down

0 comments on commit b02d3c6

Please sign in to comment.