From ef9ffe530c3245100e3a792f095c5091b8e6f71b Mon Sep 17 00:00:00 2001 From: Aidaho12 Date: Sat, 5 May 2018 18:40:41 +0600 Subject: [PATCH] v2.5 Welcome, Jinja2! code is optimized --- app/add.py | 397 +++----------------------------- app/config.py | 61 ++--- app/configshow.py | 61 ++--- app/configver.py | 117 +++++----- app/delver.py | 89 ++++--- app/diff.py | 46 ++-- app/edit.py | 67 ++---- app/funct.py | 258 +-------------------- app/ha.py | 108 ++------- app/ihap.py | 55 ++--- app/keepalivedconfig.py | 87 +++---- app/login.py | 95 ++++---- app/logs.py | 77 +++---- app/map.py | 37 +-- app/options.py | 26 ++- app/overview.py | 32 ++- app/settings.py | 47 ++-- app/templates/add.html | 359 +++++++++++++++++++++++++++++ app/templates/admin.html | 303 ++++++++++++++++++++++++ app/templates/base.html | 188 +++++++++++++++ app/templates/config.html | 67 ++++++ app/templates/configver.html | 56 +++++ app/templates/delver.html | 59 +++++ app/templates/ha.html | 88 +++++++ app/templates/ihap.html | 27 +++ app/templates/login.html | 15 ++ app/templates/logs.html | 43 ++++ app/templates/overview.html | 53 +++++ app/templates/ovw.html | 10 + app/templates/runtimeapi.html | 46 ++++ app/templates/viewlogs.html | 30 +++ app/templates/viewsettings.html | 15 ++ app/templates/viewstats.html | 20 ++ app/users.py | 234 ++----------------- app/viewlogs.py | 74 +++--- app/viewsttats.py | 46 ++-- requirements.txt | 3 +- update.sh | 2 + 38 files changed, 1962 insertions(+), 1436 deletions(-) create mode 100644 app/templates/add.html create mode 100644 app/templates/admin.html create mode 100644 app/templates/base.html create mode 100644 app/templates/config.html create mode 100644 app/templates/configver.html create mode 100644 app/templates/delver.html create mode 100644 app/templates/ha.html create mode 100644 app/templates/ihap.html create mode 100644 app/templates/login.html create mode 100644 app/templates/logs.html create mode 100644 app/templates/overview.html create mode 100644 app/templates/ovw.html create mode 100644 app/templates/runtimeapi.html create mode 100644 app/templates/viewlogs.html create mode 100644 app/templates/viewsettings.html create mode 100644 app/templates/viewstats.html diff --git a/app/add.py b/app/add.py index 208037e5..47210668 100644 --- a/app/add.py +++ b/app/add.py @@ -5,20 +5,37 @@ import os from configparser import ConfigParser, ExtendedInterpolation import funct import sql +import http +from jinja2 import Environment, FileSystemLoader +env = Environment(loader=FileSystemLoader('templates/')) +template = env.get_template('add.html') +form = cgi.FieldStorage() -funct.head("Add") -funct.check_config() +print('Content-type: text/html\n') funct.check_login() +funct.page_for_admin(level = 2) + +try: + cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) + user_id = cookie.get('uuid') + user = sql.get_user_name_by_uuid(user_id.value) + servers = sql.get_dick_permit() +except: + pass + +output_from_parsed_template = template.render(title = "Add", + role = sql.get_user_role_by_uuid(user_id.value), + user = user, + selects = servers, + add = form.getvalue('add'), + conf_add = form.getvalue('conf')) +print(output_from_parsed_template) path_config = "haproxy-webintarface.config" config = ConfigParser(interpolation=ExtendedInterpolation()) config.read(path_config) -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() if form.getvalue('mode') is not None: serv = form.getvalue('serv') @@ -54,9 +71,7 @@ if form.getvalue('mode') is not None: ssl_check = " ssl verify" else: ssl_check = "" - - - + if not ip and form.getvalue('port') is not None: bind = " bind *:"+ port + " " + ssl + "\n" elif port is not None: @@ -112,369 +127,9 @@ if form.getvalue('mode') is not None: if master[0] != None: funct.upload_and_restart(master[0], cfg) - if funct.upload_and_restart(serv, cfg): - print('' % (name, config_add)) + funct.upload_and_restart(serv, cfg) + print('' % (name, config_add)) print('') -if form.getvalue('add') is not None: - print('

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

') - print('
') - print(form.getvalue('conf')) - print('
') -print('
' - '' - '
' - '
' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '

Add listen

Select server: ' - '' - '
Note: If you reconfigure Master server, Slave will reconfigured automatically
' - '
Name:' - '' - '
IP and Port:' - ':' - '' - '
IP for bind listner, if empty will be assignet on all IPs. Start typing ip, or press down.
If you use VRRP keep in blank. If you assign an IP, the slave will not start
' - '
Mode: ' - '' - '' - '' - '' - '' - '' - '
Balance: ' - '' - '
Optinons:' - '' - '' - '
Servers:' - '' - '
' - '' - '' - ' Default params: inter 2000 rise 2 fall 5' - '
' - '' - '
') -funct.get_button("Add Listen") -print('
' - - '' - - '
' - '
' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '

Add frontend

Select server: ' - '' - '
Note: If you reconfigure Master server, Slave will reconfigured automatically
' - '
Name:' - '' - '
IP and Port:' - ':' - '' - '
IP for bind listner, if empty will be assignet on all IPs. Start typing ip, or press down.
If you use VRRP keep in blank. If you assign an IP, the slave will not start
' - '
Mode: ' - '' - '' - '' - '' - '' - '' - '
Optinons:' - '' - '' - '
Default backend' - '
Start typing backend, or press down
' - '' - ' .' - '

Note: If backend don\'t exist, you must create backend first.

' - '
') -funct.get_button("Add Frontend") -print('
' - '
' - - - '' - - '
' - '
' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '

Add backend

Select server: ' - '' - '
Note: If you reconfigure Master server, Slave will reconfigured automatically
' - '
Name:' - '' - '
Mode: ' - '' - '' - '' - '' - '' - '' - '
Balance: ' - '' - '
Optinons:' - '' - '' - '
Servers:' - '' - '
' - '' - '' - ' Default params: inter 2000 rise 2 fall 5' - '
' - '' - '
') -funct.get_button("Add Backend") -print('
' - - '
' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '
Upload SSL certificates' - 'Certificate name' - '' - 'Paste certificate content here(?)' - '
' - '' - '' - '' - '' - '

' - 'Upload' - '
' - '
' - '
') - - -funct.footer() \ No newline at end of file diff --git a/app/config.py b/app/config.py index fae21cf7..edaad85d 100644 --- a/app/config.py +++ b/app/config.py @@ -6,15 +6,26 @@ import http.cookies from configparser import ConfigParser, ExtendedInterpolation import funct import sql - +from jinja2 import Environment, FileSystemLoader +env = Environment(loader=FileSystemLoader('templates/')) +template = env.get_template('config.html') +print('Content-type: text/html\n') +funct.check_login() +funct.page_for_admin(level = 2) form = cgi.FieldStorage() serv = form.getvalue('serv') -servNew = form.getvalue('serNew') +config_read = "" +cfg = "" +stderr = "" +aftersave = "" -funct.head("Edit HAproxy config") -funct.check_config() -funct.check_login() -funct.page_for_admin(level = 1) +try: + cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) + user_id = cookie.get('uuid') + user = sql.get_user_name_by_uuid(user_id.value) + servers = sql.get_dick_permit() +except: + pass path_config = "haproxy-webintarface.config" config = ConfigParser(interpolation=ExtendedInterpolation()) @@ -23,8 +34,6 @@ config.read(path_config) log_path = config.get('main', 'log_path') hap_configs_dir = config.get('configs', 'haproxy_save_configs_dir') -funct.chooseServer("config.py", "Edit HAproxy config", "y") - if serv is not None: cfg = hap_configs_dir + serv + "-" + funct.get_data('config') + ".cfg" @@ -38,18 +47,10 @@ if form.getvalue('serv') is not None and form.getvalue('open') is not None : try: conf = open(cfg, "r") + config_read = conf.read() except IOError: print('
Can\'t read import config file
') - print("

Config from %s

" % serv) - print('
') - print('' % serv) - print('' % cfg) - print('' % conf.read()) - print('

') - funct.get_button("Just save", value="save") - funct.get_button("Save and restart") - print('

') conf.close os.system("/bin/mv %s %s.old" % (cfg, cfg)) @@ -63,27 +64,33 @@ if form.getvalue('serv') is not None and form.getvalue('config') is not None: config = form.getvalue('config') oldcfg = form.getvalue('oldconfig') save = form.getvalue('save') - + aftersave = 1 try: with open(cfg, "a") as conf: conf.write(config) except IOError: print("Can't read import config file") - - 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) - - + stderr = 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'))) os.system("/bin/rm -f " + hap_configs_dir + "*.old") - print('
Go to view stats
' % serv) - -funct.footer() \ No newline at end of file +output_from_parsed_template = template.render(h2 = 1, title = "Edit Runnig HAProxy config", + role = sql.get_user_role_by_uuid(user_id.value), + action = "config.py", + user = user, + select_id = "serv", + serv = serv, + aftersave = aftersave, + config = config_read, + cfg = cfg, + selects = servers, + stderr = stderr, + note = 1) +print(output_from_parsed_template) \ No newline at end of file diff --git a/app/configshow.py b/app/configshow.py index f14ab16b..50bf2f5b 100644 --- a/app/configshow.py +++ b/app/configshow.py @@ -1,46 +1,27 @@ #!/usr/bin/env python3 -import html -import cgi import os +import sql +import http import funct -import paramiko -from configparser import ConfigParser, ExtendedInterpolation -from datetime import datetime -from pytz import timezone - -form = cgi.FieldStorage() -serv = form.getvalue('serv') - -funct.head("Get Running Config") -funct.check_config() +from jinja2 import Environment, FileSystemLoader +env = Environment(loader=FileSystemLoader('templates/')) +template = env.get_template('config.html') +print('Content-type: text/html\n') funct.check_login() -path_config = "haproxy-webintarface.config" -config = ConfigParser(interpolation=ExtendedInterpolation()) -config.read(path_config) +try: + cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) + user_id = cookie.get('uuid') + user = sql.get_user_name_by_uuid(user_id.value) + servers = sql.get_dick_permit() +except: + pass -ssh_keys = config.get('ssh', 'ssh_keys') -ssh_user_name = config.get('ssh', 'ssh_user_name') -hap_configs_dir = config.get('configs', 'haproxy_save_configs_dir') -haproxy_config_path = config.get('haproxy', 'haproxy_config_path') - -funct.chooseServer("configshow.py", "Get Running Config", "n", onclick="showConfig()") - -print('
') -if serv is not None and form.getvalue('open') is not None : - - cfg = hap_configs_dir + serv + "-" + funct.get_data('config') + ".cfg" - - funct.get_config(serv, cfg) - - print("

Config from %s

" % serv) - print('

' - 'Expand all' - '

') - print('
') - funct.show_config(cfg) - - os.system("/bin/rm -f " + cfg) - -print('
') -funct.footer() \ No newline at end of file +output_from_parsed_template = template.render(h2 = 1, title = "Show Runnig config", + role = sql.get_user_role_by_uuid(user_id.value), + user = user, + onclick = "showConfig()", + select_id = "serv", + selects = servers, + note = 0) +print(output_from_parsed_template) \ No newline at end of file diff --git a/app/configver.py b/app/configver.py index 10fa5790..3d8d041f 100644 --- a/app/configver.py +++ b/app/configver.py @@ -1,92 +1,83 @@ #!/usr/bin/env python3 import html import cgi -import os +import os, http.cookies import funct import sql from configparser import ConfigParser, ExtendedInterpolation - -form = cgi.FieldStorage() -serv = form.getvalue('serv') -configver = form.getvalue('configver') - -funct.head("Old Versions HAproxy config") -funct.check_config() +from jinja2 import Environment, FileSystemLoader +env = Environment(loader=FileSystemLoader('templates/')) +template = env.get_template('configver.html') +print('Content-type: text/html\n') funct.check_login() funct.page_for_admin(level = 2) +form = cgi.FieldStorage() +serv = form.getvalue('serv') +config_read = "" +configver = "" +stderr = "" +aftersave = "" + +try: + cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) + user_id = cookie.get('uuid') + user = sql.get_user_name_by_uuid(user_id.value) + servers = sql.get_dick_permit() +except: + pass path_config = "haproxy-webintarface.config" config = ConfigParser(interpolation=ExtendedInterpolation()) config.read(path_config) - +form = cgi.FieldStorage() +serv = form.getvalue('serv') +configver = form.getvalue('configver') hap_configs_dir = config.get('configs', 'haproxy_save_configs_dir') -funct.chooseServer("configver.py#conf", "Old Versions HAproxy config", "y") - -if serv is not None and form.getvalue('open') is not None: - - print('

Choose old version

') - print('
') - print('

') - print('' % serv) - print('') - print('

') - - Select = form.getvalue('Select') - - if Select is not None: - - configver = form.getvalue('configver') - funct.logging(serv, "open old config %s" % configver) - - print("

Config from %s, and version is: %s

" % (serv, configver)) - print('
') - print('' % serv) - print('' % configver) - print('') - print('') - print('

' - 'Expand all' - '

') - funct.show_config(configver) - print('

') - funct.get_button("Just save", value="save") - funct.get_button("Upload and restart") - print('

') - + return_files.add(file) + return sorted(return_files, reverse=True) if form.getvalue('serv') is not None and form.getvalue('config') is not None: configver = form.getvalue('configver') configver = hap_configs_dir + configver save = form.getvalue('save') - - funct.logging(serv, "configver.py upload old config %s" % configver) - - print("
Uploaded old config ver: %s

" % configver) + try: + funct.logging(serv, "configver.py upload old config %s" % configver) + except: + pass 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) + stderr = funct.upload_and_restart(serv, configver, just_save=save) + aftersave = 1 - print('
Go to view stats
' % serv) -funct.footer() +output_from_parsed_template = template.render(h2 = 1, title = "Old Versions HAProxy config", + role = sql.get_user_role_by_uuid(user_id.value), + action = "configver.py", + user = user, + select_id = "serv", + serv = serv, + return_files = get_files(), + aftersave = aftersave, + config = config_read, + configver = configver, + selects = servers, + stderr = stderr, + open = form.getvalue('open'), + onclick = "showUploadConfig()", + note = 1) +print(output_from_parsed_template) diff --git a/app/delver.py b/app/delver.py index f21361aa..8e8a3075 100644 --- a/app/delver.py +++ b/app/delver.py @@ -1,61 +1,76 @@ #!/usr/bin/env python3 -import html +import html, http.cookies import cgi import os -import funct +import funct, sql from configparser import ConfigParser, ExtendedInterpolation import glob - +from configparser import ConfigParser, ExtendedInterpolation +from jinja2 import Environment, FileSystemLoader +env = Environment(loader=FileSystemLoader('templates/')) +template = env.get_template('delver.html') +print('Content-type: text/html\n') +funct.check_login() +funct.page_for_admin() form = cgi.FieldStorage() serv = form.getvalue('serv') +stderr = "" +aftersave = "" +file = set() -funct.head("Old Versions HAproxy config") -funct.check_config() -funct.check_login() +try: + cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) + user_id = cookie.get('uuid') + user = sql.get_user_name_by_uuid(user_id.value) + servers = sql.get_dick_permit() +except: + pass path_config = "haproxy-webintarface.config" config = ConfigParser(interpolation=ExtendedInterpolation()) config.read(path_config) - +form = cgi.FieldStorage() +serv = form.getvalue('serv') +Select = form.getvalue('del') hap_configs_dir = config.get('configs', 'haproxy_save_configs_dir') -funct.page_for_admin() -funct.chooseServer("delver.py", "Delete Versions HAproxy config", "n") +def get_files(): + import glob + file = set() + return_files = set() + for files in glob.glob(os.path.join(hap_configs_dir,'*.cfg')): + file.add(files.split('/')[6]) + files = sorted(file, reverse=True) + for file in files: + ip = file.split("-") + if serv == ip[0]: + return_files.add(file) + return sorted(return_files, reverse=True) if serv is not None and form.getvalue('open') is not None: - - print('

Choose old version

') - print('
' - '' - '
') - - os.chdir(hap_configs_dir) - - for files in sorted(glob.glob('*.cfg')): - ip = files.split("-") - if serv == ip[0]: - print('
' % (files, files, files, files, files)) - - print('' % serv) - print('') - print('') - print('

') - funct.get_button("Delete") - print('

') - - Select = form.getvalue('del') - if Select is not None: - os.chdir(hap_configs_dir) - print("The following files were deleted:
") + aftersave = 1 for get in form: if "cfg" in get: try: - os.remove(form.getvalue(get)) - print(form.getvalue(get) + "
") + os.remove(os.path.join(hap_configs_dir, form.getvalue(get))) + file.add(form.getvalue(get) + "
") funct.logging(serv, "delver.py deleted config: %s" % form.getvalue(get)) except OSError: - print ("Error: %s - %s." % (e.filename,e.strerror)) + stderr = "Error: %s - %s." % (e.filename,e.strerror) print('' % form.getvalue('serv')) -funct.footer() \ No newline at end of file +output_from_parsed_template = template.render(h2 = 1, title = "Delete old versions HAProxy config", + role = sql.get_user_role_by_uuid(user_id.value), + action = "delver.py", + user = user, + select_id = "serv", + serv = serv, + aftersave = aftersave, + return_files = get_files(), + selects = servers, + stderr = stderr, + open = form.getvalue('open'), + Select = form.getvalue('del'), + file = file) +print(output_from_parsed_template) \ No newline at end of file diff --git a/app/diff.py b/app/diff.py index 3ab6ac5b..263d0a8c 100644 --- a/app/diff.py +++ b/app/diff.py @@ -1,28 +1,28 @@ #!/usr/bin/env python3 -import html -import cgi +import os +import sql +import http import funct -import ovw - -form = cgi.FieldStorage() -serv = form.getvalue('serv') -left = form.getvalue('left') -right = form.getvalue('right') +import sql +from jinja2 import Environment, FileSystemLoader +env = Environment(loader=FileSystemLoader('templates/')) +template = env.get_template('config.html') -funct.head("Compare HAproxy configs") -funct.check_config() -funct.check_login() -funct.chooseServer("diff.py#diff", "Compare HAproxy configs", "n", onclick="showCompareConfigs()") +print('Content-type: text/html\n') +funct.check_login() -print('
') +try: + cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) + user_id = cookie.get('uuid') + user = sql.get_user_name_by_uuid(user_id.value) + servers = sql.get_dick_permit() +except: + pass -if serv is not None and form.getvalue('open') is not None : - ovw.show_compare_configs(serv) - -print('
') - -if serv is not None and right is not None: - ovw.comapre_show() - -print('
') -funct.footer() \ No newline at end of file +output_from_parsed_template = template.render(h2 = 1, title = "Compare configs", + role = sql.get_user_role_by_uuid(user_id.value), + user = user, + onclick = "showCompareConfigs()", + select_id = "serv", + selects = servers) +print(output_from_parsed_template) \ No newline at end of file diff --git a/app/edit.py b/app/edit.py index eff77d00..90dcaad2 100644 --- a/app/edit.py +++ b/app/edit.py @@ -1,52 +1,29 @@ #!/usr/bin/env python3 -import html -import cgi +import os +import sql +import http import funct import sql +from jinja2 import Environment, FileSystemLoader +env = Environment(loader=FileSystemLoader('templates/')) +template = env.get_template('runtimeapi.html') -form = cgi.FieldStorage() -serv = form.getvalue('serv') - -funct.head("Runtime API") +print('Content-type: text/html\n') funct.check_login() -funct.check_config() -print('

Runtime API

' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '
ServerDisable/Enable server or output any informationCommandSave change
' - '
' - '
' - '' - '' - '' - '' - '' - 'Enter' - '
' - '
' - '
') - -funct.footer() \ No newline at end of file +output_from_parsed_template = template.render(h2 = 1, + title = "Runtime API", + role = sql.get_user_role_by_uuid(user_id.value), + user = user, + onclick = "showRuntime()", + select_id = "serv", + selects = servers) +print(output_from_parsed_template) \ No newline at end of file diff --git a/app/funct.py b/app/funct.py index 855c8d3d..499b049a 100644 --- a/app/funct.py +++ b/app/funct.py @@ -121,189 +121,6 @@ def get_button(button, **kwargs): if value is None: value = "" print('' % (value, value, button)) - -def head(title): - print('Content-type: text/html\n') - print('%s' % title) - print('' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '
' - '
' - 'HAproxy-WI' - '' - '' - '' - '' - '' - '
') - links() - print('
') - -def links(): - print('') - -def show_login_links(): - import sql - cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) - user_id = cookie.get('uuid') - - if user_id is None: - print('
  • ') - else: - print('
  • ' % sql.get_user_name_by_uuid(user_id.value)) - -def footer(): - print('
    ' - '
    ' - '

    ' - 'UP' - '


    ' - '
    ' - '') -def get_auto_refresh(h2): - print('

    ') - print('%s' % h2) - print('' - 'restart Auto-refresh' - '' - '' - '' - '

    ' - '
    ' - '
    ' - 'Refresh Interval' - '
    ' - '
    ' - '
    ' - '
      ' - '
    • ' - 'Off ' - '
    • ' - '
    ' - '
    ' - '
    ' - '' - '
    ' - '
    ' - '' - '
    ' - '
    ' - '' - '
    ' - '
    ' - '
    ') def ssh_connect(serv, **kwargs): ssh = SSHClient() @@ -339,10 +156,8 @@ def ssh_connect(serv, **kwargs): def get_config(serv, cfg, **kwargs): if kwargs.get("keepalived"): - os.chdir(config.get('configs', 'kp_save_configs_dir')) config_path = "/etc/keepalived/keepalived.conf" else: - os.chdir(hap_configs_dir) config_path = haproxy_config_path ssh = ssh_connect(serv) @@ -433,7 +248,6 @@ def upload_and_restart(serv, cfg, **kwargs): try: ssh = ssh_connect(serv) - print('
    connected to %s
    ' % serv) except: print('
    Connect fail
    ') sftp = ssh.open_sftp() @@ -446,32 +260,20 @@ def upload_and_restart(serv, cfg, **kwargs): commands = [ "mv -f " + tmp_file + " /etc/keepalived/keepalived.conf", "systemctl restart keepalived" ] else: if kwargs.get("just_save") == "save": - commands = [ "/sbin/haproxy -q -c -f " + tmp_file, "mv -f " + tmp_file + " " + haproxy_config_path ] + commands = [ "/sbin/haproxy -q -c -f " + tmp_file + "&& mv -f " + tmp_file + " " + haproxy_config_path ] else: - commands = [ "/sbin/haproxy -q -c -f " + tmp_file, "mv -f " + tmp_file + " " + haproxy_config_path, restart_command ] + commands = [ "/sbin/haproxy -q -c -f " + tmp_file + "&& mv -f " + tmp_file + " " + haproxy_config_path + " && " + restart_command ] try: if config.get('haproxy', 'firewall_enable') == "1": commands.extend(open_port_firewalld(cfg)) except: - print('
    Please check the config for the presence of the parameter - "firewall_enable". Mast be: "0" or "1". Firewalld configure not working now
    ') + return 'Please check the config for the presence of the parameter - "firewall_enable". Mast be: "0" or "1". Firewalld configure not working now' - i = 0 for command in commands: - i = i + 1 - stdin , stdout, stderr = ssh.exec_command(command) - if i == 1: - if not stderr.read(): - print('
    Config ok
    ')
    -			else:
    -				print('
    In your config have errors, please check, and try again


    ') - return False - break - if i is not 1: - print(stderr.read().decode(encoding='UTF-8')) - - return True - - print('
    ') + stdin, stdout, stderr = ssh.exec_command(command) + + return stderr.read().decode(encoding='UTF-8') + ssh.close() def open_port_firewalld(cfg): @@ -564,7 +366,7 @@ def server_status(stdout): print(' UP running %s processes' % proc_count) else: print(' DOWN running %s processes' % proc_count) - + def ssh_command(serv, commands, **kwargs): ssh = ssh_connect(serv) @@ -589,47 +391,3 @@ 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: - listhap = sql.get_dick_permit() - - if kwargs.get("servNew"): - servNew = kwargs.get("servNew") - else: - servNew = "" - - for i in listhap: - if i[2] == serv or i[2] == servNew: - selected = 'selected' - else: - selected = '' - - print('' % (i[2], selected, i[1])) - -def chooseServer(formName, title, note, **kwargs): - servNew = form.getvalue('serNew') - - print('

    ' + title + '

    ') - print('

    Choose server

    ') - print('
    ') - print('

    ') - - if kwargs.get("onclick") is not None: - print('Show' % kwargs.get("onclick")) - else: - get_button("Open", value="open") - - print('

    ') - - if note == "y": - print('
    Note: If you reconfigure Master server, Slave will reconfigured automatically
    ') - print('
    ') - diff --git a/app/ha.py b/app/ha.py index 1fef678a..07076c80 100644 --- a/app/ha.py +++ b/app/ha.py @@ -1,93 +1,29 @@ #!/usr/bin/env python3 -import html +import html, http.cookies import cgi -import funct -import sql +import os +import funct, sql from configparser import ConfigParser, ExtendedInterpolation - -funct.head("HA") +from jinja2 import Environment, FileSystemLoader +env = Environment(loader=FileSystemLoader('templates/')) +template = env.get_template('ha.html') +print('Content-type: text/html\n') funct.check_login() funct.page_for_admin() +form = cgi.FieldStorage() +serv = form.getvalue('serv') -path_config = "haproxy-webintarface.config" -config = ConfigParser(interpolation=ExtendedInterpolation()) -config.read(path_config) -serv = "" - -print('' - '

    Configure HA

    ' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '

    Create new HA cluster

    MasterSlaveVRRP interfaceVRRP IPInstall HAProxy(?)
    ' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - 'Create' - '
    ' - - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '

    Or add VRRP to exist

    MasterSlaveVRRP interfaceVRRP IPRestart Keepalived(?)
    ' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - 'Add' - '
    ' - '
    ') +try: + cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) + user_id = cookie.get('uuid') + user = sql.get_user_name_by_uuid(user_id.value) + servers = sql.get_dick_permit() +except: + pass +output_from_parsed_template = template.render(h2 = 1, title = "Configure HA", + role = sql.get_user_role_by_uuid(user_id.value), + user = user, + serv = serv, + selects = servers) +print(output_from_parsed_template) \ No newline at end of file diff --git a/app/ihap.py b/app/ihap.py index f82d42c9..bb99c099 100644 --- a/app/ihap.py +++ b/app/ihap.py @@ -1,40 +1,29 @@ #!/usr/bin/env python3 -import html +import html, http.cookies import cgi -import funct -import sql +import os +import funct, sql from configparser import ConfigParser, ExtendedInterpolation - -funct.head("Installation HAProxy") +from jinja2 import Environment, FileSystemLoader +env = Environment(loader=FileSystemLoader('templates/')) +template = env.get_template('ihap.html') +print('Content-type: text/html\n') funct.check_login() funct.page_for_admin() +form = cgi.FieldStorage() -path_config = "haproxy-webintarface.config" -config = ConfigParser(interpolation=ExtendedInterpolation()) -config.read(path_config) -proxy = config.get('main', 'proxy') -serv = "" +try: + cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) + user_id = cookie.get('uuid') + user = sql.get_user_name_by_uuid(user_id.value) + servers = sql.get_dick_permit() +except: + pass + +output_from_parsed_template = template.render(h2 = 1, title = "Installation HAProxy", + role = sql.get_user_role_by_uuid(user_id.value), + user = user, + select_id = "haproxyaddserv", + selects = servers) +print(output_from_parsed_template) -print('' - '

    Installation HAProxy

    ' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '
    NoteServer
    ' - 'Haproxy-WI will try install haproxy-1.18.5, if it does not work then haproxy-1.15' - '' - '' - '' - 'Install' - '
    ' - '
    ') \ No newline at end of file diff --git a/app/keepalivedconfig.py b/app/keepalivedconfig.py index ec5eb778..4bbdc11a 100644 --- a/app/keepalivedconfig.py +++ b/app/keepalivedconfig.py @@ -1,5 +1,4 @@ #!/usr/bin/env python3 -# -*- coding: utf-8 -*-" import html import cgi import os @@ -7,16 +6,26 @@ import http.cookies from configparser import ConfigParser, ExtendedInterpolation import funct import sql -import codecs - +from jinja2 import Environment, FileSystemLoader +env = Environment(loader=FileSystemLoader('templates/')) +template = env.get_template('config.html') +print('Content-type: text/html\n') +funct.check_login() +funct.page_for_admin() form = cgi.FieldStorage() serv = form.getvalue('serv') -servNew = form.getvalue('serNew') +config_read = "" +cfg = "" +stderr = "" +aftersave = "" -funct.head("Edit Running Keepalived config") -funct.check_config() -funct.check_login() -funct.page_for_admin(level = 1) +try: + cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) + user_id = cookie.get('uuid') + user = sql.get_user_name_by_uuid(user_id.value) + servers = sql.is_master("123", master_slave=1) +except: + pass path_config = "haproxy-webintarface.config" config = ConfigParser(interpolation=ExtendedInterpolation()) @@ -25,29 +34,6 @@ config.read(path_config) log_path = config.get('main', 'log_path') kp_save_configs_dir = config.get('configs', 'kp_save_configs_dir') -print('

    Edit Running Keepalived config

    ' - '
    ' - '

    Choose server

    ' - '
    ' - '') -funct.get_button("Open", value="open") -print('
    ') - if serv is not None: cfg = kp_save_configs_dir+ serv + '-' + funct.get_data('config') + '.conf' @@ -61,18 +47,10 @@ if form.getvalue('serv') is not None and form.getvalue('open') is not None : try: conf = open(cfg, "r",encoding='utf-8', errors='ignore') + config_read = conf.read() except IOError: print('
    Can\'t read import config file
    ') - print("

    Config from %s

    " % serv) - print('
    ') - print('' % serv) - print('' % cfg) - print('' % conf.read()) - print('

    ') - funct.get_button("Just save", value="save") - funct.get_button("Save and restart") - print('

    ') conf.close os.system("/bin/mv %s %s.old" % (cfg, cfg)) @@ -86,21 +64,28 @@ if form.getvalue('serv') is not None and form.getvalue('config') is not None: config = form.getvalue('config') oldcfg = form.getvalue('oldconfig') save = form.getvalue('save') - + aftersave = 1 try: with open(cfg, "a") as conf: conf.write(config) except IOError: print("Can't read import config file") - - print('

    New config was saved as: %s
    ' % cfg) - - funct.upload_and_restart(serv, cfg, just_save=save, keepalived=1) - - + + stderr = funct.upload_and_restart(serv, cfg, just_save=save, keepalived=1) + os.system("/bin/diff -ub %s %s >> %s/config_edit-%s.log" % (oldcfg, cfg, log_path, funct.get_data('logs'))) - os.system("/bin/rm -f kp_config/*.old") + os.system("/bin/rm -f " + kp_save_configs_dir + "*.old") - print('
    Go to view stats
    ' % serv) - -funct.footer() \ No newline at end of file +output_from_parsed_template = template.render(h2 = 1, title = "Edit Runnig Keepalived config", + role = sql.get_user_role_by_uuid(user_id.value), + action = "keepalivedconfig.py", + user = user, + select_id = "serv", + serv = serv, + aftersave = aftersave, + config = config_read, + cfg = cfg, + selects = servers, + stderr = stderr, + keepalived = 1) +print(output_from_parsed_template) \ No newline at end of file diff --git a/app/login.py b/app/login.py index 08ab06db..8dc6cd08 100644 --- a/app/login.py +++ b/app/login.py @@ -10,52 +10,52 @@ import create_db import datetime import uuid from configparser import ConfigParser, ExtendedInterpolation +from jinja2 import Environment, FileSystemLoader +env = Environment(loader=FileSystemLoader('templates/')) +template = env.get_template('login.html') +form = cgi.FieldStorage() cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) -form = cgi.FieldStorage() +user_id = cookie.get('uuid') ref = form.getvalue('ref') login = form.getvalue('login') password = form.getvalue('pass') +db_create = "" +error_log = "" +error = "" path_config = "haproxy-webintarface.config" config = ConfigParser(interpolation=ExtendedInterpolation()) config.read(path_config) -def login_page(error): - if error == "error": - funct.head("Login page") - printError = "

    Login page. Enter please



    Somthing wrong :( I'm sad about this, but try again!

    " - else: - printError = "

    Login page. Enter please



    " +if ref is None: + ref = "/index.html" - if create_db.check_db(): - if create_db.create_table(): - print('
    DB was created
    ') - create_db.update_all() - print('
    Now you can login, default: admin/admin
    ') - create_db.update_all_silent() +if form.getvalue('error'): + error_log = '
    Somthing wrong :( I\'m sad about this, but try again!


    ' + +try: + if config.get('main', 'session_ttl'): + session_ttl = config.getint('main', 'session_ttl') +except: + error = '
    Can not find "session_ttl" parametr. Check into config, "main" section
    ' + pass - ref = form.getvalue('ref') - if ref is None: - ref = "/index.html" - - print('
    ') - print(printError) - print('

    ') - print('

    ') - print('' % ref) - print('') - print('
    ') +try: + role = sql.get_user_role_by_uuid(user_id.value) + user = sql.get_user_name_by_uuid(user_id.value) +except: + role = "" + user = "" + pass - try: - if config.get('main', 'session_ttl'): - session_ttl = config.getint('main', 'session_ttl') - except: - print('
    Can not find "session_ttl" parametr. Check into config, "main" section
    ') - -if form.getvalue('logout') is not None: - cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) - user_id = cookie.get('uuid') +if create_db.check_db(): + if create_db.create_table(): + create_db.update_all() + db_create = '
    DB was created

    Now you can login, default: admin/admin
    ' +create_db.update_all_silent() + +if form.getvalue('logout'): try: sql.delete_uuid(user_id.value) except: @@ -63,15 +63,9 @@ if form.getvalue('logout') is not None: print("Set-cookie: uuid=; expires=Wed May 18 03:33:20 2003; path=/app/; httponly") print("Content-type: text/html\n") print('') - -if login is None: - funct.head("Login page") - login_page("n") - + if login is not None and password is not None: - if form.getvalue('ref') is None: - ref = "/index.html" - + USERS = sql.select_users() session_ttl = config.getint('main', 'session_ttl') expires = datetime.datetime.utcnow() + datetime.timedelta(days=session_ttl) @@ -90,7 +84,18 @@ if login is not None and password is not None: print('Redirecting') print('') print('' % ref) - sys.exit() - login_page("error") - -funct.footer() \ No newline at end of file + sys.exit() + + print("Content-type: text/html\n") + print('') + +if login is None: + print("Content-type: text/html\n") +output_from_parsed_template = template.render(h2 = 1, title = "Login page. Enter please", + role = role, + user = user, + error_log = error_log, + error = error, + ref = ref, + db_create = db_create) +print(output_from_parsed_template) \ No newline at end of file diff --git a/app/logs.py b/app/logs.py index 3225a98d..9c5481e2 100644 --- a/app/logs.py +++ b/app/logs.py @@ -2,53 +2,46 @@ import html import cgi import funct - +import sql +import os, http +from jinja2 import Environment, FileSystemLoader +env = Environment(loader=FileSystemLoader('templates/')) +template = env.get_template('logs.html') form = cgi.FieldStorage() -serv = form.getvalue('serv') -funct.head("HAproxy Logs") -funct.check_config() +if form.getvalue('grep') is None: + grep = "" +else: + grep = form.getvalue('grep') + +if form.getvalue('rows') is None: + rows = 10 +else: + rows = form.getvalue('rows') + +print('Content-type: text/html\n') funct.check_login() -funct.get_auto_refresh("HAproxy logs") -print('' - '' - '' - '' - '' - '' - '' - '' - '' % rows) -print('' - '' - '' - '
    ServerNumber rowsEx for grep
    ' - '
    ' - '') -if serv is not None: - rows = 'value='+form.getvalue('rows') -else: - rows = 'value=10' -if form.getvalue('grep') is not None: - grep = 'value='+form.getvalue('grep') -else: - grep = ' ' - -print('
    ' % grep) -print('' - 'Show' - '
    ' - '
    ' - '
    ' - '') -funct.footer() \ No newline at end of file diff --git a/app/map.py b/app/map.py index f34181d7..290808cc 100644 --- a/app/map.py +++ b/app/map.py @@ -1,21 +1,28 @@ #!/usr/bin/env python3 -import html -import cgi import os +import sql +import http import funct -import ovw +import sql +from jinja2 import Environment, FileSystemLoader +env = Environment(loader=FileSystemLoader('templates/')) +template = env.get_template('config.html') -form = cgi.FieldStorage() -serv = form.getvalue('serv') - -funct.head("Show HAproxy config") -funct.check_config() +print('Content-type: text/html\n') funct.check_login() -funct.chooseServer("map.py", "Show HAproxy map", "n", onclick="showMap()") -print('
    ') -if serv is not None: - ovw.get_map(serv) -print('
    ') - -funct.footer() \ No newline at end of file +try: + cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) + user_id = cookie.get('uuid') + user = sql.get_user_name_by_uuid(user_id.value) + servers = sql.get_dick_permit() +except: + pass + +output_from_parsed_template = template.render(h2 = 1, title = "Show Map", + role = sql.get_user_role_by_uuid(user_id.value), + user = user, + onclick = "showMap()", + select_id = "serv", + selects = servers) +print(output_from_parsed_template) \ No newline at end of file diff --git a/app/options.py b/app/options.py index 6eb4c63b..d46fffbb 100644 --- a/app/options.py +++ b/app/options.py @@ -188,11 +188,15 @@ if serv is not None and act == "configShow": import os from datetime import datetime from pytz import timezone - hap_configs_dir = config.get('configs', 'haproxy_save_configs_dir') - cfg = hap_configs_dir + serv + "-" + funct.get_data('config') + ".cfg" - funct.get_config(serv, cfg) + if form.getvalue('configver') is None: + cfg = hap_configs_dir + serv + "-" + funct.get_data('config') + ".cfg" + funct.get_config(serv, cfg) + else: + cfg = hap_configs_dir + form.getvalue('configver') + + print('') print("

    Config from %s

    " % serv) @@ -202,8 +206,18 @@ if serv is not None and act == "configShow": print('
    ') funct.show_config(cfg) - - os.system("/bin/rm -f " + cfg) + + if form.getvalue('configver') is None: + os.system("/bin/rm -f " + cfg) + else: + print('
    ') + print('
    ') + print('' % serv) + print('' % form.getvalue('configver')) + print('') + funct.get_button("Just save", value="save") + funct.get_button("Upload and restart") + print('
    ') if form.getvalue('viewlogs') is not None: viewlog = form.getvalue('viewlogs') @@ -213,7 +227,7 @@ if form.getvalue('viewlogs') is not None: print('
    Please check the config for the presence of the parameter - "log_path".
    ') try: - log = open(log_path + viewlog, "r") + log = open(log_path + viewlog, "r",encoding='utf-8', errors='ignore') except IOError: print('
    Can\'t read import log file
    ') sys.exit() diff --git a/app/overview.py b/app/overview.py index e6db6197..75586e2e 100644 --- a/app/overview.py +++ b/app/overview.py @@ -1,14 +1,24 @@ #!/usr/bin/env python3 -import html -import cgi -import funct -import ovw - -funct.head("Overview") -funct.check_config() +import funct, sql +import os, http +from jinja2 import Environment, FileSystemLoader +env = Environment(loader=FileSystemLoader('templates/')) +template = env.get_template('ovw.html') + +print('Content-type: text/html\n') funct.check_login() -funct.get_auto_refresh("Overview") -print("") -print('
    ') -funct.footer() \ No newline at end of file +try: + cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) + user_id = cookie.get('uuid') + user = sql.get_user_name_by_uuid(user_id.value) + +except: + pass + +output_from_parsed_template = template.render(h2 = 1, + autorefresh = 1, + title = "Overview", + role = sql.get_user_role_by_uuid(user_id.value), + user = user) +print(output_from_parsed_template) \ No newline at end of file diff --git a/app/settings.py b/app/settings.py index fe6058dd..963c65ef 100644 --- a/app/settings.py +++ b/app/settings.py @@ -1,32 +1,41 @@ #!/usr/bin/env python3 -import html +import html, http import cgi import sys import os -import funct +import funct, sql from configparser import ConfigParser, ExtendedInterpolation - -funct.head("Admin area: View settings") -funct.check_config() -funct.check_login() -funct.page_for_admin() - +from jinja2 import Environment, FileSystemLoader +env = Environment(loader=FileSystemLoader('templates/')) +template = env.get_template('viewsettings.html') +form = cgi.FieldStorage() path_config = "haproxy-webintarface.config" config = ConfigParser(interpolation=ExtendedInterpolation()) config.read(path_config) fullpath = config.get('main', 'fullpath') -print('

    Admin area: View settings

    ' - '
    ' - '

    Only view, edit you can here: {fullpath}/haproxy-webintarface.config

    ' - '
    '.format(fullpath=fullpath))
    -
    +print('Content-type: text/html\n')
    +funct.check_login()
    +funct.page_for_admin()
    +try:
    +	cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
    +	user_id = cookie.get('uuid')
    +	user = sql.get_user_name_by_uuid(user_id.value)
    +	servers = sql.get_dick_permit()
    +except:
    +	pass
    +	
    +config_items_section_name = {}
     for section_name in config.sections():
    -    print('Section:', section_name)
    -    for name, value in config.items(section_name):
    -        print('  {} = {}'.format(name, value))
    -    print()
    +	config_items_section_name[section_name] = {}
    +	for name, value in config.items(section_name):
    +		config_items_section_name[section_name][name] =  value
     
    -print('
    ') -funct.footer() \ No newline at end of file + +output_from_parsed_template = template.render(h2 = 1, title = "Admin area: View settings", + role = sql.get_user_role_by_uuid(user_id.value), + user = user, + fullpath = fullpath, + config_items_section_name = config_items_section_name) +print(output_from_parsed_template) diff --git a/app/templates/add.html b/app/templates/add.html new file mode 100644 index 00000000..6bab9726 --- /dev/null +++ b/app/templates/add.html @@ -0,0 +1,359 @@ +{% extends "base.html" %} +{% block content %} +
    + +
    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

    Add listen

    Select server: + +
    Note: If you reconfigure Master server, Slave will reconfigured automatically
    +
    Name: + +
    IP and Port: + : + +
    IP for bind listner, if empty will be assignet on all IPs. Start typing ip, or press down.
    If you use VRRP keep in blank. If you assign an IP, the slave will not start
    +
    Mode: + + + + + + +
    Balance: + +
    Optinons: + + +
    Servers: + +
    + + + Default params: inter 2000 rise 2 fall 5 +
    + +
    + +
    + + + +
    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

    Add frontend

    Select server: + +
    Note: If you reconfigure Master server, Slave will reconfigured automatically
    +
    Name: + +
    IP and Port: + : + +
    IP for bind listner, if empty will be assignet on all IPs. Start typing ip, or press down.
    If you use VRRP keep in blank. If you assign an IP, the slave will not start
    +
    Mode: + + + + + + +
    Optinons: + + +
    Default backend +
    Start typing backend, or press down
    + + . +

    Note: If backend don\t exist, you must create backend first.

    +
    + +
    +
    + + + +
    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

    Add backend

    Select server: + +
    Note: If you reconfigure Master server, Slave will reconfigured automatically
    +
    Name: + +
    Mode: + + + + + + +
    Balance: + +
    Optinons: + + +
    Servers: + +
    + + + Default params: inter 2000 rise 2 fall 5 +
    + +
    + +
    +
    + +
    + + + + + + + + + + + +
    Upload SSL certificates + Certificate name + + Paste certificate content here(?) +
    + + + + +

    + Upload +
    +
    +
    + {% if add %} +
    +

    {{ add }} was success added

    + {{ conf_add }} +
    + {% endif %} +{% endblock %} \ No newline at end of file diff --git a/app/templates/admin.html b/app/templates/admin.html new file mode 100644 index 00000000..446ebaed --- /dev/null +++ b/app/templates/admin.html @@ -0,0 +1,303 @@ +{% extends "base.html" %} +{% block content %} + +
    + +
    + + + + + + + + + + + + {% for user in users %} + + + + + + + + + {% endfor %} +
    Login namePasswordEmailRoleGroup
    + + + +
    +
    + Add +

    + + + + + + + + + + + + + + + + + + + +
    +
    + + + + + + + + {% for group in groups %} + + {% if group.1 == All %} + + + + {% else %} + + + + {% endif %} + + {% endfor %} +
    NameDesciption
    {{ group.1 }}{{ group.2 }} + + + + + +
    +
    + Add +

    + + + + + + + + + + + + +
    +
    + + + + + + + + + + + + + {% for server in servers %} + + + + + + + + + + {% endfor %} +
    HostnameIPGroupEnableVirt(?)Slave for (?)
    + + + + + + + {% if server.5 == 1 %} + + {% else %} + + {% endif %} + + {% if server.4 == 1 %} + + {% else %} + + {% endif %} + + + + +
    +
    + Add +

    + + + + + + + + + + + + + + + + + + + + + +
    +
    + + + + + + + + + {% for role in roles %} + + + + + {% endfor %} +
    NameDesciption
    {{role.1}}{{role.2}}
    +
    +
    + + + + + + + + + + + +
    Upload SSH Key + Key(?) +
    + Note: Paste pem file content here + +

    + Upload +
    +
    +
    +
    +{% endblock %} \ No newline at end of file diff --git a/app/templates/base.html b/app/templates/base.html new file mode 100644 index 00000000..53af6b40 --- /dev/null +++ b/app/templates/base.html @@ -0,0 +1,188 @@ + + + {{title}} - HAproxy-WI + + + + + + + + + + + + + + + + + + + + + +
    +
    + HAproxy-WI + + + + + +
    + +
    +
    + {% if h2 %} +

    + {{title}} + {% if autorefresh %} + + + + restart Auto-refresh + + + {% endif %} +

    + {% endif %} + {% if autorefresh %} +
    +
    + Refresh Interval +
    +
    +
    + +
    +
    + +
    +
    + +
    +
    + +
    +
    +
    + {% endif %} + {% block content %}{% endblock %} +
    +
    +
    +
    +

    + UP +


    +
    + + \ No newline at end of file diff --git a/app/templates/config.html b/app/templates/config.html new file mode 100644 index 00000000..f0ed74c3 --- /dev/null +++ b/app/templates/config.html @@ -0,0 +1,67 @@ +{% extends "base.html" %} +{% block content %} +
    +

    Choose server

    +

    +

    + + {% if onclick %} + Show + {% else %} + + {% endif %} +
    +

    + {% if note %} +
    Note: If you reconfigure Master server, Slave will reconfigured automatically
    + {% endif %} + {% if config %} +

    Config from {{ serv }}

    +
    + + + +

    + + +

    +
    + {% endif %} + {% if aftersave %} +
    New config was saved as: {{ cfg }}
    +
    Go to view stats + {% if stderr %} +
    + Some errors: +
    +
    + {{stderr}} +
    + {% else %} +
    Config ok
    + {% endif %} + {% endif %} +
    +{% endblock %} \ No newline at end of file diff --git a/app/templates/configver.html b/app/templates/configver.html new file mode 100644 index 00000000..2bb4d2cf --- /dev/null +++ b/app/templates/configver.html @@ -0,0 +1,56 @@ +{% extends "base.html" %} +{% block content %} +
    +

    Choose server

    +

    +

    + + +
    +

    + {% if note %} +
    Note: If you reconfigure Master server, Slave will reconfigured automatically
    + {% endif %} + {% if open %} +
    +

    Choose old version

    +
    +

    + + + + Select +

    +
    +
    + {% endif %} + {% if aftersave %} +
    Uploaded old config ver: {{ configver }}
    +
    Go to view stats + {% if stderr %} +
    + Some errors: +
    +
    + {{stderr}} +
    + {% else %} +
    Config ok
    + {% endif %} + {% endif %} +
    +{% endblock %} \ No newline at end of file diff --git a/app/templates/delver.html b/app/templates/delver.html new file mode 100644 index 00000000..6f4aad7a --- /dev/null +++ b/app/templates/delver.html @@ -0,0 +1,59 @@ +{% extends "base.html" %} +{% block content %} +
    +

    Choose server

    +

    +

    + + +
    +

    + {% if note %} +
    Note: If you reconfigure Master server, Slave will reconfigured automatically
    + {% endif %} + {% if open %} +
    +

    Choose old version

    +
    + +
    + {% for file in return_files %} +
    + {% endfor %} + + + +

    + +

    +
    +
    + {% endif %} + {% if aftersave %} +
    The following files were deleted:
    + {% if stderr %} +
    + Some errors: +
    +
    + {{stderr}} +
    + {% else %} +
    + {% for f in file %} + {{f}} + {% endfor %} +
    + {% endif %} + {% endif %} +
    +{% endblock %} \ No newline at end of file diff --git a/app/templates/ha.html b/app/templates/ha.html new file mode 100644 index 00000000..d3c7f237 --- /dev/null +++ b/app/templates/ha.html @@ -0,0 +1,88 @@ +{% extends "base.html" %} +{% block content %} + + + + + + + + + + + + + + + + + + + +

    Create new HA cluster

    MasterSlaveVRRP interfaceVRRP IPInstall HAProxy(?)
    + + + + + + + + + + + Create +
    + + + + + + + + + + + + + + + + + + + +

    Or add VRRP to exist

    MasterSlaveVRRP interfaceVRRP IPRestart Keepalived(?)
    + + + + + + + + + + + Add +
    +
    +{% endblock %} \ No newline at end of file diff --git a/app/templates/ihap.html b/app/templates/ihap.html new file mode 100644 index 00000000..4b7639c5 --- /dev/null +++ b/app/templates/ihap.html @@ -0,0 +1,27 @@ +{% extends "base.html" %} +{% block content %} + + + + + + + + + + + +
    NoteServer
    + Haproxy-WI will try install haproxy-1.18.5, if it does not work then haproxy-1.15 + + + + Install +
    +
    +{% endblock %} \ No newline at end of file diff --git a/app/templates/login.html b/app/templates/login.html new file mode 100644 index 00000000..c1e30f3b --- /dev/null +++ b/app/templates/login.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} +{% block content %} +
    +
    + {{error_log}} + {{error}} + {{db_create}} +
    +

    +

    + + +
    +
    +{% endblock %} \ No newline at end of file diff --git a/app/templates/logs.html b/app/templates/logs.html new file mode 100644 index 00000000..0a0bf9f6 --- /dev/null +++ b/app/templates/logs.html @@ -0,0 +1,43 @@ +{% extends "base.html" %} +{% block content %} + + + + + + + + + + + + + + +
    ServerNumber rowsEx for grep
    +
    + +
    + + + + + Show + +
    +
    +
    + + +{% endblock %} \ No newline at end of file diff --git a/app/templates/overview.html b/app/templates/overview.html new file mode 100644 index 00000000..5c0f967b --- /dev/null +++ b/app/templates/overview.html @@ -0,0 +1,53 @@ +{% extends "base.html" %} +{% block content %} + + + + + + + + + {% for user in users %} + + + + + + + + {% endfor %} +
    LoginEmailGroupRole + + + Show all + + +
    {{ user.1 }} {{ user.2 }}{{ user.5 }}{{ user.4 }}
    + + + + + + + + {% for server in servers %} + + + + + + {% endfor %} +
    Server + HAproxy status + + Action + + Last edit +
    {{ server.1 }}{{ server.7 }} + show + edit + compare + map +
    +{% endblock %} diff --git a/app/templates/ovw.html b/app/templates/ovw.html new file mode 100644 index 00000000..22cbf46d --- /dev/null +++ b/app/templates/ovw.html @@ -0,0 +1,10 @@ +{% extends "base.html" %} +{% block content %} + +
    +{% endblock %} \ No newline at end of file diff --git a/app/templates/runtimeapi.html b/app/templates/runtimeapi.html new file mode 100644 index 00000000..1ebbc3cd --- /dev/null +++ b/app/templates/runtimeapi.html @@ -0,0 +1,46 @@ +{% extends "base.html" %} +{% block content %} + + + + + + + + + + + + + + + + +
    ServerDisable/Enable server or output any informationCommandSave change
    +
    + +
    + + + + + {% if role <= 2 %} + + {% endif %} + + Enter +
    +{% endblock %} \ No newline at end of file diff --git a/app/templates/viewlogs.html b/app/templates/viewlogs.html new file mode 100644 index 00000000..42e94d83 --- /dev/null +++ b/app/templates/viewlogs.html @@ -0,0 +1,30 @@ +{% extends "base.html" %} +{% block content %} +
    +

    Choose server

    +

    +

    + + {% if onclick %} + Show + {% else %} + + {% endif %} +
    +

    +
    +
    +
    + +{% endblock %} \ No newline at end of file diff --git a/app/templates/viewsettings.html b/app/templates/viewsettings.html new file mode 100644 index 00000000..1a7af7c6 --- /dev/null +++ b/app/templates/viewsettings.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} +{% block content %} +

    Only view, edit you can here: {{ fullpath }}/haproxy-webintarface.config

    +
    + {% for name, value in config_items_section_name|dictsort(false) %} +
    Section: {{name}}
    +
    + {% for param, value2 in value|dictsort(false) %} +
    {{param}} = {{value2}} + {% endfor %} +
    + {% endfor %} +
    + +{% endblock %} \ No newline at end of file diff --git a/app/templates/viewstats.html b/app/templates/viewstats.html new file mode 100644 index 00000000..b6054403 --- /dev/null +++ b/app/templates/viewstats.html @@ -0,0 +1,20 @@ +{% extends "base.html" %} +{% block content %} +
    +
    + + Show +
    +
    + + +{% endblock %} \ No newline at end of file diff --git a/app/users.py b/app/users.py index 51480075..0891d984 100644 --- a/app/users.py +++ b/app/users.py @@ -1,220 +1,32 @@ #!/usr/bin/env python3 -import html +import html, http import cgi import sys import os import funct import sql - -funct.head("Admin area: users manage") -funct.check_config() -funct.check_login() -funct.page_for_admin() - +from jinja2 import Environment, FileSystemLoader +env = Environment(loader=FileSystemLoader('templates/')) +template = env.get_template('admin.html') form = cgi.FieldStorage() -USERS = sql.select_users() -GROUPS = sql.select_groups() -SERVERS = sql.select_servers(full=1) -ROLES = sql.select_roles() +print('Content-type: text/html\n') +funct.check_login() +funct.page_for_admin() +try: + cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) + user_id = cookie.get('uuid') + user = sql.get_user_name_by_uuid(user_id.value) + servers = sql.get_dick_permit() +except: + pass -print('' - '
    ' - '' - '
    ' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '') - -for users in USERS: - print('' % users[0]) - print('' % (users[0], users[1])) - print('' % (users[0], users[3])) - print('' % (users[0], users[2])) - print('') - print('') - print('' % users[0]) - print('') -print('
    Login namePasswordEmailRoleGroup
    ') - need_id_role = "role-%s" % users[0] - sql.get_roles_select(need_id_role, selected=users[4]) - print('') - need_id_group = "usergroup-%s" % users[0] - sql.get_groups_select(need_id_group, selected=users[5]) - print('
    ' - '
    + Add') - -print('

    ' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '') -print('') - -print('
    ' - '' - '' - '' - '' - '' - '' - '') -i = 0 -for group in GROUPS: - i = i + 1 - print('' % group[0]) - if i == 1: - print('' % (group[1])) - print('' % (group[2])) - print('') - else: - print('' % (group[0], group[1])) - print('' % (group[0], group[2])) - print('' % group[0]) - print('') -print('
    NameDesciption
    %s%s
    ' - '
    + Add') - -print('

    ' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' -'' -'
    ' -'
    ' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '') - -for server in SERVERS: - print('' % server[0]) - print('' % (server[0], server[1])) - print('' % (server[0], server[2])) - print('') - print('') - print('') - print('') - print('' % server[0]) - print('') -print('
    HostnameIPGroupEnableVirt(?)Slave for (?)
    ') - need_id_group = "servergroup-%s" % server[0] - sql.get_groups_select(need_id_group, selected=server[3]) - print('') - sql.get_enable_checkbox(server[0]) - print('') - sql.get_type_ip_checkbox(server[0]) - print('
    ' - '
    + Add' - '

    ' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '') -print('') - -print('
    ' - '' - '' - '' - '' - '' - '' - '') -for role in ROLES: - print('' % role[0]) - print('' % ( role[1])) - print('' % (role[2])) - print('') - -print('
    NameDesciption
    %s%s
    ' - '
    ' - '
    ' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '' - '
    Upload SSH Key' - 'Key(?)' - '
    ' - 'Note: Paste pem file content here' - '' - '

    ' - 'Upload' - '
    ' - '
    ' - '
    ') \ No newline at end of file +output_from_parsed_template = template.render(title = "Admin area: users manage", + role = sql.get_user_role_by_uuid(user_id.value), + user = user, + users = sql.select_users(), + groups = sql.select_groups(), + servers = sql.select_servers(full=1), + roles = sql.select_roles(), + masters = sql.select_servers(get_master_servers=1)) +print(output_from_parsed_template) diff --git a/app/viewlogs.py b/app/viewlogs.py index 2d608ee5..2ae414df 100644 --- a/app/viewlogs.py +++ b/app/viewlogs.py @@ -1,39 +1,32 @@ #!/usr/bin/env python3 -import html -import cgi -import os, sys +import os +import sql +import http, cgi import funct -from configparser import ConfigParser, ExtendedInterpolation +import sql import glob import datetime +from configparser import ConfigParser, ExtendedInterpolation +from jinja2 import Environment, FileSystemLoader +env = Environment(loader=FileSystemLoader('templates/')) +template = env.get_template('viewlogs.html') form = cgi.FieldStorage() -viewlog = form.getvalue('viewlogs') - -funct.head("View logs") -funct.check_login() -funct.page_for_admin() -funct.get_auto_refresh("View logs") - path_config = "haproxy-webintarface.config" config = ConfigParser(interpolation=ExtendedInterpolation()) config.read(path_config) +print('Content-type: text/html\n') try: if config.get('main', 'log_path'): log_path = config.get('main', 'log_path') time_storage = config.getint('logs', 'log_time_storage') except: print('
    Can not find "log_path" and "log_time_storage" parametrs. Check into config
    ') -try: - os.chdir(log_path) -except IOError: - print('
    No such file or directory: "%s". Please check "log_path" in config and exist directory
    ' % log_path) - sys.exit() - -print('' - '' - '

    Choose log file


    ') + + +funct.check_login() +funct.page_for_admin() try: time_storage_hours = time_storage * 24 for dirpath, dirnames, filenames in os.walk(log_path): @@ -46,22 +39,27 @@ except: print('
    Can\'t delete old logs file.
    Please check "log_time_storage" in config and
    exist directory
    ') pass -print('' - 'Show' - '

    ' - '
    ' - '') - -funct.footer() \ No newline at end of file +def get_files(): + file = set() + for files in glob.glob(os.path.join(log_path,'*.log')): + file.add(files.split('/')[5]) + return sorted(file, reverse=True) + +output_from_parsed_template = template.render(h2 = 1, + autorefresh = 1, + title = "View logs", + role = sql.get_user_role_by_uuid(user_id.value), + user = user, + onclick = "viewLogs()", + serv = form.getvalue('viewlogs'), + select_id = "viewlogs", + selects = get_files()) +print(output_from_parsed_template) diff --git a/app/viewsttats.py b/app/viewsttats.py index 81b88842..1b2f6670 100644 --- a/app/viewsttats.py +++ b/app/viewsttats.py @@ -1,39 +1,41 @@ #!/usr/bin/env python3 -import html +import html, http, os import cgi import requests import funct import sql -from configparser import ConfigParser, ExtendedInterpolation from requests_toolbelt.utils import dump - -print("Content-type: text/html\n") - +from jinja2 import Environment, FileSystemLoader +env = Environment(loader=FileSystemLoader('templates/')) +template = env.get_template('viewstats.html') form = cgi.FieldStorage() -serv = form.getvalue('serv') +serv = form.getvalue('serv') if serv is None: first_serv = sql.get_dick_permit() for i in first_serv: serv = i[2] break -print('
    ') +print('Content-type: text/html\n') +funct.check_login() -funct.get_auto_refresh("HAproxy statistics") +try: + cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) + user_id = cookie.get('uuid') + user = sql.get_user_name_by_uuid(user_id.value) + servers = sql.get_dick_permit(virt=1) +except: + pass -print('
    ' - '
    ' - '' - 'Show' - '
    ' - '
    ') - -funct.head("Stats HAproxy configs") -print('
    ') -funct.footer() diff --git a/requirements.txt b/requirements.txt index 5e842c0f..274bfc71 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,4 +10,5 @@ numpy==1.14.0 matplotlib==2.1.2 urllib3==1.22 future==0.13.1 -mysql-connector-python==8.0.11 \ No newline at end of file +mysql-connector-python==8.0.11 +Jinja2==2.10 \ No newline at end of file diff --git a/update.sh b/update.sh index 8d197830..977354a3 100644 --- a/update.sh +++ b/update.sh @@ -10,6 +10,8 @@ chown -R apache:apache * cd app/ ./update_db.py +pip install -r requirements.txt + echo "" echo "#################" echo "Change in config:"