Skip to content

Commit

Permalink
userspacegen: perserve custom repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
mhecko committed Aug 24, 2023
1 parent 203558a commit 35358f0
Showing 1 changed file with 16 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from leapp import reporting
from leapp.exceptions import StopActorExecution, StopActorExecutionError
from leapp.libraries.actor import constants
from leapp.libraries.common import dnfplugin, mounting, overlaygen, repofileutils, rhsm, rhui, utils
from leapp.libraries.common import dnfplugin, mounting, overlaygen, repofileutils, rhsm, utils
from leapp.libraries.common.config import get_env, get_product_type
from leapp.libraries.common.config.version import get_target_major_version
from leapp.libraries.stdlib import api, CalledProcessError, config, run
Expand Down Expand Up @@ -282,27 +282,6 @@ def prepare_target_userspace(context, userspace_dir, enabled_repos, packages):
raise StopActorExecutionError(message=message, details=details)


def _get_all_rhui_pkgs():
"""
Return the list of rhui packages
Currently, do not care about what rhui we have, release, etc.
Just take all packages. We need them just for the purpose of filtering
what files we have to remove (see _prep_repository_access) and it's ok
for us to use whatever rhui rpms (the relevant rpms catch the problem,
the others are just taking bytes in memory...). It's a hot-fix. We are going
to refactor the library later completely..
"""
upg_path = rhui.get_upg_path()
pkgs = []
for rhui_map in rhui.RHUI_CLOUD_MAP[upg_path].values():
for key in rhui_map.keys():
if not key.endswith('pkg'):
continue
pkgs.append(rhui_map[key])
return pkgs


def _query_rpm_for_pkg_files(context, pkgs):
files_owned_by_rpm = set()
rpm_query_result = context.call(['rpm', '-ql'] + pkgs, split=True)
Expand Down Expand Up @@ -405,48 +384,37 @@ def _prep_repository_access(context, target_userspace):
"""
target_etc = os.path.join(target_userspace, 'etc')
target_yum_repos_d = os.path.join(target_etc, 'yum.repos.d')
backup_yum_repos_d = os.path.join(target_etc, 'yum.repos.d.backup')

_copy_certificates(context, target_userspace)

if not rhsm.skip_rhsm():
run(['rm', '-rf', os.path.join(target_etc, 'rhsm')])
context.copytree_from('/etc/rhsm', os.path.join(target_etc, 'rhsm'))
# NOTE: we cannot just remove the original target yum.repos.d dir
# as e.g. in case of RHUI a special RHUI repofiles are installed by a pkg
# when the target userspace container is created. Removing these files we loose
# RHUI target repositories. So ...->
# -> detect such a files...

# NOTE: We cannot just remove the target yum.repos.d dir and replace it with yum.repos.d from the scratch
# # that we've used to obtain the new DNF stack and install it into the target userspace. Although
# # RHUI clients are being installed in both scratch and target containers, users can request their package
# # to be installed into target userspace that might add some repos to yum.repos.d that are not in scratch.

# Detect files that are owned by some RPM - these cannot be deleted
with mounting.NspawnActions(base_dir=target_userspace) as target_context:
files_owned_by_rpms = _get_files_owned_by_rpms(target_context, '/etc/yum.repos.d')

# -> backup the orig dir & install the new one
# Backup the target yum.repos.d so we can always copy the files installed by some RPM back into yum.repos.d
# when we modify it
run(['mv', target_yum_repos_d, backup_yum_repos_d])
context.copytree_from('/etc/yum.repos.d', target_yum_repos_d)

# -> find old rhui repo files (we have to remove these as they cause duplicates)
rhui_pkgs = _get_all_rhui_pkgs()
old_files_owned_by_rhui_rpms = _get_files_owned_by_rpms(context, '/etc/yum.repos.d', rhui_pkgs)
for fname in old_files_owned_by_rhui_rpms:
api.current_logger().debug('Remove the old repofile: {}'.format(fname))
run(['rm', '-f', os.path.join(target_yum_repos_d, fname)])
# .. continue: remove our leapp rhui repo file (do not care if we are on rhui or not)
for rhui_map in rhui.gen_rhui_files_map().values():
for item in rhui_map:
if item[1] != rhui.YUM_REPOS_PATH:
continue
target_leapp_repofile = os.path.join(target_yum_repos_d, item[0])
if not os.path.isfile(target_leapp_repofile):
continue
# we found it!!
run(['rm', '-f', target_leapp_repofile])
break
# Copy the yum.repos.d from scratch - preserve any custom repositories. No need to clean-up old RHUI clients,
# we swap them for the new RHUI client in scratch (so the old one is not installed).
context.copytree_from('/etc/yum.repos.d', target_yum_repos_d)

# -> copy expected files back
# Copy back files owned by some RPM
for fname in files_owned_by_rpms:
api.current_logger().debug('Copy the backed up repo file: {}'.format(fname))
run(['mv', os.path.join(backup_yum_repos_d, fname), os.path.join(target_yum_repos_d, fname)])

# -> remove the backed up dir
# Cleanup - remove the backed up dir
run(['rm', '-rf', backup_yum_repos_d])


Expand Down

0 comments on commit 35358f0

Please sign in to comment.