-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for new pulp_rpm "prune-packages" feature.
closes #979.
- Loading branch information
Showing
5 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
|