Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utility script to detect dangling PRCs in structured content #1191

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
Empty file.
65 changes: 65 additions & 0 deletions pulp_deb/app/management/commands/check_dangling_content.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from gettext import gettext as _
from django.core.management import BaseCommand
from pulpcore.plugin.models import RepositoryVersion

from pulp_deb.app.models.content.content import Package
from pulp_deb.app.models.content.structure_content import PackageReleaseComponent
from pulp_deb.app.models.repository import AptRepository


class Command(BaseCommand):
"""
Check for dangling content in apt repository versions.
"""

help = _(__doc__)

def handle(self, *args, **options):
"""Implement the command."""
repo_qs = AptRepository.objects.all()
repo_version_qs = RepositoryVersion.objects.filter(repository__in=repo_qs)

dangling_content = False
dc_details = []

for repo_version in repo_version_qs:
content = repo_version.get_content()
package_ids = set(
Package.objects.filter(
pk__in=content.filter(pulp_type=Package.get_pulp_type())
).values_list("pulp_id", flat=True)
)
prc_qs = PackageReleaseComponent.objects.filter(
pk__in=content.filter(
pulp_type=PackageReleaseComponent.get_pulp_type()
).values_list("pk", flat=True)
)
prc_ids = set(prc_qs.values_list("package__pulp_id", flat=True))

if prc_ids != package_ids:
dangling_content = True
dc_package_ids = prc_ids - package_ids
dc_prcs = prc_qs.filter(package__pulp_id__in=dc_package_ids)
dc_details.append(
{
"repo_version": repo_version,
"dangling_prcs": list(dc_prcs),
}
)

if dangling_content:
for detail in dc_details:
dc_prcs_output = "\n".join(str(prc) for prc in detail["dangling_prcs"])
self.stdout.write(
f"\n{'-' * 40}\n"
f"Dangling content found in {detail['repo_version']}:\n"
f"{'-' * 40}\n"
f"Dangling PRCs:\n{dc_prcs_output}"
)
self.stdout.write(f"{'-' * 40}\n")
self.stdout.write(f"\nTotal affected repository versions: {len(dc_details)}\n")
self.stdout.write(f"{'=' * 40}\n")
else:
self.stdout.write(f"{'=' * 40}\n")
self.stdout.write("No dangling content found.\n")
self.stdout.write(f"{'=' * 40}\n")