Skip to content

Commit aa18b29

Browse files
committed
Complete adding the optimize flag to the file plugin
Couldn't be done earlier because it was an API-breaking change and passed an argument into the task which wasn't prepared to handle it.
1 parent 622fd0c commit aa18b29

27 files changed

Lines changed: 296 additions & 76 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added sync optimization that skips re-syncing when the remote manifest has not changed. An `optimize` flag on the sync endpoint (default `True`) allows forcing a full sync when needed.

pulp_file/app/serializers.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
PublicationSerializer,
1717
RemoteSerializer,
1818
RepositorySerializer,
19+
RepositorySyncURLSerializer,
1920
SingleArtifactContentUploadSerializer,
2021
)
2122
from pulpcore.plugin.util import get_domain_pk
@@ -139,11 +140,28 @@ class FileRepositorySerializer(RepositorySerializer):
139140
allow_null=True,
140141
)
141142

143+
last_sync_details = serializers.JSONField(
144+
help_text=_("Details about the last sync of this repository."),
145+
read_only=True,
146+
)
147+
142148
class Meta:
143-
fields = RepositorySerializer.Meta.fields + ("autopublish", "manifest")
149+
fields = RepositorySerializer.Meta.fields + ("autopublish", "manifest", "last_sync_details")
144150
model = FileRepository
145151

146152

