Skip to content

Commit

Permalink
targetuserspacecreator: Refactor gathering of repositories
Browse files Browse the repository at this point in the history
This patch unifies the collection of repositories.

Signed-off-by: Vinzenz Feenstra <[email protected]>
  • Loading branch information
vinzenz committed Nov 8, 2022
1 parent 279ebc9 commit f904598
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 228 deletions.
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 _RequestedRepos:
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_repos():
"""
Get the list of requested target repositories.
"""
result = _RequestedRepos()
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:
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_repos()
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}


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.repoinfo import RepositoryInformation
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 = RepositoryInformation(context, cloud_repo)
return info
Loading

0 comments on commit f904598

Please sign in to comment.