v8.0: Refactor code and fix various issues across modules

Added return_master setting, removed unnecessary code, and improved exception handling throughout the project. Key updates include enabling return_master by default, simplifying database update methods, handling empty IP exceptions more appropriately, and refining JavaScript validations.
pull/399/head
Aidaho 2024-08-04 17:44:11 +03:00
parent 108773c564
commit 075d2b935b
17 changed files with 73 additions and 124 deletions

View File

@ -213,7 +213,9 @@ def update_cluster(cluster_id: int, name: str, desc: str, syn_flood: int) -> Non
def update_ha_cluster_vip(cluster_id: int, router_id: int, vip: str, return_master: int, use_src: int) -> None:
try:
HaClusterVip.update(vip=vip, return_master=return_master, use_src=use_src).where((HaClusterVip.cluster_id == cluster_id) & (HaClusterVip.router_id == router_id)).execute()
HaClusterVip.update(vip=vip, return_master=return_master, use_src=use_src).where(
(HaClusterVip.cluster_id == cluster_id) & (HaClusterVip.router_id == router_id)
).execute()
except Exception as e:
out_error(e)

View File

@ -171,14 +171,10 @@ def select_service(slug: str) -> object:
def update_keepalived(serv):
query = Server.update(keepalived='1').where(Server.ip == serv)
try:
query.execute()
Server.update(keepalived='1').where(Server.ip == serv).execute()
except Exception as e:
out_error(e)
return False
else:
return True
def select_apache(serv):
@ -190,15 +186,11 @@ def select_apache(serv):
return apache
def update_apache(serv: str) -> bool:
query = Server.update(apache='1').where(Server.ip == serv)
def update_apache(serv: str) -> None:
try:
query.execute()
Server.update(apache='1').where(Server.ip == serv).execute()
except Exception as e:
out_error(e)
return False
else:
return True
def select_nginx(serv):
@ -210,14 +202,11 @@ def select_nginx(serv):
return query_res
def update_nginx(serv: str) -> bool:
query = Server.update(nginx=1).where(Server.ip == serv)
def update_nginx(serv: str) -> None:
try:
query.execute()
return True
Server.update(nginx=1).where(Server.ip == serv).execute()
except Exception as e:
out_error(e)
return False
def select_haproxy(serv):
@ -230,13 +219,10 @@ def select_haproxy(serv):
def update_haproxy(serv):
query = Server.update(haproxy=1).where(Server.ip == serv)
try:
query.execute()
return True
Server.update(haproxy=1).where(Server.ip == serv).execute()
except Exception as e:
out_error(e)
return False
def select_keepalived(serv):

View File

@ -38,9 +38,7 @@ def update_user(user, email, role, user_id, enabled):
def update_user_from_admin_area(user_id, **kwargs):
try:
query = User.update(**kwargs).where(User.user_id == user_id)
print(query)
query.execute()
User.update(**kwargs).where(User.user_id == user_id).execute()
except Exception as e:
out_error(e)

View File

@ -161,7 +161,7 @@ class HAClusterVIP(BaseModel):
return_master: Optional[bool] = 1
virt_server: Optional[bool] = 1
router_id: Optional[int] = None
servers: Dict[int, HAClusterServer]
servers: List[HAClusterServer]
class HAClusterRequest(BaseModel):

View File

