Skip to content

Commit

Permalink
Added progress-reports for number of Artifacts and Content-pre-repo i…
Browse files Browse the repository at this point in the history
…n an export.

Added testing for same.

Required PR: pulp#872
closes #6541
  • Loading branch information
ggainey committed Aug 27, 2020
1 parent 8f50ab9 commit a11e10f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGES/6541.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added progress-reports to the PulpExport task.
40 changes: 29 additions & 11 deletions pulpcore/app/importexport.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
from django.conf import settings

from pulpcore.app.apps import get_plugin_config
from pulpcore.app.models.progress import ProgressReport
from pulpcore.app.models.repository import Repository
from pulpcore.app.modelresource import (
ArtifactResource,
ContentArtifactResource,
RepositoryResource,
)
from pulpcore.constants import TASK_STATES


def _write_export(the_tarfile, resource, dest_dir=None):
Expand Down Expand Up @@ -72,17 +74,18 @@ def export_artifacts(export, artifacts):
Raises:
ValidationError: When path is not in the ALLOWED_EXPORT_PATHS setting
"""
for artifact in artifacts:
dest = artifact.file.name

if settings.DEFAULT_FILE_STORAGE != "pulpcore.app.models.storage.FileSystem":
with tempfile.TemporaryDirectory() as temp_dir:
with tempfile.NamedTemporaryFile(dir=temp_dir) as temp_file:
temp_file.write(artifact.file.read())
temp_file.flush()
export.tarfile.add(temp_file.name, dest)
else:
export.tarfile.add(artifact.file.path, dest)
data = dict(message="Exporting Artifacts", code="export-artifacts", total=len(artifacts))
with ProgressReport(**data) as pb:
for artifact in pb.iter(artifacts):
dest = artifact.file.name
if settings.DEFAULT_FILE_STORAGE != "pulpcore.app.models.storage.FileSystem":
with tempfile.TemporaryDirectory() as temp_dir:
with tempfile.NamedTemporaryFile(dir=temp_dir) as temp_file:
temp_file.write(artifact.file.read())
temp_file.flush()
export.tarfile.add(temp_file.name, dest)
else:
export.tarfile.add(artifact.file.path, dest)

resource = ArtifactResource()
resource.queryset = artifacts
Expand Down Expand Up @@ -135,6 +138,21 @@ def _combine_content_mappings(map1, map2):
content_mapping, resource.content_mapping
)

msg = (
f"Exporting content for {plugin_name} "
f"repository-version {repository_version.repository.name}/{repository_version.number}"
)
content_count = repository_version.content.count()
data = dict(
message=msg,
code="export-repo-version-content",
total=content_count,
done=content_count,
state=TASK_STATES.COMPLETED,
)
pb = ProgressReport(**data)
pb.save()

if content_mapping:
# write the content mapping to tarfile
cm_json = json.dumps(content_mapping).encode("utf8")
Expand Down
10 changes: 10 additions & 0 deletions pulpcore/tests/functional/api/using_plugin/test_pulpexport.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
RepositorySyncURL,
RemotesFileApi,
)
from pulpcore.constants import TASK_STATES

NUM_REPOS = 3
MAX_EXPORTS = 3
Expand Down Expand Up @@ -186,6 +187,15 @@ def _gen_export(self, exporter, body={}):
task = self.client.get(export_response.task)
resources = task["created_resources"]
self.assertEqual(1, len(resources))
reports = task["progress_reports"]
found_artifacts = False
found_content = False
for r in reports:
self.assertEqual(TASK_STATES.COMPLETED, r["state"])
found_artifacts |= r["code"] == "export-artifacts"
found_content |= r["code"] == "export-repo-version-content"
self.assertTrue(found_artifacts, "No artifacts exported!")
self.assertTrue(found_content, "No content exported!")
export_href = resources[0]
export = self.exports_api.read(export_href)
self.assertIsNotNone(export)
Expand Down

0 comments on commit a11e10f

Please sign in to comment.