From d84a6f531b11cf21556fb5a9d44f46cee2235ade Mon Sep 17 00:00:00 2001 From: Aidaho12 Date: Tue, 1 May 2018 12:28:19 +0600 Subject: [PATCH] v2.3.1 Check ssh access before add new server bug fixed --- README.md | 3 +++ app/add.py | 16 +++++++--------- app/create_db.py | 23 ++++++++++++++--------- app/funct.py | 12 +++++++----- app/sql.py | 26 ++++++++++++++++---------- inc/style.css | 13 ++++++++++--- 6 files changed, 57 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 653b9483..6f1a3c3f 100644 --- a/README.md +++ b/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 diff --git a/app/add.py b/app/add.py index 3460eb57..208037e5 100644 --- a/app/add.py +++ b/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('' % (name, config_add)) + print('' % (name, config_add)) print('') - + if form.getvalue('add') is not None: - print('

' + form.getvalue('add') + ' was successfully added

') + print('

' + form.getvalue('add') + ' was successfully added

') print('
') print(form.getvalue('conf')) - print('
') + print('
') print('
' '
' '') -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('
Authentication failed, please verify your credentials
') 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('') 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: diff --git a/app/sql.py b/app/sql.py index f21efd6b..740a6adc 100644 --- a/app/sql.py +++ b/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('X') if form.getvalue('serverdel') is not None: print('Content-type: text/html\n') diff --git a/inc/style.css b/inc/style.css index 5a9627f3..6aaf7764 100644 --- a/inc/style.css +++ b/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%;