mirror of https://github.com/Aidaho12/haproxy-wi
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.pull/399/head
parent
98fb3fb288
commit
aa4016d40c
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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')
|
||||
|
||||
|
|
Loading…
Reference in New Issue