From 653443cdde021292ecdbcf18d1af61498dbd32cf Mon Sep 17 00:00:00 2001 From: Aidaho12 Date: Tue, 24 Apr 2018 13:38:48 +0600 Subject: [PATCH] v2.0.5 Master/slave servers --- README.md | 17 ++++++++++++----- cgi-bin/config.py | 6 ++++++ cgi-bin/configver.py | 6 ++++++ cgi-bin/create_db.py | 25 +++++++++++++++++++++++-- cgi-bin/funct.py | 6 +++--- cgi-bin/sql.py | 42 ++++++++++++++++++++++++++++++++---------- cgi-bin/update_db.py | 19 +++++++++++++++++++ cgi-bin/users.py | 17 ++++++++++++++++- inc/awesome.css | 4 ++-- inc/users.js | 4 +++- 10 files changed, 122 insertions(+), 24 deletions(-) create mode 100644 cgi-bin/update_db.py diff --git a/README.md b/README.md index e6b61ea..1eb6263 100644 --- a/README.md +++ b/README.md @@ -13,10 +13,11 @@ A simple web interface(user-frendly web GUI) for managing Haproxy servers. Leave 7. Add sections: listen, frontend, backend from web interface 8. Editing configs 9. Rollback to previous versions of the config -10. Comparing versions of configs -11. Users roles: admin, editor, viewer -12. Server groups -13. Telegram notification +10. Master/slave servers +11. Comparing versions of configs +12. Users roles: admin, editor, viewer +13. Server groups +14. Telegram notification # Install @@ -32,7 +33,7 @@ $ chmod +x haproxy-wi/cgi-bin/*.py For Apache do virtualhost with cgi-bin. Like this: ``` - + ServerName haproxy-wi ErrorLog /var/log/httpd/haproxy-wi.error.log CustomLog /var/log/httpd/haproxy-wi.access.log combined @@ -80,6 +81,12 @@ For Runtime API enable state file on HAproxt servers and need install socat on a ``` ![alt text](image/haproxy-wi-logs.jpeg "View logs page") +# Update DB +``` +$ cd /var/www/haproxy-wi/cgi-bin +$ ./update_db.py +``` + # Further development and support Offer your ideas and wishes, ask questions. All this is [welcomed](https://github.com/Aidaho12/haproxy-wi/issues) diff --git a/cgi-bin/config.py b/cgi-bin/config.py index f49867b..40c511a 100644 --- a/cgi-bin/config.py +++ b/cgi-bin/config.py @@ -5,6 +5,7 @@ import os import http.cookies from configparser import ConfigParser, ExtendedInterpolation import funct +import sql form = cgi.FieldStorage() serv = form.getvalue('serv') @@ -61,6 +62,11 @@ if form.getvalue('serv') is not None and form.getvalue('config') is not None: print("
New config was saved as: %s

" % cfg) + MASTERS = sql.is_master(serv) + for master in MASTERS: + if master[0] != None: + funct.upload_and_restart(master[0], cfg, just_save=save) + funct.upload_and_restart(serv, cfg, just_save=save) os.system("/bin/diff -ub %s %s >> %s/config_edit-%s.log" % (oldcfg, cfg, log_path, funct.get_data('logs'))) diff --git a/cgi-bin/configver.py b/cgi-bin/configver.py index dfe73ae..10fa579 100644 --- a/cgi-bin/configver.py +++ b/cgi-bin/configver.py @@ -3,6 +3,7 @@ import html import cgi import os import funct +import sql from configparser import ConfigParser, ExtendedInterpolation form = cgi.FieldStorage() @@ -79,6 +80,11 @@ if form.getvalue('serv') is not None and form.getvalue('config') is not None: print("
Uploaded old config ver: %s

" % configver) + MASTERS = sql.is_master(serv) + for master in MASTERS: + if master[0] != None: + funct.upload_and_restart(master[0], configver, just_save=save) + funct.upload_and_restart(serv, configver, just_save=save) print('
Go to view stats
' % serv) diff --git a/cgi-bin/create_db.py b/cgi-bin/create_db.py index bd93273..53f5a67 100644 --- a/cgi-bin/create_db.py +++ b/cgi-bin/create_db.py @@ -167,8 +167,8 @@ def update_db_v_2_0_1_1(): try: cur.execute(sql) except sqltool.Error as e: - if e.args[0] == 'duplicate column name: enable': - print('Already updated. No run more. Thx =^.^=') + if e.args[0] == 'duplicate column name: enable' or e == "1060 (42S21): Duplicate column name 'enable' ": + print('Updating... go to version 2.0.5') return False else: print("An error occurred:", e) @@ -179,9 +179,30 @@ def update_db_v_2_0_1_1(): cur.close() con.close() +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: + print("DB was update to 2.0.5") + return True + cur.close() + con.close() + def update_all(): update_db_v_2_0_1() update_db_v_2_0_1_1() + update_db_v_2_0_5() #if check_db(): # create_table() diff --git a/cgi-bin/funct.py b/cgi-bin/funct.py index d86bfe3..0d61dbe 100644 --- a/cgi-bin/funct.py +++ b/cgi-bin/funct.py @@ -194,7 +194,7 @@ def links(): '') print('' '' - '' + '' '') def show_login_links(): @@ -528,7 +528,7 @@ def chooseServer(formName, title, note, **kwargs): print('

') - #if note == "y": - # print('

Note: If you reconfigure First server, second will reconfigured automatically

') + if note == "y": + print('

Note: If you reconfigure First server, second will reconfigured automatically

