Skip to content

Commit

Permalink
Change content app's working directory dynamically
Browse files Browse the repository at this point in the history
As of this commit, content app is no longer storing temporary files in the /var/run/ directory. The temporary files were created during on-demand downloading and were not removed until, e.g., restarting pulp services.

closes #9000
  • Loading branch information
lubosmj committed Jul 19, 2021
1 parent 9e22dda commit 4e8ebcf
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES/9000.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Updated the content app's working directory to ``WORKING_DIRECTORY`` specified in ``settings.py``.
2 changes: 2 additions & 0 deletions pulpcore/content/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def save_heartbeat_blocking():


async def server(*args, **kwargs):
os.chdir(settings.WORKING_DIRECTORY)

asyncio.ensure_future(_heartbeat())
for pulp_plugin in pulp_plugin_configs():
if pulp_plugin.name != "pulpcore.app":
Expand Down
6 changes: 6 additions & 0 deletions pulpcore/content/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,12 @@ def _save_artifact(self, download_result, remote_artifact):
**download_result.artifact_attributes, file=download_result.path
)
artifact.save()
else:
if download_result.path.startswith(str(settings.WORKING_DIRECTORY)):
# remove a downloaded file that will be never used since another downloaded
# file was already associated with an artifact
os.unlink(download_result.path)

update_content_artifact = True
if content_artifact._state.adding:
# This is the first time pull-through content was requested.
Expand Down
14 changes: 8 additions & 6 deletions pulpcore/tests/unit/content/test_handler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import tempfile

from unittest.mock import Mock

from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase

from pulpcore.content import Handler
Expand All @@ -20,28 +21,29 @@ def setUp(self):
)
self.ra2 = Mock(content_artifact=self.ca2)

def download_result_mock(self, path):
def download_result_mock(self):
dr = Mock()
dr.artifact_attributes = {"size": 0}
for digest_type in Artifact.DIGEST_FIELDS:
dr.artifact_attributes[digest_type] = "abc123"
dr.path = SimpleUploadedFile(name=path, content="")
tmp_file = tempfile.NamedTemporaryFile(delete=False)
dr.path = tmp_file.name
return dr

def test_save_artifact(self):
"""Artifact needs to be created."""
cch = Handler()
new_artifact = cch._save_artifact(self.download_result_mock("c1"), self.ra1)
new_artifact = cch._save_artifact(self.download_result_mock(), self.ra1)
c1 = Content.objects.get(pk=self.c1.pk)
self.assertIsNotNone(new_artifact)
self.assertEqual(c1._artifacts.get().pk, new_artifact.pk)

def test_save_artifact_artifact_already_exists(self):
"""Artifact turns out to already exist."""
cch = Handler()
new_artifact = cch._save_artifact(self.download_result_mock("c1"), self.ra1)
new_artifact = cch._save_artifact(self.download_result_mock(), self.ra1)

existing_artifact = cch._save_artifact(self.download_result_mock("c2"), self.ra2)
existing_artifact = cch._save_artifact(self.download_result_mock(), self.ra2)
c2 = Content.objects.get(pk=self.c2.pk)
self.assertEqual(existing_artifact.pk, new_artifact.pk)
self.assertEqual(c2._artifacts.get().pk, existing_artifact.pk)

0 comments on commit 4e8ebcf

Please sign in to comment.