Skip to content

Commit

Permalink
Add support for new pulp_rpm "prune-packages" feature.
Browse files Browse the repository at this point in the history
closes #979.
  • Loading branch information
ggainey authored and mdellweg committed Jun 17, 2024
1 parent 5bd2e45 commit 4b2fd49
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES/979.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Added the "pulp rpm prune-packages" command to support new RPM feature.

See [2909](https://github.com/pulp/pulp_rpm/issues/2909) for details.
18 changes: 18 additions & 0 deletions pulp-glue/pulp_glue/rpm/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
PulpRemoteContext,
PulpRepositoryContext,
PulpRepositoryVersionContext,
PulpViewSetContext,
)
from pulp_glue.common.i18n import get_translation

Expand Down Expand Up @@ -356,3 +357,20 @@ def sync(self, body: t.Optional[EntityDefinition] = None) -> t.Any:
)

return super().sync(body)


class PulpRpmPruneContext(PulpViewSetContext):
ID_PREFIX: t.ClassVar[str] = "rpm_prune"
NEEDS_PLUGINS = [PluginRequirement("rpm", specifier=">=3.27.0.dev")]

def prune_packages(
self,
repo_hrefs: t.List[t.Union[str, PulpRpmRepositoryContext]],
keep_days: t.Optional[int],
dry_run: t.Optional[bool],
) -> t.Any:
body: t.Dict[str, t.Any] = {}
body["repo_hrefs"] = repo_hrefs
body["keep_days"] = keep_days
body["dry_run"] = dry_run
return self.call("prune_packages", body=body)
2 changes: 2 additions & 0 deletions pulpcore/cli/rpm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pulpcore.cli.rpm.comps import comps_upload
from pulpcore.cli.rpm.content import content
from pulpcore.cli.rpm.distribution import distribution
from pulpcore.cli.rpm.prune import prune_packages
from pulpcore.cli.rpm.publication import publication
from pulpcore.cli.rpm.remote import remote
from pulpcore.cli.rpm.repository import repository
Expand All @@ -35,4 +36,5 @@ def mount(main: click.Group, **kwargs: t.Any) -> None:
)
)
rpm.add_command(comps_upload)
rpm.add_command(prune_packages)
main.add_command(rpm)
90 changes: 90 additions & 0 deletions pulpcore/cli/rpm/prune.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import gettext
import typing as t

import click
from pulp_glue.common.context import PulpException
from pulp_glue.rpm.context import PulpRpmPruneContext, PulpRpmRepositoryContext

from pulpcore.cli.common.generic import (
PulpCLIContext,
pass_pulp_context,
pulp_command,
resource_option,
)

_ = gettext.gettext

multi_repository_option = resource_option(
"--repository",
"repositories",
default_plugin="rpm",
default_type="rpm",
context_table={"rpm:rpm": PulpRpmRepositoryContext},
multiple=True,
href_pattern=PulpRpmRepositoryContext.HREF_PATTERN,
help=_(
"RPM Repository to prune, in the form 'rpm:rpm:<name>' or by href."
" Can be called multiple times."
),
)


@pulp_command()
@multi_repository_option
@click.option(
"--all-repositories",
type=bool,
is_flag=True,
show_default=True,
default=False,
help=_("Prune *all* repositories accessible to the invoking user."),
)
@click.option(
"--keep-days",
type=int,
default=14,
help=_("Prune packages that were added to the specified repositories more than N days ago."),
)
@click.option(
"--dry-run",
type=bool,
is_flag=True,
show_default=True,
default=False,
help=_("Evaluate the prune-status of the specified repositories but DO NOT make any changes."),
)
@pass_pulp_context
def prune_packages(
pulp_ctx: PulpCLIContext,
repositories: t.Iterable[PulpRpmRepositoryContext],
all_repositories: t.Optional[bool],
keep_days: t.Optional[int],
dry_run: t.Optional[bool],
) -> None:
"""
Prune older Packages from the current-version of a repository/repositories.
Repositories can be specified by repeated --repository arguments.
"All" repositories can be specified by --all-repositories.
At least one repository, or --all-repositories, must be specified.
You may not specify --all-repositories *and* one or more specific repositories.
"""
prune_ctx = PulpRpmPruneContext(pulp_ctx)
if not (all_repositories or repositories):
raise PulpException(
_("at least one --repository, or --all-repositories, must be specified")
)
elif all_repositories and repositories:
raise PulpException(
_("cannot specify --all-repositories and --repository at the same time")
)

repos_list: t.List[t.Union[str, PulpRpmRepositoryContext]] = (
["*"] if all_repositories else list(repositories)
)

result = prune_ctx.prune_packages(repos_list, keep_days, dry_run)
pulp_ctx.output_result(result)
37 changes: 37 additions & 0 deletions tests/scripts/pulp_rpm/test_prune.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash

# shellcheck source=tests/scripts/config.source
. "$(dirname "$(dirname "$(realpath "$0")")")"/config.source

pulp debug has-plugin --name "rpm" --specifier ">=3.27.0.dev" || exit 23

cleanup() {
pulp rpm repository destroy --name "cli_test_rpm_prune" || true
pulp rpm repository destroy --name "cli_test_rpm_prune_2" || true
pulp rpm remote destroy --name "cli_test_rpm_prune" || true
}
trap cleanup EXIT

expect_succ pulp rpm remote create --name "cli_test_rpm_prune" --url "$RPM_REMOTE_URL" --policy on_demand
expect_succ pulp rpm repository create --name "cli_test_rpm_prune" --remote "cli_test_rpm_prune" --no-autopublish
repo_href="$(echo "$OUTPUT" | jq -r '.pulp_href')"
expect_succ pulp rpm repository create --name "cli_test_rpm_prune_2" --remote "cli_test_rpm_prune" --no-autopublish
repo_href_2="$(echo "$OUTPUT" | jq -r '.pulp_href')"
expect_succ pulp rpm repository sync --repository "cli_test_rpm_prune"
expect_succ pulp rpm repository sync --repository "cli_test_rpm_prune_2"

expect_fail pulp rpm prune-packages
expect_fail pulp rpm prune-packages --repository "rpm:rpm:cli_test_rpm_prune" --all-repositories
expect_fail pulp rpm prune-packages --repository "rpm:rpm:cli_test_rpm_prune" --keep-days foo
expect_fail pulp rpm prune-packages --repository "rpm:rpm:cli_test_rpm_prune" --concurrency bar

expect_succ pulp rpm prune-packages --repository "rpm:rpm:cli_test_rpm_prune" --keep-days 0 --dry-run
expect_succ pulp rpm prune-packages --repository "${repo_href}" --keep-days 0 --dry-run
expect_succ pulp rpm prune-packages --repository "${repo_href}" --repository "${repo_href_2}" --dry-run
expect_succ pulp rpm prune-packages --repository "rpm:rpm:cli_test_rpm_prune" --repository "rpm:rpm:cli_test_rpm_prune_2" --dry-run
expect_succ pulp rpm prune-packages --repository "rpm:rpm:cli_test_rpm_prune" --repository "${repo_href}" --repository "${repo_href_2}" --dry-run
expect_succ pulp rpm prune-packages --all-repositories --dry-run
expect_succ pulp rpm prune-packages --repository "rpm:rpm:cli_test_rpm_prune" --keep-days 0



0 comments on commit 4b2fd49

Please sign in to comment.