mirror of https://github.com/Aidaho12/haproxy-wi
parent
23cdc92940
commit
cd462dcb7d
18
README.md
18
README.md
|
@ -25,9 +25,9 @@ For install just [dowload](https://github.com/Aidaho12/haproxy-wi/archive/master
|
|||
$ cd /var/www/
|
||||
$ unzip master.zip
|
||||
$ mv haproxy-wi-master/ haproxy-wi
|
||||
$ chown -R apache:apache haproxy-wi/
|
||||
$ pip install -r haproxy-wi/requirements.txt
|
||||
$ cd haproxy-wi/cgi-bin
|
||||
$ chmod +x *.py
|
||||
$ chmod +x haproxy-wi/cgi-bin/*.py
|
||||
```
|
||||
|
||||
For Apache do virtualhost with cgi-bin. Like this:
|
||||
|
@ -48,7 +48,16 @@ For Apache do virtualhost with cgi-bin. Like this:
|
|||
</Directory>
|
||||
</VirtualHost>
|
||||
```
|
||||
# Database support
|
||||
|
||||
Default Haproxy-WI use Sqlite, if you want use MySQL enable in config, and create database:
|
||||
|
||||
## For MySQL support:
|
||||
```
|
||||
MariaDB [(none)]> create user 'haproxy-wi'@'%';
|
||||
MariaDB [(none)]> create database haproxywi;
|
||||
MariaDB [(none)]> grant all on haproxywi.* to 'haproxy-wi'@'%' IDENTIFIED BY 'haproxy-wi';
|
||||
```
|
||||
![alt text](image/haproxy-wi-overview.jpeg "Overview page")
|
||||
|
||||
# Settings
|
||||
|
@ -64,9 +73,10 @@ Copy ssh key on all HAproxy servers
|
|||
For Runtime API enable state file on HAproxt servers and need install socat on all haproxy servers:
|
||||
```
|
||||
global
|
||||
server-state-file /etc/haproxy/haproxy/haproxy.state
|
||||
stats socket *:1999 level admin
|
||||
server-state-file /etc/haproxy/haproxy/haproxy.state
|
||||
defaults
|
||||
load-server-state-from-file global
|
||||
load-server-state-from-file global
|
||||
```
|
||||
![alt text](image/haproxy-wi-logs.jpeg "View logs page")
|
||||
|
||||
|
|
|
@ -1,116 +1,157 @@
|
|||
#!/usr/bin/env python3
|
||||
import cgi
|
||||
import html
|
||||
import sqlite3 as sqlite
|
||||
import os
|
||||
import sys
|
||||
from configparser import ConfigParser, ExtendedInterpolation
|
||||
|
||||
db = "haproxy-wi.db"
|
||||
path_config = "haproxy-webintarface.config"
|
||||
config = ConfigParser(interpolation=ExtendedInterpolation())
|
||||
config.read(path_config)
|
||||
|
||||
mysql_enable = config.get('mysql', 'enable')
|
||||
|
||||
if mysql_enable == '1':
|
||||
mysql_user = config.get('mysql', 'mysql_user')
|
||||
mysql_password = config.get('mysql', 'mysql_password')
|
||||
mysql_db = config.get('mysql', 'mysql_db')
|
||||
mysql_host = config.get('mysql', 'mysql_host')
|
||||
from mysql.connector import errorcode
|
||||
import mysql.connector as sqltool
|
||||
else:
|
||||
db = "haproxy-wi.db"
|
||||
import sqlite3 as sqltool
|
||||
|
||||
def check_db():
|
||||
if os.path.isfile(db):
|
||||
if os.path.getsize(db) > 100:
|
||||
with open(db,'r', encoding = "ISO-8859-1") as f:
|
||||
header = f.read(100)
|
||||
if header.startswith('SQLite format 3'):
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
if mysql_enable == '0':
|
||||
if os.path.isfile(db):
|
||||
if os.path.getsize(db) > 100:
|
||||
with open(db,'r', encoding = "ISO-8859-1") as f:
|
||||
header = f.read(100)
|
||||
if header.startswith('SQLite format 3'):
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
else:
|
||||
return True
|
||||
else:
|
||||
return True
|
||||
|
||||
con, cur = get_cur()
|
||||
sql = """ select id from `groups` where id='1' """
|
||||
try:
|
||||
cur.execute(sql)
|
||||
except sqltool.Error as err:
|
||||
#print('<div class="alert alert-danger">')
|
||||
#if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
|
||||
# print("Something is wrong with your user name or password")
|
||||
#elif err.errno == errorcode.ER_BAD_DB_ERROR:
|
||||
# print("Database does not exist")
|
||||
#else:
|
||||
# print(err)
|
||||
print('</div>')
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
con.close()
|
||||
|
||||
def get_cur():
|
||||
con = sqlite.connect(db, isolation_level=None)
|
||||
cur = con.cursor()
|
||||
if mysql_enable == '0':
|
||||
con = sqltool.connect(db, isolation_level=None)
|
||||
else:
|
||||
con = sqltool.connect(user=mysql_user, password=mysql_password,
|
||||
host=mysql_host,
|
||||
database=mysql_db)
|
||||
cur = con.cursor()
|
||||
return con, cur
|
||||
|
||||
|
||||
def create_table():
|
||||
con, cur = get_cur()
|
||||
sql = """
|
||||
BEGIN TRANSACTION;
|
||||
CREATE TABLE IF NOT EXISTS `user` (
|
||||
if mysql_enable == '0':
|
||||
sql = """
|
||||
CREATE TABLE IF NOT EXISTS user (
|
||||
`id` INTEGER NOT NULL,
|
||||
`username` VARCHAR ( 64 ) UNIQUE,
|
||||
`email` VARCHAR ( 120 ) UNIQUE,
|
||||
`password` VARCHAR ( 128 ),
|
||||
`role` VARCHAR ( 128 ),
|
||||
`groups` VARCHAR ( 120 ),
|
||||
PRIMARY KEY(`id`)
|
||||
);
|
||||
INSERT INTO user (username, email, password, role, groups) VALUES ('admin','admin@localhost','admin','admin','1'),
|
||||
('editor','editor@localhost','editor','editor','1'),
|
||||
('guest','guest@localhost','guest','guest','1');
|
||||
CREATE TABLE IF NOT EXISTS `servers` (
|
||||
`id` INTEGER NOT NULL,
|
||||
`hostname` VARCHAR ( 64 ) UNIQUE,
|
||||
`ip` VARCHAR ( 64 ) UNIQUE,
|
||||
`groups` VARCHAR ( 64 ),
|
||||
PRIMARY KEY(`id`)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS `roles_users` (
|
||||
`user_id` INTEGER,
|
||||
`role_id` INTEGER,
|
||||
FOREIGN KEY(`user_id`) REFERENCES `user`(`id`),
|
||||
FOREIGN KEY(`role_id`) REFERENCES `role`(`id`)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS `role` (
|
||||
`id` INTEGER NOT NULL,
|
||||
`name` VARCHAR ( 80 ) UNIQUE,
|
||||
`description` VARCHAR ( 255 ),
|
||||
PRIMARY KEY(`id`)
|
||||
);
|
||||
INSERT INTO `role` (name, description) VALUES ('admin','Can do everything'),
|
||||
('editor','Can edit configs'),
|
||||
('guest','Read only access');
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `groups` (
|
||||
`id` INTEGER NOT NULL,
|
||||
`username` VARCHAR ( 64 ) UNIQUE,
|
||||
`email` VARCHAR ( 120 ) UNIQUE,
|
||||
`password` VARCHAR ( 128 ),
|
||||
`role` VARCHAR ( 128 ),
|
||||
`groups` VARCHAR ( 120 ),
|
||||
PRIMARY KEY(`id`)
|
||||
);
|
||||
INSERT INTO `user` (username, email, password, role, groups) VALUES ('admin','admin@localhost','admin','admin','1'),
|
||||
('editor','editor@localhost','editor','editor','1'),
|
||||
('guest','guest@localhost','guest','guest','1');
|
||||
CREATE TABLE IF NOT EXISTS `servers` (
|
||||
`name` VARCHAR ( 80 ) UNIQUE,
|
||||
`description` VARCHAR ( 255 ),
|
||||
PRIMARY KEY(`id`)
|
||||
);
|
||||
INSERT INTO `groups` (name, description) VALUES ('All','All servers enter in this group');
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `servers` (
|
||||
`id` INTEGER NOT NULL,
|
||||
`hostname` VARCHAR ( 64 ) UNIQUE,
|
||||
`ip` VARCHAR ( 64 ) UNIQUE,
|
||||
`groups` VARCHAR ( 64 ),
|
||||
PRIMARY KEY(`id`)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS `roles_users` (
|
||||
`user_id` INTEGER,
|
||||
`role_id` INTEGER,
|
||||
FOREIGN KEY(`user_id`) REFERENCES `user`(`id`),
|
||||
FOREIGN KEY(`role_id`) REFERENCES `role`(`id`)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS `role` (
|
||||
`id` INTEGER NOT NULL,
|
||||
`name` VARCHAR ( 80 ) UNIQUE,
|
||||
`description` VARCHAR ( 255 ),
|
||||
PRIMARY KEY(`id`)
|
||||
);
|
||||
INSERT INTO `role` (name, description) VALUES ('admin','Can do everything'),
|
||||
('editor','Can edit configs'),
|
||||
('guest','Read only access');
|
||||
CREATE TABLE IF NOT EXISTS `groups` (
|
||||
`id` INTEGER NOT NULL,
|
||||
`name` VARCHAR ( 80 ) UNIQUE,
|
||||
`description` VARCHAR ( 255 ),
|
||||
PRIMARY KEY(`id`)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `groups` (
|
||||
`id` INTEGER NOT NULL,
|
||||
`name` VARCHAR ( 80 ) UNIQUE,
|
||||
`description` VARCHAR ( 255 ),
|
||||
PRIMARY KEY(`id`)
|
||||
);
|
||||
INSERT INTO `groups` (name, description) VALUES ('All','All servers enter in this group');
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `servers` (
|
||||
`id` INTEGER NOT NULL,
|
||||
`hostname` VARCHAR ( 64 ) UNIQUE,
|
||||
`ip` VARCHAR ( 64 ) UNIQUE,
|
||||
`groups` VARCHAR ( 64 ),
|
||||
PRIMARY KEY(`id`)
|
||||
);
|
||||
|
||||
COMMIT;
|
||||
"""
|
||||
|
||||
try:
|
||||
cur.executescript(sql)
|
||||
except sqlite.Error as e:
|
||||
print("An error occurred:", e.args[0])
|
||||
return False
|
||||
PRIMARY KEY(`id`)
|
||||
);
|
||||
"""
|
||||
try:
|
||||
cur.executescript(sql)
|
||||
except sqltool.Error as e:
|
||||
print("An error occurred:", e)
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
else:
|
||||
return True
|
||||
try:
|
||||
for line in open("haproxy-wi.db.sql"):
|
||||
cur.execute(line)
|
||||
except sqltool.Error as e:
|
||||
print('<div class="alert alert-danger">')
|
||||
print("An error occurred:", e)
|
||||
print('</div>')
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
cur.close()
|
||||
con.close()
|
||||
|
||||
def update_db_v_2_0_1():
|
||||
con, cur = get_cur()
|
||||
sql = """
|
||||
ALTER TABLE servers ADD COLUMN type_ip INTEGER NOT NULL DEFAULT(0);
|
||||
ALTER TABLE `servers` ADD COLUMN type_ip INTEGER NOT NULL DEFAULT 0;
|
||||
"""
|
||||
try:
|
||||
cur.execute(sql)
|
||||
except sqlite.Error as e:
|
||||
except sqltool.Error as e:
|
||||
if e.args[0] == 'duplicate column name: type_ip':
|
||||
print('Updating... go to version 2.0.1.1')
|
||||
return False
|
||||
else:
|
||||
print("An error occurred:", e.args[0])
|
||||
print("An error occurred:", e)
|
||||
return False
|
||||
else:
|
||||
print("DB was update to 2.0.1")
|
||||
|
@ -121,16 +162,16 @@ def update_db_v_2_0_1():
|
|||
def update_db_v_2_0_1_1():
|
||||
con, cur = get_cur()
|
||||
sql = """
|
||||
ALTER TABLE `servers` ADD COLUMN enable INTEGER NOT NULL DEFAULT(1);
|
||||
ALTER TABLE `servers` ADD COLUMN enable INTEGER NOT NULL DEFAULT 1;
|
||||
"""
|
||||
try:
|
||||
cur.executescript(sql)
|
||||
except sqlite.Error as e:
|
||||
cur.execute(sql)
|
||||
except sqltool.Error as e:
|
||||
if e.args[0] == 'duplicate column name: enable':
|
||||
print('Already updated. No run more. Thx =^.^=')
|
||||
return False
|
||||
else:
|
||||
print("An error occurred:", e.args[0])
|
||||
print("An error occurred:", e)
|
||||
return False
|
||||
else:
|
||||
print("DB was update to 2.0.1.1")
|
||||
|
|
|
@ -146,7 +146,7 @@ def head(title):
|
|||
'<div class="top-menu">'
|
||||
'<div class="LogoText">'
|
||||
'<span style="padding: 10px;">HAproxy-WI</span>'
|
||||
'<a href="#" id="hide_menu" title="Hide menu" style="margin-left: 16px;margin-top: -10px;position: absolute;">'
|
||||
'<a href="#" id="hide_menu" title="Hide menu" style="margin-left: 23px; position: absolute;">'
|
||||
'<span class="ui-state-default ui-corner-all">'
|
||||
'<span class="ui-icon ui-icon-arrowthick-1-w" id="arrow"></span>'
|
||||
'</span>'
|
||||
|
@ -194,7 +194,7 @@ def links():
|
|||
'</li>')
|
||||
print('</ul>'
|
||||
'</nav>'
|
||||
'<div class="copyright-menu">HAproxy-WI v2.0.3</div>'
|
||||
'<div class="copyright-menu">HAproxy-WI v2.0.4</div>'
|
||||
'</div>')
|
||||
|
||||
def show_login_links():
|
||||
|
@ -528,7 +528,7 @@ def chooseServer(formName, title, note, **kwargs):
|
|||
|
||||
print('</p></form>')
|
||||
|
||||
if note == "y":
|
||||
print('<p><b>Note:</b> If you reconfigure First server, second will reconfigured automatically</p>')
|
||||
#if note == "y":
|
||||
# print('<p><b>Note:</b> If you reconfigure First server, second will reconfigured automatically</p>')
|
||||
print('</center>')
|
||||
|
||||
|
|
|
@ -11,6 +11,14 @@ haproxy_configs_server = localhost
|
|||
#Dir where configs will be save
|
||||
haproxy_save_configs_dir = /var/www/haproxy-wi/cgi-bin/hap_config/
|
||||
|
||||
[mysql]
|
||||
#Enable MySQL DB. If default will be used Sqlite DB. Default disable
|
||||
enable = 0
|
||||
mysql_user = haproxy-wi
|
||||
mysql_password = haproxy-wi
|
||||
mysql_db = haproxywi
|
||||
mysql_host = 127.0.0.1
|
||||
|
||||
[ssh]
|
||||
#If ssh connect disable entare password for ssh connect. Default enable
|
||||
ssh_keys_enable = 1
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
CREATE TABLE IF NOT EXISTS `user` (`id` INTEGER NOT NULL AUTO_INCREMENT,`username` VARCHAR ( 64 ) UNIQUE,`email` VARCHAR ( 120 ) UNIQUE, `password` VARCHAR ( 128 ),`role` VARCHAR ( 128 ),`groups` VARCHAR ( 120 ),PRIMARY KEY(`id`) );
|
||||
INSERT INTO `user` (username, email, password, role, groups) VALUES ('admin','admin@localhost','admin','admin','1');
|
||||
INSERT INTO `user` (username, email, password, role, groups) VALUES ('editor','editor@localhost','editor','editor','1');
|
||||
INSERT INTO `user` (username, email, password, role, groups) VALUES ('guest','guest@localhost','guest','guest','1');
|
||||
CREATE TABLE IF NOT EXISTS `servers` (`id` INTEGER NOT NULL AUTO_INCREMENT,`hostname` VARCHAR ( 64 ) UNIQUE,`ip` VARCHAR ( 64 ) UNIQUE,`groups` VARCHAR ( 64 ),PRIMARY KEY(`id`) );
|
||||
CREATE TABLE IF NOT EXISTS `role` (`id` INTEGER NOT NULL AUTO_INCREMENT,`name` VARCHAR ( 80 ) UNIQUE,`description` VARCHAR ( 255 ),PRIMARY KEY(`id`) );
|
||||
INSERT INTO `role` (name, description) VALUES ('admin','Can do everything');
|
||||
INSERT INTO `role` (name, description) VALUES ('editor','Can edit configs');
|
||||
INSERT INTO `role` (name, description) VALUES ('guest','Read only access');
|
||||
CREATE TABLE IF NOT EXISTS `groups` (`id` INTEGER NOT NULL AUTO_INCREMENT,`name` VARCHAR ( 80 ) UNIQUE,`description` VARCHAR ( 255 ),PRIMARY KEY(`id`));
|
||||
INSERT INTO `groups` (name, description) VALUES ('All','All servers enter in this group');
|
||||
CREATE TABLE IF NOT EXISTS `servers` (`id` INTEGER NOT NULL AUTO_INCREMENT,`hostname` VARCHAR ( 64 ) UNIQUE,`ip` VARCHAR ( 64 ) UNIQUE,`groups` VARCHAR ( 64 ), PRIMARY KEY(`id`));
|
|
@ -78,7 +78,4 @@ if login is not None and password is not None:
|
|||
sys.exit()
|
||||
login_page("error")
|
||||
|
||||
funct.footer()
|
||||
|
||||
|
||||
|
||||
funct.footer()
|
140
cgi-bin/sql.py
140
cgi-bin/sql.py
|
@ -1,21 +1,31 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-"
|
||||
import sqlite3 as sqlite
|
||||
import cgi
|
||||
|
||||
def get_cur():
|
||||
con = sqlite.connect("haproxy-wi.db", isolation_level=None)
|
||||
cur = con.cursor()
|
||||
return con, cur
|
||||
import create_db
|
||||
from configparser import ConfigParser, ExtendedInterpolation
|
||||
|
||||
path_config = "haproxy-webintarface.config"
|
||||
config = ConfigParser(interpolation=ExtendedInterpolation())
|
||||
config.read(path_config)
|
||||
|
||||
mysql_enable = config.get('mysql', 'enable')
|
||||
|
||||
if mysql_enable == '1':
|
||||
from mysql.connector import errorcode
|
||||
import mysql.connector as sqltool
|
||||
else:
|
||||
db = "haproxy-wi-test.db"
|
||||
import sqlite3 as sqltool
|
||||
|
||||
def add_user(user, email, password, role, group):
|
||||
con, cur = get_cur()
|
||||
con, cur = create_db.get_cur()
|
||||
sql = """INSERT INTO user (username, email, password, role, groups) VALUES ('%s', '%s', '%s', '%s', '%s')""" % (user, email, password, role, group)
|
||||
try:
|
||||
with con:
|
||||
cur.executescript(sql)
|
||||
except sqlite.Error as e:
|
||||
cur.execute(sql)
|
||||
con.commit()
|
||||
except sqltool.Error as e:
|
||||
print('<span class="alert alert-danger" id="error">An error occurred: ' + e.args[0] + ' <a title="Close" id="errorMess"><b>X</b></a></span>')
|
||||
con.rollback()
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
@ -23,7 +33,7 @@ def add_user(user, email, password, role, group):
|
|||
con.close()
|
||||
|
||||
def update_user(user, email, password, role, group, id):
|
||||
con, cur = get_cur()
|
||||
con, cur = create_db.get_cur()
|
||||
sql = """update user set username = '%s',
|
||||
email = '%s',
|
||||
password = '%s',
|
||||
|
@ -31,10 +41,11 @@ def update_user(user, email, password, role, group, id):
|
|||
groups = '%s'
|
||||
where id = '%s'""" % (user, email, password, role, group, id)
|
||||
try:
|
||||
with con:
|
||||
cur.executescript(sql)
|
||||
except sqlite.Error as e:
|
||||
cur.execute(sql)
|
||||
con.commit()
|
||||
except sqltool.Error as e:
|
||||
print('<span class="alert alert-danger" id="error">An error occurred: ' + e.args[0] + ' <a title="Close" id="errorMess"><b>X</b></a></span>')
|
||||
con.rollback()
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
@ -42,25 +53,27 @@ def update_user(user, email, password, role, group, id):
|
|||
con.close()
|
||||
|
||||
def delete_user(id):
|
||||
con, cur = get_cur()
|
||||
con, cur = create_db.get_cur()
|
||||
sql = """delete from user where id = '%s'""" % (id)
|
||||
try:
|
||||
with con:
|
||||
cur.executescript(sql)
|
||||
except sqlite.Error as e:
|
||||
cur.execute(sql)
|
||||
con.commit()
|
||||
except sqltool.Error as e:
|
||||
print("An error occurred:", e.args[0])
|
||||
con.rollback()
|
||||
else:
|
||||
return True
|
||||
cur.close()
|
||||
|
||||
def add_group(name, description):
|
||||
con, cur = get_cur()
|
||||
con, cur = create_db.get_cur()
|
||||
sql = """INSERT INTO groups (name, description) VALUES ('%s', '%s')""" % (name, description)
|
||||
try:
|
||||
with con:
|
||||
cur.execute(sql)
|
||||
except sqlite.Error as e:
|
||||
cur.execute(sql)
|
||||
con.commit()
|
||||
except sqltool.Error as e:
|
||||
print('<span class="alert alert-danger" id="error">An error occurred: ' + e.args[0] + ' <a title="Close" id="errorMess"><b>X</b></a></span>')
|
||||
con.rollback()
|
||||
return False
|
||||
else:
|
||||
print(cur.lastrowid)
|
||||
|
@ -69,19 +82,20 @@ def add_group(name, description):
|
|||
con.close()
|
||||
|
||||
def delete_group(id):
|
||||
con, cur = get_cur()
|
||||
con, cur = create_db.get_cur()
|
||||
sql = """delete from groups where id = '%s'""" % (id)
|
||||
try:
|
||||
with con:
|
||||
cur.executescript(sql)
|
||||
except sqlite.Error as e:
|
||||
cur.execute(sql)
|
||||
con.commit()
|
||||
except sqltool.Error as e:
|
||||
print('<span class="alert alert-danger" id="error">An error occurred: ' + e.args[0] + ' <a title="Close" id="errorMess"><b>X</b></a></span>')
|
||||
con.rollback()
|
||||
else:
|
||||
return True
|
||||
cur.close()
|
||||
|
||||
def update_group(name, descript, id):
|
||||
con, cur = get_cur()
|
||||
con, cur = create_db.get_cur()
|
||||
sql = """
|
||||
update groups set
|
||||
name = '%s',
|
||||
|
@ -89,10 +103,11 @@ def update_group(name, descript, id):
|
|||
where id = '%s';
|
||||
""" % (name, descript, id)
|
||||
try:
|
||||
with con:
|
||||
cur.executescript(sql)
|
||||
except sqlite.Error as e:
|
||||
cur.execute(sql)
|
||||
con.commit()
|
||||
except sqltool.Error as e:
|
||||
print('<span class="alert alert-danger" id="error">An error occurred: ' + e.args[0] + ' <a title="Close" id="errorMess"><b>X</b></a></span>')
|
||||
con.rollback()
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
@ -100,13 +115,14 @@ def update_group(name, descript, id):
|
|||
con.close()
|
||||
|
||||
def add_server(hostname, ip, group, typeip, enable):
|
||||
con, cur = get_cur()
|
||||
con, cur = create_db.get_cur()
|
||||
sql = """INSERT INTO servers (hostname, ip, groups, type_ip, enable) VALUES ('%s', '%s', '%s', '%s', '%s')""" % (hostname, ip, group, typeip, enable)
|
||||
try:
|
||||
with con:
|
||||
cur.executescript(sql)
|
||||
except sqlite.Error as e:
|
||||
cur.execute(sql)
|
||||
con.commit()
|
||||
except sqltool.Error as e:
|
||||
print('<span class="alert alert-danger" id="error">An error occurred: ' + e.args[0] + ' <a title="Close" id="errorMess"><b>X</b></a></span>')
|
||||
con.rollback()
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
@ -114,20 +130,21 @@ def add_server(hostname, ip, group, typeip, enable):
|
|||
con.close()
|
||||
|
||||
def delete_server(id):
|
||||
con, cur = get_cur()
|
||||
con, cur = create_db.get_cur()
|
||||
sql = """delete from servers where id = '%s'""" % (id)
|
||||
try:
|
||||
with con:
|
||||
cur.executescript(sql)
|
||||
except sqlite.Error as e:
|
||||
cur.execute(sql)
|
||||
con.commit()
|
||||
except sqltool.Error as e:
|
||||
print('<span class="alert alert-danger" id="error">An error occurred: ' + e.args[0] + ' <a title="Close" id="errorMess"><b>X</b></a></span>')
|
||||
con.rollback()
|
||||
else:
|
||||
return True
|
||||
cur.close()
|
||||
con.close()
|
||||
|
||||
def update_server(hostname, ip, group, typeip, enable, id):
|
||||
con, cur = get_cur()
|
||||
con, cur = create_db.get_cur()
|
||||
sql = """update servers set
|
||||
hostname = '%s',
|
||||
ip = '%s',
|
||||
|
@ -136,21 +153,22 @@ def update_server(hostname, ip, group, typeip, enable, id):
|
|||
enable = '%s'
|
||||
where id = '%s'""" % (hostname, ip, group, typeip, enable, id)
|
||||
try:
|
||||
with con:
|
||||
cur.executescript(sql)
|
||||
except sqlite.Error as e:
|
||||
cur.execute(sql)
|
||||
con.commit()
|
||||
except sqltool.Error as e:
|
||||
print('<span class="alert alert-danger" id="error">An error occurred: ' + e.args[0] + ' <a title="Close" id="errorMess"><b>X</b></a></span>')
|
||||
con.rollback()
|
||||
cur.close()
|
||||
con.close()
|
||||
|
||||
def select_users(**kwargs):
|
||||
con, cur = get_cur()
|
||||
con, cur = create_db.get_cur()
|
||||
sql = """select * from user ORDER BY id"""
|
||||
if kwargs.get("user") is not None:
|
||||
sql = """select * from user where username='%s' """ % kwargs.get("user")
|
||||
try:
|
||||
cur.execute(sql)
|
||||
except sqlite.Error as e:
|
||||
except sqltool.Error as e:
|
||||
print("An error occurred:", e.args[0])
|
||||
else:
|
||||
return cur.fetchall()
|
||||
|
@ -158,13 +176,13 @@ def select_users(**kwargs):
|
|||
con.close()
|
||||
|
||||
def select_groups(**kwargs):
|
||||
con, cur = get_cur()
|
||||
con, cur = create_db.get_cur()
|
||||
sql = """select * from groups ORDER BY id"""
|
||||
if kwargs.get("group") is not None:
|
||||
sql = """select * from groups where name='%s' """ % kwargs.get("group")
|
||||
try:
|
||||
cur.execute(sql)
|
||||
except sqlite.Error as e:
|
||||
except sqltool.Error as e:
|
||||
print('<span class="alert alert-danger" id="error">An error occurred: ' + e.args[0] + ' <a title="Close" id="errorMess"><b>X</b></a></span>')
|
||||
else:
|
||||
return cur.fetchall()
|
||||
|
@ -172,11 +190,11 @@ def select_groups(**kwargs):
|
|||
con.close()
|
||||
|
||||
def select_user_name_group(id):
|
||||
con, cur = get_cur()
|
||||
con, cur = create_db.get_cur()
|
||||
sql = """select name from groups where id='%s' """ % id
|
||||
try:
|
||||
cur.execute(sql)
|
||||
except sqlite.Error as e:
|
||||
except sqltool.Error as e:
|
||||
print('<span class="alert alert-danger" id="error">An error occurred: ' + e.args[0] + ' <a title="Close" id="errorMess"><b>X</b></a></span>')
|
||||
else:
|
||||
return cur.fetchone()
|
||||
|
@ -201,7 +219,7 @@ def get_groups_select(id, **kwargs):
|
|||
print('</select>')
|
||||
|
||||
def select_servers(**kwargs):
|
||||
con, cur = get_cur()
|
||||
con, cur = create_db.get_cur()
|
||||
sql = """select * from servers where enable = '1' ORDER BY groups """
|
||||
if kwargs.get("server") is not None:
|
||||
sql = """select * from servers where hostname='%s' """ % kwargs.get("server")
|
||||
|
@ -209,7 +227,7 @@ def select_servers(**kwargs):
|
|||
sql = """select * from servers ORDER BY groups """
|
||||
try:
|
||||
cur.execute(sql)
|
||||
except sqlite.Error as e:
|
||||
except sqltool.Error as e:
|
||||
print("An error occurred:", e.args[0])
|
||||
else:
|
||||
return cur.fetchall()
|
||||
|
@ -217,11 +235,11 @@ def select_servers(**kwargs):
|
|||
con.close()
|
||||
|
||||
def get_type_ip_checkbox(id, **kwargs):
|
||||
con, cur = get_cur()
|
||||
con, cur = create_db.get_cur()
|
||||
sql = """select id, type_ip from servers where id='%s' """ % id
|
||||
try:
|
||||
cur.execute(sql)
|
||||
except sqlite.Error as e:
|
||||
except sqltool.Error as e:
|
||||
print("An error occurred:", e.args[0])
|
||||
else:
|
||||
for server in cur.fetchall():
|
||||
|
@ -234,11 +252,11 @@ def get_type_ip_checkbox(id, **kwargs):
|
|||
con.close()
|
||||
|
||||
def get_enable_checkbox(id, **kwargs):
|
||||
con, cur = get_cur()
|
||||
con, cur = create_db.get_cur()
|
||||
sql = """select id, enable from servers where id='%s' """ % id
|
||||
try:
|
||||
cur.execute(sql)
|
||||
except sqlite.Error as e:
|
||||
except sqltool.Error as e:
|
||||
print("An error occurred:", e.args[0])
|
||||
else:
|
||||
for server in cur.fetchall():
|
||||
|
@ -255,7 +273,7 @@ def get_dick_permit(**kwargs):
|
|||
import os
|
||||
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
||||
login = cookie.get('login')
|
||||
con, cur = get_cur()
|
||||
con, cur = create_db.get_cur()
|
||||
sql = """ select * from user where username = '%s' """ % login.value
|
||||
if kwargs.get('virt'):
|
||||
type_ip = ""
|
||||
|
@ -263,7 +281,7 @@ def get_dick_permit(**kwargs):
|
|||
type_ip = "and type_ip = 0"
|
||||
try:
|
||||
cur.execute(sql)
|
||||
except sqlite.Error as e:
|
||||
except sqltool.Error as e:
|
||||
print("An error occurred:", e.args[0])
|
||||
else:
|
||||
for group in cur:
|
||||
|
@ -273,7 +291,7 @@ def get_dick_permit(**kwargs):
|
|||
sql = """ select * from servers where groups like '%{group}%' and enable = 1 {type_ip} """.format(group=group[5], type_ip=type_ip)
|
||||
try:
|
||||
cur.execute(sql)
|
||||
except sqlite.Error as e:
|
||||
except sqltool.Error as e:
|
||||
print("An error occurred:", e.args[0])
|
||||
else:
|
||||
return cur.fetchall()
|
||||
|
@ -354,13 +372,13 @@ def show_update_group(group):
|
|||
print('</tr>')
|
||||
|
||||
def select_roles(**kwargs):
|
||||
con, cur = get_cur()
|
||||
con, cur = create_db.get_cur()
|
||||
sql = """select * from role ORDER BY id"""
|
||||
if kwargs.get("role") is not None:
|
||||
sql = """select * from role where name='%s' """ % kwargs.get("group")
|
||||
try:
|
||||
cur.execute(sql)
|
||||
except sqlite.Error as e:
|
||||
except sqltool.Error as e:
|
||||
print("An error occurred:", e.args[0])
|
||||
else:
|
||||
return cur.fetchall()
|
||||
|
@ -368,13 +386,13 @@ def select_roles(**kwargs):
|
|||
con.close()
|
||||
|
||||
def select_roles(**kwargs):
|
||||
con, cur = get_cur()
|
||||
con, cur = create_db.get_cur()
|
||||
sql = """select * from role ORDER BY id"""
|
||||
if kwargs.get("roles") is not None:
|
||||
sql = """select * from role where name='%s' """ % kwargs.get("roles")
|
||||
try:
|
||||
cur.execute(sql)
|
||||
except sqlite.Error as e:
|
||||
except sqltool.Error as e:
|
||||
print("An error occurred:", e.args[0])
|
||||
else:
|
||||
return cur.fetchall()
|
||||
|
|
|
@ -9,4 +9,5 @@ networkx==2.1
|
|||
numpy==1.14.0
|
||||
matplotlib==2.1.2
|
||||
urllib3==1.22
|
||||
future==0.13.1
|
||||
future==0.13.1
|
||||
mysqlclient==1.3.12
|
Loading…
Reference in New Issue