From ea722cee9408fbccf6d23211e1b7287e9ff10670 Mon Sep 17 00:00:00 2001 From: Michal Hecko Date: Sun, 3 Nov 2024 22:45:52 +0100 Subject: [PATCH] commands: add library function for easier loading of cfgs --- commands/command_utils.py | 32 +++++++++++++++++++++++++++++++- commands/preupgrade/__init__.py | 6 +----- commands/upgrade/__init__.py | 11 +---------- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/commands/command_utils.py b/commands/command_utils.py index 4f6f99eb09..f6535779a2 100644 --- a/commands/command_utils.py +++ b/commands/command_utils.py @@ -1,9 +1,11 @@ import json +import hashlib import os import re +from leapp.actors import config as actor_config from leapp.exceptions import CommandError -from leapp.utils import path +from leapp.utils import audit, path HANA_BASE_PATH = '/hana/shared' HANA_SAPCONTROL_PATH_X86_64 = 'exe/linuxx86_64/hdb/sapcontrol' @@ -140,3 +142,31 @@ def vet_upgrade_path(args): flavor=flavor, choices=','.join(supported_target_versions))) return (target_release, flavor) + + +def load_actor_configs_and_store_it_in_db(context, repositories, framework_cfg): + """ + Load actor configuration so that actor's can access it and store it into leapp db. + + :param context: Current execution context + :param repositories: Discovered repositories + :param framework_cfg: Leapp's configuration + """ + # Read the Actor Config and validate it against the schemas saved in the + # configuration. + + actor_config_schemas = tuple(actor.config_schemas for actor in repositories.actors) + actor_config_schemas = actor_config.normalize_schemas(actor_config_schemas) + actor_config_path = framework_cfg.get('actor_config', 'path') + + # Note: actor_config.load() stores the loaded actor config into a global + # variable which can then be accessed by functions in that file. Is this + # the right way to store that information? + actor_cfg = actor_config.load(actor_config_path, actor_config_schemas) + + # Dump the collected configuration, checksum it and store it inside the DB + config_text = json.dumps(actor_cfg) + config_text_hash = hashlib.sha256(config_text.encode('utf-8')).hexdigest() + config_data = audit.ActorConfigData(config=config_text, hash_id=config_text_hash) + db_config = audit.ActorConfig(config=config_data, context=context) + db_config.store() diff --git a/commands/preupgrade/__init__.py b/commands/preupgrade/__init__.py index cb07d1d96e..7dd91a04ee 100644 --- a/commands/preupgrade/__init__.py +++ b/commands/preupgrade/__init__.py @@ -62,11 +62,7 @@ def preupgrade(args, breadcrumbs): workflow = repositories.lookup_workflow('IPUWorkflow')() - # Read the Actor Config and validate it against the schemas saved in the configuration. - actor_config_schemas = tuple(actor.config_schemas for actor in repositories.actors) - actor_config_schemas = actor_config.normalize_schemas(actor_config_schemas) - actor_config_path = cfg.get('actor_config', 'path') - actor_config.load(actor_config_path, actor_config_schemas) + command_utils.load_actor_configs_and_store_it_in_db(context, repositories, cfg) util.warn_if_unsupported(configuration) util.process_whitelist_experimental(repositories, workflow, configuration, logger) diff --git a/commands/upgrade/__init__.py b/commands/upgrade/__init__.py index 8dbeda0a12..02f1ca2aaf 100644 --- a/commands/upgrade/__init__.py +++ b/commands/upgrade/__init__.py @@ -2,7 +2,6 @@ import sys import uuid -from leapp.actors import config as actor_config from leapp.cli.commands import command_utils from leapp.cli.commands.config import get_config from leapp.cli.commands.upgrade import breadcrumbs, util @@ -92,15 +91,7 @@ def upgrade(args, breadcrumbs): raise CommandError(exc.message) workflow = repositories.lookup_workflow('IPUWorkflow')(auto_reboot=args.reboot) - # Read the Actor Config and validate it against the schemas saved in the - # configuration. - actor_config_schemas = tuple(actor.config_schemas for actor in repositories.actors) - actor_config_schemas = actor_config.normalize_schemas(actor_config_schemas) - actor_config_path = cfg.get('actor_config', 'path') - # Note: actor_config.load() stores the loaded actor config into a global - # variable which can then be accessed by functions in that file. Is this - # the right way to store that information? - actor_config.load(actor_config_path, actor_config_schemas) + command_utils.load_actor_configs_and_store_it_in_db(context, repositories, cfg) util.process_whitelist_experimental(repositories, workflow, configuration, logger) util.warn_if_unsupported(configuration)