diff --git a/app/add.py b/app/add.py index a13fb20a..47a28a48 100644 --- a/app/add.py +++ b/app/add.py @@ -25,6 +25,7 @@ try: servers = sql.get_dick_permit() user_group = sql.get_user_group_by_uuid(user_id.value) token = sql.get_token(user_id.value) + except: pass @@ -37,6 +38,7 @@ template = template.render(title = "Add", conf_add = form.getvalue('conf'), group = user_group, versions = funct.versions(), + options = sql.select_options(), token = token) print(template) diff --git a/app/configver.py b/app/configver.py deleted file mode 100644 index cc9cf498..00000000 --- a/app/configver.py +++ /dev/null @@ -1,72 +0,0 @@ -#!/usr/bin/env python3 -import html -import cgi -import os, http.cookies -import funct -import sql -from jinja2 import Environment, FileSystemLoader -env = Environment(loader=FileSystemLoader('templates/')) -template = env.get_template('configver.html') - -funct.check_login() -funct.page_for_admin(level = 2) - -form = cgi.FieldStorage() -serv = form.getvalue('serv') -view = form.getvalue('view') -hap_configs_dir = funct.get_config_var('configs', 'haproxy_save_configs_dir') -stderr = "" -aftersave = "" -configver = "" - -try: - cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) - user_id = cookie.get('uuid') - user = sql.get_user_name_by_uuid(user_id.value) - servers = sql.get_dick_permit() - token = sql.get_token(user_id.value) -except: - pass - - -if serv is not None and form.getvalue('config') is not None: - configver = form.getvalue('configver') - configver = hap_configs_dir + configver - save = form.getvalue('save') - aftersave = 1 - try: - funct.logging(serv, "configver.py upload old config %s" % configver) - except: - pass - - MASTERS = sql.is_master(serv) - for master in MASTERS: - if master[0] != None: - funct.upload_and_restart(master[0], configver, just_save=save) - - stderr = funct.upload_and_restart(serv, configver, just_save=save) - - if save: - c = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) - c["restart"] = form.getvalue('serv') - print(c) - -print('Content-type: text/html\n') -template = template.render(h2 = 1, title = "Old Versions HAProxy config", - role = sql.get_user_role_by_uuid(user_id.value), - action = "configver.py", - user = user, - select_id = "serv", - serv = serv, - return_files = funct.get_files(), - aftersave = aftersave, - configver = configver, - selects = servers, - stderr = stderr, - open = form.getvalue('open'), - onclick = "showUploadConfig()", - note = 1, - token = token, - versions = funct.versions(), - view = view) -print(template) diff --git a/app/create_db.py b/app/create_db.py index 04021234..c7e74113 100644 --- a/app/create_db.py +++ b/app/create_db.py @@ -122,6 +122,8 @@ def create_table(**kwargs): CREATE TABLE IF NOT EXISTS `metrics` (`serv` varchar(64), curr_con INTEGER, cur_ssl_con INTEGER, sess_rate INTEGER, max_sess_rate INTEGER,`date` timestamp default '0000-00-00 00:00:00'); CREATE TABLE IF NOT EXISTS `settings` (`param` varchar(64) UNIQUE, value varchar(64), section varchar(64), `desc` varchar(100)); CREATE TABLE IF NOT EXISTS `version` (`version` varchar(64)); + CREATE TABLE IF NOT EXISTS `options` ( `id` INTEGER NOT NULL, `options` VARCHAR ( 64 ), `groups` VARCHAR ( 120 ), PRIMARY KEY(`id`)); + ); """ try: cur.executescript(sql) @@ -377,7 +379,7 @@ def update_db_v_3_4_5_22(**kwargs): def update_ver(**kwargs): con, cur = get_cur() - sql = """update version set version = '3.4.6.1'; """ + sql = """update version set version = '3.4.7'; """ try: cur.execute(sql) con.commit() diff --git a/app/edit.py b/app/edit.py index 6eccb56d..5a143e75 100644 --- a/app/edit.py +++ b/app/edit.py @@ -24,11 +24,7 @@ try: servbackend = "" except: pass - -try: - current_ver = funct.check_ver() -except: - pass + output_from_parsed_template = template.render(h2 = 1, title = "Runtime API", @@ -39,6 +35,6 @@ output_from_parsed_template = template.render(h2 = 1, selects = servers, token = token, serv = serv, - current_ver = current_ver, + versions = funct.versions(), servbackend = servbackend) print(output_from_parsed_template) \ No newline at end of file diff --git a/app/funct.py b/app/funct.py index 13ed46d2..ec1a749d 100644 --- a/app/funct.py +++ b/app/funct.py @@ -99,7 +99,7 @@ def telegram_send_mess(mess, **kwargs): bot = telebot.TeleBot(token=token_bot) bot.send_message(chat_id=channel_name, text=mess) except: - mess = "Fatal: Can't send message. Add Telegram chanel before use alerting at this servers group" + mess = " Fatal: Can't send message. Add Telegram chanel before use alerting at this servers group" print(mess) logging('localhost', mess, haproxywi=1) sys.exit() diff --git a/app/haproxy-wi.db.sql b/app/haproxy-wi.db.sql index 392189b6..2298262a 100644 --- a/app/haproxy-wi.db.sql +++ b/app/haproxy-wi.db.sql @@ -14,4 +14,6 @@ CREATE TABLE IF NOT EXISTS `token` (`user_id` INTEGER, `token` varchar(64), `exp CREATE TABLE IF NOT EXISTS `cred` (`id` integer primary key AUTO_INCREMENT, `name` VARCHAR ( 64 ) UNIQUE, `enable` INTEGER NOT NULL DEFAULT 1, `username` VARCHAR ( 64 ) NOT NULL, `password` VARCHAR ( 64 ) NOT NULL, groups INTEGER NOT NULL DEFAULT 1 ); CREATE TABLE IF NOT EXISTS `telegram` (`id` integer primary key auto_increment, `token` VARCHAR ( 64 ), `chanel_name` INTEGER NOT NULL DEFAULT 1, `groups` INTEGER NOT NULL DEFAULT 1); CREATE TABLE IF NOT EXISTS `metrics` (`serv` varchar(64), curr_con INTEGER, cur_ssl_con INTEGER, sess_rate INTEGER, max_sess_rate INTEGER,`date` DATETIME default '0000-00-00 00:00:00'); -CREATE TABLE IF NOT EXISTS `settings` (`param` varchar(64) UNIQUE, value varchar(64), section varchar(64), `desc` varchar(100)); \ No newline at end of file +CREATE TABLE IF NOT EXISTS `settings` (`param` varchar(64) UNIQUE, value varchar(64), section varchar(64), `desc` varchar(100)); +CREATE TABLE IF NOT EXISTS `version` (`version` varchar(64)); +CREATE TABLE IF NOT EXISTS `options` ( `id` INTEGER NOT NULL, `options` VARCHAR ( 64 ), `groups` VARCHAR ( 120 ), PRIMARY KEY(`id`)); \ No newline at end of file diff --git a/app/sql.py b/app/sql.py index 1ca24b0a..d7e1d336 100644 --- a/app/sql.py +++ b/app/sql.py @@ -18,6 +18,7 @@ def out_error(e): error = e else: error = e.args[0] + print('Content-type: text/html\n') print('An error occurred: ' + error + ' X') def add_user(user, email, password, role, group, activeuser): @@ -612,6 +613,20 @@ def select_telegram(**kwargs): cur.close() con.close() +def insert_new_telegram(token, chanel, group): + con, cur = create_db.get_cur() + sql = """insert into telegram(`token`, `chanel_name`, `groups`) values ('%s', '%s', '%s') """ % (token, chanel, group) + try: + cur.execute(sql) + con.commit() + except sqltool.Error as e: + print('An error occurred: ' + e.args[0] + ' X') + con.rollback() + else: + return True + cur.close() + con.close() + def update_telegram(token, chanel, group, id): con, cur = create_db.get_cur() sql = """ update telegram set @@ -628,6 +643,66 @@ def update_telegram(token, chanel, group, id): cur.close() con.close() +def insert_new_option(option, group): + con, cur = create_db.get_cur() + sql = """insert into options(`options`, `groups`) values ('%s', '%s') """ % (option, group) + try: + cur.execute(sql) + con.commit() + except sqltool.Error as e: + out_error(e) + con.rollback() + else: + return True + cur.close() + con.close() + +def select_options(**kwargs): + con, cur = create_db.get_cur() + sql = """select * from options """ + if kwargs.get('option'): + sql = """select * from options where options = '%s' """ % kwargs.get('option') + if kwargs.get('group'): + sql = """select options from options where groups = '{}' and options like '{}%' """.format(kwargs.get('group'), kwargs.get('term')) + #print(sql) + try: + cur.execute(sql) + except sqltool.Error as e: + out_error(e) + else: + return cur.fetchall() + cur.close() + con.close() + +def update_options(option, id): + con, cur = create_db.get_cur() + sql = """ update options set + `option` = '%s', + where id = '%s' """ % (option, id) + try: + cur.execute(sql) + con.commit() + except sqltool.Error as e: + out_error(e) + con.rollback() + cur.close() + con.close() + +def delete_option(id): + con, cur = create_db.get_cur() + sql = """ delete from options where id = %s """ % (id) + try: + cur.execute(sql) + con.commit() + except sqltool.Error as e: + out_error(e) + con.rollback() + else: + return True + cur.close() + con.close() + + def insert_mentrics(serv, curr_con, cur_ssl_con, sess_rate, max_sess_rate): con, cur = create_db.get_cur() if mysql_enable == '1': @@ -1102,6 +1177,15 @@ def get_ver(): cur.close() con.close() +def show_update_option(option): + from jinja2 import Environment, FileSystemLoader + env = Environment(loader=FileSystemLoader('templates/ajax')) + template = env.get_template('/new_option.html') + + print('Content-type: text/html\n') + template = template.render(options=select_options(option=option)) + print(template) + def show_update_telegram(token, page): from jinja2 import Environment, FileSystemLoader env = Environment(loader=FileSystemLoader('templates/ajax')) @@ -1371,6 +1455,45 @@ if form.getvalue('telegramdel') is not None: print('Content-type: text/html\n') if delete_telegram(form.getvalue('telegramdel')): print("Ok") + +if form.getvalue('getoption'): + group = form.getvalue('getoption') + term = form.getvalue('term') + print('Content-type: application/json\n') + options = select_options(group=group,term=term) + a = "" + a = {} + v = 0 + for i in options: + a[v] = i[0] + v = v + 1 + import json + print(json.dumps(a)) + + +if form.getvalue('newtoption'): + option = form.getvalue('newtoption') + group = form.getvalue('newoptiongroup') + if option is None or group is None: + print('Content-type: text/html\n') + print(error_mess) + else: + if insert_new_option(option, group): + show_update_option(option) + +if form.getvalue('updateoption') is not None: + option = form.getvalue('updateoption') + id = form.getvalue('optionid') + print('Content-type: text/html\n') + if token is None or chanel is None or group is None: + print(error_mess) + else: + update_options(option, id) + +if form.getvalue('optiondel') is not None: + print('Content-type: text/html\n') + if delete_option(form.getvalue('optiondel')): + print("Ok") if form.getvalue('updatetoken') is not None: token = form.getvalue('updatetoken') diff --git a/app/templates/add.html b/app/templates/add.html index 00d9f26e..5a78e43a 100644 --- a/app/templates/add.html +++ b/app/templates/add.html @@ -6,6 +6,7 @@
Id | +Optinons | ++ |
+ {{ option.0 }} + | ++ + | ++ + | + {% endif %} +