diff --git a/README.md b/README.md index 12e16b40..3f45d184 100644 --- a/README.md +++ b/README.md @@ -20,31 +20,23 @@ A simple web interface(user-frendly web GUI) for managing Haproxy servers. Leave 12. Telegram notification # Install -Can be used as a service, or via fastaci apache + fastCGI(recommend, because it works faster). -How to use the service: - -For install just dowload archive and untar somewhere: +For install just [dowload](https://github.com/Aidaho12/haproxy-wi/archive/master.zip) archive and untar somewhere: ``` -$ cd /opt +$ cd /var/www/ $ unzip master.zip $ mv haproxy-wi-master/ haproxy-wi -$ cd /opt/haproxy-wi -$ chmod +x install.sh -$ sudo ./install.sh +$ cd haproxy-wi/cgi-bin +$ chmod +x *.py +$ ./create_db.py ``` -For Apache just do virtualhost with cgi-bin. +For Apache do virtualhost with cgi-bin. ![alt text](image/haproxy-wi-overview.jpeg "Overview page") # Settings ``` -cd $HOME_HAPROXY-WI/cgi-bin/ -chmod +x *.py -``` - -Run create_db.py for DB createing Edit $HOME_HAPROXY-WI/cgi-bin/haproxy-webintarface.config with your env @@ -63,15 +55,13 @@ 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") -# Start and autostart if service -``` -systemctl enable haproxy-wi.service -systemctl start haproxy-wi.service -``` +# Update DB -# Deb support - -Sorry, but not use, because adapt yourself +For update db: +``` +$ cd /var/www/haproxy-wi/cgi-bin +$ ./create_db.py +``` # Further development and support diff --git a/cgi-bin/config.py b/cgi-bin/config.py index bd240c7b..be9a5232 100644 --- a/cgi-bin/config.py +++ b/cgi-bin/config.py @@ -44,7 +44,7 @@ if form.getvalue('serv') is not None and form.getvalue('open') is not None : print('
') print('' % serv) print('' % cfg) - print('' % conf.read()) + print('' % conf.read()) print('

