diff --git a/api/api_funct.py b/api/api_funct.py
index 4a5fe6d6..d7ba44bc 100644
--- a/api/api_funct.py
+++ b/api/api_funct.py
@@ -524,7 +524,7 @@ def show_log(server_id):
data[server_id] = {"error": "Cannot find the server"}
return dict(error=data)
- out = funct.show_haproxy_log(ip, rows=rows, waf=str(waf), grep=grep, hour=str(hour), minut=str(minute), hour1=str(hour1), minut1=str(minute1), html=0)
+ out = funct.show_roxy_log(ip, rows=rows, waf=str(waf), grep=grep, hour=str(hour), minut=str(minute), hour1=str(hour1), minut1=str(minute1), html=0)
data = {server_id: out}
return dict(log=data)
diff --git a/app/config.py b/app/config.py
index f40dd91e..c42d54d7 100644
--- a/app/config.py
+++ b/app/config.py
@@ -32,7 +32,7 @@ except Exception as e:
if service in ('haproxy', 'nginx', 'keepalived', 'apache'):
service_desc = sql.select_service(service)
if funct.check_login(service=service_desc.service_id):
- title = f"{service_desc.service} config view page"
+ title = f"Working with {service_desc.service} configuration files"
action = f"config.py?service={service_desc.slug}"
configs_dir = funct.get_config_var('configs', 'kp_save_configs_dir')
file_format = 'conf'
@@ -60,8 +60,6 @@ if serv is not None and form.getvalue('open') is not None and form.getvalue('new
is_serv_protected = sql.is_serv_protected(serv)
server_id = sql.select_server_id_by_ip(serv)
is_restart = sql.select_service_setting(server_id, service, 'restart')
- service_desc = sql.select_service(service)
- title = f'{service_desc.service} config edit page'
if service == 'keepalived':
error = funct.get_config(serv, cfg, keepalived=1)
diff --git a/app/create_db.py b/app/create_db.py
index 4e3df8ce..5682d373 100644
--- a/app/create_db.py
+++ b/app/create_db.py
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
import distro
-from db_model import *
-from funct import check_ver
+
+from modules.db_model import *
def default_values():
@@ -58,7 +58,7 @@ def default_values():
'desc': 'Socket port for HAProxy', 'group': '1'},
{'param': 'haproxy_sock_port', 'value': '1999', 'section': 'haproxy', 'desc': 'HAProxy sock port',
'group': '1'},
- {'param': 'apache_log_path', 'value': '/var/log/' + apache_dir + '/', 'section': 'logs',
+ {'param': 'apache_log_path', 'value': f'/var/log/{apache_dir}/', 'section': 'logs',
'desc': 'Path to Apache logs. Apache service for Roxy-WI', 'group': '1'},
{'param': 'nginx_path_logs', 'value': '/var/log/nginx/', 'section': 'nginx',
'desc': 'The path for NGINX logs', 'group': '1'},
@@ -656,95 +656,68 @@ def default_values():
print(str(e))
-def update_db_v_3_4_5_22():
- try:
- Version.insert(version='3.4.5.2').execute()
- except Exception as e:
- print('Cannot insert version %s' % e)
-
-
# Needs for updating user_group. Do not delete
-def update_db_v_4_3_0(**kwargs):
+def update_db_v_4_3_0():
try:
UserGroups.insert_from(
User.select(User.user_id, User.groups), fields=[UserGroups.user_id, UserGroups.user_group_id]
).on_conflict_ignore().execute()
except Exception as e:
- if kwargs.get('silent') != 1:
- if e.args[0] == 'duplicate column name: haproxy' or str(e) == '(1060, "Duplicate column name \'haproxy\'")':
- print('Updating... go to version 4.3.1')
- else:
- print("An error occurred:", e)
+ if e.args[0] == 'duplicate column name: haproxy' or str(e) == '(1060, "Duplicate column name \'haproxy\'")':
+ print('Updating... go to version 4.3.1')
+ else:
+ print("An error occurred:", e)
-def update_db_v_5_1_3(**kwargs):
- cursor = conn.cursor()
- sql = """ALTER TABLE `servers` ADD COLUMN protected INTEGER NOT NULL DEFAULT 0;"""
- try:
- cursor.execute(sql)
- except Exception as e:
- if kwargs.get('silent') != 1:
- if str(e) == 'duplicate column name: protected' or str(e) == '(1060, "Duplicate column name \'protected\'")':
- print('Updating... DB has been updated to version 5.1.3')
- else:
- print("An error occurred:", e)
- else:
- print("DB has been updated to version 5.1.3")
-
-
-def update_db_v_5_2_4(**kwargs):
+def update_db_v_5_2_4():
cursor = conn.cursor()
sql = """ALTER TABLE `user` ADD COLUMN user_services varchar(20) DEFAULT '1 2 3 4';"""
try:
cursor.execute(sql)
except Exception as e:
- if kwargs.get('silent') != 1:
- if str(e) == 'duplicate column name: user_services' or str(e) == '(1060, "Duplicate column name \'user_services\'")':
- print('Updating... DB has been updated to version 5.2.4')
- else:
- print("An error occurred:", e)
+ if str(e) == 'duplicate column name: user_services' or str(e) == '(1060, "Duplicate column name \'user_services\'")':
+ print('Updating... DB has been updated to version 5.2.4')
+ else:
+ print("An error occurred:", e)
else:
print("Updating... DB has been updated to version 5.2.4")
-def update_db_v_5_2_4_1(**kwargs):
+def update_db_v_5_2_4_1():
cursor = conn.cursor()
sql = """ALTER TABLE `servers` ADD COLUMN nginx_metrics integer DEFAULT 0;"""
try:
cursor.execute(sql)
except Exception as e:
- if kwargs.get('silent') != 1:
- if str(e) == 'duplicate column name: nginx_metrics' or str(e) == '(1060, "Duplicate column name \'nginx_metrics\'")':
- print('Updating... DB has been updated to version 5.2.4-1')
- else:
- print("An error occurred:", e)
+ if str(e) == 'duplicate column name: nginx_metrics' or str(e) == '(1060, "Duplicate column name \'nginx_metrics\'")':
+ print('Updating... DB has been updated to version 5.2.4-1')
+ else:
+ print("An error occurred:", e)
else:
print("Updating... DB has been updated to version 5.2.4-1")
-def update_db_v_5_2_5_1(**kwargs):
+def update_db_v_5_2_5_1():
query = User.update(role='user').where(User.role == 'editor')
try:
query.execute()
except Exception as e:
print("An error occurred:", e)
else:
- if kwargs.get('silent') != 1:
- print("Updating... DB has been updated to version 5.2.5-1")
+ print("Updating... DB has been updated to version 5.2.5-1")
-def update_db_v_5_2_5_2(**kwargs):
+def update_db_v_5_2_5_2():
query = Role.delete().where(Role.name == 'editor')
try:
query.execute()
except Exception as e:
print("An error occurred:", e)
else:
- if kwargs.get('silent') != 1:
- print("Updating... DB has been updated to version 5.2.5-2")
+ print("Updating... DB has been updated to version 5.2.5-2")
-def update_db_v_5_2_5_3(**kwargs):
+def update_db_v_5_2_5_3():
cursor = conn.cursor()
sql = list()
sql.append("alter table user add column last_login_date timestamp default '0000-00-00 00:00:00'")
@@ -755,22 +728,20 @@ def update_db_v_5_2_5_3(**kwargs):
except Exception:
pass
else:
- if kwargs.get('silent') != 1:
- print('Updating... DB has been updated to version 5.2.5-3')
+ print('Updating... DB has been updated to version 5.2.5-3')
-def update_db_v_5_2_6(**kwargs):
+def update_db_v_5_2_6():
query = Setting.delete().where(Setting.param == 'haproxy_enterprise')
try:
query.execute()
except Exception as e:
print("An error occurred:", e)
else:
- if kwargs.get('silent') != 1:
- print("Updating... DB has been updated to version 5.2.6")
+ print("Updating... DB has been updated to version 5.2.6")
-def update_db_v_5_3_0(**kwargs):
+def update_db_v_5_3_0():
groups = ''
query = Groups.select()
@@ -816,24 +787,22 @@ def update_db_v_5_3_0(**kwargs):
try:
Setting.insert_many(data_source).on_conflict_ignore().execute()
except Exception as e:
- if kwargs.get('silent') != 1:
- if str(e) == 'columns param, group are not unique':
- pass
- else:
- print("An error occurred:", e)
- except Exception as e:
- if kwargs.get('silent') != 1:
- if (
- str(e) == 'columns param, group are not unique'
- or str(e) == '(1062, "Duplicate entry \'nginx_container_name\' for key \'param\'")'
- or str(e) == 'UNIQUE constraint failed: settings.param, settings.group'
- ):
+ if str(e) == 'columns param, group are not unique':
pass
else:
print("An error occurred:", e)
+ except Exception as e:
+ if (
+ str(e) == 'columns param, group are not unique'
+ or str(e) == '(1062, "Duplicate entry \'nginx_container_name\' for key \'param\'")'
+ or str(e) == 'UNIQUE constraint failed: settings.param, settings.group'
+ ):
+ pass
+ else:
+ print("An error occurred:", e)
-def update_db_v_5_3_1(**kwargs):
+def update_db_v_5_3_1():
cursor = conn.cursor()
sql = """
ALTER TABLE `servers` ADD COLUMN keepalived_active INTEGER NOT NULL DEFAULT 0;
@@ -841,16 +810,15 @@ def update_db_v_5_3_1(**kwargs):
try:
cursor.execute(sql)
except Exception as e:
- if kwargs.get('silent') != 1:
- if e.args[0] == 'duplicate column name: keepalived_active' or str(e) == '(1060, "Duplicate column name \'keepalived_active\'")':
- print('Updating... DB has been updated to version 5.3.1')
- else:
- print("An error occurred:", e)
+ if e.args[0] == 'duplicate column name: keepalived_active' or str(e) == '(1060, "Duplicate column name \'keepalived_active\'")':
+ print('Updating... DB has been updated to version 5.3.1')
+ else:
+ print("An error occurred:", e)
else:
print("Updating... DB has been updated to version 5.3.1")
-def update_db_v_5_3_2_2(**kwargs):
+def update_db_v_5_3_2_2():
cursor = conn.cursor()
sql = """
ALTER TABLE `servers` ADD COLUMN keepalived_alert INTEGER NOT NULL DEFAULT 0;
@@ -858,53 +826,49 @@ def update_db_v_5_3_2_2(**kwargs):
try:
cursor.execute(sql)
except Exception as e:
- if kwargs.get('silent') != 1:
- if e.args[0] == 'duplicate column name: keepalived_alert' or str(e) == '(1060, "Duplicate column name \'keepalived_alert\'")':
- print('Updating... DB has been updated to version 5.3.2')
- else:
- print("An error occurred:", e)
+ if e.args[0] == 'duplicate column name: keepalived_alert' or str(e) == '(1060, "Duplicate column name \'keepalived_alert\'")':
+ print('Updating... DB has been updated to version 5.3.2')
+ else:
+ print("An error occurred:", e)
else:
print("Updating... DB has been updated to version 5.3.2")
-def update_db_v_5_4_2(**kwargs):
+def update_db_v_5_4_2():
cursor = conn.cursor()
sql = """ALTER TABLE `smon` ADD COLUMN slack_channel_id integer DEFAULT '0';"""
try:
cursor.execute(sql)
except Exception as e:
- if kwargs.get('silent') != 1:
- if str(e) == 'duplicate column name: slack_channel_id' or str(e) == '(1060, "Duplicate column name \'slack_channel_id\'")':
- print('Updating... DB has been updated to version 5.4.2')
- else:
- print("An error occurred:", e)
+ if str(e) == 'duplicate column name: slack_channel_id' or str(e) == '(1060, "Duplicate column name \'slack_channel_id\'")':
+ print('Updating... DB has been updated to version 5.4.2')
+ else:
+ print("An error occurred:", e)
else:
print("Updating... DB has been updated to version 5.4.2")
-def update_db_v_5_4_3(**kwargs):
+def update_db_v_5_4_3():
query = Setting.update(param='nginx_path_logs', value='/var/log/nginx/').where(Setting.param == 'nginx_path_error_logs')
try:
query.execute()
except Exception as e:
print("An error occurred:", e)
else:
- if kwargs.get('silent') != 1:
- print("Updating... DB has been updated to version 5.4.3")
+ print("Updating... DB has been updated to version 5.4.3")
-def update_db_v_5_4_3_1(**kwargs):
+def update_db_v_5_4_3_1():
query = Setting.update(value='/etc/nginx/').where(Setting.param == 'nginx_dir')
try:
query.execute()
except Exception as e:
print("An error occurred:", e)
else:
- if kwargs.get('silent') != 1:
- print("Updating... DB has been updated to version 5.4.3-1")
+ print("Updating... DB has been updated to version 5.4.3-1")
-def update_db_v_6_0(**kwargs):
+def update_db_v_6_0():
cursor = conn.cursor()
sql = list()
sql.append("alter table servers add column apache integer default 0")
@@ -917,22 +881,20 @@ def update_db_v_6_0(**kwargs):
except Exception:
pass
else:
- if kwargs.get('silent') != 1:
- print('Updating... DB has been updated to version 6.0.0.0')
+ print('Updating... DB has been updated to version 6.0.0.0')
-def update_db_v_6_0_1(**kwargs):
+def update_db_v_6_0_1():
query = Groups.update(name='Default').where(Groups.group_id == '1')
try:
query.execute()
except Exception as e:
print("An error occurred:", e)
else:
- if kwargs.get('silent') != 1:
- print("Updating... DB has been updated to version 6.0.0.0-1")
+ print("Updating... DB has been updated to version 6.0.0.0-1")
-def update_db_v_6_1_0(**kwargs):
+def update_db_v_6_1_0():
for service_id in range(1, 5):
try:
servers_id = Server.select(Server.server_id).where(Server.type_ip == 0).execute()
@@ -941,14 +903,13 @@ def update_db_v_6_1_0(**kwargs):
server_id=server_id, service_id=service_id
).on_conflict_ignore().execute()
except Exception as e:
- if kwargs.get('silent') != 1:
- if e.args[0] == 'duplicate column name: haproxy' or str(e) == '(1060, "Duplicate column name \'haproxy\'")':
- print('Updating... go to version 6.1.0')
- else:
- print("An error occurred:", e)
+ if e.args[0] == 'duplicate column name: haproxy' or str(e) == '(1060, "Duplicate column name \'haproxy\'")':
+ print('Updating... go to version 6.1.0')
+ else:
+ print("An error occurred:", e)
-def update_db_v_6_1_3(**kwargs):
+def update_db_v_6_1_3():
if mysql_enable == '1':
cursor = conn.cursor()
sql = list()
@@ -961,8 +922,7 @@ def update_db_v_6_1_3(**kwargs):
except Exception:
pass
else:
- if kwargs.get('silent') != 1:
- print('Updating... DB has been updated to version 6.1.3.0')
+ print('Updating... DB has been updated to version 6.1.3.0')
else:
pass
@@ -988,18 +948,14 @@ def update_db_v_6_1_4():
def update_ver():
- query = Version.update(version='6.2.0.0')
try:
- query.execute()
+ Version.update(version='6.2.0.0').execute()
except Exception:
print('Cannot update version')
def update_all():
- if check_ver() is None:
- update_db_v_3_4_5_22()
update_db_v_4_3_0()
- update_db_v_5_1_3()
update_db_v_5_2_4()
update_db_v_5_2_4_1()
update_db_v_5_2_5_1()
@@ -1020,30 +976,6 @@ def update_all():
update_ver()
-def update_all_silent():
- if check_ver() is None:
- update_db_v_3_4_5_22()
- update_db_v_4_3_0(silent=1)
- update_db_v_5_1_3(silent=1)
- update_db_v_5_2_4(silent=1)
- update_db_v_5_2_4_1(silent=1)
- update_db_v_5_2_5_1(silent=1)
- update_db_v_5_2_5_2(silent=1)
- update_db_v_5_2_5_3(silent=1)
- update_db_v_5_2_6(silent=1)
- update_db_v_5_3_0(silent=1)
- update_db_v_5_3_1(silent=1)
- update_db_v_5_3_2_2(silent=1)
- update_db_v_5_4_2(silent=1)
- update_db_v_5_4_3(silent=1)
- update_db_v_5_4_3_1(silent=1)
- update_db_v_6_0(silent=1)
- update_db_v_6_0_1(silent=1)
- update_db_v_6_1_3(silent=1)
- update_db_v_6_1_4()
- update_ver()
-
-
if __name__ == "__main__":
create_tables()
default_values()
diff --git a/app/funct.py b/app/funct.py
index 1b10875a..f5b0759c 100644
--- a/app/funct.py
+++ b/app/funct.py
@@ -108,7 +108,7 @@ def get_data(log_type, **kwargs):
return now_utc.strftime(fmt)
-def get_user_group(**kwargs):
+def get_user_group(**kwargs) -> str:
import sql
user_group = ''
@@ -129,21 +129,23 @@ def get_user_group(**kwargs):
return user_group
-def logging(server_ip, action, **kwargs):
+def logging(server_ip: str, action: str, **kwargs) -> None:
import sql
import distro
login = ''
-
+ cur_date = get_data('logs')
+ cur_date_in_log = get_data('date_in_log')
log_path = get_config_var('main', 'log_path')
+
+ if not os.path.exists(log_path):
+ os.makedirs(log_path)
+
try:
user_group = get_user_group()
except Exception:
user_group = ''
- if not os.path.exists(log_path):
- os.makedirs(log_path)
-
try:
ip = cgi.escape(os.environ["REMOTE_ADDR"])
except Exception:
@@ -171,33 +173,30 @@ def logging(server_ip, action, **kwargs):
if kwargs.get('haproxywi') == 1:
if kwargs.get('login'):
- mess = get_data('date_in_log') + " from " + ip + " user: " + login + ", group: " + user_group + ", " + \
- action + " for: " + server_ip + "\n"
+ mess = f"{cur_date_in_log} from {ip} user: {login}, group: {user_group}, {action} for: {server_ip}\n"
if kwargs.get('keep_history'):
try:
keep_action_history(kwargs.get('service'), action, server_ip, login, ip)
except Exception as e:
print(str(e))
else:
- mess = get_data('date_in_log') + " " + action + " from " + ip + "\n"
- log = open(log_path + "/roxy-wi-" + get_data('logs') + ".log", "a")
+ mess = f"{cur_date_in_log} {action} from {ip}\n"
+ log_file = f"{log_path}/roxy-wi-{cur_date}.log"
elif kwargs.get('provisioning') == 1:
- mess = get_data('date_in_log') + " from " + ip + " user: " + login + ", group: " + user_group + ", " + \
- action + "\n"
- log = open(log_path + "/provisioning-" + get_data('logs') + ".log", "a")
+ mess = f"{cur_date_in_log} from {ip} user: {login}, group: {user_group}, {action}\n"
+ log_file = f"{log_path}/provisioning-{cur_date}.log"
else:
- mess = get_data('date_in_log') + " from " + ip + " user: " + login + ", group: " + user_group + ", " + \
- action + " for: " + server_ip + "\n"
- log = open(log_path + "/config_edit-" + get_data('logs') + ".log", "a")
+ mess = f"{cur_date_in_log} from {ip} user: {login}, group: {user_group}, {action} for: {server_ip}\n"
+ log_file = f"{log_path}/config_edit-{cur_date}.log"
if kwargs.get('keep_history'):
keep_action_history(kwargs.get('service'), action, server_ip, login, ip)
try:
- log.write(mess)
- log.close()
+ with open(log_file, 'a') as log:
+ log.write(mess)
except IOError as e:
- print('
Cannot write log. Please check log_path in config %e
' % e)
+ print(f'
Cannot write log. Please check log_path in config {str(e)}
')
def keep_action_history(service: str, action: str, server_ip: str, login: str, user_ip: str):
@@ -213,7 +212,7 @@ def keep_action_history(service: str, action: str, server_ip: str, login: str, u
sql.insert_action_history(service, action, server_id, user_id, user_ip)
except Exception as e:
- logging('localhost', 'Cannot save a history: ' + str(e), haproxywi=1)
+ logging('localhost', f'Cannot save a history: {str(e)}', haproxywi=1)
def telegram_send_mess(mess, **kwargs):
@@ -239,7 +238,6 @@ def telegram_send_mess(mess, **kwargs):
if token_bot == '' or channel_name == '':
mess = " Can't send message. Add Telegram channel before use alerting at this servers group"
- print(mess)
logging('localhost', mess, haproxywi=1)
if proxy is not None and proxy != '' and proxy != 'None':
@@ -248,7 +246,6 @@ def telegram_send_mess(mess, **kwargs):
bot = telebot.TeleBot(token=token_bot)
bot.send_message(chat_id=channel_name, text=mess)
except Exception as e:
- print(str(e))
logging('localhost', str(e), haproxywi=1)
@@ -282,7 +279,6 @@ def slack_send_mess(mess, **kwargs):
try:
client.chat_postMessage(channel='#' + channel_name, text=mess)
except SlackApiError as e:
- print('error: ' + str(e))
logging('localhost', str(e), haproxywi=1)
@@ -1178,44 +1174,36 @@ def show_log(stdout, **kwargs):
if kwargs.get('grep'):
grep = kwargs.get('grep')
grep = re.sub(r'[?|$|.|!|^|*|\]|\[|,| |]', r'', grep)
-
for line in stdout:
- if kwargs.get("html") != 0:
- i = i + 1
- if kwargs.get('grep'):
- line = line.replace(grep, '' + grep + '')
- line_class = "line3" if i % 2 == 0 else "line"
- out += '
' + line + '
'
- else:
- out += line
+ i = i + 1
+ if kwargs.get('grep'):
+ line = line.replace(grep, f'{grep}')
+ line_class = "line3" if i % 2 == 0 else "line"
+ out += f'
{line}
'
return out
def show_finding_in_config(stdout: str, **kwargs) -> str:
- i = 0
- out = ''
grep = ''
- line_class = 'line'
+ out = '
--
'
if kwargs.get('grep'):
grep = kwargs.get('grep')
grep = re.sub(r'[?|$|!|^|*|\]|\[|,| |]', r'', grep)
- out += '
--
'
for line in stdout:
- i = i + 1
if kwargs.get('grep'):
- line = line.replace(grep, '' + grep + '')
- line_class = "line" if '--' in line else "line3"
- out += '
' + line + '
'
+ line = line.replace(grep, f'{grep}')
+ line_class = "line" if '--' in line else "line3"
+ out += f'