Skip to content

Commit

Permalink
Touch artifacts and content to prevent cleanup
Browse files Browse the repository at this point in the history
This change prepares for the async orphan cleanup that is expected to
come with pulpcore 3.15.

fixes #9134
  • Loading branch information
mdellweg committed Jul 28, 2021
1 parent e764ecf commit 693fc9d
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES/9134.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added touch statements to artifacts and content units where cleanup needs to be prevented.
5 changes: 5 additions & 0 deletions pulp_container/app/registry_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,11 +680,13 @@ def put(self, request, path, pk=None):
artifact.save()
except IntegrityError:
artifact = Artifact.objects.get(sha256=artifact.sha256)
artifact.touch()
try:
blob = models.Blob(digest=digest, media_type=models.MEDIA_TYPE.REGULAR_BLOB)
blob.save()
except IntegrityError:
blob = models.Blob.objects.get(digest=digest)
blob.touch()
try:
blob_artifact = ContentArtifact(
artifact=artifact, content=blob, relative_path=digest
Expand Down Expand Up @@ -827,6 +829,7 @@ def put(self, request, path, pk=None):
manifest.save()
except IntegrityError:
manifest = models.Manifest.objects.get(digest=manifest.digest)
manifest.touch()
ca = ContentArtifact(artifact=artifact, content=manifest, relative_path=manifest.digest)
try:
ca.save()
Expand All @@ -846,6 +849,7 @@ def put(self, request, path, pk=None):
tag.save()
except IntegrityError:
tag = models.Tag.objects.get(name=tag.name, tagged_manifest=manifest)
tag.touch()

tags_to_remove = models.Tag.objects.filter(
pk__in=repository.latest_version().content.all(), name=tag
Expand Down Expand Up @@ -899,4 +903,5 @@ def receive_artifact(self, chunk):
artifact.save()
except IntegrityError:
artifact = Artifact.objects.get(sha256=artifact.sha256)
artifact.touch()
return artifact
6 changes: 5 additions & 1 deletion pulp_container/app/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ def validate(self, data):
manifest = models.Manifest.objects.get(
pk__in=new_data["latest_version"].content.all(), digest=new_data["digest"]
)
manifest.touch()
except models.Manifest.DoesNotExist:
raise serializers.ValidationError(
_(
Expand Down Expand Up @@ -608,7 +609,9 @@ def validate(self, data):
_("Only one of 'containerfile' and 'containerfile_artifact' may be specified.")
)
data["containerfile_artifact"] = Artifact.init_and_validate(data.pop("containerfile"))
elif "containerfile_artifact" not in data:
elif "containerfile_artifact" in data:
data["containerfile_artifact"].touch()
else:
raise serializers.ValidationError(
_("'containerfile' or 'containerfile_artifact' must " "be specified.")
)
Expand All @@ -627,6 +630,7 @@ def validate(self, data):
)
try:
artifact = artifactfield.run_validation(data=url)
artifact.touch()
artifacts[artifact.pk] = relative_path
except serializers.ValidationError as e:
# Append the URL of missing Artifact to the error message
Expand Down
1 change: 1 addition & 0 deletions pulp_container/app/tasks/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def get_or_create_blob(layer_json, manifest, path):
"""
try:
blob = Blob.objects.get(digest=layer_json["digest"])
blob.touch()
except Blob.DoesNotExist:
layer_file_name = "{}{}".format(path, layer_json["digest"][7:])
layer_artifact = Artifact.init_and_validate(layer_file_name)
Expand Down
1 change: 1 addition & 0 deletions pulp_container/app/tasks/sync_stages.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ async def run(self):
except IntegrityError:
del tag.artifact_attributes["file"]
saved_artifact = Artifact.objects.get(**tag.artifact_attributes)
saved_artifact.touch()

tag_name = tag.url.split("/")[-1]
tag_dc = DeclarativeContent(Tag(name=tag_name))
Expand Down
8 changes: 6 additions & 2 deletions pulp_container/app/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ def add(self, request, pk):
if "content_units" in request.data:
for url in request.data["content_units"]:
content = NamedModelViewSet.get_resource(url, Content)
content.touch()
add_content_units.append(str(content.pk))

result = dispatch(
Expand Down Expand Up @@ -668,7 +669,7 @@ def copy_tags(self, request, pk):

result = dispatch(
tasks.recursive_add_content,
[repository],
[repository, source_latest.repository],
kwargs={
"repository_pk": str(repository.pk),
"content_units": [str(pk) for pk in tags_to_add.values_list("pk", flat=True)],
Expand Down Expand Up @@ -705,7 +706,7 @@ def copy_manifests(self, request, pk):
manifests_to_add = manifests_in_repo.filter(**filters)
result = dispatch(
tasks.recursive_add_content,
[repository],
[repository, source_latest.repository],
kwargs={
"repository_pk": str(repository.pk),
"content_units": [str(manifest.pk) for manifest in manifests_to_add],
Expand Down Expand Up @@ -740,9 +741,12 @@ def build_image(self, request, pk):
containerfile.save()
except IntegrityError:
containerfile = Artifact.objects.get(sha256=containerfile.sha256)
containerfile.touch()
tag = serializer.validated_data["tag"]

artifacts = serializer.validated_data["artifacts"]
for artifact in artifacts:
artifact.touch()

result = dispatch(
tasks.build_image_from_containerfile,
Expand Down

0 comments on commit 693fc9d

Please sign in to comment.