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 committed Jun 5, 2024
1 parent 422a7d7 commit 191bf8d
Show file tree
Hide file tree
Showing 5 changed files with 159 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.
8 changes: 8 additions & 0 deletions pulp-glue/pulp_glue/rpm/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,11 @@ def sync(self, body: t.Optional[EntityDefinition] = None) -> t.Any:
)

return super().sync(body)


class PulpRpmPruneContext(PulpEntityContext):
PRUNE_PACKAGES_ID: t.ClassVar[str] = "rpm_prune_prune_packages"
NEEDS_PLUGINS = [PluginRequirement("rpm", specifier=">=3.27.0.dev")]

def prune_packages(self, body: EntityDefinition) -> t.Any:
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)
109 changes: 109 additions & 0 deletions pulpcore/cli/rpm/prune.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import gettext
import typing as t

import click
from pulp_glue.common.context import EntityFieldDefinition, 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",
default_plugin="rpm",
default_type="rpm",
context_table={"rpm:rpm": PulpRpmRepositoryContext},
multiple=True,
href_pattern=PulpRpmRepositoryContext.HREF_PATTERN,
help=_(
"Repository to prune, in the form '[[<plugin>:]<resource_type>:]<name>' or by href."
" Can be called multiple times."
),
)


@pulp_command()
@multi_repository_option
@click.option(
"--repository-href",
multiple=True,
help=_("Repository HREF of a Reppository to prune. Can be called multiple times."),
)
@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(
"--concurrency", type=int, default=10, help=_("How many repositories to prune concurrently.")
)
@click.option(
"--dry-run",
type=bool,
is_flag=True,
show_default=True,
default=False,
help=_("Evaluate the prune-status of the specified repositries but DO NOT make any changes."),
)
@pass_pulp_context
def prune_packages(
pulp_ctx: PulpCLIContext,
repository: t.Iterable[EntityFieldDefinition],
repository_href: t.Iterable[str],
all_repositories: t.Optional[bool],
keep_days: t.Optional[int],
concurrency: 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 and/or --repository-href 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.
"""
entity_ctx = PulpRpmPruneContext(pulp_ctx)

if not (all_repositories or repository or repository_href):
raise PulpException(_("at least one repository, or --all-repositories, must be specified"))
elif all_repositories and (repository or repository_href):
raise PulpException(
_("cannot specify --all-repositories and --repository at the same time")
)

if all_repositories:
repo_hrefs = ["*"]
else:
repo_hrefs = [
repository_ctx.pulp_href
for repository_ctx in repository
if isinstance(repository_ctx, PulpRpmRepositoryContext)
] + list(repository_href)

params = {
"repo_hrefs": repo_hrefs,
"keep_days": keep_days,
"repo_concurrency": concurrency,
"dry_run": dry_run,
}
result = entity_ctx.prune_packages(body=params)
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" --concurrency 1 --keep-days 0 --dry-run
expect_succ pulp rpm prune-packages --repository-href "${repo_href}" --concurrency 1 --keep-days 0 --dry-run
expect_succ pulp rpm prune-packages --repository-href "${repo_href}" --repository-href "${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-href "${repo_href}" --repository-href "${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" --concurrency 1 --keep-days 0



0 comments on commit 191bf8d

Please sign in to comment.