') print('') diff --git a/cgi-bin/sql.py b/cgi-bin/sql.py index dbb8c76..318faa1 100644 --- a/cgi-bin/sql.py +++ b/cgi-bin/sql.py @@ -14,7 +14,7 @@ if mysql_enable == '1': from mysql.connector import errorcode import mysql.connector as sqltool else: - db = "haproxy-wi-test.db" + db = "haproxy-wi.db" import sqlite3 as sqltool def add_user(user, email, password, role, group): @@ -114,9 +114,9 @@ def update_group(name, descript, id): cur.close() con.close() -def add_server(hostname, ip, group, typeip, enable): +def add_server(hostname, ip, group, typeip, enable, master): 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) + sql = """INSERT INTO servers (hostname, ip, groups, type_ip, enable, master) VALUES ('%s', '%s', '%s', '%s', '%s', '%s')""" % (hostname, ip, group, typeip, enable, master) try: cur.execute(sql) con.commit() @@ -143,15 +143,16 @@ def delete_server(id): cur.close() con.close() -def update_server(hostname, ip, group, typeip, enable, id): +def update_server(hostname, ip, group, typeip, enable, master, id): con, cur = create_db.get_cur() sql = """update servers set hostname = '%s', ip = '%s', groups = '%s', type_ip = '%s', - enable = '%s' - where id = '%s'""" % (hostname, ip, group, typeip, enable, id) + enable = '%s', + master = '%s' + where id = '%s'""" % (hostname, ip, group, typeip, enable, master, id) try: cur.execute(sql) con.commit() @@ -225,6 +226,8 @@ def select_servers(**kwargs): sql = """select * from servers where hostname='%s' """ % kwargs.get("server") if kwargs.get("full") is not None: sql = """select * from servers ORDER BY groups """ + if kwargs.get("get_master_servers") is not None: + sql = """select id,hostname from servers where master = 0 and type_ip = 0 and enable = 1 ORDER BY groups """ try: cur.execute(sql) except sqltool.Error as e: @@ -298,6 +301,16 @@ def get_dick_permit(**kwargs): cur.close() con.close() +def is_master(ip): + con, cur = create_db.get_cur() + sql = """ select slave.ip from servers left join servers as slave on servers.id = slave.master where servers.ip = '%s' """ % ip + try: + cur.execute(sql) + except sqltool.Error as e: + return False + else: + return cur + def show_update_servers(): SERVERS = select_servers() print('' @@ -337,7 +350,6 @@ def show_update_user(user): need_id_group = "usergroup-%s" % users[0] get_groups_select(need_id_group, selected=users[5]) print('') - #print('' % users[0]) print('' % users[0]) print('') @@ -357,7 +369,15 @@ def show_update_server(server): print('') get_type_ip_checkbox(server[0]) print('') - #print('' % server[0]) + print('') print('' % server[0]) print('') @@ -455,12 +475,13 @@ if form.getvalue('newserver') is not None: group = form.getvalue('newservergroup') typeip = form.getvalue('typeip') enable = form.getvalue('enable') + master = form.getvalue('slave') if ip is None or group is None: print('Content-type: text/html\n') print(error_mess) else: print('Content-type: text/html\n') - if add_server(hostname, ip, group, typeip, enable): + if add_server(hostname, ip, group, typeip, enable, master): show_update_server(hostname) if form.getvalue('serverdel') is not None: @@ -497,11 +518,12 @@ if form.getvalue('updateserver') is not None: group = form.getvalue('servergroup') typeip = form.getvalue('typeip') enable = form.getvalue('enable') + master = form.getvalue('slave') id = form.getvalue('id') if name is None or ip is None: print('Content-type: text/html\n') print(error_mess) else: print('Content-type: text/html\n') - update_server(name, ip, group, typeip, enable, id) + update_server(name, ip, group, typeip, enable, master, id) \ No newline at end of file diff --git a/cgi-bin/update_db.py b/cgi-bin/update_db.py new file mode 100644 index 0000000..27fa495 --- /dev/null +++ b/cgi-bin/update_db.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*-" +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.db" + import sqlite3 as sqltool + +create_db.update_all() \ No newline at end of file diff --git a/cgi-bin/users.py b/cgi-bin/users.py index 3877441..3c1cf7f 100644 --- a/cgi-bin/users.py +++ b/cgi-bin/users.py @@ -122,6 +122,7 @@ print('

Slave for (?)' '' '' '') @@ -140,7 +141,15 @@ for server in SERVERS: print('') - #print('' % server[0]) + print('') print('' % server[0]) print('') print('
') sql.get_type_ip_checkbox(server[0]) print('
' @@ -152,6 +161,7 @@ print('' 'Group' 'Enable' 'Virt' + 'Slave for' '' '' '' @@ -161,6 +171,11 @@ sql.get_groups_select("new-server-group-add") print('' '' '' + '' '' '') print('') diff --git a/inc/awesome.css b/inc/awesome.css index a8fdd9c..181c6c0 100644 --- a/inc/awesome.css +++ b/inc/awesome.css @@ -106,8 +106,8 @@ font-family: "Font Awesome 5 Solid"; content: "\f0ad"; } -.add-admin:before { +.add-admin::before { display: none; font-family: "Font Awesome 5 Solid"; content: "\f00c"; -} \ No newline at end of file +} diff --git a/inc/users.js b/inc/users.js index c3b9ea9..59dddce 100644 --- a/inc/users.js +++ b/inc/users.js @@ -85,7 +85,8 @@ $( function() { newip: $('#new-ip').val(), newservergroup: $('#new-server-group-add').val(), typeip: typeip, - enable: enable + enable: enable, + slave: $('#slavefor option:selected' ).val() }, type: "GET", success: function( data ) { @@ -282,6 +283,7 @@ function updateServer(id) { servergroup: $('#servergroup-'+id+' option:selected' ).val(), typeip: typeip, enable: enable, + slave: $('#slavefor-'+id+' option:selected' ).val(), id: id }, type: "GET",