From 228e231f211653e3b2571c32b2b90a06c9f0b2c7 Mon Sep 17 00:00:00 2001 From: Gerrod Ubben Date: Thu, 14 Oct 2021 15:23:39 -0400 Subject: [PATCH] Add new options to orphan clenaup command fixes: #398 --- CHANGES/398.feature | 1 + pulpcore/cli/common/generic.py | 4 +-- pulpcore/cli/core/orphan.py | 23 ++++++++++++++-- tests/scripts/pulpcore/test_orphans.sh | 37 ++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 CHANGES/398.feature diff --git a/CHANGES/398.feature b/CHANGES/398.feature new file mode 100644 index 000000000..ca5cf313b --- /dev/null +++ b/CHANGES/398.feature @@ -0,0 +1 @@ +Added `--content-hrefs` and `--protection-time` options to orphan cleanup command. diff --git a/pulpcore/cli/common/generic.py b/pulpcore/cli/common/generic.py index 42711853e..a9bf6386b 100644 --- a/pulpcore/cli/common/generic.py +++ b/pulpcore/cli/common/generic.py @@ -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 diff --git a/pulpcore/cli/core/orphan.py b/pulpcore/cli/core/orphan.py index ba96be8c3..d09f4d0c0 100644 --- a/pulpcore/cli/core/orphan.py +++ b/pulpcore/cli/core/orphan.py @@ -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 @@ -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) diff --git a/tests/scripts/pulpcore/test_orphans.sh b/tests/scripts/pulpcore/test_orphans.sh index 27cb70620..9d2990a1e 100755 --- a/tests/scripts/pulpcore/test_orphans.sh +++ b/tests/scripts/pulpcore/test_orphans.sh @@ -6,3 +6,40 @@ 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" --max-version "3.13.0" +then + expect_fail pulp orphan cleanup --content-hrefs '["/pulp/api/v3/content/random/"]' +else + 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 + if pulp debug has-plugin --name "core" --min-version "3.15.0" + then + test "$(echo "$OUTPUT" | jq -r length)" -eq "$count" + else + test "$(echo "$OUTPUT" | jq -r length)" -eq "$((count-1))" + expect_succ pulp orphan cleanup + fi +fi + +if pulp debug has-plugin --name "core" --max-version "3.14.0" +then + expect_fail pulp orphan cleanup --protection-time 0 +else + expect_succ pulp orphan cleanup --protection-time 0 + expect_succ pulp file content list + test "$(echo "$OUTPUT" | jq -r length)" -eq "0" +fi