diff --git a/app/modules/config/config.py b/app/modules/config/config.py index 0676914..05c2b21 100644 --- a/app/modules/config/config.py +++ b/app/modules/config/config.py @@ -3,8 +3,8 @@ import re import http.cookies import modules.db.sql as sql +import modules.server.ssh as mod_ssh import modules.common.common as common -import modules.server.server as server_mod import modules.roxywi.common as roxywi_common import modules.roxy_wi_tools as roxy_wi_tools from modules.service.common import is_not_allowed_to_restart, get_correct_apache_service_name @@ -34,7 +34,7 @@ def get_config(server_ip, cfg, **kwargs): config_path = sql.get_setting('haproxy_config_path') try: - with server_mod.ssh_connect(server_ip) as ssh: + with mod_ssh.ssh_connect(server_ip) as ssh: ssh.get_sftp(config_path, cfg) except Exception as e: roxywi_common.logging('Roxy-WI server', f'error: cannot get config: {e}', roxywi=1) @@ -46,7 +46,7 @@ def upload(server_ip, path, file, **kwargs): full_path = path try: - with server_mod.ssh_connect(server_ip) as ssh: + with mod_ssh.ssh_connect(server_ip) as ssh: ssh.put_sftp(file, full_path) except Exception as e: error = str(e.args) @@ -219,7 +219,7 @@ def upload_and_restart(server_ip: str, cfg: str, **kwargs): return error try: - error = server_mod.ssh_command(server_ip, commands) + error = mod_ssh.ssh_command(server_ip, commands) try: if action == 'reload' or action == 'restart': roxywi_common.logging(server_ip, f'Service has been {action}ed', login=login, keep_history=1, service=service) @@ -327,7 +327,7 @@ def diff_config(oldcfg, cfg, **kwargs): except Exception: login = '' - output, stderr = server_mod.subprocess_execute(cmd) + output, stderr = mod_ssh.subprocess_execute(cmd) if kwargs.get('return_diff'): for line in output: @@ -382,7 +382,7 @@ def get_ssl_cert(server_ip: str) -> None: cert_path = sql.get_setting('cert_path') commands = [f"openssl x509 -in {cert_path}/{cert_id} -text"] try: - server_mod.ssh_command(server_ip, commands, ip="1") + mod_ssh.ssh_command(server_ip, commands, ip="1") except Exception as e: print(f'error: Cannot connect to the server {e.args[0]}') @@ -391,7 +391,7 @@ def get_ssl_certs(server_ip: str) -> None: cert_path = sql.get_setting('cert_path') commands = [f"sudo ls -1t {cert_path} |grep -E 'pem|crt|key'"] try: - server_mod.ssh_command(server_ip, commands, ip="1") + mod_ssh.ssh_command(server_ip, commands, ip="1") except Exception as e: print(f'error: Cannot connect to the server: {e.args[0]}') @@ -402,7 +402,7 @@ def del_ssl_cert(server_ip: str) -> None: cert_path = sql.get_setting('cert_path') commands = [f"sudo rm -f {cert_path}/{cert_id}"] try: - server_mod.ssh_command(server_ip, commands, ip="1") + mod_ssh.ssh_command(server_ip, commands, ip="1") except Exception as e: print(f'error: Cannot delete the certificate {e.args[0]}') diff --git a/app/modules/service/common.py b/app/modules/service/common.py index 4080e96..50f3e08 100644 --- a/app/modules/service/common.py +++ b/app/modules/service/common.py @@ -2,8 +2,12 @@ import os import http.cookies import modules.db.sql as sql +import modules.server.ssh as mod_ssh import modules.common.common as common import modules.server.server as server_mod +import modules.roxy_wi_tools as roxy_wi_tools + +get_config = roxy_wi_tools.GetConfigVar() def check_haproxy_version(server_ip): @@ -98,7 +102,7 @@ def check_haproxy_config(server_ip): else: commands = [f"haproxy -q -c -f {config_path}"] - with server_mod.ssh_connect(server_ip) as ssh: + with mod_ssh.ssh_connect(server_ip) as ssh: for command in commands: stdin, stdout, stderr = ssh.run_command(command) if not stderr.read(): @@ -110,10 +114,118 @@ def check_haproxy_config(server_ip): def check_nginx_config(server_ip): commands = [f"nginx -q -t -p {sql.get_setting('nginx_dir')}"] - with server_mod.ssh_connect(server_ip) as ssh: + with mod_ssh.ssh_connect(server_ip) as ssh: for command in commands: stdin, stdout, stderr = ssh.run_command(command) if not stderr.read(): return True else: return False + + +def overview_backends(server_ip: str, service: str) -> None: + from jinja2 import Environment, FileSystemLoader + + import modules.config.section as section_mod + import modules.roxywi.common as roxywi_common + + env = Environment(loader=FileSystemLoader('templates/ajax'), autoescape=True) + template = env.get_template('haproxyservers_backends.html') + format_file = 'cfg' + + if service == 'haproxy': + configs_dir = get_config.get_config_var('configs', 'haproxy_save_configs_dir') + format_file = 'cfg' + elif service == 'keepalived': + configs_dir = get_config.get_config_var('configs', 'kp_save_configs_dir') + format_file = 'conf' + + if service != 'nginx' and service != 'apache': + try: + sections = section_mod.get_sections(configs_dir + roxywi_common.get_files(configs_dir, format_file)[0], + service=service) + except Exception as e: + roxywi_common.logging('Roxy-WI server', str(e), roxywi=1) + + try: + cfg = f"{configs_dir}{server_ip}-{get_date.return_date('config')}.{format_file}" + except Exception as e: + roxywi_common.logging('Roxy-WI server', f' Cannot generate a cfg path {e}', roxywi=1) + try: + if service == 'keepalived': + config_mod.get_config(server_ip, cfg, keepalived=1) + else: + config_mod.get_config(server_ip, cfg) + except Exception as e: + roxywi_common.logging('Roxy-WI server', f' Cannot download a config {e}', roxywi=1) + try: + sections = section_mod.get_sections(cfg, service=service) + except Exception as e: + roxywi_common.logging('Roxy-WI server', f' Cannot get sections from config file {e}', roxywi=1) + sections = 'Cannot get backends' + else: + sections = section_mod.get_remote_sections(server_ip, service) + + template = template.render(backends=sections, serv=server_ip, service=service) + print(template) + + +def get_overview_last_edit(server_ip: str, service: str) -> None: + if service == 'nginx': + config_path = sql.get_setting('nginx_config_path') + elif service == 'keepalived': + config_path = sql.get_setting('keepalived_config_path') + else: + config_path = sql.get_setting('haproxy_config_path') + commands = ["ls -l %s |awk '{ print $6\" \"$7\" \"$8}'" % config_path] + try: + print(server_mod.ssh_command(server_ip, commands)) + except Exception as e: + print(f'error: Cannot get last date {e} for server {serv}') + + +def overview_service(server_id: int, name: str, service: str) -> None: + import asyncio + from jinja2 import Environment, FileSystemLoader + + async def async_get_overviewServers(serv1, serv2, service): + if service == 'haproxy': + cmd = 'echo "show info" |nc %s %s -w 1|grep -e "node\|Nbproc\|Maxco\|MB\|Nbthread"' % ( + serv2, sql.get_setting('haproxy_sock_port')) + out = server_mod.subprocess_execute(cmd) + return_out = "" + + for k in out: + if "Ncat:" not in k: + for r in k: + return_out += r + return_out += "
" + else: + return_out = "Cannot connect to HAProxy" + else: + return_out = '' + + server_status = (serv1, serv2, return_out) + return server_status + + async def get_runner_overviewServers(**kwargs): + env = Environment(loader=FileSystemLoader('templates/ajax'), + extensions=['jinja2.ext.loopcontrols', 'jinja2.ext.do']) + template = env.get_template('overviewServers.html') + + servers = [] + cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) + user_id = cookie.get('uuid') + role = sql.get_user_role_by_uuid(user_id.value) + futures = [async_get_overviewServers(kwargs.get('server1'), kwargs.get('server2'), kwargs.get('service'))] + + for i, future in enumerate(asyncio.as_completed(futures)): + result = await future + servers.append(result) + servers_sorted = sorted(servers, key=common.get_key) + template = template.render(service_status=servers_sorted, role=role, id=kwargs.get('id'), service_page=service) + print(template) + + ioloop = asyncio.get_event_loop() + ioloop.run_until_complete(get_runner_overviewServers(server1=name, server2=serv, id=server_id, service=service)) + ioloop.close() diff --git a/app/options.py b/app/options.py index e8622c9..e7750de 100644 --- a/app/options.py +++ b/app/options.py @@ -235,47 +235,9 @@ if form.getvalue('action_service') is not None: roxy.action_service(action, serv) if act == "overviewHapserverBackends": - import modules.config.section as section_mod - - env = Environment(loader=FileSystemLoader('templates/ajax'), autoescape=True) - template = env.get_template('haproxyservers_backends.html') - service = form.getvalue('service') - format_file = 'cfg' - - if service == 'haproxy': - configs_dir = get_config.get_config_var('configs', 'haproxy_save_configs_dir') - format_file = 'cfg' - elif service == 'keepalived': - configs_dir = get_config.get_config_var('configs', 'kp_save_configs_dir') - format_file = 'conf' - - if service != 'nginx' and service != 'apache': - try: - sections = section_mod.get_sections(configs_dir + roxywi_common.get_files(configs_dir, format_file)[0], service=service) - except Exception as e: - roxywi_common.logging('Roxy-WI server', str(e), roxywi=1) - - try: - cfg = configs_dir + serv + "-" + get_date.return_date('config') + '.' + format_file - except Exception as e: - roxywi_common.logging('Roxy-WI server', ' Cannot generate a cfg path ' + str(e), roxywi=1) - try: - if service == 'keepalived': - error = config_mod.get_config(serv, cfg, keepalived=1) - else: - error = config_mod.get_config(serv, cfg) - except Exception as e: - roxywi_common.logging('Roxy-WI server', ' Cannot download a config ' + str(e), roxywi=1) - try: - sections = section_mod.get_sections(cfg, service=service) - except Exception as e: - roxywi_common.logging('Roxy-WI server', ' Cannot get sections from config file ' + str(e), roxywi=1) - sections = 'Cannot get backends' - else: - sections = section_mod.get_remote_sections(serv, service) + service = common.checkAjaxInput(form.getvalue('service')) - template = template.render(backends=sections, serv=serv, service=service) - print(template) + service_common.overview_backends(serv, service) if form.getvalue('show_userlists'): configs_dir = get_config.get_config_var('configs', 'haproxy_save_configs_dir') @@ -302,17 +264,9 @@ if form.getvalue('show_userlists'): print(sections) if act == "overviewHapservers": - if form.getvalue('service') == 'nginx': - config_path = sql.get_setting('nginx_config_path') - elif form.getvalue('service') == 'keepalived': - config_path = '/etc/keepalived/keepalived.conf' - else: - config_path = sql.get_setting('haproxy_config_path') - commands = ["ls -l %s |awk '{ print $6\" \"$7\" \"$8}'" % config_path] - try: - print(server_mod.ssh_command(serv, commands)) - except Exception as e: - print(f'error: Cannot get last date {e} for server {serv}') + service = common.checkAjaxInput(form.getvalue('service')) + + service_common.get_overview_last_edit(serv, service) if act == "overview": import modules.roxywi.overview as roxy_overview @@ -327,52 +281,11 @@ if act == "overviewwaf": roxy_waf.waf_overview(serv, waf_service) if act == "overviewServers": - import asyncio - - async def async_get_overviewServers(serv1, serv2, service): - if service == 'haproxy': - cmd = 'echo "show info" |nc %s %s -w 1|grep -e "node\|Nbproc\|Maxco\|MB\|Nbthread"' % ( - serv2, sql.get_setting('haproxy_sock_port')) - out = server_mod.subprocess_execute(cmd) - return_out = "" - - for k in out: - if "Ncat:" not in k: - for r in k: - return_out += r - return_out += "
" - else: - return_out = "Cannot connect to HAProxy" - else: - return_out = '' - - server_status = (serv1, serv2, return_out) - return server_status - - async def get_runner_overviewServers(**kwargs): - env = Environment(loader=FileSystemLoader('templates/ajax'), - extensions=['jinja2.ext.loopcontrols', 'jinja2.ext.do']) - template = env.get_template('overviewServers.html') - - servers = [] - cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) - user_id = cookie.get('uuid') - role = sql.get_user_role_by_uuid(user_id.value) - futures = [async_get_overviewServers(kwargs.get('server1'), kwargs.get('server2'), kwargs.get('service'))] - - for i, future in enumerate(asyncio.as_completed(futures)): - result = await future - servers.append(result) - servers_sorted = sorted(servers, key=common.get_key) - template = template.render(service_status=servers_sorted, role=role, id=kwargs.get('id'), service_page=service) - print(template) + server_id = common.checkAjaxInput(form.getvalue('id')) + name = common.checkAjaxInput(form.getvalue('name')) + service = common.checkAjaxInput(form.getvalue('service')) - server_id = form.getvalue('id') - name = form.getvalue('name') - service = form.getvalue('service') - ioloop = asyncio.get_event_loop() - ioloop.run_until_complete(get_runner_overviewServers(server1=name, server2=serv, id=server_id, service=service)) - ioloop.close() + service_common.overview_service(server_id, name, service) if form.getvalue('action'): haproxy_user = sql.get_setting('stats_user')