From bfcead643034d9504fee04195b7b94d80edd0164 Mon Sep 17 00:00:00 2001 From: Aidaho Date: Tue, 23 Jan 2024 09:49:47 +0300 Subject: [PATCH] v7.1.1.0 Fix interface bugs --- app/modules/common/common.py | 2 +- app/modules/config/add.py | 4 +- app/modules/roxywi/overview.py | 10 +- app/modules/roxywi/user.py | 10 +- app/modules/service/common.py | 23 ++-- app/modules/service/ha_cluster.py | 10 +- app/modules/tools/common.py | 2 +- app/routes/main/routes.py | 3 + app/templates/add.html | 6 +- app/templates/add_nginx.html | 2 +- app/templates/ajax/ha/clusters.html | 2 +- app/templates/ajax/overview.html | 2 +- inc/add.js | 158 +++++++++++++--------------- 13 files changed, 108 insertions(+), 126 deletions(-) diff --git a/app/modules/common/common.py b/app/modules/common/common.py index 4b13850f..012253e0 100644 --- a/app/modules/common/common.py +++ b/app/modules/common/common.py @@ -67,7 +67,7 @@ def check_is_conf(config_path: str) -> bool: if 'conf' in config_path or 'cfg' in config_path: return True - return False + raise Exception('error: nice try') def string_to_dict(dict_string) -> dict: diff --git a/app/modules/config/add.py b/app/modules/config/add.py index 66f7bf7f..06c3473a 100644 --- a/app/modules/config/add.py +++ b/app/modules/config/add.py @@ -305,7 +305,7 @@ def create_map(server_ip: str, map_name: str, group: str) -> str: try: server_mod.subprocess_execute(f'mkdir -p {map_path}') except Exception as e: - assert Exception(f'error: cannot create a local folder for maps: {e}') + raise Exception(f'error: cannot create a local folder for maps: {e}') try: open(full_path, 'a').close() try: @@ -313,7 +313,7 @@ def create_map(server_ip: str, map_name: str, group: str) -> str: except Exception: pass except IOError as e: - assert Exception(f'error: Cannot create a new {map_name} map. {e}, ') + raise Exception(f'error: Cannot create a new {map_name} map. {e}, ') else: return 'success: ' diff --git a/app/modules/roxywi/overview.py b/app/modules/roxywi/overview.py index e41f7d2d..6094dc58 100644 --- a/app/modules/roxywi/overview.py +++ b/app/modules/roxywi/overview.py @@ -10,7 +10,7 @@ import modules.server.server as server_mod import modules.service.common as service_common -def user_owv(): +def user_owv() -> str: lang = roxywi_common.get_user_lang_for_flask() roles = sql.select_roles() user_params = roxywi_common.get_users_params() @@ -25,7 +25,7 @@ def user_owv(): return render_template('ajax/show_users_ovw.html', users=users, users_groups=users_groups, lang=lang, roles=roles) -def show_sub_ovw() -> None: +def show_sub_ovw() -> str: lang = roxywi_common.get_user_lang_for_flask() return render_template('ajax/show_sub_ovw.html', sub=sql.select_user_all(), lang=lang) @@ -104,7 +104,7 @@ def show_overview(serv) -> str: return render_template('ajax/overview.html', service_status=servers_sorted, role=role, lang=lang) -def show_haproxy_binout(server_ip: str) -> None: +def show_haproxy_binout(server_ip: str) -> str: port = sql.get_setting('haproxy_sock_port') bin_bout = [] cmd = "echo 'show stat' |nc {} {} |cut -d ',' -f 1-2,9|grep -E '[0-9]'|awk -F',' '{{sum+=$3;}}END{{print sum;}}'".format( @@ -135,7 +135,7 @@ def show_nginx_connections(server_ip: str) -> str: page = sql.get_setting('nginx_stats_page') url = f'http://{server_ip}:{port}/{page}' - r = requests.get(url, auth=(user, password)) + r = requests.get(url, auth=(user, password), timeout=5) if r.status_code == 200: bin_bout = [0, 0] @@ -159,7 +159,7 @@ def show_apache_bytes(server_ip: str) -> str: bin_bout = [] url = f'http://{server_ip}:{port}/{page}?auto' - r = requests.get(url, auth=(user, password)) + r = requests.get(url, auth=(user, password), timeout=5) if r.status_code == 200: for line in r.text.split('\n'): diff --git a/app/modules/roxywi/user.py b/app/modules/roxywi/user.py index 386f8eff..ac648ca2 100644 --- a/app/modules/roxywi/user.py +++ b/app/modules/roxywi/user.py @@ -71,7 +71,7 @@ def update_user_password(password, uuid, user_id_from_get): return 'ok' -def get_user_services(user_id: int) -> None: +def get_user_services(user_id: int) -> str: lang = roxywi_common.get_user_lang_for_flask() services = sql.select_services() @@ -83,8 +83,8 @@ def get_user_services(user_id: int) -> None: def change_user_services(user: str, user_id: int, user_services: str) -> str: services = '' - for k, v in user_services.items(): - for k2, v2 in v.items(): + for _k, v in user_services.items(): + for k2, _v2 in v.items(): services += ' ' + k2 try: @@ -112,7 +112,7 @@ def get_user_active_group(uuid: str, group: str) -> str: return render_template('ajax/user_current_group.html', groups=groups, group=group, id=group_id, lang=lang) -def show_user_groups_and_roles(user_id: int, lang: str) -> None: +def show_user_groups_and_roles(user_id: int, lang: str) -> str: groups = sql.select_user_groups_with_names(user_id, user_not_in_group=1) roles = sql.select_roles() user_groups = sql.select_user_groups_with_names(user_id) @@ -126,7 +126,7 @@ def is_current_user(user_id: int, user_uuid: str) -> bool: return False -def save_user_group_and_role(user: str, groups_and_roles: str, user_uuid: str) -> str: +def save_user_group_and_role(user: str, groups_and_roles: dict, user_uuid: str) -> str: resp = make_response('ok') for k, v in groups_and_roles.items(): user_id = int(k) diff --git a/app/modules/service/common.py b/app/modules/service/common.py index 4c1b3f87..2c8c7368 100644 --- a/app/modules/service/common.py +++ b/app/modules/service/common.py @@ -50,7 +50,7 @@ def get_exp_version(server_ip: str, service_name: str) -> str: if service_name == 'haproxy': commands = ["/opt/prometheus/exporters/haproxy_exporter --version 2>&1 |head -1|awk '{print $3}'"] elif service_name == 'nginx': - commands = ["/opt/prometheus/exporters/nginx_exporter 2>&1 |head -1 |awk -F\"=\" '{print $2}'|awk '{print $1}'"] + commands = ["/opt/prometheus/exporters/nginx_exporter --version 2>&1 |head -1 |awk -F\"version\" '{print $2}'|awk '{print $1}'"] elif service_name == 'node': commands = ["node_exporter --version 2>&1 |head -1|awk '{print $3}'"] elif service_name == 'apache': @@ -119,7 +119,7 @@ def check_haproxy_config(server_ip): print(f'error: {e}') -def check_nginx_config(server_ip): +def check_nginx_config(server_ip) -> bool: commands = [f"nginx -q -t -p {sql.get_setting('nginx_dir')}"] with mod_ssh.ssh_connect(server_ip) as ssh: @@ -131,7 +131,7 @@ def check_nginx_config(server_ip): return False -def overview_backends(server_ip: str, service: str) -> None: +def overview_backends(server_ip: str, service: str) -> str: import modules.config.config as config_mod lang = roxywi_common.get_user_lang_for_flask() @@ -188,18 +188,13 @@ def get_overview_last_edit(server_ip: str, service: str) -> str: def get_stat_page(server_ip: str, service: str) -> str: - if service in ('nginx', 'apache'): - stats_user = sql.get_setting(f'{service}_stats_user') - stats_pass = sql.get_setting(f'{service}_stats_password') - stats_port = sql.get_setting(f'{service}_stats_port') - stats_page = sql.get_setting(f'{service}_stats_page') - else: - stats_user = sql.get_setting('stats_user') - stats_pass = sql.get_setting('stats_password') - stats_port = sql.get_setting('stats_port') - stats_page = sql.get_setting('stats_page') + stats_user = sql.get_setting(f'{service}_stats_user') + stats_pass = sql.get_setting(f'{service}_stats_password') + stats_port = sql.get_setting(f'{service}_stats_port') + stats_page = sql.get_setting(f'{service}_stats_page') + try: - response = requests.get(f'http://{server_ip}:{stats_port}/{stats_page}', auth=(stats_user, stats_pass)) + response = requests.get(f'http://{server_ip}:{stats_port}/{stats_page}', auth=(stats_user, stats_pass), timeout=5) except requests.exceptions.ConnectTimeout: return 'error: Oops. Connection timeout occurred!' except requests.exceptions.ReadTimeout: diff --git a/app/modules/service/ha_cluster.py b/app/modules/service/ha_cluster.py index 891475ff..e11b0ade 100644 --- a/app/modules/service/ha_cluster.py +++ b/app/modules/service/ha_cluster.py @@ -31,11 +31,11 @@ def create_cluster(cluster: object, group_id: int) -> str: except Exception as e: return f'error: Cannon add VIP: {e}' - for slave_id, value in cluster['servers'].items(): + for _slave_id, value in cluster['servers'].items(): if value['master']: master_ip = value['ip'] - for slave_id, value in cluster['servers'].items(): + for _slave_id, value in cluster['servers'].items(): if value['master']: continue try: @@ -194,7 +194,7 @@ def update_slaves(json_data: object, router_id: int) -> None: server_ids = [] server_ids_from_json = [] - for slave_id, value in cluster['servers'].items(): + for _slave_id, value in cluster['servers'].items(): if value['master']: master_ip = common.is_ip_or_dns(value['ip']) @@ -226,7 +226,7 @@ def update_slaves(json_data: object, router_id: int) -> None: except Exception as e: raise Exception(f'error: Cannot recreate slaves server: {e}') - for slave_id, value in cluster['servers'].items(): + for _slave_id, value in cluster['servers'].items(): if value['master']: continue try: @@ -251,7 +251,7 @@ def add_or_update_virt(cluster: object, cluster_id: int, vip_id: int, group_id: vip = common.is_ip_or_dns(cluster['vip']) cluster_name = common.checkAjaxInput(cluster['name']) - for slave_id, value in cluster['servers'].items(): + for _slave_id, value in cluster['servers'].items(): if value['master']: master_ip = common.is_ip_or_dns(value['ip']) diff --git a/app/modules/tools/common.py b/app/modules/tools/common.py index a2b8d797..b5cc617f 100644 --- a/app/modules/tools/common.py +++ b/app/modules/tools/common.py @@ -64,7 +64,7 @@ def is_tool_active(tool_name: str) -> str: def update_cur_tool_versions() -> None: tools = sql.get_all_tools() - for s, v in tools.items(): + for s, _v in tools.items(): update_cur_tool_version(s) diff --git a/app/routes/main/routes.py b/app/routes/main/routes.py index becb9d5a..be0fc6e0 100644 --- a/app/routes/main/routes.py +++ b/app/routes/main/routes.py @@ -137,6 +137,7 @@ def nettols_check(check): @login_required @get_user_params() def service_history(service, server_ip): + history = '' server_ip = common.checkAjaxInput(server_ip) if service in ('haproxy', 'nginx', 'keepalived', 'apache', 'cluster'): @@ -154,6 +155,8 @@ def service_history(service, server_ip): history = sql.select_action_history_by_server_id(server_id) elif service == 'user': history = sql.select_action_history_by_user_id(server_ip) + else: + abort(404, f'History not found') kwargs = { 'user_params': g.user_params, diff --git a/app/templates/add.html b/app/templates/add.html index 0bb44ee3..5c76e3b4 100644 --- a/app/templates/add.html +++ b/app/templates/add.html @@ -784,7 +784,7 @@ {% for option in options %} - {% if option.groups == group or group == '1' %} + {% if option.groups|string() == user_params['group_id']|string() or group|string() == '1' %} {{ option.id }} @@ -833,7 +833,7 @@ {% for s in saved_servers %} - {% if s.groups == group or group == '1' %} + {% if s.groups|string() == user_params['group_id']|string() or group|string() == '1' %} @@ -1115,7 +1115,7 @@ - +