mirror of https://github.com/Aidaho12/haproxy-wi
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
214 lines
5.4 KiB
214 lines
5.4 KiB
7 years ago
|
#!/usr/bin/env python3
|
||
7 years ago
|
import cgi
|
||
|
import html
|
||
7 years ago
|
import os
|
||
|
import sys
|
||
7 years ago
|
from configparser import ConfigParser, ExtendedInterpolation
|
||
7 years ago
|
|
||
7 years ago
|
path_config = "haproxy-webintarface.config"
|
||
|
config = ConfigParser(interpolation=ExtendedInterpolation())
|
||
|
config.read(path_config)
|
||
7 years ago
|
|
||
7 years ago
|
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
|
||
|
|
||
7 years ago
|
def check_db():
|
||
7 years ago
|
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
|
||
7 years ago
|
else:
|
||
7 years ago
|
con, cur = get_cur()
|
||
|
sql = """ select id from `groups` where id='1' """
|
||
|
try:
|
||
|
cur.execute(sql)
|
||
|
except sqltool.Error as err:
|
||
7 years ago
|
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)
|
||
7 years ago
|
print('</div>')
|
||
|
return True
|
||
|
else:
|
||
|
return False
|
||
|
con.close()
|
||
|
|
||
7 years ago
|
def get_cur():
|
||
7 years ago
|
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()
|
||
7 years ago
|
return con, cur
|
||
7 years ago
|
|
||
7 years ago
|
def create_table():
|
||
|
con, cur = get_cur()
|
||
7 years ago
|
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` (
|
||
7 years ago
|
`id` INTEGER NOT NULL,
|
||
7 years ago
|
`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` (
|
||
7 years ago
|
`id` INTEGER NOT NULL,
|
||
|
`hostname` VARCHAR ( 64 ) UNIQUE,
|
||
|
`ip` VARCHAR ( 64 ) UNIQUE,
|
||
|
`groups` VARCHAR ( 64 ),
|
||
7 years ago
|
PRIMARY KEY(`id`)
|
||
|
);
|
||
|
"""
|
||
|
try:
|
||
|
cur.executescript(sql)
|
||
|
except sqltool.Error as e:
|
||
|
print("An error occurred:", e)
|
||
|
return False
|
||
|
else:
|
||
|
return True
|
||
7 years ago
|
else:
|
||
7 years ago
|
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
|
||
7 years ago
|
cur.close()
|
||
|
con.close()
|
||
|
|
||
7 years ago
|
def update_db_v_2_0_1():
|
||
|
con, cur = get_cur()
|
||
|
sql = """
|
||
7 years ago
|
ALTER TABLE `servers` ADD COLUMN type_ip INTEGER NOT NULL DEFAULT 0;
|
||
7 years ago
|
"""
|
||
|
try:
|
||
7 years ago
|
cur.execute(sql)
|
||
7 years ago
|
except sqltool.Error as e:
|
||
7 years ago
|
if e.args[0] == 'duplicate column name: type_ip':
|
||
7 years ago
|
print('Updating... go to version 2.0.1.1<br />')
|
||
7 years ago
|
return False
|
||
|
else:
|
||
7 years ago
|
print("An error occurred:", e)
|
||
7 years ago
|
return False
|
||
|
else:
|
||
7 years ago
|
print("DB was update to 2.0.1<br />")
|
||
7 years ago
|
return True
|
||
|
cur.close()
|
||
|
con.close()
|
||
|
|
||
|
def update_db_v_2_0_1_1():
|
||
|
con, cur = get_cur()
|
||
|
sql = """
|
||
7 years ago
|
ALTER TABLE `servers` ADD COLUMN enable INTEGER NOT NULL DEFAULT 1;
|
||
7 years ago
|
"""
|
||
|
try:
|
||
7 years ago
|
cur.execute(sql)
|
||
|
except sqltool.Error as e:
|
||
7 years ago
|
if e.args[0] == 'duplicate column name: enable' or e == "1060 (42S21): Duplicate column name 'enable' ":
|
||
7 years ago
|
print('Updating... go to version 2.0.5<br />')
|
||
7 years ago
|
return False
|
||
|
else:
|
||
7 years ago
|
print("An error occurred:", e)
|
||
7 years ago
|
return False
|
||
|
else:
|
||
7 years ago
|
print("DB was update to 2.0.1.1<br />")
|
||
7 years ago
|
return True
|
||
|
cur.close()
|
||
|
con.close()
|
||
|
|
||
7 years ago
|
def update_db_v_2_0_5():
|
||
|
con, cur = get_cur()
|
||
|
sql = """
|
||
|
ALTER TABLE `servers` ADD COLUMN master INTEGER NOT NULL DEFAULT 0;
|
||
|
"""
|
||
|
try:
|
||
|
cur.execute(sql)
|
||
|
except sqltool.Error as e:
|
||
|
if e.args[0] == 'duplicate column name: master':
|
||
|
print('Already updated. No run more. Thx =^.^=')
|
||
|
return False
|
||
|
else:
|
||
|
print("An error occurred:", e)
|
||
|
return False
|
||
|
else:
|
||
7 years ago
|
print("DB was update to 2.0.5<br />")
|
||
7 years ago
|
return True
|
||
|
cur.close()
|
||
|
con.close()
|
||
|
|
||
7 years ago
|
def update_all():
|
||
|
update_db_v_2_0_1()
|
||
|
update_db_v_2_0_1_1()
|
||
7 years ago
|
update_db_v_2_0_5()
|
||
7 years ago
|
|
||
|
#if check_db():
|
||
|
# create_table()
|
||
|
#else:
|
||
|
# print('DB already exists, try update')
|
||
|
#update_all()
|
||
|
#if update_db_v_2_0_1():
|
||
|
# print('DB was property update to version 2.0.1.')
|
||
|
#update_db_v_2_0_1_1()
|