Browse Source

v2.3.1

Check ssh access before add new server
bug fixed
pull/19/head
Aidaho12 7 years ago
parent
commit
d84a6f531b
  1. 3
      README.md
  2. 16
      app/add.py
  3. 23
      app/create_db.py
  4. 12
      app/funct.py
  5. 26
      app/sql.py
  6. 13
      inc/style.css

3
README.md

@ -123,6 +123,9 @@ Do this:
$ cd /var/www/haproxy-wi/app
$ ./update_db.py
```
and check executeble py files
If you see plain text, check section "Directory" in httpd conf
# Further development and support

16
app/add.py

@ -2,9 +2,9 @@
import html
import cgi
import os
from configparser import ConfigParser, ExtendedInterpolation
import funct
import sql
from configparser import ConfigParser, ExtendedInterpolation
funct.head("Add")
funct.check_config()
@ -18,7 +18,7 @@ funct.page_for_admin(level = 2)
hap_configs_dir = config.get('configs', 'haproxy_save_configs_dir')
cert_path = config.get('haproxy', 'cert_path')
listhap = sql.get_dick_permit()
form = cgi.FieldStorage()
form = cgi.FieldStorage()
if form.getvalue('mode') is not None:
serv = form.getvalue('serv')
@ -95,9 +95,6 @@ if form.getvalue('mode') is not None:
servers_split = ""
config_add = name + "\n" + bind + mode + "\n" + balance + options_split + backend + servers_split + "\n"
os.chdir(config.get('configs', 'haproxy_save_configs_dir'))
cfg = hap_configs_dir + serv + "-" + funct.get_data('config') + ".cfg"
funct.get_config(serv, cfg)
@ -114,16 +111,17 @@ if form.getvalue('mode') is not None:
for master in MASTERS:
if master[0] != None:
funct.upload_and_restart(master[0], cfg)
if funct.upload_and_restart(serv, cfg):
print('<meta http-equiv="refresh" content="30; url=add.py?add=%s&conf=%s">' % (name, config_add))
print('<meta http-equiv="refresh" content="5; url=add.py?add=%s&conf=%s">' % (name, config_add))
print('</div>')
if form.getvalue('add') is not None:
print('<h3 class="addSuc"> ' + form.getvalue('add') + ' was successfully added</h3>')
print('<div class="added"><h3 class="addSuc"> ' + form.getvalue('add') + ' was successfully added</h3>')
print('<div class="line3">')
print(form.getvalue('conf'))
print('</div>')
print('</div></div>')
print('<div id="tabs">'
'<ul>'

23
app/create_db.py

@ -10,6 +10,7 @@ config = ConfigParser(interpolation=ExtendedInterpolation())
config.read(path_config)
mysql_enable = config.get('mysql', 'enable')
fullpath = config.get('main', 'fullpath')
if mysql_enable == '1':
mysql_user = config.get('mysql', 'mysql_user')
@ -19,7 +20,7 @@ if mysql_enable == '1':
from mysql.connector import errorcode
import mysql.connector as sqltool
else:
db = "haproxy-wi.db"
db = fullpath+"/app/haproxy-wi.db"
import sqlite3 as sqltool
def check_db():
@ -54,15 +55,19 @@ def check_db():
con.close()
def get_cur():
if mysql_enable == '0':
con = sqltool.connect(db, isolation_level=None)
try:
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()
except sqltool.Error as e:
print("An error occurred:", e)
else:
con = sqltool.connect(user=mysql_user, password=mysql_password,
host=mysql_host,
database=mysql_db)
cur = con.cursor()
return con, cur
return con, cur
def create_table():
con, cur = get_cur()
if mysql_enable == '0':

12
app/funct.py

@ -7,7 +7,6 @@ from paramiko import SSHClient
from datetime import datetime
from pytz import timezone
from configparser import ConfigParser, ExtendedInterpolation
import sql
path_config = "haproxy-webintarface.config"
config = ConfigParser(interpolation=ExtendedInterpolation())
@ -299,7 +298,7 @@ def get_auto_refresh(h2):
'</div>'
'</div>')
def ssh_connect(serv):
def ssh_connect(serv, **kwargs):
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
@ -309,7 +308,10 @@ def ssh_connect(serv):
ssh.connect(hostname = serv, username = ssh_user_name, pkey = k )
else:
ssh.connect(hostname = serv, username = ssh_user_name, password = config.get('ssh', 'ssh_pass'))
return ssh
if kwargs.get('check'):
return True
else:
return ssh
except paramiko.AuthenticationException:
print('<div class="alert alert-danger">Authentication failed, please verify your credentials</div>')
return False
@ -426,7 +428,6 @@ def upload_and_restart(serv, cfg, **kwargs):
sftp.put(cfg, tmp_file)
sftp.close()
if kwargs.get("keepalived") == 1:
print("123")
if kwargs.get("just_save") == "save":
commands = [ "mv -f " + tmp_file + " /etc/keepalived/keepalived.conf" ]
else:
@ -460,7 +461,7 @@ def upload_and_restart(serv, cfg, **kwargs):
print('</center>')
ssh.close()
def open_port_firewalld(cfg):
try:
conf = open(cfg, "r")
@ -577,6 +578,7 @@ def ssh_command(serv, commands, **kwargs):
ssh.close()
def choose_only_select(serv, **kwargs):
import sql
if kwargs.get("virt"):
listhap = sql.get_dick_permit(virt=1)
else:

26
app/sql.py

@ -1,8 +1,8 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-"
import cgi
import create_db
from configparser import ConfigParser, ExtendedInterpolation
import create_db
path_config = "haproxy-webintarface.config"
config = ConfigParser(interpolation=ExtendedInterpolation())
@ -13,10 +13,9 @@ 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"
else:
import sqlite3 as sqltool
def add_user(user, email, password, role, group):
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)
@ -92,7 +91,8 @@ def delete_group(id):
con.rollback()
else:
return True
cur.close()
cur.close()
con.close()
def update_group(name, descript, id):
con, cur = create_db.get_cur()
@ -321,15 +321,17 @@ def get_dick_permit(**kwargs):
def is_master(ip, **kwargs):
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
sql = """ select slave.ip from servers as master left join servers as slave on master.id = slave.master where master.ip = '%s' """ % ip
if kwargs.get('master_slave'):
sql = """ select master.hostname, master.ip, slave.hostname, slave.ip from servers as master left join servers as slave on master.id = slave.master where slave.master > 0 """
try:
cur.execute(sql)
cur.execute(sql)
except sqltool.Error as e:
return False
print("An error occurred:", e)
else:
return cur.fetchall()
cur.close()
con.close()
def show_update_servers():
SERVERS = select_servers()
@ -490,6 +492,7 @@ if form.getvalue('userdel') is not None:
print("Ok")
if form.getvalue('newserver') is not None:
import funct
hostname = form.getvalue('newserver')
ip = form.getvalue('newip')
group = form.getvalue('newservergroup')
@ -501,8 +504,11 @@ if form.getvalue('newserver') is not None:
print(error_mess)
else:
print('Content-type: text/html\n')
if add_server(hostname, ip, group, typeip, enable, master):
show_update_server(hostname)
if funct.ssh_connect(ip, check=1):
if add_server(hostname, ip, group, typeip, enable, master):
show_update_server(hostname)
else:
print('<span class="alert alert-danger" id="error"><a title="Close" id="errorMess"><b>X</b></a></span>')
if form.getvalue('serverdel') is not None:
print('Content-type: text/html\n')

13
inc/style.css

@ -530,7 +530,7 @@ a:focus {
padding-left: 10px;
margin-bottom: 10px;
}
.alert-danger, .alert-info, .alert-success, .alert-warning {
.alert-danger, .alert-info, .alert-success, .alert-warning, .added {
width: 400px;
margin-left: 15px;
}
@ -543,11 +543,10 @@ a:focus {
margin-top: 15px;
margin-bottom: -50px;
}
.alert-success {
.alert-success, .added{
color: #3c763d;
background-color: #dff0d8;
border-color: #d6e9c6;
}
.alert-info {
color: #0c5460;
@ -559,6 +558,14 @@ a:focus {
background-color: #fff3cd;
border-color: #ffeeba;
}
.added {
position: absolute;
margin-left: 30px;
margin-top: 790px;
padding-bottom: 20px;
padding-right: 20px;
padding-left: 10px;
}
label {
display: inline-block;
max-width: 100%;

Loading…
Cancel
Save