From aa4016d40c749b0635ccf615f0c068758d627f1f Mon Sep 17 00:00:00 2001 From: Aidaho Date: Mon, 21 Oct 2024 15:52:29 +0300 Subject: [PATCH] v8.1.0.1: Refactor server creation to use kwargs Updated the server creation functions to utilize keyword arguments, simplifying the code and making it more maintainable. Additionally, improved the handling of VIP updates and error logging for HA clusters. --- app/modules/db/ha_cluster.py | 22 ++++++------- app/modules/db/server.py | 8 ++--- app/modules/server/server.py | 7 ++--- app/modules/service/ha_cluster.py | 52 ++++++++++++++++++++++++------- app/static/js/ha.js | 2 +- app/views/server/views.py | 21 ++++++++++--- 6 files changed, 74 insertions(+), 38 deletions(-) diff --git a/app/modules/db/ha_cluster.py b/app/modules/db/ha_cluster.py index 0455c255..423067fb 100644 --- a/app/modules/db/ha_cluster.py +++ b/app/modules/db/ha_cluster.py @@ -249,9 +249,9 @@ def update_ha_cluster_vip(cluster_id: int, router_id: int, vip: str, return_mast out_error(e) -def update_ha_virt_ip(vip_id: int, vip: str) -> None: +def update_ha_virt_ip(vip_id: int, **kwargs) -> None: try: - Server.update(ip=vip).where(Server.server_id == HaClusterVirt.get(HaClusterVirt.vip_id == vip_id).virt_id).execute() + Server.update(**kwargs).where(Server.server_id == HaClusterVirt.get(HaClusterVirt.vip_id == vip_id).virt_id).execute() except Exception as e: out_error(e) @@ -271,6 +271,15 @@ def check_ha_virt(vip_id: int) -> bool: return True +def select_ha_virts(cluster_id: int) -> HaClusterVirt: + try: + return HaClusterVirt.select().where(HaClusterVirt.cluster_id == cluster_id).execute() + except HaClusterVirt.DoesNotExist: + pass + except Exception as e: + out_error(e) + + def select_ha_cluster_name_and_slaves() -> object: try: return HaCluster.select(HaCluster.id, HaCluster.name, HaClusterSlave.server_id).join(HaClusterSlave).execute() @@ -290,12 +299,3 @@ def update_master_server_by_slave_ip(master_id: int, slave_ip: str) -> None: Server.update(master=master_id).where(Server.ip == slave_ip).execute() except Exception as e: out_error(e) - - -def get_cred_id_by_server_ip(server_ip): - try: - cred = Server.get(Server.ip == server_ip) - except Exception as e: - return out_error(e) - else: - return cred.cred_id diff --git a/app/modules/db/server.py b/app/modules/db/server.py index 91f03123..b24abbf0 100644 --- a/app/modules/db/server.py +++ b/app/modules/db/server.py @@ -5,13 +5,9 @@ from app.modules.db.common import out_error, not_unique_error from app.modules.roxywi.exception import RoxywiResourceNotFound -def add_server(hostname, ip, group, type_ip, enable, master, cred, port, desc, haproxy, nginx, apache, firewall): +def add_server(**kwargs): try: - server_id = Server.insert( - hostname=hostname, ip=ip, group_id=group, type_ip=type_ip, enabled=enable, master=master, cred_id=cred, - port=port, description=desc, haproxy=haproxy, nginx=nginx, apache=apache, firewall_enable=firewall - ).execute() - return server_id + return Server.insert(**kwargs).execute() except IntegrityError as e: not_unique_error(e) except Exception as e: diff --git a/app/modules/server/server.py b/app/modules/server/server.py index d87e2cd9..c35e13d4 100644 --- a/app/modules/server/server.py +++ b/app/modules/server/server.py @@ -414,11 +414,8 @@ def show_firewalld_rules(server_ip) -> str: return render_template('ajax/firewall_rules.html', input_chain=input_chain2, IN_public_allow=in_public_allow, output_chain=output_chain, lang=lang) -def create_server(hostname, ip, group, type_ip, enable, master, cred, port, desc, haproxy, nginx, apache, firewall, **kwargs) -> int: - if not roxywi_auth.is_admin(level=2, role_id=kwargs.get('role_id')): - raise Exception('error: not enough permission') - - last_id = server_sql.add_server(hostname, ip, group, type_ip, enable, master, cred, port, desc, haproxy, nginx, apache, firewall) +def create_server(**kwargs) -> int: + last_id = server_sql.add_server(**kwargs) return last_id diff --git a/app/modules/service/ha_cluster.py b/app/modules/service/ha_cluster.py index 81ef46ee..ca76f8e5 100644 --- a/app/modules/service/ha_cluster.py +++ b/app/modules/service/ha_cluster.py @@ -1,5 +1,7 @@ from typing import Union +from matplotlib.artist import kwdoc + 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 @@ -107,6 +109,14 @@ def update_cluster(cluster: HAClusterRequest, cluster_id: int, group_id: int) -> except Exception as e: raise Exception(f'error: Cannot add service {service}: {e}') + virts = ha_sql.select_ha_virts(cluster_id) + if virts: + for virt in virts: + try: + add_or_update_virt(cluster, servers, cluster_id, virt.vip_id, group_id) + except Exception as e: + roxywi_common.logging(cluster_id, f'Cannot update cluster virtual server for VIP {virt.vip}: {e}', roxywi=1, service='HA cluster') + roxywi_common.logging(cluster_id, f'Cluster {cluster.name} has been updated', keep_history=1, roxywi=1, service='HA cluster') @@ -248,30 +258,50 @@ def add_or_update_virt(cluster: Union[HAClusterRequest, HAClusterVIP], servers: if value['master']: master_id = value['id'] + services = ha_sql.select_cluster_services(cluster_id) + for service in services: + haproxy = 1 if service.service_id == '1' else 0 + nginx = 1 if service.service_id == '2' else 0 + apache = 1 if service.service_id == '4' else 0 + + kwargs = { + 'haproxy': haproxy, + 'nginx': nginx, + 'apache': apache, + 'ip': vip, + } + if ha_sql.check_ha_virt(vip_id): + vip_from_db = ha_sql.select_cluster_vip_by_vip_id(cluster_id, vip_id) + vip = vip_from_db.vip + kwargs['ip'] = vip try: - ha_sql.update_ha_virt_ip(vip_id, vip) + ha_sql.update_ha_virt_ip(vip_id, **kwargs) roxywi_common.logging(cluster_id, f'Cluster virtual server for VIP {vip} has been updated', keep_history=1, roxywi=1, service='HA cluster') except Exception as e: roxywi_common.logging(cluster_id, f'Cannot update cluster virtual server for VIP {vip}: {e}', roxywi=1, service='HA cluster') else: - services = ha_sql.select_cluster_services(cluster_id) - for service in services: - haproxy = 1 if service.service_id == '1' else 0 - nginx = 1 if service.service_id == '2' else 0 - apache = 1 if service.service_id == '4' else 0 try: server = server_sql.get_server_by_id(master_id) c = ha_sql.get_cluster(cluster_id) - virt_id = server_sql.add_server( - f'{vip}-VIP', vip, group_id, '1', '1', '0', server.cred_id, server.port, - f'VRRP IP for {c.name} cluster', haproxy, nginx, apache, server.firewall_enable - ) - HaClusterVirt.insert(cluster_id=cluster_id, virt_id=virt_id, vip_id=vip_id).execute() + kwargs.setdefault('cred_id', server.cred_id) + kwargs.setdefault('hostname', f'{vip}-VIP') + kwargs.setdefault('type_ip', 1) + kwargs.setdefault('enabled', 1) + kwargs.setdefault('master', 0) + kwargs.setdefault('port', server.port) + kwargs.setdefault('description', f'VRRP IP for {c.name} cluster') + kwargs.setdefault('firewall_enable', server.firewall_enable) + kwargs.setdefault('group_id', group_id) + virt_id = server_sql.add_server(**kwargs) roxywi_common.logging(cluster_id, f'New cluster virtual server for VIP: {vip} has been created', keep_history=1, roxywi=1, service='HA cluster') except Exception as e: roxywi_common.logging(cluster_id, f'error: Cannot create new cluster virtual server for VIP: {vip}: {e}', roxywi=1, service='HA cluster') + try: + HaClusterVirt.insert(cluster_id=cluster_id, virt_id=virt_id, vip_id=vip_id).execute() + except Exception as e: + roxywi_common.logging(cluster_id, f'error: Cannot save cluster virtual server for VIP: {vip}: {e}', roxywi=1, service='HA cluster') def _create_or_update_master_slaves_servers(cluster_id: int, servers: dict, router_id: int, create: bool = False) -> None: diff --git a/app/static/js/ha.js b/app/static/js/ha.js index 28bcd56f..5f01735d 100644 --- a/app/static/js/ha.js +++ b/app/static/js/ha.js @@ -637,7 +637,7 @@ function saveVip(jsonData, cluster_id, dialog_id, edited, vip_id='', deleted=fal return_master = '1'; } if ($('#vrrp-ip-add-virt_server').is(':checked')) { - virt_server = '1'; + virt_server = true; } if ($('#vrrp-ip-add-use_src').is(':checked')) { use_src = '1'; diff --git a/app/views/server/views.py b/app/views/server/views.py index b2a79b89..db3c9886 100644 --- a/app/views/server/views.py +++ b/app/views/server/views.py @@ -171,11 +171,24 @@ class ServerView(MethodView): """ group = SupportClass.return_group_id(body) + kwargs = { + 'hostname': body.hostname, + 'ip': str(body.ip), + 'group_id': group, + 'type_ip': body.type_ip, + 'enabled': body.enabled, + 'master': body.master, + 'cred_id': body.cred_id, + 'port': body.port, + 'description': body.description, + 'firewall_enable': body.firewall_enable, + 'haproxy': body.haproxy, + 'nginx': body.nginx, + 'apache': body.apache + } + try: - last_id = server_mod.create_server( - body.hostname, str(body.ip), group, body.type_ip, body.enabled, body.master, body.cred_id, body.port, body.description, - body.haproxy, body.nginx, body.apache, body.firewall_enable - ) + last_id = server_mod.create_server(**kwargs) except Exception as e: return roxywi_common.handler_exceptions_for_json_data(e, 'Cannot create a server')