Pavel Loginov 2021-09-04 14:50:26 +06:00
parent 0dd4142945
commit 4113fe67be
4 changed files with 201 additions and 188 deletions

View File

@ -35,11 +35,14 @@ def get_config_var(sec, var):
except Exception: except Exception:
print('Content-type: text/html\n') print('Content-type: text/html\n')
print('<center><div class="alert alert-danger">Check the config file, whether it exists and the path. Must be: app/roxy-wi.cfg</div>') print('<center><div class="alert alert-danger">Check the config file, whether it exists and the path. Must be: app/roxy-wi.cfg</div>')
return
try: try:
return config.get(sec, var) return config.get(sec, var)
except Exception: except Exception:
print('Content-type: text/html\n') print('Content-type: text/html\n')
print('<center><div class="alert alert-danger">Check the config file. Presence section %s and parameter %s</div>' % (sec, var)) print('<center><div class="alert alert-danger">Check the config file. Presence section %s and parameter %s</div>' % (sec, var))
return
def get_data(log_type, **kwargs): def get_data(log_type, **kwargs):
@ -107,7 +110,7 @@ def get_user_group(**kwargs):
return user_group return user_group
def logging(serv, action, **kwargs): def logging(server_ip, action, **kwargs):
import sql import sql
import http.cookies import http.cookies
import distro import distro
@ -115,7 +118,6 @@ def logging(serv, action, **kwargs):
log_path = get_config_var('main', 'log_path') log_path = get_config_var('main', 'log_path')
try: try:
user_group = get_user_group() user_group = get_user_group()
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
except: except:
user_group = '' user_group = ''
@ -128,6 +130,7 @@ def logging(serv, action, **kwargs):
ip = '' ip = ''
try: try:
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
user_uuid = cookie.get('uuid') user_uuid = cookie.get('uuid')
login = sql.get_user_name_by_uuid(user_uuid.value) login = sql.get_user_name_by_uuid(user_uuid.value)
except Exception: except Exception:
@ -156,7 +159,7 @@ def logging(serv, action, **kwargs):
elif kwargs.get('haproxywi') == 1: elif kwargs.get('haproxywi') == 1:
if kwargs.get('login'): if kwargs.get('login'):
mess = get_data('date_in_log') + " from " + ip + " user: " + login + ", group: " + user_group + ", " + \ mess = get_data('date_in_log') + " from " + ip + " user: " + login + ", group: " + user_group + ", " + \
action + " for: " + serv + "\n" action + " for: " + server_ip + "\n"
else: else:
mess = get_data('date_in_log') + " " + action + " from " + ip + "\n" mess = get_data('date_in_log') + " " + action + " from " + ip + "\n"
log = open(log_path + "/roxy-wi-"+get_data('logs')+".log", "a") log = open(log_path + "/roxy-wi-"+get_data('logs')+".log", "a")
@ -166,7 +169,7 @@ def logging(serv, action, **kwargs):
log = open(log_path + "/provisioning-"+get_data('logs')+".log", "a") log = open(log_path + "/provisioning-"+get_data('logs')+".log", "a")
else: else:
mess = get_data('date_in_log') + " from " + ip + " user: " + login + ", group: " + user_group + ", " + \ mess = get_data('date_in_log') + " from " + ip + " user: " + login + ", group: " + user_group + ", " + \
action + " for: " + serv + "\n" action + " for: " + server_ip + "\n"
log = open(log_path + "/config_edit-"+get_data('logs')+".log", "a") log = open(log_path + "/config_edit-"+get_data('logs')+".log", "a")
try: try:
@ -289,8 +292,7 @@ def is_admin(**kwargs):
try: try:
return True if role <= level else False return True if role <= level else False
except Exception as e: except Exception:
# print('error: '+str(e))
return False return False
@ -306,7 +308,7 @@ def page_for_admin(**kwargs):
sys.exit() sys.exit()
def return_ssh_keys_path(serv, **kwargs): def return_ssh_keys_path(server_ip, **kwargs):
import sql import sql
full_path = get_config_var('main', 'fullpath') full_path = get_config_var('main', 'fullpath')
ssh_enable = '' ssh_enable = ''
@ -321,7 +323,7 @@ def return_ssh_keys_path(serv, **kwargs):
ssh_user_password = sshs.password ssh_user_password = sshs.password
ssh_key_name = full_path+'/keys/%s.pem' % sshs.name ssh_key_name = full_path+'/keys/%s.pem' % sshs.name
else: else:
for sshs in sql.select_ssh(serv=serv): for sshs in sql.select_ssh(serv=server_ip):
ssh_enable = sshs.enable ssh_enable = sshs.enable
ssh_user_name = sshs.username ssh_user_name = sshs.username
ssh_user_password = sshs.password ssh_user_password = sshs.password
@ -330,13 +332,13 @@ def return_ssh_keys_path(serv, **kwargs):
return ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name return ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name
def ssh_connect(serv): def ssh_connect(server_ip):
import paramiko import paramiko
from paramiko import SSHClient from paramiko import SSHClient
import sql import sql
ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = return_ssh_keys_path(serv) ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = return_ssh_keys_path(server_ip)
servers = sql.select_servers(server=serv) servers = sql.select_servers(server=server_ip)
ssh_port = 22 ssh_port = 22
for server in servers: for server in servers:
@ -353,9 +355,9 @@ def ssh_connect(serv):
# k = paramiko.pkey.load_private_key_file(ssh_key_name, password=cloud) # k = paramiko.pkey.load_private_key_file(ssh_key_name, password=cloud)
# else: # else:
k = paramiko.pkey.load_private_key_file(ssh_key_name) k = paramiko.pkey.load_private_key_file(ssh_key_name)
ssh.connect(hostname=serv, port=ssh_port, username=ssh_user_name, pkey=k, timeout=11, banner_timeout=200) ssh.connect(hostname=server_ip, port=ssh_port, username=ssh_user_name, pkey=k, timeout=11, banner_timeout=200)
else: else:
ssh.connect(hostname=serv, port=ssh_port, username=ssh_user_name, password=ssh_user_password, timeout=11, banner_timeout=200) ssh.connect(hostname=server_ip, port=ssh_port, username=ssh_user_name, password=ssh_user_password, timeout=11, banner_timeout=200)
return ssh return ssh
except paramiko.AuthenticationException as e: except paramiko.AuthenticationException as e:
logging('localhost', ' ' + str(e), haproxywi=1) logging('localhost', ' ' + str(e), haproxywi=1)
@ -379,7 +381,7 @@ def ssh_connect(serv):
print(str(e)) print(str(e))
def get_config(serv, cfg, **kwargs): def get_config(server_ip, cfg, **kwargs):
import sql import sql
if kwargs.get("keepalived"): if kwargs.get("keepalived"):
@ -389,22 +391,23 @@ def get_config(serv, cfg, **kwargs):
else: else:
config_path = sql.get_setting('haproxy_config_path') config_path = sql.get_setting('haproxy_config_path')
ssh = ssh_connect(serv) ssh = ssh_connect(server_ip)
try: try:
sftp = ssh.open_sftp() sftp = ssh.open_sftp()
except Exception as e: except Exception as e:
logging('localhost', str(e), haproxywi=1) logging('localhost', str(e), haproxywi=1)
return
try: try:
sftp.get(config_path, cfg) sftp.get(config_path, cfg)
except Exception as e: except Exception as e:
logging('localhost', str(e), haproxywi=1) logging('localhost', str(e), haproxywi=1)
return
try: try:
sftp.close() sftp.close()
ssh.close() ssh.close()
except Exception as e: except Exception as e:
ssh = str(e) logging('localhost', str(e), haproxywi=1)
logging('localhost', ssh, haproxywi=1) return
return ssh
def diff_config(oldcfg, cfg): def diff_config(oldcfg, cfg):
@ -549,7 +552,7 @@ def get_userlists(config):
return return_config return return_config
def get_backends_from_config(serv, backends=''): def get_backends_from_config(server_ip, backends=''):
configs_dir = get_config_var('configs', 'haproxy_save_configs_dir') configs_dir = get_config_var('configs', 'haproxy_save_configs_dir')
format_cfg = 'cfg' format_cfg = 'cfg'
@ -558,11 +561,12 @@ def get_backends_from_config(serv, backends=''):
except Exception as e: except Exception as e:
logging('localhost', str(e), haproxywi=1) logging('localhost', str(e), haproxywi=1)
try: try:
cfg = configs_dir + serv + "-" + get_data('config') + '.'+format_cfg cfg = configs_dir + server_ip + "-" + get_data('config') + '.'+format_cfg
except Exception: except Exception:
logging('localhost', ' Cannot generate cfg path', haproxywi=1) logging('localhost', ' Cannot generate cfg path', haproxywi=1)
return
try: try:
error = get_config(serv, cfg) get_config(server_ip, cfg)
except Exception: except Exception:
logging('localhost', ' Cannot download config', haproxywi=1) logging('localhost', ' Cannot download config', haproxywi=1)
print('error: Cannot get backends') print('error: Cannot get backends')
@ -613,7 +617,6 @@ def show_installation_output(error, output, service):
try: try:
print(l) print(l)
break break
return False
except Exception: except Exception:
print(output) print(output)
break break
@ -622,7 +625,7 @@ def show_installation_output(error, output, service):
return True return True
def install_haproxy(serv, **kwargs): def install_haproxy(server_ip, **kwargs):
import sql import sql
script = "install_haproxy.sh" script = "install_haproxy.sh"
hap_sock_p = str(sql.get_setting('haproxy_sock_port')) hap_sock_p = str(sql.get_setting('haproxy_sock_port'))
@ -634,12 +637,12 @@ def install_haproxy(serv, **kwargs):
haproxy_ver = kwargs.get('hapver') haproxy_ver = kwargs.get('hapver')
server_for_installing = kwargs.get('server') server_for_installing = kwargs.get('server')
ssh_port = 22 ssh_port = 22
ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = return_ssh_keys_path(serv) ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = return_ssh_keys_path(server_ip)
if ssh_enable == 0: if ssh_enable == 0:
ssh_key_name = '' ssh_key_name = ''
servers = sql.select_servers(server=serv) servers = sql.select_servers(server=server_ip)
for server in servers: for server in servers:
ssh_port = str(server[10]) ssh_port = str(server[10])
@ -659,7 +662,7 @@ def install_haproxy(serv, **kwargs):
" SOCK_PORT=" + hap_sock_p + " STAT_PORT=" + stats_port + " STAT_FILE="+server_state_file + " SOCK_PORT=" + hap_sock_p + " STAT_PORT=" + stats_port + " STAT_FILE="+server_state_file +
" SSH_PORT=" + ssh_port + " STATS_USER=" + stats_user + " SSH_PORT=" + ssh_port + " STATS_USER=" + stats_user +
" STATS_PASS='" + stats_password + "' HAPVER=" + haproxy_ver + " SYN_FLOOD=" + syn_flood_protect + " STATS_PASS='" + stats_password + "' HAPVER=" + haproxy_ver + " SYN_FLOOD=" + syn_flood_protect +
" HOST=" + serv + " USER=" + ssh_user_name + " PASS='" + ssh_user_password + "' KEY=" + ssh_key_name] " HOST=" + server_ip + " USER=" + ssh_user_name + " PASS='" + ssh_user_password + "' KEY=" + ssh_key_name]
output, error = subprocess_execute(commands[0]) output, error = subprocess_execute(commands[0])
if server_for_installing: if server_for_installing:
@ -668,18 +671,18 @@ def install_haproxy(serv, **kwargs):
service = ' HAProxy' service = ' HAProxy'
if show_installation_output(error, output, service): if show_installation_output(error, output, service):
sql.update_haproxy(serv) sql.update_haproxy(server_ip)
os.system("rm -f %s" % script) os.system("rm -f %s" % script)
def waf_install(serv): def waf_install(server_ip):
import sql import sql
script = "waf.sh" script = "waf.sh"
tmp_config_path = sql.get_setting('tmp_config_path') tmp_config_path = sql.get_setting('tmp_config_path')
proxy = sql.get_setting('proxy') proxy = sql.get_setting('proxy')
haproxy_dir = sql.get_setting('haproxy_dir') haproxy_dir = sql.get_setting('haproxy_dir')
ver = check_haproxy_version(serv) ver = check_haproxy_version(server_ip)
os.system("cp scripts/%s ." % script) os.system("cp scripts/%s ." % script)
@ -691,19 +694,21 @@ def waf_install(serv):
commands = ["sudo chmod +x " + tmp_config_path+script + " && " + tmp_config_path+script + " PROXY=" + proxy_serv + commands = ["sudo chmod +x " + tmp_config_path+script + " && " + tmp_config_path+script + " PROXY=" + proxy_serv +
" HAPROXY_PATH=" + haproxy_dir + " VERSION=" + ver] " HAPROXY_PATH=" + haproxy_dir + " VERSION=" + ver]
error = str(upload(serv, tmp_config_path, script)) error = str(upload(server_ip, tmp_config_path, script))
if error: if error:
print('error: '+error) print('error: '+error)
logging('localhost', error, haproxywi=1) logging('localhost', error, haproxywi=1)
os.system("rm -f %s" % script) os.system("rm -f %s" % script)
stderr = ssh_command(serv, commands, print_out="1") ssh_command(server_ip, commands, print_out="1")
sql.insert_waf_metrics_enable(serv, "0") sql.insert_waf_metrics_enable(server_ip, "0")
sql.insert_waf_rules(serv) sql.insert_waf_rules(server_ip)
def install_nginx(serv, **kwargs): def install_nginx(server_ip, **kwargs):
import sql import sql
script = "install_nginx.sh" script = "install_nginx.sh"
stats_user = sql.get_setting('nginx_stats_user') stats_user = sql.get_setting('nginx_stats_user')
@ -713,7 +718,8 @@ def install_nginx(serv, **kwargs):
config_path = sql.get_setting('nginx_config_path') config_path = sql.get_setting('nginx_config_path')
server_for_installing = kwargs.get('server') server_for_installing = kwargs.get('server')
proxy = sql.get_setting('proxy') proxy = sql.get_setting('proxy')
ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = return_ssh_keys_path(serv) ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name = return_ssh_keys_path(server_ip)
ssh_port = '22'
if ssh_enable == 0: if ssh_enable == 0:
ssh_key_name = '' ssh_key_name = ''
@ -725,7 +731,7 @@ def install_nginx(serv, **kwargs):
else: else:
proxy_serv = '' proxy_serv = ''
servers = sql.select_servers(server=serv) servers = sql.select_servers(server=server_ip)
for server in servers: for server in servers:
ssh_port = str(server[10]) ssh_port = str(server[10])
@ -734,7 +740,7 @@ def install_nginx(serv, **kwargs):
commands = ["chmod +x " + script + " && ./" + script + " PROXY=" + proxy_serv + " STATS_USER=" + stats_user + commands = ["chmod +x " + script + " && ./" + script + " PROXY=" + proxy_serv + " STATS_USER=" + stats_user +
" STATS_PASS='" + stats_password + "' SSH_PORT=" + ssh_port + " CONFIG_PATH=" + config_path + " STATS_PASS='" + stats_password + "' SSH_PORT=" + ssh_port + " CONFIG_PATH=" + config_path +
" STAT_PORT=" + stats_port + " STAT_PAGE=" + stats_page+" SYN_FLOOD=" + syn_flood_protect + " STAT_PORT=" + stats_port + " STAT_PAGE=" + stats_page+" SYN_FLOOD=" + syn_flood_protect +
" HOST=" + serv + " USER=" + ssh_user_name + " PASS='" + ssh_user_password + "' KEY=" + ssh_key_name] " HOST=" + server_ip + " USER=" + ssh_user_name + " PASS='" + ssh_user_password + "' KEY=" + ssh_key_name]
output, error = subprocess_execute(commands[0]) output, error = subprocess_execute(commands[0])
if server_for_installing: if server_for_installing:
@ -742,7 +748,7 @@ def install_nginx(serv, **kwargs):
else: else:
service = ' Nginx' service = ' Nginx'
if show_installation_output(error, output, service): if show_installation_output(error, output, service):
sql.update_nginx(serv) sql.update_nginx(server_ip)
os.system("rm -f %s" % script) os.system("rm -f %s" % script)
@ -763,11 +769,11 @@ def update_haproxy_wi(service):
print(stderr) print(stderr)
def check_haproxy_version(serv): def check_haproxy_version(server_ip):
import sql import sql
hap_sock_p = sql.get_setting('haproxy_sock_port') hap_sock_p = sql.get_setting('haproxy_sock_port')
ver = "" ver = ""
cmd = "echo 'show info' |nc %s %s |grep Version |awk '{print $2}'" % (serv, hap_sock_p) cmd = "echo 'show info' |nc %s %s |grep Version |awk '{print $2}'" % (server_ip, hap_sock_p)
output, stderr = subprocess_execute(cmd) output, stderr = subprocess_execute(cmd)
for line in output: for line in output:
ver = line ver = line
@ -775,45 +781,48 @@ def check_haproxy_version(serv):
return ver return ver
def upload(serv, path, file, **kwargs): def upload(server_ip, path, file, **kwargs):
error = "" error = ""
full_path = path + file full_path = path + file
if kwargs.get('dir') == "fullpath": if kwargs.get('dir') == "fullpath":
full_path = path full_path = path
try: try:
ssh = ssh_connect(serv) ssh = ssh_connect(server_ip)
except Exception as e: except Exception as e:
error = e.args error = str(e.args)
logging('localhost', str(e.args[0]), haproxywi=1) logging('localhost', error, haproxywi=1)
print(' Cannot upload '+file+' to '+full_path+' to server: '+serv+' error: '+str(e.args)) print(' Cannot upload '+file+' to '+full_path+' to server: '+server_ip+' error: ' + error)
return error
try: try:
sftp = ssh.open_sftp() sftp = ssh.open_sftp()
except Exception as e: except Exception as e:
error = e.args error = str(e.args)
logging('localhost', str(e.args[0]), haproxywi=1) logging('localhost', error, haproxywi=1)
print('Cannot upload '+file+' to '+full_path+' to server: '+serv+' error: '+str(e.args)) print('Cannot upload '+file+' to '+full_path+' to server: '+server_ip+' error: ' + error)
return error
try: try:
file = sftp.put(file, full_path) file = sftp.put(file, full_path)
except Exception as e: except Exception as e:
error = e.args error = str(e.args)
print('Cannot upload '+file+' to '+full_path+' to server: '+serv+' error: '+str(e.args)) print('Cannot upload '+file+' to '+full_path+' to server: '+server_ip+' error: '+ error)
logging('localhost', ' Cannot upload '+file+' to '+full_path+' to server: '+serv+' Error: '+str(e.args), haproxywi=1) logging('localhost', ' Cannot upload '+file+' to '+full_path+' to server: '+server_ip+' Error: '+ error, haproxywi=1)
return error
try: try:
sftp.close() sftp.close()
ssh.close() ssh.close()
except Exception as e: except Exception as e:
error = e.args error = str(e.args)
logging('localhost', str(error[0]), haproxywi=1) logging('localhost', error, haproxywi=1)
print('Cannot upload '+file+' to '+full_path+' to server: '+serv+' error: '+str(e.args)) print('Cannot upload '+file+' to '+full_path+' to server: '+server_ip+' error: '+ error)
return str(error) return str(error)
def upload_and_restart(serv, cfg, **kwargs): def upload_and_restart(server_ip, cfg, **kwargs):
import sql import sql
error = "" error = ""
@ -845,8 +854,8 @@ def upload_and_restart(serv, cfg, **kwargs):
commands = [check_and_move + " && sudo systemctl reload nginx"] commands = [check_and_move + " && sudo systemctl reload nginx"]
else: else:
commands = [check_and_move + " && sudo systemctl restart nginx"] commands = [check_and_move + " && sudo systemctl restart nginx"]
if sql.return_firewall(serv): if sql.return_firewall(server_ip):
commands[0] += open_port_firewalld(cfg, serv=serv, service='nginx') commands[0] += open_port_firewalld(cfg, serv=server_ip, service='nginx')
else: else:
haproxy_enterprise = sql.get_setting('haproxy_enterprise') haproxy_enterprise = sql.get_setting('haproxy_enterprise')
@ -866,12 +875,12 @@ def upload_and_restart(serv, cfg, **kwargs):
commands = [check_config + move_config + " && sudo systemctl reload "+haproxy_service_name+""] commands = [check_config + move_config + " && sudo systemctl reload "+haproxy_service_name+""]
else: else:
commands = [check_config + move_config + " && sudo systemctl restart "+haproxy_service_name+""] commands = [check_config + move_config + " && sudo systemctl restart "+haproxy_service_name+""]
if sql.return_firewall(serv): if sql.return_firewall(server_ip):
commands[0] += open_port_firewalld(cfg, serv=serv) commands[0] += open_port_firewalld(cfg, serv=server_ip)
error += str(upload(serv, tmp_file, cfg, dir='fullpath')) error += str(upload(server_ip, tmp_file, cfg, dir='fullpath'))
try: try:
error += ssh_command(serv, commands) error += ssh_command(server_ip, commands)
except Exception as e: except Exception as e:
error += e error += e
if error: if error:
@ -880,24 +889,25 @@ def upload_and_restart(serv, cfg, **kwargs):
return error return error
def master_slave_upload_and_restart(serv, cfg, just_save, **kwargs): def master_slave_upload_and_restart(server_ip, cfg, just_save, **kwargs):
import sql import sql
masters = sql.is_master(serv) masters = sql.is_master(server_ip)
error = "" error = ""
for master in masters: for master in masters:
if master[0] is not None: if master[0] is not None:
error += upload_and_restart(master[0], cfg, just_save=just_save, nginx=kwargs.get('nginx')) error += upload_and_restart(master[0], cfg, just_save=just_save, nginx=kwargs.get('nginx'))
error += upload_and_restart(serv, cfg, just_save=just_save, nginx=kwargs.get('nginx')) error += upload_and_restart(server_ip, cfg, just_save=just_save, nginx=kwargs.get('nginx'))
return error return error
def open_port_firewalld(cfg, serv, **kwargs): def open_port_firewalld(cfg, server_ip, **kwargs):
try: try:
conf = open(cfg, "r") conf = open(cfg, "r")
except IOError: except IOError:
print('<div class="alert alert-danger">Cannot read exported config file</div>') print('<div class="alert alert-danger">Cannot read exported config file</div>')
return
firewalld_commands = ' &&' firewalld_commands = ' &&'
ports = '' ports = ''
@ -934,14 +944,14 @@ def open_port_firewalld(cfg, serv, **kwargs):
pass pass
firewalld_commands += 'sudo firewall-cmd --reload -q' firewalld_commands += 'sudo firewall-cmd --reload -q'
logging(serv, ' Next ports have been opened: ' + ports) logging(server_ip, ' Next ports have been opened: ' + ports)
return firewalld_commands return firewalld_commands
def check_haproxy_config(serv): def check_haproxy_config(server_ip):
import sql import sql
commands = ["haproxy -q -c -f %s" % sql.get_setting('haproxy_config_path')] commands = ["haproxy -q -c -f %s" % sql.get_setting('haproxy_config_path')]
ssh = ssh_connect(serv) ssh = ssh_connect(server_ip)
for command in commands: for command in commands:
stdin, stdout, stderr = ssh.exec_command(command, get_pty=True) stdin, stdout, stderr = ssh.exec_command(command, get_pty=True)
if not stderr.read(): if not stderr.read():
@ -1105,14 +1115,15 @@ def server_status(stdout):
return proc_count return proc_count
def ssh_command(serv, commands, **kwargs): def ssh_command(server_ip, commands, **kwargs):
ssh = ssh_connect(serv) ssh = ssh_connect(server_ip)
for command in commands: for command in commands:
try: try:
stdin, stdout, stderr = ssh.exec_command(command, get_pty=True) stdin, stdout, stderr = ssh.exec_command(command, get_pty=True)
except Exception as e: except Exception as e:
logging('localhost', ' ' + str(e), haproxywi=1) logging('localhost', ' ' + str(e), haproxywi=1)
return str(e)
if kwargs.get("ip") == "1": if kwargs.get("ip") == "1":
show_ip(stdout) show_ip(stdout)
@ -1151,11 +1162,11 @@ def subprocess_execute(cmd):
return output, stderr return output, stderr
def show_backends(serv, **kwargs): def show_backends(server_ip, **kwargs):
import json import json
import sql import sql
hap_sock_p = sql.get_setting('haproxy_sock_port') hap_sock_p = sql.get_setting('haproxy_sock_port')
cmd = 'echo "show backend" |nc %s %s' % (serv, hap_sock_p) cmd = 'echo "show backend" |nc %s %s' % (server_ip, hap_sock_p)
output, stderr = subprocess_execute(cmd) output, stderr = subprocess_execute(cmd)
if stderr: if stderr:
logging('localhost', ' '+stderr, haproxywi=1) logging('localhost', ' '+stderr, haproxywi=1)
@ -1177,7 +1188,7 @@ def show_backends(serv, **kwargs):
return ret return ret
def get_files(dir=get_config_var('configs', 'haproxy_save_configs_dir'), format='cfg', **kwargs): def get_files(dir=get_config_var('configs', 'haproxy_save_configs_dir'), format='cfg'):
import glob import glob
if format == 'log': if format == 'log':
file = [] file = []
@ -1321,12 +1332,12 @@ def check_user_group(**kwargs):
sys.exit() sys.exit()
def check_is_server_in_group(serv): def check_is_server_in_group(server_ip):
import sql import sql
group_id = get_user_group(id=1) group_id = get_user_group(id=1)
servers = sql.select_servers(server=serv) servers = sql.select_servers(server=server_ip)
for s in servers: for s in servers:
if (s[2] == serv and int(s[3]) == int(group_id)) or group_id == 1: if (s[2] == server_ip and int(s[3]) == int(group_id)) or group_id == 1:
return True return True
else: else:
logging('localhost', ' has tried to actions in not his group server ', haproxywi=1, login=1) logging('localhost', ' has tried to actions in not his group server ', haproxywi=1, login=1)
@ -1334,9 +1345,9 @@ def check_is_server_in_group(serv):
sys.exit() sys.exit()
def check_service(serv, service_name): def check_service(server_ip, service_name):
commands = ["systemctl is-active "+service_name] commands = ["systemctl is-active "+service_name]
return ssh_command(serv, commands) return ssh_command(server_ip, commands)
def get_services_status(): def get_services_status():
@ -1385,15 +1396,15 @@ def get_services_status():
return services return services
def is_file_exists(serv: str, file: str): def is_file_exists(server_ip: str, file: str):
cmd = ['[ -f ' + file + ' ] && echo yes || echo no'] cmd = ['[ -f ' + file + ' ] && echo yes || echo no']
out = ssh_command(serv, cmd) out = ssh_command(server_ip, cmd)
return True if 'yes' in out else False return True if 'yes' in out else False
def is_service_active(serv: str, service_name: str): def is_service_active(server_ip: str, service_name: str):
cmd = ['systemctl is-active ' + service_name] cmd = ['systemctl is-active ' + service_name]
out = ssh_command(serv, cmd) out = ssh_command(server_ip, cmd)
return True if 'active' in out else False return True if 'active' in out else False

