|
|
|
@ -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 += "<br />"
|
|
|
|
|
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()
|
|
|
|
|