@ -108,10 +108,10 @@ def get_remote_files(server_ip: str, config_dir: str, file_format: str):
return config_files
def get_system_info(server_ip: str) -> str:
def get_system_info(server_ip: str) -> None:
server_ip = common.is_ip_or_dns(server_ip)
if server_ip == '':
return 'error: IP cannot be empty'
raise Exception('IP cannot be empty')
server_id = server_sql.select_server_id_by_ip(server_ip)
command = "sudo lshw -quiet -json"
@ -120,7 +120,7 @@ def get_system_info(server_ip: str) -> str:
try:
sys_info_returned = ssh_command(server_ip, command, timeout=5)
except Exception as e:
raise Exception(e)
raise Exception(f'Cannot connect to the server to get system info: {server_ip}: {e}')
if 'not found' in sys_info_returned:
raise Exception(f'You should install lshw on the server {server_ip}. Update System info after installation.')
@ -128,12 +128,12 @@ def get_system_info(server_ip: str) -> str:
try:
os_info = ssh_command(server_ip, command1)
except Exception as e:
raise Exception(e)
raise Exception(f'Cannot connect to the server to get OS info: {server_ip} : {e}')
os_info = os_info.strip()
try:
system_info = json.loads(sys_info_returned)
system_info['id']
_ = system_info['id']
except Exception:
sys_info_returned = json.loads(sys_info_returned)
try:
@ -351,7 +351,7 @@ def get_system_info(server_ip: str) -> str:
try:
server_sql.insert_system_info(server_id, os_info, sys_info, cpu, ram, network, disks)
except Exception as e:
raise f'error: Cannot get system info from server: {e}'
raise e
def show_system_info(server_ip: str, server_id: int) -> str:
@ -359,7 +359,7 @@ def show_system_info(server_ip: str, server_id: int) -> str:
try:
get_system_info(server_ip)
except Exception as e:
return f'{e}'
return f'Cannot get system info: {e}'
try:
system_info = server_sql.select_one_system_info(server_id)
except Exception as e:
@ -422,46 +422,44 @@ def create_server(hostname, ip, group, type_ip, enable, master, cred, port, desc
return last_id
def update_server_after_creating(hostname: str, ip: str, scan_server: int) -> None:
def update_server_after_creating(hostname: str, ip: str) -> None:
try:
checker_sql.insert_new_checker_setting_for_server(ip)
except Exception as e:
roxywi_common.handle_exceptions(e, ip, f'Cannot insert Checker settings for {hostname}', roxywi=1, login=1)
raise Exception(f'Cannot insert Checker settings for {hostname}: {e}')
try:
if scan_server:
nginx_config_path = sql.get_setting('nginx_config_path')
haproxy_config_path = sql.get_setting('haproxy_config_path')
haproxy_dir = sql.get_setting('haproxy_dir')
apache_config_path = sql.get_setting('apache_config_path')
keepalived_config_path = sql.get_setting('keepalived_config_path')
nginx_config_path = sql.get_setting('nginx_config_path')
haproxy_config_path = sql.get_setting('haproxy_config_path')
haproxy_dir = sql.get_setting('haproxy_dir')
apache_config_path = sql.get_setting('apache_config_path')
keepalived_config_path = sql.get_setting('keepalived_config_path')
if is_file_exists(ip, nginx_config_path):
service_sql.update_nginx(ip)
if is_file_exists(ip, nginx_config_path):
service_sql.update_nginx(ip)
if is_file_exists(ip, haproxy_config_path):
service_sql.update_haproxy(ip)
if is_file_exists(ip, haproxy_config_path):
service_sql.update_haproxy(ip)
if is_file_exists(ip, keepalived_config_path):
service_sql.update_keepalived(ip)
if is_file_exists(ip, keepalived_config_path):
service_sql.update_keepalived(ip)
if is_file_exists(ip, apache_config_path):
service_sql.update_apache(ip)
if is_file_exists(ip, apache_config_path):
service_sql.update_apache(ip)
if is_file_exists(ip, haproxy_dir + '/waf/bin/modsecurity'):
waf_sql.insert_waf_metrics_enable(ip, "0")
waf_sql.insert_waf_rules(ip)
if is_file_exists(ip, haproxy_dir + '/waf/bin/modsecurity'):
waf_sql.insert_waf_metrics_enable(ip, "0")
waf_sql.insert_waf_rules(ip)
if is_service_active(ip, 'firewalld'):
server_sql.update_firewall(ip)
if is_service_active(ip, 'firewalld'):
server_sql.update_firewall(ip)
except Exception as e:
roxywi_common.handle_exceptions(e, ip, f'Cannot get scan the server {hostname}', roxywi=1, login=1)
raise Exception(f'Cannot get scan the server {hostname}: {e}')
try:
get_system_info(ip)
except Exception as e:
roxywi_common.handle_exceptions(e, ip, f'Cannot get information from {hostname}', roxywi=1, login=1)
raise Exception(f'Cannot get information from {hostname}: {e}')
def delete_server(server_id: int) -> None:

View File

@ -148,7 +148,7 @@ def update_vip(cluster_id: int, router_id: int, cluster: Union[HAClusterRequest,
servers = _get_servers_dict(cluster)
try:
ha_sql.update_ha_cluster_vip(cluster_id, router_id, cluster.vip, cluster.return_master, cluster.use_src)
ha_sql.update_ha_cluster_vip(cluster_id, router_id, str(cluster.vip), cluster.return_master, cluster.use_src)
except Exception as e:
raise Exception(f'error: Cannot update VIP: {e}')
@ -261,7 +261,7 @@ def add_or_update_virt(cluster: Union[HAClusterRequest, HAClusterVIP], servers:
nginx = 0
apache = 0
master_ip = None
vip = cluster.vip
vip = str(cluster.vip)
for value in servers:
if value['master']:

View File

@ -339,12 +339,12 @@ def install_service(service: str, json_data: Union[str, ServiceInstall]) -> dict
try:
inv, server_ips = generate_functions[service](json_data, service)
except Exception as e:
roxywi_common.handle_exceptions(e, 'Roxy-WI server', f'Cannot generate inv {service}', roxywi=1)
raise Exception(f'Cannot generate inv {service}: {e}')
try:
service_actions_after_install(server_ips, service, json_data)
return run_ansible(inv, server_ips, service)
except Exception as e:
roxywi_common.handle_exceptions(e, 'Roxy-WI server', f'Cannot install {service}', roxywi=1)
raise Exception(f'Cannot install {service}: {e}')
def _install_ansible_collections():

View File

@ -39,13 +39,6 @@ $( function() {
let id = $(this).attr('id').split('-');
updateServer(id[1])
});
$( "#scan_server" ).change(function() {
if ($('#scan_server').is(':checked')) {
$('.services_for_scan').hide();
} else {
$('.services_for_scan').show();
}
});
});
function addServer(dialog_id) {
toastr.clear()
@ -54,17 +47,12 @@ function addServer(dialog_id) {
let ip = $('#new-ip').val();
let server_group = $('#new-server-group-add').val();
let cred = $('#credentials').val();
let scan_server = 0;
let type_ip = 0;
let enable = 0;
let haproxy = 0;
let nginx = 0;
let apache = 0;
let firewall = 0;
let add_to_smon = 0;
if ($('#scan_server').is(':checked')) {
scan_server = '1';
}
if ($('#type_ip').is(':checked')) {
type_ip = '1';
}
@ -83,9 +71,6 @@ function addServer(dialog_id) {
if ($('#firewall').is(':checked')) {
firewall = '1';
}
if ($('#add_to_smon').is(':checked')) {
add_to_smon = '1';
}
let allFields = $([]).add($('#new-server-add')).add($('#new-ip')).add($('#new-port'))
allFields.removeClass("ui-state-error");
valid = valid && checkLength($('#new-server-add'), "Hostname", 1);
@ -113,7 +98,6 @@ function addServer(dialog_id) {
'nginx': nginx,
"apache": apache,
"firewall_enable": firewall,
"add_to_smon": add_to_smon,
"enabled": enable,
"master": $('#slavefor').val(),
"cred_id": cred,

View File

@ -87,7 +87,7 @@ function addCreds(dialog_id) {
let id = data.id;
common_ajax_action_after_success(dialog_id, 'ssh-table-' + id, 'ssh_enable_table', data.data);
$('select:regex(id, credentials)').append('<option value=' + id + '>' + ssh_add_div.val() + '</option>').selectmenu("refresh");
$('select:regex(id, ssh-key-name)').append('<option value=' + ssh_add_div.val() + '_' + group_name + '>' + ssh_add_div.val() + '_' + group_name + '</option>').selectmenu("refresh");
$('select:regex(id, ssh-key-name)').append('<option value=' + id + '>' + ssh_add_div.val() + '_' + group_name + '</option>').selectmenu("refresh");
$("input[type=submit], button").button();
$("input[type=checkbox]").checkboxradio();
$("select").selectmenu();

View File

@ -298,7 +298,7 @@ function saveCluster(jsonData, cluster_id=0, edited=0, reconfigure=0) {
let req_method = 'POST';
let url = api_prefix + '/ha/cluster';
if ($('#virt_server').is(':checked')) {
virt_server = '1';
virt_server = 1;
}
if ($('#return_master').is(':checked')) {
return_master = '1';
@ -776,7 +776,7 @@ function createJsonCluster(div_id) {
}
function createJsonVip(div_id) {
let jsonData = {};
jsonData = {'servers': {}};
jsonData = {'servers': []};
$(div_id).each(function () {
let this_id = $(this).attr('id').split('-')[1];
let eth1 = $('#slave_int-' + this_id).val();
@ -786,9 +786,9 @@ function createJsonVip(div_id) {
let ip = $('#master_int_div-' + this_id).attr('data-ip');
let name = $('#master_int_div-' + this_id).parent().text().replace('\n','').replace('\t','').trim();
if (eth) {
jsonData['servers'][this_id] = {'eth': eth, 'ip': ip, 'name': name, 'master': 1};
jsonData['servers'].push({'id': this_id, 'eth': eth, 'ip': ip, 'name': name, 'master': 1});
} else {
jsonData['servers'][this_id] = {'eth': eth1, 'ip': ip1, 'name': name1, 'master': 0};
jsonData['servers'].push({'id': this_id,'eth': eth1, 'ip': ip1, 'name': name1, 'master': 0});
}
});
@ -819,7 +819,7 @@ function clearClusterDialog(edited=0) {
$('#vrrp-ip-edit').val('');
$('#cur_master_ver').text('');
$('#virt_server').prop('checked', true);
$('#return_master').prop('checked', false);
$('#return_master').prop('checked', true);
$('#use_src').prop('checked', false);
$('#hap').prop('checked', false);
$('#hap_docker').prop('checked', false);

View File

@ -111,8 +111,7 @@ $( function() {
if (data.status === 'failed') {
toastr.error(data.error);
} else {
// parseAnsibleJsonOutput(data, service + ' GeoIP', '#geoip_service');
toastr.success('GeoIP has been installed');
parseAnsibleJsonOutput(data, service + ' GeoIP', '#geoip_service');
$("#geoip_service").trigger("selectmenuchange");
}
}
@ -183,8 +182,8 @@ function installService(service) {
if (data.status === 'failed') {
toastr.error(data.error);
} else {
parseAnsibleJsonOutput(data, nice_names[service], select_id);
$(select_id).trigger("selectmenuchange");
toastr.success(nice_service_name[service] + ' has been installed');
$("#ajax").empty();
}
}
@ -260,7 +259,6 @@ function showServiceVersion(service) {
ver_div.text(service + ' has not installed');
install_div.text('Install');
install_div.attr('title', 'Install');
toastr.warning('Cannot get version');
} else {
ver_div.text(data.Version);
ver_div.css('font-weight', 'bold');

View File

@ -26,7 +26,7 @@
</td>
<td>
{% for r in roles %}
{% if r.role_id == USER.role|int() %}
{% if r.role_id == USER.role_id|int() %}
{{ r.name }}
{% endif %}
{% endfor %}
@ -58,7 +58,7 @@
</td>
<td>
{% for r in roles %}
{% if r.role_id == USER.role|int() %}
{% if r.role_id == USER.role_id|int() %}
{{ r.name }}
{% endif %}
{% endfor %}

View File

@ -26,30 +26,6 @@
<td class="padding20" title="{{lang.words.virtual|title()}} IP(VRRP)">{{lang.words.virt|title()}}</td>
<td>{{ checkbox('type_ip') }}</td>
</tr>
<tr>
<td class="padding20 help_cursor" title="{{lang.phrases.add_to_smon_desc}}">{{lang.words.add|title()}} {{lang.words.in}} SMON</td>
<td>{{ checkbox('add_to_smon', checked='checked') }}</td>
</tr>
<tr>
<td class="padding20 help_cursor" title="{{lang.phrases.scan_title}}">{{lang.words.scan|title()}} {{lang.words.the}} {{lang.words.server}}</td>
<td>{{ checkbox('scan_server', checked='checked') }}</td>
</tr>
<tr class="services_for_scan" style="display: none">
<td class="padding20" title="{{lang.words.is_there}} HAProxy?">HAProxy</td>
<td>{{ checkbox('haproxy') }} </td>
</tr>
<tr class="services_for_scan" style="display: none">
<td class="padding20" title="{{lang.words.is_there}} NGINX?">NGINX</td>
<td>{{ checkbox('nginx') }}</td>
</tr>
<tr class="services_for_scan" style="display: none">
<td class="padding20" title="{{lang.words.is_there}} Apache?">Apache</td>
<td>{{ checkbox('apache') }}</td>
</tr>
<tr class="services_for_scan" style="display: none">
<td class="padding20" title="{{lang.words.is_there}} Firewall?">Firewall</td>
<td>{{ checkbox('firewall') }}</td>
</tr>
<tr>
<td class="padding20 help_cursor" title="{{lang.phrases.slave_for_title}}">{{lang.words.slave_for}}</td>
<td>

View File

@ -95,6 +95,7 @@ class HAView(MethodView):
settings.setdefault('vip', vip.vip)
settings.setdefault('virt_server', is_virt)
settings.setdefault('use_src', vip.use_src)
settings.setdefault('return_master', vip.return_master)
for slave in slaves:
if slave[31]:

View File

@ -80,13 +80,16 @@ class InstallView(MethodView):
return roxywi_common.handler_exceptions_for_json_data(e, '')
try:
output = service_mod.install_service(service, body)
if len(output['failures']) > 0 or len(output['dark']) > 0:
raise Exception(f'Cannot install {service.title()}. Check Apache error log')
if 'api' in request.url:
try:
service_sql.update_hapwi_server(server_id, body.checker, body.metrics, body.auto_start, service)
except Exception as e:
return roxywi_common.handler_exceptions_for_json_data(e, f'Cannot update Tools settings for {service.title()}')
return BaseResponse().model_dump(mode='json'), 201
except Exception as e:
return roxywi_common.handler_exceptions_for_json_data(e, f'Cannot install {service.title()}')
if 'api' in request.url:
try:
service_sql.update_hapwi_server(server_id, body.checker, body.metrics, body.auto_start, service)
if len(output['failures']) > 0 or len(output['dark']) > 0:
raise Exception(f'Cannot install {service.title()}. Check Apache error log')
except Exception as e:
return roxywi_common.handler_exceptions_for_json_data(e, f'Cannot update Tools settings for {service.title()}')
else:
return output
return BaseResponse().model_dump(mode='json'), 201

View File

@ -166,7 +166,7 @@ class ServerView(MethodView):
try:
last_id = server_mod.create_server(
body.hostname, body.ip, group, body.type_ip, body.enabled, body.master, body.cred_id, body.port, body.description,
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
)
except Exception as e:
@ -174,7 +174,7 @@ class ServerView(MethodView):
roxywi_common.logging(body.ip, f'A new server {body.hostname} has been created', login=1, keep_history=1, service='server')
try:
server_mod.update_server_after_creating(body.hostname, body.ip, body.scan_server)
server_mod.update_server_after_creating(body.hostname, str(body.ip))
except Exception as e:
roxywi_common.logging(body.ip, f'Cannot get system info from {body.hostname}: {e}', login=1, keep_history=1, service='server', mes_type='error')
@ -183,7 +183,7 @@ class ServerView(MethodView):
else:
try:
user_subscription = roxywi_common.return_user_status()
except Exception as e:
except Exception:
user_subscription = roxywi_common.return_unsubscribed_user_status()
lang = roxywi_common.get_user_lang_for_flask()
kwargs = {

View File

@ -127,7 +127,10 @@ class ServiceView(MethodView):
data = ErrorResponse(error=str(e)).model_dump(mode='json')
elif service == 'keepalived':
cmd = "sudo /usr/sbin/keepalived -v 2>&1|head -1|awk '{print $2}'"
version = server_mod.ssh_command(server.ip, cmd)
try:
version = server_mod.ssh_command(server.ip, cmd)
except Exception as e:
return roxywi_common.handler_exceptions_for_json_data(e, 'Cannot get version')
if version == '/usr/sbin/keepalived:\r\n':
return ErrorResponse(error='Cannot get keepalived').model_dump(mode='json')
data = {'Version': version}