View File

@ -49,7 +49,7 @@ try:
prometheus = '' prometheus = ''
host = '' host = ''
else: else:
users = sql.select_users(online=1) users = sql.select_users()
cmd = "ps ax |grep 'metrics_worker\|metrics_waf_worker.py\|metrics_nginx_worker.py' |grep -v grep |wc -l" cmd = "ps ax |grep 'metrics_worker\|metrics_waf_worker.py\|metrics_nginx_worker.py' |grep -v grep |wc -l"
metrics_worker, stderr = funct.subprocess_execute(cmd) metrics_worker, stderr = funct.subprocess_execute(cmd)
cmd = "ps ax |grep 'checker_worker\|checker_nginx' |grep -v grep |wc -l" cmd = "ps ax |grep 'checker_worker\|checker_nginx' |grep -v grep |wc -l"

View File

@ -309,21 +309,22 @@ def select_users(**kwargs):
query = User.select().where(User.user_id == kwargs.get("id")) query = User.select().where(User.user_id == kwargs.get("id"))
elif kwargs.get("group") is not None: elif kwargs.get("group") is not None:
query = (User. query = (User.
select(). select(
User,
UserGroups,
Case(0, [((
User.last_login_date >= funct.get_data('regular', timedelta_minutes_minus=15)
), 0)], 1).alias('last_login')).
join(UserGroups, on=(User.user_id == UserGroups.user_id)). join(UserGroups, on=(User.user_id == UserGroups.user_id)).
where(UserGroups.user_group_id == kwargs.get("group")) where(UserGroups.user_group_id == kwargs.get("group"))
) )
elif kwargs.get('online') is not None: else:
query = (User. query = User.select(
select(
User, User,
Case(0, [(( Case(0, [((
User.last_login_date >= funct.get_data('regular', timedelta_minutes_minus=15) User.last_login_date >= funct.get_data('regular', timedelta_minutes_minus=15)
), 0)], 1).alias('last_login') ), 0)], 1).alias('last_login')
) ).order_by(User.user_id)
)
else:
query = User.select().order_by(User.user_id)
try: try:
query_res = query.execute() query_res = query.execute()
@ -500,13 +501,15 @@ def write_user_uuid(login, user_uuid):
def write_user_token(login, user_token): def write_user_token(login, user_token):
token_ttl = int(get_setting('token_ttl'))
try: try:
user_id = User.get(User.username == login) user_id = User.get(User.username == login)
except Exception as e: except Exception as e:
out_error(e) out_error(e)
try: try:
Token.insert(user_id=user_id, token=user_token, exp=funct.get_data('regular')).execute() Token.insert(user_id=user_id, token=user_token, exp=funct.get_data('regular', timedelta=token_ttl)).execute()
except Exception as e: except Exception as e:
out_error(e) out_error(e)
@ -556,8 +559,8 @@ def get_username_groupid_from_api_token(token):
def get_token(uuid): def get_token(uuid):
try:
query = Token.select().join(UUID, on=(Token.user_id == UUID.user_id)).where(UUID.uuid == uuid).limit(1) query = Token.select().join(UUID, on=(Token.user_id == UUID.user_id)).where(UUID.uuid == uuid).limit(1)
try:
query_res = query.execute() query_res = query.execute()
except Exception as e: except Exception as e:
out_error(e) out_error(e)
@ -578,9 +581,8 @@ def delete_uuid(uuid):
def delete_old_uuid(): def delete_old_uuid():
token_ttl = int(get_setting('token_ttl'))
query = UUID.delete().where((UUID.exp < funct.get_data('regular')) | (UUID.exp.is_null(True))) query = UUID.delete().where((UUID.exp < funct.get_data('regular')) | (UUID.exp.is_null(True)))
query1 = Token.delete().where((Token.exp > funct.get_data('regular', timedelta=token_ttl)) | (Token.exp.is_null(True)) ) query1 = Token.delete().where((Token.exp < funct.get_data('regular')) | (Token.exp.is_null(True)))
try: try:
query.execute() query.execute()
query1.execute() query1.execute()
@ -1473,7 +1475,7 @@ def select_metrics_http(serv, **kwargs):
def select_servers_metrics_for_master(**kwargs): def select_servers_metrics_for_master(**kwargs):
if kwargs.get('group') is not None: if kwargs.get('group') is not None:
query = Server.select(Server.ip).where((Server.metrics == 1) & (Server.groups == kwargs.get(group))) query = Server.select(Server.ip).where((Server.metrics == 1) & (Server.groups == kwargs.get('group')))
else: else:
query = Server.select(Server.ip).where(Server.metrics == 1) query = Server.select(Server.ip).where(Server.metrics == 1)
try: try:

View File

@ -164,9 +164,9 @@
{% if s.6|int() >= 1 and service != 'keepalived' %} {% if s.6|int() >= 1 and service != 'keepalived' %}
<span <span
{% if keep_alive|int() >= 1 %} {% if keep_alive|int() >= 1 %}
class="shield green" title="Auto start enabled"> class="shield green" title="Auto start is enabled">
{% else %} {% else %}
class="shield red" title="Auto start enabled, but keep alive service does not work"> class="shield red" title="Auto start is enabled, but Keep alive service does not work">
{% endif %} {% endif %}
</span> </span>
{% endif %} {% endif %}