Add install script, support telegram
pull/19/head
Aidaho12 2018-01-29 11:13:18 +06:00
parent bc9bb95b28
commit f451e44f5f
15 changed files with 89 additions and 58 deletions

View File

@ -13,26 +13,19 @@ A simple web interface(user-frendly web GUI) for managing Haproxy servers
7. Rollback to previous versions of the config 7. Rollback to previous versions of the config
8. Comparing versions of configs 8. Comparing versions of configs
9. Users roles: admin, viewer 9. Users roles: admin, viewer
10. Telegram notification
# Install # Install
For install just dowload archive and untar somewhere: For install just dowload archive and untar somewhere:
``` ```
$ cd /opt $ cd /opt
$ unzip master.zip $ unzip master.zip
$ mv haproxy-web-interface-master/ haproxy $ mv haproxy-wi-master/ haproxy-wi
$ cd /opt/haproxy $ cd /opt/haproxy-wi
$ pip3 install -r requirements.txt $ chmod +x install.sh
$ chmod +x server.py
$ chmod +x -R cgi-bin/
$ chown user:user -R haproxy
``` ```
Edit listserv.py, add your HAproxy servers. Edit listserv.py, add your HAproxy servers.
If foler not /opt/haproxy/, edit server.py:
```
path_config = "/opt/haproxy/haproxy-webintarface.config"
```
# Settings # Settings
edit haproxy-webintarface.config with your env edit haproxy-webintarface.config with your env
@ -46,24 +39,12 @@ global
``` ```
![alt text](image/4.jpeg "View logs page") ![alt text](image/4.jpeg "View logs page")
# Start # Start and auto start
Create systemd service to auto start:
``` ```
[Unit] systemctl enable haproxy-wi.service
Description=Haproxy web interface systemctl start haproxy-wi.service
After=syslog.target network.target
[Service]
Type=simple
User=user_name
ExecStart=/opt/haproxy/server.py >> /opt/haproxy/log/haproxy-webface.log
RestartSec=2s
Restart=on-failure
TimeoutStopSec=1s
[Install]
WantedBy=multi-user.target
``` ```
![alt text](image/1.jpeg "Start page")
# Deb support
Sorry, but not use, because adapt yourself

View File

