Skip to content

Commit

Permalink
Add new options to orphan clenaup command
Browse files Browse the repository at this point in the history
fixes: #398
  • Loading branch information
gerrod3 authored and mdellweg committed Oct 15, 2021
1 parent db0da0a commit 8f24466
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES/398.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added `--content-hrefs` and `--protection-time` options to orphan cleanup command.
4 changes: 2 additions & 2 deletions pulpcore/cli/common/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ def get_help_record(self, ctx: click.Context) -> Optional[Tuple[str, str]]:
return None
synopsis, help_text = tmp
entity_ctx = ctx.find_object(PulpEntityContext)
assert entity_ctx is not None
help_text = help_text.format(entity=entity_ctx.ENTITY, entities=entity_ctx.ENTITIES)
if entity_ctx is not None:
help_text = help_text.format(entity=entity_ctx.ENTITY, entities=entity_ctx.ENTITIES)
return synopsis, help_text


Expand Down
23 changes: 21 additions & 2 deletions pulpcore/cli/core/orphan.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import gettext
from typing import Any

import click

from pulpcore.cli.common.context import PluginRequirement, PulpContext, pass_pulp_context
from pulpcore.cli.common.generic import load_json_callback, pulp_option

_ = gettext.gettext

Expand All @@ -16,13 +18,30 @@ def orphan() -> None:


@orphan.command()
@pulp_option(
"--content-hrefs",
help=_("List of specific Contents to delete if they are orphans"),
callback=load_json_callback,
needs_plugins=[PluginRequirement("core", "3.14")],
)
@pulp_option(
"--protection-time",
"orphan_protection_time",
type=int,
help=_(
"How long in minutes Pulp should hold orphan Content and Artifacts before becoming"
" candidates for cleanup task"
),
needs_plugins=[PluginRequirement("core", "3.15")],
)
@pass_pulp_context
def cleanup(pulp_ctx: PulpContext) -> None:
def cleanup(pulp_ctx: PulpContext, **kwargs: Any) -> None:
"""
Cleanup orphaned content.
"""
body = {k: v for k, v in kwargs.items() if v is not None}
if pulp_ctx.has_plugin(PluginRequirement("core", "3.14")):
result = pulp_ctx.call("orphans_cleanup_cleanup")
result = pulp_ctx.call("orphans_cleanup_cleanup", body=body)
else:
result = pulp_ctx.call("orphans_delete")
pulp_ctx.output_result(result)
31 changes: 31 additions & 0 deletions tests/scripts/pulpcore/test_orphans.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,34 @@
expect_succ pulp orphan cleanup

test "$(echo "${OUTPUT}" | jq -r '.state' )" = "completed"

pulp debug has-plugin --name "file" || exit 3

if pulp debug has-plugin --name "core" --min-version "3.14.0"
then
dd if=/dev/urandom of=test_1.txt bs=2MiB count=1
dd if=/dev/urandom of=test_2.txt bs=2MiB count=1
dd if=/dev/urandom of=test_3.txt bs=2MiB count=1

expect_succ pulp file content upload --file test_1.txt --relative-path orphan_test/test_1.txt
expect_succ pulp file content upload --file test_2.txt --relative-path orphan_test/test_2.txt
expect_succ pulp file content upload --file test_3.txt --relative-path orphan_test/test_3.txt

content_href="$(echo "$OUTPUT" | jq -r .pulp_href)"
expect_succ pulp file content list
count="$(echo "$OUTPUT" | jq -r length)"
expect_succ pulp orphan cleanup --content-hrefs "[\"$content_href\"]"

expect_succ pulp file content list
test "$(echo "$OUTPUT" | jq -r length)" -eq "$((count-1))"
expect_succ pulp orphan cleanup
else
expect_fail pulp orphan cleanup --content-hrefs '["/pulp/api/v3/content/random/"]'
fi

if pulp debug has-plugin --name "core" --min-version "3.15.0"
then
expect_succ pulp orphan cleanup --protection-time 0
else
expect_fail pulp orphan cleanup --protection-time 0
fi

0 comments on commit 8f24466

Please sign in to comment.