') funct.get_button("Just save", value="save") funct.get_button("Save and restart") diff --git a/cgi-bin/create_db.py b/cgi-bin/create_db.py index 179a61b6..e70a9634 100644 --- a/cgi-bin/create_db.py +++ b/cgi-bin/create_db.py @@ -1,8 +1,23 @@ #!/usr/bin/env python3 import sqlite3 as sqlite +import os +import sys + +db = "haproxy-wi.db" + +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'): + print("SQLite3 database has been detected.") + return False + else: + return True def get_cur(): - con = sqlite.connect("haproxy-wi.db", isolation_level=None) + con = sqlite.connect(db, isolation_level=None) cur = con.cursor() return con, cur @@ -63,4 +78,50 @@ def create_table(): cur.close() con.close() -create_table() \ No newline at end of file +def update_db_v_2_0_1(): + con, cur = get_cur() + sql = """ + ALTER TABLE `servers` ADD COLUMN type_ip INTEGER NOT NULL DEFAULT(0); + """ + try: + cur.executescript(sql) + except sqlite.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]) + return False + else: + print("DB was update to 2.0.1") + return True + cur.close() + con.close() + +def update_db_v_2_0_1_1(): + con, cur = get_cur() + sql = """ + ALTER TABLE `servers` ADD COLUMN enable INTEGER NOT NULL DEFAULT(1); + """ + try: + cur.executescript(sql) + except sqlite.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]) + return False + else: + print("DB was update to 2.0.1.1") + return True + cur.close() + con.close() + +if check_db(): + create_table() +else: + print('DB already exists, try update') +if update_db_v_2_0_1(): + print('DB was property update to version 2.0.1.') +update_db_v_2_0_1_1() \ No newline at end of file diff --git a/cgi-bin/funct.py b/cgi-bin/funct.py index a265f789..9d8465ec 100644 --- a/cgi-bin/funct.py +++ b/cgi-bin/funct.py @@ -148,8 +148,6 @@ def head(title): '' '' '') - if config.get('main', 'logo_enable') == "1": - print('

' % config.get('main', 'logo_path')) links() print('
') @@ -190,7 +188,7 @@ def links(): '') print('' '' - '' + '' '
') def show_login_links(): @@ -198,9 +196,9 @@ def show_login_links(): login = cookie.get('login') if login is None: - print('
  • ') + print('
  • ') else: - print('
  • ' % login.value) + print('
  • ' % login.value) def footer(): print('' diff --git a/cgi-bin/haproxy-webintarface.config b/cgi-bin/haproxy-webintarface.config index 79ddcf10..419742f3 100644 --- a/cgi-bin/haproxy-webintarface.config +++ b/cgi-bin/haproxy-webintarface.config @@ -6,9 +6,6 @@ server_bind_ip = 0.0.0.0 server_port = 8000 log_path = %(fullpath)s/log/ time_zone = UTC -#Enable logo on top menu. Default disable -logo_enable = 0 -logo_path = /logo.png [configs] #Server for save configs from HAproxy servers diff --git a/cgi-bin/sql.py b/cgi-bin/sql.py index f17c7cbb..b451f2ef 100644 --- a/cgi-bin/sql.py +++ b/cgi-bin/sql.py @@ -98,9 +98,9 @@ def update_group(name, descript, id): cur.close() con.close() -def add_server(hostname, ip, group, typeip): +def add_server(hostname, ip, group, typeip, enable): con, cur = get_cur() - sql = """INSERT INTO servers (hostname, ip, groups, type_ip) VALUES ('%s', '%s', '%s', '%s')""" % (hostname, ip, group, typeip) + 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) @@ -125,14 +125,15 @@ def delete_server(id): cur.close() con.close() -def update_server(hostname, ip, group, typeip, id): +def update_server(hostname, ip, group, typeip, enable, id): con, cur = get_cur() sql = """update servers set hostname = '%s', ip = '%s', groups = '%s', - type_ip = '%s' - where id = '%s'""" % (hostname, ip, group, typeip, id) + type_ip = '%s', + enable = '%s' + where id = '%s'""" % (hostname, ip, group, typeip, enable, id) try: with con: cur.executescript(sql) @@ -200,9 +201,11 @@ def get_groups_select(id, **kwargs): def select_servers(**kwargs): con, cur = get_cur() - sql = """select * from servers ORDER BY groups """ + 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") + if kwargs.get("full") is not None: + sql = """select * from servers ORDER BY groups """ try: cur.execute(sql) except sqlite.Error as e: @@ -225,7 +228,24 @@ def get_type_ip_checkbox(id, **kwargs): checked = 'checked' else: checked = "" - print('' % (server[0],server[0], checked)) + print('' % (server[0],server[0], checked)) + cur.close() + con.close() + +def get_enable_checkbox(id, **kwargs): + con, cur = get_cur() + sql = """select id, enable from servers where id='%s' """ % id + try: + cur.execute(sql) + except sqlite.Error as e: + print("An error occurred:", e.args[0]) + else: + for server in cur.fetchall(): + if server[1] == 1: + checked = 'checked' + else: + checked = "" + print('' % (server[0],server[0], checked)) cur.close() con.close() @@ -239,7 +259,7 @@ def get_dick_permit(**kwargs): if kwargs.get('virt'): type_ip = "" else: - type_ip = "type_ip = 0" + type_ip = "and type_ip = 0" try: cur.execute(sql) except sqlite.Error as e: @@ -247,13 +267,9 @@ def get_dick_permit(**kwargs): else: for group in cur: if group[5] == '1': - if kwargs.get('virt') is None: - type_ip = 'where ' + type_ip - sql = """ select * from servers %s """ % type_ip + sql = """ select * from servers where enable = 1 %s """ % type_ip else: - if kwargs.get('virt') is None: - type_ip = 'and ' + type_ip - sql = """ select * from servers where groups like '%{group}%' {type_ip} """.format(group=group[5], type_ip=type_ip) + 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: @@ -273,12 +289,15 @@ def show_update_servers(): '') for server in SERVERS: print('' % server[0]) - print('' % (server[0], server[1])) - print('' % (server[0], server[2])) + print('' % (server[0], server[1])) + print('' % (server[0], server[2])) print('') get_groups_select("123", selected=server[3]) print('') print('') + get_enable_checkbox(server[0]) + print('') + print('') get_type_ip_checkbox(server[0]) print('') print('' % server[0]) @@ -307,10 +326,14 @@ def show_update_server(server): SERVERS = select_servers(server=server) for server in SERVERS: print('' % server[0]) - print('' % (server[0], server[1])) - print('' % (server[0], server[2])) + print('' % (server[0], server[1])) + print('' % (server[0], server[2])) print('') - get_groups_select("123", selected=server[3]) + need_id_group = "servergroup-%s" % server[0] + get_groups_select(need_id_group, selected=server[3]) + print('') + print('') + get_enable_checkbox(server[0]) print('') print('') get_type_ip_checkbox(server[0]) @@ -412,12 +435,13 @@ if form.getvalue('newserver') is not None: ip = form.getvalue('newip') group = form.getvalue('newservergroup') typeip = form.getvalue('typeip') + enable = form.getvalue('enable') 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): + if add_server(hostname, ip, group, typeip, enable): show_update_server(hostname) if form.getvalue('serverdel') is not None: @@ -453,11 +477,12 @@ if form.getvalue('updateserver') is not None: ip = form.getvalue('ip') group = form.getvalue('servergroup') typeip = form.getvalue('typeip') + enable = form.getvalue('enable') 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, id) + update_server(name, ip, group, typeip, enable, id) \ No newline at end of file diff --git a/cgi-bin/users.py b/cgi-bin/users.py index 1a866614..7b653b0c 100644 --- a/cgi-bin/users.py +++ b/cgi-bin/users.py @@ -15,7 +15,7 @@ form = cgi.FieldStorage() USERS = sql.select_users() GROUPS = sql.select_groups() -SERVERS = sql.select_servers() +SERVERS = sql.select_servers(full=1) ROLES = sql.select_roles() print('' @@ -85,7 +85,7 @@ print('
    ' 'Desciption' '' '' - '') + '') for group in GROUPS: print('' % group[0]) print('' % (group[0], group[1])) @@ -115,6 +115,8 @@ print('

    Hostname' '' '' + '' + '' '' '' '') @@ -127,6 +129,12 @@ for server in SERVERS: need_id_group = "servergroup-%s" % server[0] sql.get_groups_select(need_id_group, selected=server[3]) print('') + print('') + print('') print('' % server[0]) print('' % server[0]) print('') @@ -137,6 +145,8 @@ print('
    IPGroupEnableVirt
    ') + sql.get_enable_checkbox(server[0]) + print('') + sql.get_type_ip_checkbox(server[0]) + print('
    ' 'New hostname' 'IP' 'Group' + 'Enable' + 'Virt' '' '' '' @@ -144,7 +154,9 @@ print('' '') sql.get_groups_select("new-server-group-add") print('' - '' + '' + '' + '' '') print('') diff --git a/haproxy-wi.service b/haproxy-wi.service deleted file mode 100644 index 6eb3acdc..00000000 --- a/haproxy-wi.service +++ /dev/null @@ -1,16 +0,0 @@ -[Unit] -Description=Haproxy web interface -After=syslog.target network.target - -[Service] -Type=simple -User=haproxy-wi - -ExecStart=/opt/haproxy-wi/server.py >> /opt/haproxy-wi/log/haproxy-webface.log - -RestartSec=2s -Restart=on-failure -TimeoutStopSec=1s - -[Install] -WantedBy=multi-user.target diff --git a/inc/users.js b/inc/users.js index 0aa27d02..0969f4e0 100644 --- a/inc/users.js +++ b/inc/users.js @@ -49,17 +49,22 @@ $( function() { }); $('#add-server').click(function() { $('#error').remove(); - var typeip; + var typeip = 0; + var enable = 0; if ($('#typeip').is(':checked')) { typeip = '1'; } + if ($('#enable').is(':checked')) { + enable = '1'; + } $.ajax( { url: "sql.py", data: { newserver: $('#new-server-add').val(), newip: $('#new-ip').val(), newservergroup: $('#new-server-group-add').val(), - typeip: typeip + typeip: typeip, + enable: enable }, type: "GET", success: function( data ) { @@ -209,10 +214,14 @@ function updateGroup(id) { } ); } function updateServer(id) { - var typeip; + var typeip = 0; + var enable = 0; if ($('#typeip-'+id).is(':checked')) { typeip = '1'; } + if ($('#enable-'+id).is(':checked')) { + enable = '1'; + } $.ajax( { url: "sql.py", data: { @@ -220,6 +229,7 @@ function updateServer(id) { ip: $('#ip-'+id).val(), servergroup: $('#servergroup-'+id+' option:selected' ).val(), typeip: typeip, + enable: enable, id: id }, type: "GET", diff --git a/install.sh b/install.sh deleted file mode 100644 index 711c0273..00000000 --- a/install.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/bash - -echo "Enter dir for HAproxy-WI. Default: [/opt/haproxy-wi]" -read DIR -echo "Enter user for HAproxy-WI. Defailt: [haproxy-wi]" -read USER - -if [[ $DIR = "" ]]; then - DIR="/opt/haproxy-wi" -else - sed -i "s!/opt/haproxy-wi!$DIR!" haproxy-webintarface.config - sed -i "s!/opt/haproxy-wi!$DIR!" server.py - sed -i "s!/opt/haproxy-wi!$DIR!" haproxy-wi.service -fi - -if [[ $USER = "" ]]; then - USER="haproxy-wi" -fi - -echo "Install req" -pip3 install -r requirements.txt - -echo "Add user $USER" -useradd $USER -d $DIR -s /sbin/nologin - -chmod +x server.py -chmod +x cgi-bin/*.py -chown $USER:$USER -R * - -echo "Creating service" -sed -i "s/haproxy-wi/$USER/" haproxy-wi.service -mv haproxy-wi.service /etc/systemd/system -systemctl daemon-reload \ No newline at end of file diff --git a/server.py b/server.py deleted file mode 100644 index 54a2cc34..00000000 --- a/server.py +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env python3 -import os, sys -import configparser - -path_config = "/opt/haproxy-wi/haproxy-webintarface.config" -config = configparser.ConfigParser() -config.read(path_config) -log_path = config.get('main', 'log_path') -fullpath = config.get('main', 'fullpath') -server_bind_ip = config.get('main', 'server_bind_ip') -server_port = config.getint('main', 'server_port') - -from http.server import HTTPServer, CGIHTTPRequestHandler - -sys.stderr = open(log_path + '/haproxy-monitor.log', 'w') -webdir = fullpath -os.chdir(webdir) -server_address = (server_bind_ip, server_port) -httpd = HTTPServer(server_address, CGIHTTPRequestHandler) -httpd.serve_forever() \ No newline at end of file