From 666fc39e1d8c2b2ea5798f8403fd40af983040c3 Mon Sep 17 00:00:00 2001 From: Aidaho Date: Mon, 4 Mar 2024 09:34:24 +0300 Subject: [PATCH] v7.2.1.0 SQL fixes --- app/modules/config/config.py | 6 +++--- app/modules/db/channel.py | 7 ------- app/modules/db/config.py | 5 ++--- app/modules/db/history.py | 27 +++++++++++++++++++++++++++ app/modules/db/smon.py | 1 + app/modules/service/action.py | 2 +- app/modules/service/ha_cluster.py | 1 - app/routes/add/routes.py | 4 ++-- app/routes/ha/routes.py | 3 +-- app/routes/portscanner/routes.py | 1 - app/routes/runtime/routes.py | 4 ++-- app/routes/server/routes.py | 1 - 12 files changed, 39 insertions(+), 23 deletions(-) diff --git a/app/modules/config/config.py b/app/modules/config/config.py index e85a195b..41b37019 100644 --- a/app/modules/config/config.py +++ b/app/modules/config/config.py @@ -34,7 +34,7 @@ def _replace_config_path_to_correct(config_path: str) -> str: try: return config_path.replace('92', '/') except Exception as e: - roxywi_common.handle_exceptions(e, 'Roxy-WI server', f'Cannot sanitize config file', roxywi=1) + roxywi_common.handle_exceptions(e, 'Roxy-WI server', 'Cannot sanitize config file', roxywi=1) def get_config(server_ip, cfg, service='haproxy', **kwargs): @@ -136,7 +136,7 @@ def _generate_command(service: str, server_id: int, just_save: str, config_path: reload_or_restart_command = '' else: if service_common.is_not_allowed_to_restart(server_id, service, just_save): - raise Exception(f'error: This server is not allowed to be restarted') + raise Exception('error: This server is not allowed to be restarted') if service == 'waf': commands = f'{move_config} {reload_or_restart_command}' @@ -227,7 +227,7 @@ def upload_and_restart(server_ip: str, cfg: str, just_save: str, service: str, * try: upload(server_ip, tmp_file, cfg) except Exception as e: - roxywi_common.handle_exceptions(e, 'Roxy-WI server', f'Cannot upload config', login=login) + roxywi_common.handle_exceptions(e, 'Roxy-WI server', 'Cannot upload config', login=login) try: if just_save != 'test': diff --git a/app/modules/db/channel.py b/app/modules/db/channel.py index f0a4aa4d..450a280e 100644 --- a/app/modules/db/channel.py +++ b/app/modules/db/channel.py @@ -51,13 +51,6 @@ def get_user_pd_by_group(group): out_error(e) -def get_user_pd_by_group(group): - try: - return PD.select().where(PD.groups == group).execute() - except Exception as e: - out_error(e) - - def get_pd_by_ip(ip): query = PD.select().join(Server, on=(Server.groups == PD.groups)).where(Server.ip == ip) try: diff --git a/app/modules/db/config.py b/app/modules/db/config.py index 95a5f416..d25ed1b0 100644 --- a/app/modules/db/config.py +++ b/app/modules/db/config.py @@ -6,8 +6,7 @@ import app.modules.roxy_wi_tools as roxy_wi_tools def insert_config_version(server_id: int, user_id: int, service: str, local_path: str, remote_path: str, diff: str): - time_zone = get_setting('time_zone') - get_date = roxy_wi_tools.GetDate(time_zone) + get_date = roxy_wi_tools.GetDate(get_setting('time_zone')) cur_date = get_date.return_date('regular') try: ConfigVersion.insert( @@ -62,4 +61,4 @@ def select_remote_path_from_version(server_ip: str, service: str, local_path: st except Exception as e: out_error(e) else: - return query_res \ No newline at end of file + return query_res diff --git a/app/modules/db/history.py b/app/modules/db/history.py index 81712b4b..ed31fe14 100644 --- a/app/modules/db/history.py +++ b/app/modules/db/history.py @@ -90,3 +90,30 @@ def delete_action_history_for_period(): query.execute() except Exception as e: out_error(e) + + +def select_action_history_by_server_id(server_id: int): + try: + return ActionHistory.select().where(ActionHistory.server_id == server_id).execute() + except Exception as e: + out_error(e) + + +def select_action_history_by_user_id(user_id: int): + try: + return ActionHistory.select().where(ActionHistory.user_id == user_id).execute() + except Exception as e: + out_error(e) + + +def select_action_history_by_server_id_and_service(server_id: int, service: str): + query = ActionHistory.select().where( + (ActionHistory.server_id == server_id) + & (ActionHistory.service == service) + ) + try: + query_res = query.execute() + except Exception as e: + out_error(e) + else: + return query_res diff --git a/app/modules/db/smon.py b/app/modules/db/smon.py index 6e0dddc0..636571a5 100644 --- a/app/modules/db/smon.py +++ b/app/modules/db/smon.py @@ -645,6 +645,7 @@ def count_agents() -> int: def delete_smon_history(): + get_date = roxy_wi_tools.GetDate(get_setting('time_zone')) cur_date = get_date.return_date('regular', timedelta_minus=1) query = SmonHistory.delete().where(SmonHistory.date < cur_date) try: diff --git a/app/modules/service/action.py b/app/modules/service/action.py index 7e0aa127..11255466 100644 --- a/app/modules/service/action.py +++ b/app/modules/service/action.py @@ -39,7 +39,7 @@ def service_action(server_ip: str, action: str, service: str) -> str: server_id = server_sql.select_server_id_by_ip(server_ip=server_ip) if service_common.is_not_allowed_to_restart(server_id, service, action): - return f'error: This server is not allowed to be restarted' + return 'error: This server is not allowed to be restarted' try: if service != 'keepalived': diff --git a/app/modules/service/ha_cluster.py b/app/modules/service/ha_cluster.py index 967a0a78..d3795566 100644 --- a/app/modules/service/ha_cluster.py +++ b/app/modules/service/ha_cluster.py @@ -1,6 +1,5 @@ import json -import app.modules.db.sql as sql import app.modules.db.server as server_sql import app.modules.db.ha_cluster as ha_sql import app.modules.db.service as service_sql diff --git a/app/routes/add/routes.py b/app/routes/add/routes.py index 54ef0ad3..8c5cb5da 100644 --- a/app/routes/add/routes.py +++ b/app/routes/add/routes.py @@ -6,7 +6,7 @@ from flask_login import login_required from app.routes.add import bp import app.modules.db.sql as sql import app.modules.db.add as add_sql -from middleware import check_services, get_user_params +from app.middleware import check_services, get_user_params import app.modules.config.add as add_mod import app.modules.common.common as common import app.modules.roxywi.auth as roxywi_auth @@ -128,7 +128,7 @@ def add_haproxy(): else: return 'error: The name cannot be empty' - if request.form.get('backends') != '': + if request.form.get('backends') != '' and request.form.get('backends'): backend = f" default_backend {request.form.get('backends')}\n" if request.form.get('maxconn'): diff --git a/app/routes/ha/routes.py b/app/routes/ha/routes.py index d9e245c6..1c460fab 100644 --- a/app/routes/ha/routes.py +++ b/app/routes/ha/routes.py @@ -4,8 +4,7 @@ from flask import render_template, g, request, jsonify from flask_login import login_required from app.routes.ha import bp -from middleware import get_user_params, check_services -import app.modules.db.sql as sql +from app.middleware import get_user_params, check_services import app.modules.db.ha_cluster as ha_sql import app.modules.db.server as server_sql import app.modules.db.service as service_sql diff --git a/app/routes/portscanner/routes.py b/app/routes/portscanner/routes.py index f65c0901..bedbf88a 100644 --- a/app/routes/portscanner/routes.py +++ b/app/routes/portscanner/routes.py @@ -3,7 +3,6 @@ from flask_login import login_required from app.routes.portscanner import bp from app.middleware import get_user_params -import app.modules.db.sql as sql import app.modules.db.server as server_sql import app.modules.db.portscanner as ps_sql import app.modules.common.common as common diff --git a/app/routes/runtime/routes.py b/app/routes/runtime/routes.py index 1d457894..ba938bb8 100644 --- a/app/routes/runtime/routes.py +++ b/app/routes/runtime/routes.py @@ -2,7 +2,7 @@ from flask import render_template, request, g from flask_login import login_required from app.routes.runtime import bp -from middleware import get_user_params +from app.middleware import get_user_params import app.modules.common.common as common import app.modules.config.runtime as runtime import app.modules.service.haproxy as service_haproxy @@ -11,7 +11,7 @@ import app.modules.service.haproxy as service_haproxy @bp.before_request @login_required def before_request(): - """ Protect all of the admin endpoints. """ + """ Protect all the admin endpoints. """ pass diff --git a/app/routes/server/routes.py b/app/routes/server/routes.py index 28445bd7..f52b5c31 100644 --- a/app/routes/server/routes.py +++ b/app/routes/server/routes.py @@ -4,7 +4,6 @@ from flask import render_template, request from flask_login import login_required from app.routes.server import bp -import app.modules.db.sql as sql import app.modules.db.cred as cred_sql import app.modules.db.group as group_sql import app.modules.db.server as server_sql