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

targetuserspacecreator: Refactor gathering of repositories #973

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
from collections import defaultdict

from leapp.libraries.common import repofileutils
from leapp.libraries.stdlib import api
from leapp.models import TargetRepositories

DEFAULT_RHSM_REPOFILE = '/etc/yum.repos.d/redhat.repo'

REPO_KIND_RHUI = 'rhui'
REPO_KIND_RHSM = 'rhsm'
REPO_KIND_CUSTOM = 'custom'


class _RequestedRepoIDs(object):
def __init__(self):
self.rhel_repos = set()
self.custom_repos = set()

@property
def combined(self):
return self.rhel_repos | self.custom_repos


def _get_requested_repo_ids():
"""
Get all requested target repositories.
"""
result = _RequestedRepoIDs()
for msg in api.consume(TargetRepositories):
result.rhel_repos.update({r.repoid for r in msg.rhel_repos})
result.custom_repos.update({r.repoid for r in msg.custom_repos})
return result


class RepositoryInformation(object):
def __init__(self, context, cloud_repo=None):
self.repos = []
self.rfiles = []
self.mapped = defaultdict(list)
self.repo_type_map = defaultdict(set)
self._target_repo_ids = _get_requested_repo_ids()
self._load_repofiles(context=context, cloud_repo=cloud_repo)

def _load_repofiles(self, context, cloud_repo=None):
for rfile in repofileutils.get_parsed_repofiles(
context=context,
kind_resolve=resolve_repo_file_kind(cloud_repo)):
self.add_file(rfile)

@property
def rhsm_repoids(self):
return self.repo_type_map[REPO_KIND_RHSM]

@property
def rhui_repoids(self):
return self.repo_type_map[REPO_KIND_RHUI]

@property
def rhel_repoids(self):
return self.rhsm_repoids | self.rhui_repoids

@property
def custom_repoids(self):
return self.repo_type_map[REPO_KIND_CUSTOM]

@property
def missing_custom_repoids(self):
return self._target_repo_ids.custom_repos - self.custom_repoids

@property
def target_repoids(self):
return (self._target_repo_ids.custom_repos & self.custom_repoids) | (
self._target_repo_ids.rhel_repos & self.rhel_repoids)

def add_file(self, rfile):
self.rfiles.append(rfile)
for repo in rfile.data:
self.add(repo)

def add(self, repo):
self.repos.append(repo)
self.mapped[repo.repoid].append(repo)
self.repo_type_map[repo.kind].add(repo.repoid)

@property
def duplicated_repoids(self):
return {k: v for k, v in self.mapped.items() if len(v) > 1}
Comment on lines +86 to +87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The property name suggests that it contains a collection of repoids that are duplicated, whereas a dictionary is returned. Every use of this property access to the values of the property requests only its keys().

Copy link
Member Author

@vinzenz vinzenz Jan 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well it is right now, the point is that you can now report which repo files do contain the duplications. But this would be a follow up improvement. And it is a collection, key = repoid, value = list of repos with the same repo id)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

list of repos here means the list of repository files



def resolve_repo_file_kind(cloud_repo):
def resolver(path):
if path == DEFAULT_RHSM_REPOFILE:
return REPO_KIND_RHSM
if cloud_repo and path == cloud_repo:
return REPO_KIND_RHUI
return REPO_KIND_CUSTOM
return resolver
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os

from leapp.libraries.actor import repoinfo
from leapp.libraries.common import rhsm, rhui


def _install_custom_repofiles(context, custom_repofiles):
"""
Install the required custom repository files into the container.

The repository files are copied from the host into the /etc/yum.repos.d
directory into the container.

:param context: the container where the repofiles should be copied
:type context: mounting.IsolatedActions class
:param custom_repofiles: list of custom repo files
:type custom_repofiles: List(CustomTargetRepositoryFile)
"""
for rfile in custom_repofiles:
_dst_path = os.path.join('/etc/yum.repos.d', os.path.basename(rfile.file))
context.copy_to(rfile.file, _dst_path)


def prepare_repository_collection(context, indata, prod_cert_path):
rhsm.set_container_mode(context)
rhsm.switch_certificate(context, indata.rhsm_info, prod_cert_path)
if indata.rhui_info:
rhui.copy_rhui_data(context, indata.rhui_info.provider)
_install_custom_repofiles(context, indata.custom_repofiles)


def collect_repositories(context, cloud_repo=None):
info = repoinfo.RepositoryInformation(context, cloud_repo)
return info
Loading