@ -13,7 +13,7 @@ from pytz import timezone
funct.head("Add") funct.head("Add")
funct.check_config() funct.check_config()
funct.check_login("add.py") funct.check_login()
path_config = "haproxy-webintarface.config" path_config = "haproxy-webintarface.config"
config = configparser.ConfigParser() config = configparser.ConfigParser()
@ -122,6 +122,7 @@ for i in sorted(listhap.listhap):
print('<option value="%s">%s</option>' % (listhap.listhap.get(i), i)) print('<option value="%s">%s</option>' % (listhap.listhap.get(i), i))
print('</select>' print('</select>'
'<div class="tooltip tooltipTop"><b>Note:</b> If you reconfigure First server, second will reconfigured automatically</div>'
'</td>' '</td>'
'</tr>' '</tr>'
'<tr>' '<tr>'
@ -135,7 +136,7 @@ print('</select>'
'<td class="addOption">' '<td class="addOption">'
'<input type="text" name="ip" id="ip" title="" size="15" placeholder="172.28.0.1"><b>:</b>' '<input type="text" name="ip" id="ip" title="" size="15" placeholder="172.28.0.1"><b>:</b>'
'<input type="text" name="port" required title="Port for bind listner" size="5" placeholder="8080">' '<input type="text" name="port" required title="Port for bind listner" size="5" placeholder="8080">'
'<div class="tooltip tooltipTop">IP for bind listner, if empty will be assignet on all IPs. Start typing ip, or press down.</div>' '<div class="tooltip tooltipTop">IP for bind listner, <b>if empty will be assignet on all IPs</b>. Start typing ip, or press down.</div>'
'</td>' '</td>'
'</tr>' '</tr>'
'<tr>' '<tr>'
@ -202,6 +203,7 @@ for i in sorted(listhap.listhap):
print('<option value="%s">%s</option>' % (listhap.listhap.get(i), i)) print('<option value="%s">%s</option>' % (listhap.listhap.get(i), i))
print('</select>' print('</select>'
'<div class="tooltip tooltipTop"><b>Note:</b> If you reconfigure First server, second will reconfigured automatically</div>'
'</td>' '</td>'
'</tr>' '</tr>'
'<tr>' '<tr>'
@ -215,7 +217,7 @@ print('</select>'
'<td class="addOption">' '<td class="addOption">'
'<input type="text" name="ip" id="ip1" size="15" placeholder="172.28.0.1"><b>:</b>' '<input type="text" name="ip" id="ip1" size="15" placeholder="172.28.0.1"><b>:</b>'
'<input type="text" name="port" required title="Port for bind listner" size="5" placeholder="8080">' '<input type="text" name="port" required title="Port for bind listner" size="5" placeholder="8080">'
'<div class="tooltip tooltipTop">IP for bind listner, if empty will be assignet on all IPs. Start typing ip, or press down.</div>' '<div class="tooltip tooltipTop">IP for bind listner, <b>if empty will be assignet on all IPs</b>. Start typing ip, or press down.</div>'
'</td>' '</td>'
'</tr>' '</tr>'
'<tr>' '<tr>'
@ -274,6 +276,7 @@ for i in sorted(listhap.listhap):
print('<option value="%s">%s</option>' % (listhap.listhap.get(i), i)) print('<option value="%s">%s</option>' % (listhap.listhap.get(i), i))
print('</select>' print('</select>'
'<div class="tooltip tooltipTop"><b>Note:</b> If you reconfigure First server, second will reconfigured automatically</div>'
'</td>' '</td>'
'</tr>' '</tr>'
'<tr>' '<tr>'

View File

@ -17,7 +17,7 @@ servNew = form.getvalue('serNew')
funct.head("Edit HAproxy config") funct.head("Edit HAproxy config")
funct.check_config() funct.check_config()
funct.check_login("config.py") funct.check_login()
path_config = "haproxy-webintarface.config" path_config = "haproxy-webintarface.config"
config = configparser.ConfigParser() config = configparser.ConfigParser()

View File

@ -15,7 +15,7 @@ servNew = form.getvalue('serNew')
funct.head("Show HAproxy config") funct.head("Show HAproxy config")
funct.check_config() funct.check_config()
funct.check_login("configshow.py") funct.check_login()
path_config = "haproxy-webintarface.config" path_config = "haproxy-webintarface.config"
config = configparser.ConfigParser() config = configparser.ConfigParser()

View File

@ -17,7 +17,7 @@ configver = form.getvalue('configver')
funct.head("Old Versions HAproxy config") funct.head("Old Versions HAproxy config")
funct.check_config() funct.check_config()
funct.check_login("configver.py") funct.check_login()
path_config = "haproxy-webintarface.config" path_config = "haproxy-webintarface.config"
config = configparser.ConfigParser() config = configparser.ConfigParser()

View File

@ -17,7 +17,7 @@ right = form.getvalue('right')
funct.head("Compare HAproxy configs") funct.head("Compare HAproxy configs")
funct.check_config() funct.check_config()
funct.check_login("diff.py") funct.check_login()
path_config = "haproxy-webintarface.config" path_config = "haproxy-webintarface.config"
config = configparser.ConfigParser() config = configparser.ConfigParser()

View File

@ -13,12 +13,12 @@ serv = form.getvalue('serv')
head("Edit & show HAproxy settings") head("Edit & show HAproxy settings")
funct.check_login("edit.py") funct.check_login()
print('<center><h2>Edit & show HAproxy settings</h2></center>') print('<center><h2>Edit & show HAproxy settings</h2></center>')
print('<center><h3>Choose server & action: Disable/Enable server or output any information about the server:</h3>') print('<center><h3>Choose server & action: Disable/Enable server or output any information about the server:</h3>')
print('<form action="edit.py" method="get">') print('<form action="edit.py" method="get">')
print('<p><select autofocus required name="serv">') print('<p><select autofocus required name="serv" id="serv">')
print('<option disabled selected>Choose server</option>') print('<option disabled selected>Choose server</option>')
funct.choose_server_with_vip(serv) funct.choose_server_with_vip(serv)
@ -44,7 +44,7 @@ else:
selected3 = '' selected3 = ''
print('</select>') print('</select>')
print('<select autofocus required name="servaction">') print('<select autofocus required name="servaction" id="chooseServer">')
print('<option disabled selected>Choose action</option>') print('<option disabled selected>Choose action</option>')
print('<option value=1 %s>Disable server</option>' % selected1) print('<option value=1 %s>Disable server</option>' % selected1)
print('<option value=2 %s>Enable server</option>' % selected2) print('<option value=2 %s>Enable server</option>' % selected2)

View File

@ -57,9 +57,10 @@ def telegram_send_mess(mess):
bot = telegram.Bot(token=token_bot, request=pp) bot = telegram.Bot(token=token_bot, request=pp)
bot.send_message(chat_id=channel_name, text=mess) bot.send_message(chat_id=channel_name, text=mess)
def check_login(ref): def check_login():
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
login = cookie.get('login') login = cookie.get('login')
ref = os.environ.get("SCRIPT_NAME")
if login is None: if login is None:
print('<meta http-equiv="refresh" content="0; url=login.py?ref=%s">' % ref) print('<meta http-equiv="refresh" content="0; url=login.py?ref=%s">' % ref)

View File

@ -13,7 +13,7 @@ login = form.getvalue('login')
password = form.getvalue('pass') password = form.getvalue('pass')
USERS = 'cgi-bin/users' USERS = 'cgi-bin/users'
funct.check_login("login.py") funct.check_login()
try: try:
with open(USERS, "r") as user: with open(USERS, "r") as user:
@ -32,19 +32,14 @@ def login_page(error):
ref = "/index.html" ref = "/index.html"
funct.head("Login page") funct.head("Login page")
<<<<<<< HEAD
print('<center><form name="auth" action="login.py" class="form-horizontal" method="post">') print('<center><form name="auth" action="login.py" class="form-horizontal" method="post">')
=======
print('<center><div class="conteiner"><form name="auth" action="login.py" class="form-horizontal" method="post">')
>>>>>>> parent of fe00260... v1.3
print(printError) print(printError)
print('<label for="login"> Login: </label> <input type="text" name="login" class="form-control">') print('<label for="login"> Login: </label> <input type="text" name="login" required class="form-control">')
print('<label for="pass"> Pass: </label> <input type="password" name="pass" class="form-control">') print('<label for="pass"> Pass: </label> <input type="password" name="pass" required class="form-control">')
print('<input type="hidden" value="%s" name="ref">' % ref) print('<input type="hidden" value="%s" name="ref">' % ref)
print('<button type="submit" name="Login" value="Enter">Sign</button>') print('<button type="submit" name="Login" value="Enter">Sign Up</button>')
print('</form></div></center>') print('</form></center>')
if form.getvalue('logout') is not None: if form.getvalue('logout') is not None:
print("Set-cookie: login=; expires=Wed May 18 03:33:20 2003; path=/cgi-bin/; httponly") print("Set-cookie: login=; expires=Wed May 18 03:33:20 2003; path=/cgi-bin/; httponly")
@ -71,6 +66,7 @@ if login is not None and password is not None:
print('<html><head><title>Redirecting</title><meta charset="UTF-8">') print('<html><head><title>Redirecting</title><meta charset="UTF-8">')
print('<link href="/style.css" rel="stylesheet">') print('<link href="/style.css" rel="stylesheet">')
print('<meta http-equiv="refresh" content="0; url=%s">' % ref) print('<meta http-equiv="refresh" content="0; url=%s">' % ref)
funct.logging("localhost", "login now")
break break
login_page("error") login_page("error")

View File

@ -12,7 +12,7 @@ serv = form.getvalue('serv')
funct.head("HAproxy Logs") funct.head("HAproxy Logs")
funct.check_config() funct.check_config()
funct.check_login("config.py") funct.check_login()
path_config = "haproxy-webintarface.config" path_config = "haproxy-webintarface.config"
config = configparser.ConfigParser() config = configparser.ConfigParser()
@ -21,7 +21,7 @@ config.read(path_config)
print('<center><h2>HAproxy Logs</h2></center>') print('<center><h2>HAproxy Logs</h2></center>')
print('<center><h3>Choose server & number rows</h3>') print('<center><h3>Choose server & number rows</h3>')
print('<form action="logs.py" method="get">') print('<form action="logs.py" method="get">')
print('<p><select autofocus required name="serv">') print('<p><select autofocus required name="serv" id="serv">')
print('<option disabled>Choose server</option>') print('<option disabled>Choose server</option>')
for i in sorted(listhap.listhap): for i in sorted(listhap.listhap):

View File

@ -8,7 +8,7 @@ import configparser
from requests_toolbelt.utils import dump from requests_toolbelt.utils import dump
funct.check_config() funct.check_config()
funct.check_login("config.py") funct.check_login()
path_config = "haproxy-webintarface.config" path_config = "haproxy-webintarface.config"
config = configparser.ConfigParser() config = configparser.ConfigParser()
@ -55,3 +55,4 @@ print('</select><input type="submit"></p></form>')
data = dump.dump_all(response) data = dump.dump_all(response)
print('<a name="conf"></a>') print('<a name="conf"></a>')
print(data.decode('utf-8')) print(data.decode('utf-8'))

View File

@ -1,6 +1,6 @@
[main] [main]
#Path to files destination #Path to files destination
fullpath = /opt/haproxy/ fullpath = /opt/haproxy-wi/
server_bind_ip = 0.0.0.0 server_bind_ip = 0.0.0.0
server_port = 8000 server_port = 8000
log_path = %(fullpath)s/log/ log_path = %(fullpath)s/log/
@ -13,13 +13,13 @@ logo_path = /logo.png
#Server for save configs from HAproxy servers #Server for save configs from HAproxy servers
haproxy_configs_server = localhost haproxy_configs_server = localhost
#Dir where configs will be save #Dir where configs will be save
haproxy_save_configs_dir = /opt/haproxy/cgi-bin/hap_config/ haproxy_save_configs_dir = /opt/haproxy-wi/cgi-bin/hap_config/
[ssh] [ssh]
#If ssh connect disable entare password for ssh connect. Default enable #If ssh connect disable entare password for ssh connect. Default enable
ssh_keys_enable = 1 ssh_keys_enable = 1
#SSH keys to connect without password to HAproxy servers #SSH keys to connect without password to HAproxy servers
ssh_keys = /opt/haproxy/cgi-bin/id_rsa.pem ssh_keys = /opt/haproxy-wi/cgi-bin/id_rsa.pem
#Username for connect ssh #Username for connect ssh
ssh_user_name = root ssh_user_name = root
ssh_pass = ssh_pass =

16
haproxy-wi.service Normal file
View File

@ -0,0 +1,16 @@
[Unit]
Description=Haproxy web interface
After=syslog.target network.target
[Service]
Type=simple
User=haproxy-wi
ExecStart=/opt/haproxy/server.py >> /opt/haproxy/log/haproxy-webface.log
RestartSec=2s
Restart=on-failure
TimeoutStopSec=1s
[Install]
WantedBy=multi-user.target

33
install.sh Normal file
View File

@ -0,0 +1,33 @@
#!/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

View File

@ -2,7 +2,7 @@
import os, sys import os, sys
import configparser import configparser
path_config = "/opt/haproxy/haproxy-webintarface.config" path_config = "/opt/haproxy-wi/haproxy-webintarface.config"
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(path_config) config.read(path_config)
log_path = config.get('main', 'log_path') log_path = config.get('main', 'log_path')