From bf7469cc3fd456a7a1c679c21910f844a55d2713 Mon Sep 17 00:00:00 2001 From: Aidaho Date: Sun, 1 Sep 2024 09:03:53 +0300 Subject: [PATCH] v8.0: Refactor and simplify config upload and restart logic. Consolidate user and server queries, remove redundant code, and streamline logging actions. Enhance function readability and efficiency by eliminating unnecessary variables and try-except blocks. The refactor ensures service consistency and maintains error handling robustness. --- app/modules/config/config.py | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/app/modules/config/config.py b/app/modules/config/config.py index a2707e20..da1ea288 100644 --- a/app/modules/config/config.py +++ b/app/modules/config/config.py @@ -205,12 +205,10 @@ def upload_and_restart(server_ip: str, cfg: str, just_save: str, service: str, * :return: Error message or service title """ - user_id = g.user_params['user_id'] - user = user_sql.get_user_id(user_id) - file_format = config_common.get_file_format(service) + user = user_sql.get_user_id(g.user_params['user_id']) config_path = kwargs.get('config_file_name') - config_date = get_date.return_date('config') server_id = server_sql.select_server_id_by_ip(server_ip=server_ip) + tmp_file = f"{sql.get_setting('tmp_config_path')}/{get_date.return_date('config')}.{config_common.get_file_format(service)}" if config_path and config_path != 'undefined': config_path = _replace_config_path_to_correct(kwargs.get('config_file_name')) @@ -220,8 +218,6 @@ def upload_and_restart(server_ip: str, cfg: str, just_save: str, service: str, * common.check_is_conf(config_path) - tmp_file = f"{sql.get_setting('tmp_config_path')}/{config_date}.{file_format}" - try: os.system(f"dos2unix -q {cfg}") except OSError as e: @@ -232,12 +228,6 @@ def upload_and_restart(server_ip: str, cfg: str, just_save: str, service: str, * except Exception as e: roxywi_common.handle_exceptions(e, 'Roxy-WI server', 'Cannot upload config', login=user.username) - try: - if just_save != 'test': - roxywi_common.logging(server_ip, 'A new config file has been uploaded', login=user.username, keep_history=1, service=service) - except Exception as e: - roxywi_common.logging('Roxy-WI server', str(e), roxywi=1) - # If master then save a version of config in a new way if not kwargs.get('slave') and service != 'waf': _create_config_version(server_id, server_ip, service, config_path, user.username, cfg, kwargs.get('oldcfg'), tmp_file) @@ -252,11 +242,10 @@ def upload_and_restart(server_ip: str, cfg: str, just_save: str, service: str, * except Exception as e: roxywi_common.handle_exceptions(e, 'Roxy-WI server', f'Cannot {just_save} {service}', roxywi=1) - try: - if just_save in ('reload', 'restart'): - roxywi_common.logging(server_ip, f'Service has been {just_save}ed', login=user.username, keep_history=1, service=service) - except Exception as e: - roxywi_common.logging('Roxy-WI server', str(e), roxywi=1) + if just_save in ('reload', 'restart'): + roxywi_common.logging(server_ip, f'Service has been {just_save}ed', login=user.username, keep_history=1, service=service) + if just_save != 'test': + roxywi_common.logging(server_ip, 'A new config file has been uploaded', login=user.username, keep_history=1, service=service) if error.strip() != 'haproxy' and error.strip() != 'nginx': return error.strip() or service.title() @@ -265,7 +254,8 @@ def upload_and_restart(server_ip: str, cfg: str, just_save: str, service: str, * def master_slave_upload_and_restart(server_ip: str, cfg: str, just_save: str, service: str, **kwargs: Any) -> str: """ - This method `master_slave_upload_and_restart` performs the upload and restart operation on a master server and its associated slave servers. It takes the following parameters: + This method `master_slave_upload_and_restart` performs the upload and restart operation on a master server and its + associated slave servers. It takes the following parameters: :param server_ip: The IP address of the server to perform the operation on. :param cfg: The configuration file to upload and restart. @@ -281,7 +271,7 @@ def master_slave_upload_and_restart(server_ip: str, cfg: str, just_save: str, se config_file_name = kwargs.get('config_file_name') old_cfg = kwargs.get('oldcfg') waf = kwargs.get('waf') - server_name = server_sql.get_hostname_by_server_ip(server_ip) + server = server_sql.get_server_by_ip(server_ip) for master in masters: if master[0] is not None: @@ -299,7 +289,7 @@ def master_slave_upload_and_restart(server_ip: str, cfg: str, just_save: str, se except Exception as e: output = f'error: {e}' - output = server_name + ':\n' + output + output = server.hostname + ':\n' + output output = output + slave_output return output @@ -485,7 +475,7 @@ def show_config(server_ip: str, service: str, config_file_name: str, configver: user_id = claims['user_id'] group_id = claims['group'] configs_dir = config_common.get_config_dir(service) - server_id = server_sql.select_server_id_by_ip(server_ip) + server = server_sql.get_server_by_ip(server_ip) try: config_file_name = config_file_name.replace('/', '92') @@ -521,9 +511,9 @@ def show_config(server_ip: str, service: str, config_file_name: str, configver: 'service': service, 'config_file_name': config_file_name, 'is_serv_protected': server_sql.is_serv_protected(server_ip), - 'is_restart': service_sql.select_service_setting(server_id, service, 'restart'), + 'is_restart': service_sql.select_service_setting(server.server_id, service, 'restart'), 'lang': roxywi_common.get_user_lang_for_flask(), - 'hostname': server_sql.get_hostname_by_server_ip(server_ip) + 'hostname': server.hostname } return render_template('ajax/config_show.html', **kwargs)