153+
class FileRepositorySyncURLSerializer(RepositorySyncURLSerializer):
154+
"""
155+
Serializer for File Repository Sync URL.
156+
"""
157+
158+
optimize = serializers.BooleanField(
159+
help_text=_("Whether or not to optimize sync."),
160+
required=False,
161+
default=True,
162+
)
163+
164+
147165
class FileRemoteSerializer(RemoteSerializer):
148166
"""
149167
Serializer for File Remotes.

pulp_file/app/viewsets.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
)
1919
from pulpcore.plugin.serializers import (
2020
AsyncOperationResponseSerializer,
21-
RepositorySyncURLSerializer,
2221
TaskGroupOperationResponseSerializer,
2322
)
2423
from pulpcore.plugin.tasking import dispatch
@@ -55,6 +54,7 @@
5554
FilePublicationSerializer,
5655
FileRemoteSerializer,
5756
FileRepositorySerializer,
57+
FileRepositorySyncURLSerializer,
5858
)
5959

6060

@@ -257,14 +257,14 @@ class FileRepositoryViewSet(RepositoryViewSet, ModifyRepositoryActionMixin, Role
257257
summary="Sync from a remote",
258258
responses={202: AsyncOperationResponseSerializer},
259259
)
260-
@action(detail=True, methods=["post"], serializer_class=RepositorySyncURLSerializer)
260+
@action(detail=True, methods=["post"], serializer_class=FileRepositorySyncURLSerializer)
261261
def sync(self, request, pk):
262262
"""
263263
Synchronizes a repository.
264264
265265
The ``repository`` field has to be provided.
266266
"""
267-
serializer = RepositorySyncURLSerializer(
267+
serializer = FileRepositorySyncURLSerializer(
268268
data=request.data, context={"request": request, "repository_pk": pk}
269269
)
270270
serializer.is_valid(raise_exception=True)
@@ -273,6 +273,7 @@ def sync(self, request, pk):
273273
remote = serializer.validated_data.get("remote", repository.remote)
274274

275275
mirror = serializer.validated_data.get("mirror", False)
276+
optimize = serializer.validated_data.get("optimize", True) # noqa
276277
if mirror and repository.autopublish:
277278
raise ValidationError("Cannot use mirror mode with autopublished repository.")
278279
result = dispatch(
@@ -283,6 +284,7 @@ def sync(self, request, pk):
283284
"remote_pk": str(remote.pk),
284285
"repository_pk": str(repository.pk),
285286
"mirror": mirror,
287+
"optimize": optimize,
286288
},
287289
)
288290
return OperationPostponedResponse(result, request)

pulp_file/tests/functional/api/test_acs.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import pytest
66

7-
from pulpcore.client.pulp_file import RepositorySyncURL
7+
from pulpcore.client.pulp_file import FileRepositorySyncURL
88
from pulpcore.client.pulp_file.exceptions import ApiException
99
from pulpcore.tests.functional.utils import (
1010
download_file,
@@ -108,7 +108,7 @@ def test_acs_sync(
108108
assert acs_server.requests_record[0].path == basic_manifest_path
109109

110110
# Sync the repository
111-
repository_sync_data = RepositorySyncURL(remote=main_remote.pulp_href)
111+
repository_sync_data = FileRepositorySyncURL(remote=main_remote.pulp_href)
112112
monitor_task(
113113
file_bindings.RepositoriesFileApi.sync(file_repo.pulp_href, repository_sync_data).task
114114
)
@@ -167,7 +167,7 @@ def test_acs_sync_with_paths(
167167
assert expected_request_paths == actual_requested_paths
168168

169169
# Sync the repository
170-
repository_sync_data = RepositorySyncURL(remote=main_remote.pulp_href)
170+
repository_sync_data = FileRepositorySyncURL(remote=main_remote.pulp_href)
171171
monitor_task(
172172
file_bindings.RepositoriesFileApi.sync(file_repo.pulp_href, repository_sync_data).task
173173
)
@@ -231,7 +231,7 @@ def test_serving_acs_content(
231231
)
232232

233233
# Sync the repository
234-
repository_sync_data = RepositorySyncURL(remote=main_remote.pulp_href)
234+
repository_sync_data = FileRepositorySyncURL(remote=main_remote.pulp_href)
235235
monitor_task(
236236
file_bindings.RepositoriesFileApi.sync(file_repo.pulp_href, repository_sync_data).task
237237
)

pulp_file/tests/functional/api/test_auto_publish.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44

55
from pulpcore.client.pulp_file import (
6-
RepositorySyncURL,
6+
FileRepositorySyncURL,
77
)
88
from pulpcore.tests.functional.utils import get_files_in_manifest
99

@@ -48,7 +48,7 @@ def test_auto_publish_and_distribution(
4848
expected_files = get_files_in_manifest(remote.url)
4949

5050
# Sync from the remote
51-
body = RepositorySyncURL(remote=remote.pulp_href)
51+
body = FileRepositorySyncURL(remote=remote.pulp_href)
5252
monitor_task(file_bindings.RepositoriesFileApi.sync(repo.pulp_href, body).task)
5353
repo = file_bindings.RepositoriesFileApi.read(repo.pulp_href)
5454

@@ -73,7 +73,7 @@ def test_auto_publish_and_distribution(
7373
assert files_in_first_publication == expected_files
7474

7575
# Assert that mirror=True is not allowed when autopublish=True
76-
body = RepositorySyncURL(remote=remote.pulp_href, mirror=True)
76+
body = FileRepositorySyncURL(remote=remote.pulp_href, mirror=True)
7777
with pytest.raises(file_bindings.ApiException) as exc:
7878
file_bindings.RepositoriesFileApi.sync(repo.pulp_href, body)
7979
assert exc.value.status == 400

pulp_file/tests/functional/api/test_bad_sync.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pytest
66
from aiohttp import web
77

8-
from pulpcore.client.pulp_file import RepositorySyncURL
8+
from pulpcore.client.pulp_file import FileRepositorySyncURL
99

1010

1111
@pytest.fixture
@@ -23,7 +23,7 @@ def _perform_sync(url, policy="immediate"):
2323
}
2424
remote = gen_object_with_cleanup(file_bindings.RemotesFileApi, remote_data)
2525

26-
body = RepositorySyncURL(remote=remote.pulp_href)
26+
body = FileRepositorySyncURL(remote=remote.pulp_href)
2727
monitor_task(file_bindings.RepositoriesFileApi.sync(file_repo.pulp_href, body).task)
2828
return file_repo
2929

pulp_file/tests/functional/api/test_distributed_publication.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
import requests
99
from django.conf import settings
1010

11-
from pulpcore.client.pulp_file import FileFileDistribution, FileFilePublication, RepositorySyncURL
11+
from pulpcore.client.pulp_file import (
12+
FileFileDistribution,
13+
FileFilePublication,
14+
FileRepositorySyncURL,
15+
)
1216

1317

1418
@dataclass
@@ -82,7 +86,7 @@ def ctx(
8286
# Sync to get files into the repo
8387
monitor_task(
8488
file_bindings.RepositoriesFileApi.sync(
85-
repo.pulp_href, RepositorySyncURL(remote=remote.pulp_href)
89+
repo.pulp_href, FileRepositorySyncURL(remote=remote.pulp_href)
8690
).task
8791
)
8892
repo = file_bindings.RepositoriesFileApi.read(repo.pulp_href)

pulp_file/tests/functional/api/test_download_policies.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from aiohttp.client_exceptions import ClientResponseError
1111
from bs4 import BeautifulSoup
1212

13-
from pulpcore.client.pulp_file import FileFilePublication, RepositorySyncURL
13+
from pulpcore.client.pulp_file import FileFilePublication, FileRepositorySyncURL
1414
from pulpcore.tests.functional.utils import download_file, get_files_in_manifest
1515

1616
OBJECT_STORAGES = (
@@ -67,7 +67,7 @@ def test_download_policy(
6767
expected_files = get_files_in_manifest(remote.url)
6868

6969
# Sync from the remote and assert that a new repository version is created
70-
body = RepositorySyncURL(remote=remote.pulp_href)
70+
body = FileRepositorySyncURL(remote=remote.pulp_href)
7171
monitor_task(file_bindings.RepositoriesFileApi.sync(file_repo.pulp_href, body).task)
7272
file_repo = file_bindings.RepositoriesFileApi.read(file_repo.pulp_href)
7373
assert file_repo.latest_version_href.endswith("/versions/1/")

pulp_file/tests/functional/api/test_git_sync.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from pulpcore.client.pulp_file import RepositorySyncURL
5+
from pulpcore.client.pulp_file import FileRepositorySyncURL
66
from pulpcore.tests.functional.utils import PulpTaskError
77

88
GIT_REMOTE_URL = "https://github.com/pulp/pulp-smash.git"
@@ -65,7 +65,7 @@ def test_git_sync(file_bindings, file_repo, file_git_remote_factory, monitor_tas
6565
"""Test syncing from a public Git repository."""
6666
remote = file_git_remote_factory(url=GIT_REMOTE_URL, git_ref=git_ref)
6767

68-
body = RepositorySyncURL(remote=remote.pulp_href)
68+
body = FileRepositorySyncURL(remote=remote.pulp_href)
6969
monitor_task(file_bindings.RepositoriesFileApi.sync(file_repo.pulp_href, body).task)
7070

7171
file_repo = file_bindings.RepositoriesFileApi.read(file_repo.pulp_href)
@@ -81,7 +81,7 @@ def test_git_sync_idempotent(file_bindings, file_repo, file_git_remote_factory,
8181
"""Syncing the same Git ref twice should not create a new repository version."""
8282
remote = file_git_remote_factory(url=GIT_REMOTE_URL, git_ref="main")
8383

84-
body = RepositorySyncURL(remote=remote.pulp_href)
84+
body = FileRepositorySyncURL(remote=remote.pulp_href)
8585
monitor_task(file_bindings.RepositoriesFileApi.sync(file_repo.pulp_href, body).task)
8686

8787
file_repo = file_bindings.RepositoriesFileApi.read(file_repo.pulp_href)
@@ -104,7 +104,7 @@ def test_git_sync_invalid_url(file_bindings, file_repo, file_git_remote_factory,
104104
"""Syncing with an invalid Git URL should raise a task error."""
105105
remote = file_git_remote_factory(url="https://invalid.example.com/no-such-repo.git")
106106

107-
body = RepositorySyncURL(remote=remote.pulp_href)
107+
body = FileRepositorySyncURL(remote=remote.pulp_href)
108108
with pytest.raises(PulpTaskError) as exc:
109109
monitor_task(file_bindings.RepositoriesFileApi.sync(file_repo.pulp_href, body).task)
110110
assert "Failed to clone git repository" in exc.value.task.error["description"]
@@ -115,7 +115,7 @@ def test_git_sync_invalid_ref(file_bindings, file_repo, file_git_remote_factory,
115115
"""Syncing with a non-existent git ref should raise a task error."""
116116
remote = file_git_remote_factory(url=GIT_REMOTE_URL, git_ref="this-ref-does-not-exist-abc123")
117117

118-
body = RepositorySyncURL(remote=remote.pulp_href)
118+
body = FileRepositorySyncURL(remote=remote.pulp_href)
119119
with pytest.raises(PulpTaskError) as exc:
120120
monitor_task(file_bindings.RepositoriesFileApi.sync(file_repo.pulp_href, body).task)
121121
error_desc = exc.value.task.error["description"]
@@ -135,7 +135,7 @@ def test_git_sync_lfs(
135135
remote = file_git_remote_factory(url=LFS_REMOTE_URL)
136136
file_repo = file_repo_with_auto_publish
137137

138-
body = RepositorySyncURL(remote=remote.pulp_href)
138+
body = FileRepositorySyncURL(remote=remote.pulp_href)
139139
monitor_task(file_bindings.RepositoriesFileApi.sync(file_repo.pulp_href, body).task)
140140

141141
file_repo = file_bindings.RepositoriesFileApi.read(file_repo.pulp_href)

pulp_file/tests/functional/api/test_publish.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from pulpcore.client.pulp_file import (
1010
FileFilePublication,
11-
RepositorySyncURL,
11+
FileRepositorySyncURL,
1212
)
1313
from pulpcore.client.pulp_file.exceptions import ApiException
1414
from pulpcore.tests.functional.utils import download_file
@@ -29,7 +29,7 @@ def test_crd_publications(
2929

3030
# Sync from the remote
3131
initial_repo_version = file_repo.latest_version_href
32-
body = RepositorySyncURL(remote=remote.pulp_href)
32+
body = FileRepositorySyncURL(remote=remote.pulp_href)
3333
monitor_task(file_bindings.RepositoriesFileApi.sync(file_repo.pulp_href, body).task)
3434
first_repo_version_href = file_bindings.RepositoriesFileApi.read(
3535
file_repo.pulp_href

0 commit comments

Comments
 (0)