From 4fb998182be59ae30a18ca1d8d0ddf4cbf5151dd Mon Sep 17 00:00:00 2001 From: mhecko Date: Thu, 24 Aug 2023 12:49:57 +0200 Subject: [PATCH] userspacegen: perserve custom repositories --- .../libraries/userspacegen.py | 62 +++++-------------- 1 file changed, 15 insertions(+), 47 deletions(-) diff --git a/repos/system_upgrade/common/actors/targetuserspacecreator/libraries/userspacegen.py b/repos/system_upgrade/common/actors/targetuserspacecreator/libraries/userspacegen.py index b1b53be084..1d8858eb30 100644 --- a/repos/system_upgrade/common/actors/targetuserspacecreator/libraries/userspacegen.py +++ b/repos/system_upgrade/common/actors/targetuserspacecreator/libraries/userspacegen.py @@ -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) @@ -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])