From ceb0a7898e443b78e773da3a5f53e6221302d297 Mon Sep 17 00:00:00 2001 From: Inessa Vasilevskaya Date: Thu, 16 Nov 2023 13:16:01 +0100 Subject: [PATCH] [WIP] [DEBUG] POC of custom modifications tracking Please do NOT review until marked as ready. All custom modifications ideas will be put there. --- .../actors/trackcustommodifications/actor.py | 19 ++++++ .../libraries/trackcustommodifications.py | 62 +++++++++++++++++++ .../common/models/custommodifications.py | 11 ++++ 3 files changed, 92 insertions(+) create mode 100644 repos/system_upgrade/common/actors/trackcustommodifications/actor.py create mode 100644 repos/system_upgrade/common/actors/trackcustommodifications/libraries/trackcustommodifications.py create mode 100644 repos/system_upgrade/common/models/custommodifications.py diff --git a/repos/system_upgrade/common/actors/trackcustommodifications/actor.py b/repos/system_upgrade/common/actors/trackcustommodifications/actor.py new file mode 100644 index 0000000000..8c9832c6f7 --- /dev/null +++ b/repos/system_upgrade/common/actors/trackcustommodifications/actor.py @@ -0,0 +1,19 @@ +from leapp.actors import Actor +from leapp.libraries.actor.trackcustommodifications import check_for_modifications +from leapp.models import CustomModifications +from leapp.tags import FactsPhaseTag, IPUWorkflowTag + + +class TrackCustomModificationsActor(Actor): + """ + Collects information about files in leapp directories that have been modified or newly added. + """ + + name = 'track_custom_modifications_actor' + produces = (CustomModifications,) + tags = (IPUWorkflowTag, FactsPhaseTag) + + def process(self): + changed, custom = check_for_modifications() + for msg in changed + custom: + self.produce(msg) diff --git a/repos/system_upgrade/common/actors/trackcustommodifications/libraries/trackcustommodifications.py b/repos/system_upgrade/common/actors/trackcustommodifications/libraries/trackcustommodifications.py new file mode 100644 index 0000000000..f2cbea7d71 --- /dev/null +++ b/repos/system_upgrade/common/actors/trackcustommodifications/libraries/trackcustommodifications.py @@ -0,0 +1,62 @@ +import os + +from leapp.exceptions import StopActorExecution +from leapp.libraries.common.config.version import get_source_major_version, get_target_major_version +from leapp.libraries.stdlib import api, CalledProcessError, run +from leapp.models import CustomModifications + + +RPMS_TO_CHECK = ['leapp-upgrade-el{source}toel{target}'] +DIRS_TO_SCAN = ['/usr/share/leapp-repository'] + + +def _get_rpms_to_check(): + return [rpm.format(source=get_source_major_version(), target=get_target_major_version()) for rpm in RPMS_TO_CHECK] + + +def check_for_modifications(dirs=DIRS_TO_SCAN): + """ + This will return a tuple (changes, custom) is case any untypical files or changes to shipped leapp files are + discovered. + (None, None) means that no modifications have been found. + """ + rpms = _get_rpms_to_check() + source_of_truth = [] + for rpm in rpms: + get_rpm_files_command = ['rpm', '-ql', rpm] + try: + rpm_files = run(get_rpm_files_command) + source_of_truth.extend(rpm_files['stdout'].strip().split()) + except CalledProcessError: + api.current_logger().warning( + 'Could not get list of installed files from rpm %s'.format(rpm) + ) + raise StopActorExecution() + leapp_files = [] + for directory in dirs: + get_files_command = ['find', directory, '-type', 'f'] + try: + files = run(get_files_command) + leapp_files.extend(files['stdout'].strip().split()) + except CalledProcessError: + api.current_logger().warning( + 'Could not get list of leapp files in %s'.format(directory) + ) + raise StopActorExecution() + custom_files = set(leapp_files) - set(source_of_truth) + # Now let's check for modifications + modified_files = [] + for rpm in rpms: + try: + modified = run(['rpm', '-V', rpm]) + modified_files.extend(modified['stdout'].strip().split()) + except CalledProcessError: + api.current_logger().warning( + 'Could not check authenticity of the files from %s'.format(rpm) + ) + raise StopActorExecution() + # NOTE(ivasilev) Now the fun part TBD - the mapping between actor file / library and actor's + # name + # XXX Leaving blank atm as it's tbd + return ([CustomModifications(actor_name='', filename=f, type='modified') for f in modified_files], + [CustomModifications(actor_name='', filename=f, type='custom') for f in custom_files]) diff --git a/repos/system_upgrade/common/models/custommodifications.py b/repos/system_upgrade/common/models/custommodifications.py new file mode 100644 index 0000000000..0b708dce62 --- /dev/null +++ b/repos/system_upgrade/common/models/custommodifications.py @@ -0,0 +1,11 @@ +from leapp.models import fields, Model +from leapp.topics import SystemFactsTopic + + +class CustomModifications(Model): + """Model to store any custom or modified files that are discovered in leapp directories""" + topic = SystemFactsTopic + + filename = fields.String() + actor_name = fields.String() + type = fields.StringEnum(choices=['custom', 'modified'])