From e602e1c2341488c1cdcd461f0c5efdf9ed0ebc9f Mon Sep 17 00:00:00 2001 From: Pavel Loginov Date: Sun, 16 Jan 2022 16:50:53 +0300 Subject: [PATCH] v5.4.0.0 Changelog: https://haproxy-wi.org/changelog.py#5_4_0 --- app/config.py | 13 ++- app/create_db.py | 10 +- app/funct.py | 52 ++++++--- app/options.py | 24 +++- .../ansible/roles/haproxy/tasks/logs.yml | 11 +- .../haproxy/templates/haproxy_rsyslog.conf.j2 | 17 ++- app/sql.py | 22 +++- app/templates/ajax/config_show.html | 28 ++++- app/templates/ajax/show_configs_files.html | 24 ++++ app/templates/ajax/show_list_version.html | 2 - app/templates/config.html | 30 ++--- app/templates/delver.html | 2 +- app/templates/hapservers.html | 4 + inc/script.js | 104 ++++++++++++------ 14 files changed, 247 insertions(+), 96 deletions(-) create mode 100644 app/templates/ajax/show_configs_files.html diff --git a/app/config.py b/app/config.py index f504ba82..3fd0c1f1 100644 --- a/app/config.py +++ b/app/config.py @@ -12,6 +12,10 @@ funct.check_login() form = funct.form serv = form.getvalue('serv') service = form.getvalue('service') +try: + config_file_name = form.getvalue('config_file_name').replace('92', '/') +except: + config_file_name = '' config_read = "" cfg = "" stderr = "" @@ -57,7 +61,7 @@ if serv is not None and form.getvalue('open') is not None: except Exception: pass elif service == 'nginx': - error = funct.get_config(serv, cfg, nginx=1) + error = funct.get_config(serv, cfg, nginx=1, config_file_name=config_file_name) try: funct.logging(serv, " Nginx config has been opened ") except Exception: @@ -74,7 +78,7 @@ if serv is not None and form.getvalue('open') is not None: config_read = conf.read() conf.close() except IOError: - error += '
Cannot read import config file' + error += '
Cannot read imported config file' os.system("/bin/mv %s %s.old" % (cfg, cfg)) @@ -90,12 +94,12 @@ if serv is not None and form.getvalue('config') is not None: with open(cfg, "a") as conf: conf.write(config) except IOError: - print("error: Cannot read import config file") + print("error: Cannot read imported config file") if service == 'keepalived': stderr = funct.upload_and_restart(serv, cfg, just_save=save, keepalived=1, oldcfg=oldcfg) elif service == 'nginx': - stderr = funct.master_slave_upload_and_restart(serv, cfg, just_save=save, nginx=1, oldcfg=oldcfg) + stderr = funct.master_slave_upload_and_restart(serv, cfg, just_save=save, nginx=1, oldcfg=oldcfg, config_file_name=config_file_name) else: stderr = funct.master_slave_upload_and_restart(serv, cfg, just_save=save, oldcfg=oldcfg) @@ -126,5 +130,6 @@ template = template.render(h2=1, title=title, error=error, service=service, user_services=user_services, + config_file_name=config_file_name, token=token) print(template) diff --git a/app/create_db.py b/app/create_db.py index f0180032..811ef9de 100644 --- a/app/create_db.py +++ b/app/create_db.py @@ -29,7 +29,7 @@ def default_values(): {'param': 'lists_path', 'value': 'lists', 'section': 'main', 'desc': 'Path to the black and the wild list. The value of this paramer should be specified as a relative path beginning with $HOME_ROXY-WI', 'group': '1'}, - {'param': 'local_path_logs', 'value': '/var/log/haproxy.log', 'section': 'logs', + {'param': 'haproxy_path_logs', 'value': '/var/log/haproxy/access.log', 'section': 'haproxy', 'desc': 'The default local path for saving logs', 'group': '1'}, {'param': 'syslog_server_enable', 'value': '0', 'section': 'logs', 'desc': 'Enable getting logs from a syslog server; (0 - no, 1 - yes)', 'group': '1'}, @@ -67,10 +67,10 @@ def default_values(): 'group': '1'}, {'param': 'nginx_stats_page', 'value': 'stats', 'section': 'nginx', 'desc': 'URI Stats for web page Nginx', 'group': '1'}, - {'param': 'nginx_dir', 'value': '/etc/nginx/conf.d/', 'section': 'nginx', 'desc': 'Path to the Nginx directory', - 'group': '1'}, - {'param': 'nginx_config_path', 'value': '/etc/nginx/conf.d/default.conf', 'section': 'nginx', - 'desc': 'Path to the Nginx configuration file', 'group': '1'}, + {'param': 'nginx_dir', 'value': '/etc/nginx/conf.d/', 'section': 'nginx', + 'desc': 'Path to the Nginx directory with config files', 'group': '1'}, + {'param': 'nginx_config_path', 'value': '/etc/nginx/nginx.conf', 'section': 'nginx', + 'desc': 'Path to the main Nginx configuration file', 'group': '1'}, {'param': 'ldap_enable', 'value': '0', 'section': 'ldap', 'desc': 'Enable LDAP (1 - yes, 0 - no)', 'group': '1'}, {'param': 'ldap_server', 'value': '', 'section': 'ldap', 'desc': 'IP address of the LDAP server', 'group': '1'}, diff --git a/app/funct.py b/app/funct.py index 1b4a954f..ddeb6223 100644 --- a/app/funct.py +++ b/app/funct.py @@ -429,11 +429,11 @@ def ssh_connect(server_ip): def get_config(server_ip, cfg, **kwargs): import sql - if kwargs.get("keepalived"): + if kwargs.get("keepalived") or kwargs.get("service") == 'keepalived': config_path = "/etc/keepalived/keepalived.conf" - elif kwargs.get("nginx"): - config_path = sql.get_setting('nginx_config_path') - elif kwargs.get("waf"): + elif kwargs.get("nginx") or kwargs.get("service") == 'nginx': + config_path = kwargs.get('config_file_name') + elif kwargs.get("waf") or kwargs.get("service") == 'waf': config_path = sql.get_setting('haproxy_dir') + '/waf/rules/' + kwargs.get("waf_rule_file") else: config_path = sql.get_setting('haproxy_config_path') @@ -928,8 +928,12 @@ def upload_and_restart(server_ip, cfg, **kwargs): login = 1 if service == "nginx": - config_path = sql.get_setting('nginx_config_path') + # config_path = sql.get_setting('nginx_config_path') + config_path = kwargs.get('config_file_name') tmp_file = sql.get_setting('tmp_config_path') + "/" + get_data('config') + ".conf" + elif service == "keepalived": + config_path = "/etc/keepalived/keepalived.conf" + tmp_file = sql.get_setting('tmp_config_path') + "/" + get_data('config') + ".cfg" else: config_path = sql.get_setting('haproxy_config_path') tmp_file = sql.get_setting('tmp_config_path') + "/" + get_data('config') + ".cfg" @@ -940,13 +944,14 @@ def upload_and_restart(server_ip, cfg, **kwargs): return 'Please install dos2unix' if service == "keepalived": + move_config = "sudo mv -f " + tmp_file + " " + config_path if action == "save": - commands = ["sudo mv -f " + tmp_file + " /etc/keepalived/keepalived.conf"] + commands = [move_config] elif action == "reload": commands = [ - "sudo mv -f " + tmp_file + " /etc/keepalived/keepalived.conf && sudo systemctl reload keepalived"] + move_config + " && sudo systemctl reload keepalived"] else: - commands = ["sudo mv -f " + tmp_file + " /etc/keepalived/keepalived.conf && sudo systemctl restart keepalived"] + commands = [move_config + " && sudo systemctl restart keepalived"] elif service == "nginx": is_docker = sql.select_service_setting(server_id, 'nginx', 'dockerized') if is_docker == '1': @@ -1015,10 +1020,11 @@ def upload_and_restart(server_ip, cfg, **kwargs): if not kwargs.get('slave'): diff = '' old_cfg = kwargs.get('oldcfg') - if kwargs.get('oldcfg') is None: + if not old_cfg: old_cfg = tmp_file + '.old' + print(old_cfg) try: - get_config(server_ip, old_cfg) + get_config(server_ip, old_cfg, service=service, config_file_name=config_path) except Exception: logging('localhost', ' Cannot download config', haproxywi=1) try: @@ -1055,14 +1061,24 @@ def master_slave_upload_and_restart(server_ip, cfg, just_save, **kwargs): masters = sql.is_master(server_ip) for master in masters: if master[0] is not None: - error = upload_and_restart(master[0], cfg, just_save=just_save, nginx=kwargs.get('nginx'), slave=1) + error = upload_and_restart(master[0], + cfg, + just_save=just_save, + nginx=kwargs.get('nginx'), + config_file_name=kwargs.get('config_file_name'), + slave=1) if kwargs.get('login'): login = kwargs.get('login') else: login = '' - error = upload_and_restart(server_ip, cfg, just_save=just_save, - nginx=kwargs.get('nginx'), oldcfg=kwargs.get('oldcfg'), login=login) + error = upload_and_restart(server_ip, + cfg, + just_save=just_save, + nginx=kwargs.get('nginx'), + config_file_name=kwargs.get('config_file_name'), + oldcfg=kwargs.get('oldcfg'), + login=login) return error @@ -1195,7 +1211,7 @@ def show_haproxy_log(serv, rows=10, waf='0', grep=None, hour='00', minut='00', h local_path_logs = sql.get_setting('nginx_path_error_logs') commands = ["sudo cat %s| awk '$2>\"%s:00\" && $2<\"%s:00\"' |tail -%s %s %s" % (local_path_logs, date, date1, rows, grep_act, exgrep_act)] else: - local_path_logs = sql.get_setting('local_path_logs') + local_path_logs = sql.get_setting('haproxy_path_logs') commands = ["sudo cat %s| awk '$3>\"%s:00\" && $3<\"%s:00\"' |tail -%s %s %s" % (local_path_logs, date, date1, rows, grep_act, exgrep_act)] syslog_server = serv else: @@ -1401,6 +1417,14 @@ def get_files(dir=get_config_var('configs', 'haproxy_save_configs_dir'), format= return file +def get_remote_files(server_ip: str, config_dir: str, file_format: str): + if config_dir[-1] != '/': + config_dir += '/' + commands = ['ls ' + config_dir + '*.' + file_format] + config_files = ssh_command(server_ip, commands) + + return config_files + def get_key(item): return item[0] diff --git a/app/options.py b/app/options.py index 3f60400f..a88a4036 100644 --- a/app/options.py +++ b/app/options.py @@ -688,7 +688,6 @@ if act == "overviewwaf": template = template.render(service_status=servers_sorted, role=sql.get_user_role_by_uuid(user_id.value)) print(template) - if act == "overviewServers": import asyncio @@ -1135,6 +1134,10 @@ if serv is not None and act == "configShow": user_uuid = cookie.get('uuid') role_id = sql.get_user_role_by_uuid(user_uuid.value) service = form.getvalue('service') + try: + config_file_name = form.getvalue('config_file_name').replace('/', '92') + except: + config_file_name = '' if service == 'keepalived': configs_dir = funct.get_config_var('configs', 'kp_save_configs_dir') @@ -1149,7 +1152,7 @@ if serv is not None and act == "configShow": if form.getvalue('configver') is None: cfg = configs_dir + serv + "-" + funct.get_data('config') + cfg if service == 'nginx': - funct.get_config(serv, cfg, nginx=1) + funct.get_config(serv, cfg, nginx=1, config_file_name=form.getvalue('config_file_name')) elif service == 'keepalived': funct.get_config(serv, cfg, keepalived=1) else: @@ -1174,12 +1177,29 @@ if serv is not None and act == "configShow": configver=form.getvalue('configver'), role=role_id, service=service, + config_file_name=config_file_name, is_serv_protected=is_serv_protected) print(template) if form.getvalue('configver') is None: os.system("/bin/rm -f " + cfg) +if act == 'configShowFiles': + config_dir = funct.get_config_var('configs', 'nginx_save_configs_dir') + nginx_config_dir = sql.get_setting('nginx_dir') + try: + config_file_name = form.getvalue('config_file_name').replace('92', '/') + except: + config_file_name = '' + return_files = funct.get_remote_files(serv, nginx_config_dir, 'conf') + return_files += ' ' + sql.get_setting('nginx_config_path') + from jinja2 import Environment, FileSystemLoader + + env = Environment(loader=FileSystemLoader('templates/'), autoescape=True) + template = env.get_template('ajax/show_configs_files.html') + template = template.render(serv=serv, return_files=return_files, config_file_name=config_file_name) + print(template) + if form.getvalue('master'): master = form.getvalue('master') slave = form.getvalue('slave') diff --git a/app/scripts/ansible/roles/haproxy/tasks/logs.yml b/app/scripts/ansible/roles/haproxy/tasks/logs.yml index 7fda5a05..e247dab8 100644 --- a/app/scripts/ansible/roles/haproxy/tasks/logs.yml +++ b/app/scripts/ansible/roles/haproxy/tasks/logs.yml @@ -1,20 +1,11 @@ - name: Installing HAProxy conf for rsyslog template: src: /var/www/haproxy-wi/app/scripts/ansible/roles/haproxy/templates/haproxy_rsyslog.conf.j2 - dest: /etc/rsyslog.d/haproxy.conf - force: no - ignore_errors: yes - - -- name: Installing rsyslog config - template: - src: /var/www/haproxy-wi/app/scripts/ansible/roles/haproxy/templates/rsyslog.conf - dest: /etc/rsyslog.conf + dest: /etc/rsyslog.d/49-haproxy.conf force: no ignore_errors: yes notify: restart rsyslog - - name: Installing HAProxy conf for logrotate template: src: /var/www/haproxy-wi/app/scripts/ansible/roles/haproxy/templates/logrotate.conf.j2 diff --git a/app/scripts/ansible/roles/haproxy/templates/haproxy_rsyslog.conf.j2 b/app/scripts/ansible/roles/haproxy/templates/haproxy_rsyslog.conf.j2 index c245d0ab..420d98ce 100644 --- a/app/scripts/ansible/roles/haproxy/templates/haproxy_rsyslog.conf.j2 +++ b/app/scripts/ansible/roles/haproxy/templates/haproxy_rsyslog.conf.j2 @@ -1,2 +1,15 @@ -if $programname startswith 'haproxy' then /var/log/haproxy.log -& stop \ No newline at end of file +$ModLoad imudp +$UDPServerAddress 127.0.0.1 +$UDPServerRun 514 + +if $programname startswith 'haproxy' then { + if $syslogseverity == 6 then + action(type="omfile" file="/var/log/haproxy/access.log") + stop + if $syslogseverity <= 3 then + action(type="omfile" file="/var/log/haproxy/error.log") + stop + if $syslogseverity <= 5 then + action(type="omfile" file="/var/log/haproxy/status.log") + stop +} \ No newline at end of file diff --git a/app/sql.py b/app/sql.py index eb50dcc2..86b2b8a6 100644 --- a/app/sql.py +++ b/app/sql.py @@ -135,7 +135,7 @@ def add_setting_for_new_group(group_id): {'param': 'cert_path', 'value': '/etc/ssl/certs/', 'section': 'main', 'desc': 'Path to SSL dir. Folder owner must be a user which set in the SSH settings. The path must be valid', 'group': group_id}, - {'param': 'local_path_logs', 'value': '/var/log/haproxy.log', 'section': 'logs', + {'param': 'haproxy_path_logs', 'value': '/var/log/haproxy/access.log', 'section': 'haproxy', 'desc': 'The default local path for saving logs', 'group': group_id}, {'param': 'syslog_server_enable', 'value': '0', 'section': 'logs', 'desc': 'Enable getting logs from a syslog server; (0 - no, 1 - yes)', 'group': group_id}, @@ -169,10 +169,10 @@ def add_setting_for_new_group(group_id): 'group': group_id}, {'param': 'nginx_stats_page', 'value': 'stats', 'section': 'nginx', 'desc': 'URI Stats for web page Nginx', 'group': group_id}, - {'param': 'nginx_dir', 'value': '/etc/nginx/conf.d/', 'section': 'nginx', 'desc': 'Path to the Nginx directory', - 'group': group_id}, - {'param': 'nginx_config_path', 'value': '/etc/nginx/conf.d/default.conf', 'section': 'nginx', - 'desc': 'Path to the Nginx configuration file', 'group': group_id}, + {'param': 'nginx_dir', 'value': '/etc/nginx/conf.d/', 'section': 'nginx', + 'desc': 'Path to the Nginx directory with config files', 'group': group_id}, + {'param': 'nginx_config_path', 'value': '/etc/nginx/nginx.conf', 'section': 'nginx', + 'desc': 'Path to the main Nginx configuration file', 'group': group_id}, {'param': 'ldap_enable', 'value': '0', 'section': 'ldap', 'desc': 'Enable LDAP (1 - yes, 0 - no)', 'group': group_id}, {'param': 'ldap_server', 'value': '', 'section': 'ldap', 'desc': 'IP address of the LDAP server', 'group': group_id}, @@ -2948,6 +2948,18 @@ def delete_config_version(service: str, local_path: str): return True +def select_remote_path_from_version(server_ip: str, service: str, local_path: str): + server_id = select_server_id_by_ip(server_ip) + try: + query_res = ConfigVersion.get((ConfigVersion.server_id == server_id) & + (ConfigVersion.service == service) & + (ConfigVersion.local_path == local_path)).remote_path + except Exception as e: + out_error(e) + else: + return query_res + + def insert_system_info(server_id: int, os_info: str, sys_info: str, cpu: str, ram: str, network: str, disks: str) -> bool: try: SystemInfo.insert(server_id=server_id, os_info=os_info, sys_info=sys_info, cpu=cpu, ram=ram, diff --git a/app/templates/ajax/config_show.html b/app/templates/ajax/config_show.html index 0b201c73..419ee591 100644 --- a/app/templates/ajax/config_show.html +++ b/app/templates/ajax/config_show.html @@ -1,9 +1,9 @@
-

Config from {{serv}}

+

Config {% if config_file_name != 'undefined' %}{{config_file_name.replace('92', '/')}}{%endif%} from {{serv}}

{% if role <= 3 %} {% if not is_serv_protected or role <= 2 %} - Edit + Edit {% endif %} {% endif %} Expand all @@ -28,6 +28,15 @@ {% endif %} {% endif %} {% if service == 'nginx' %} + {% if i == 0 and "server {" not in line %} + +

+ + {{ i }} +   {{ line }} +
+ {% continue %} + {% endif %} {%- if "server {" in line -%} {% if i > 1 %}
@@ -44,6 +53,21 @@
{% continue %} {% endif %} + {%- if "http {" in line -%} + {% if i > 1 %} +
+ {% endif %} + {{ line }} +
+ {% continue %} + {% endif %} + {%- if "events {" in line -%} +
+ {{ line }} +
+ {% continue %} + {% endif %} + {% if "listen " in line or "location" in line or "server_name" in line or "}" in line %} {% if "#" not in line %} diff --git a/app/templates/ajax/show_configs_files.html b/app/templates/ajax/show_configs_files.html new file mode 100644 index 00000000..2b31123e --- /dev/null +++ b/app/templates/ajax/show_configs_files.html @@ -0,0 +1,24 @@ +{% from 'include/input_macros.html' import input %} +
+
+ {% if config_file_name == '' %} +

Config files from {{serv}}

+ {% endif %} +

+ + + {{ input('serv', type='hidden', value=serv) }} + {{ input('open', type='hidden', value='open') }} + Open +

+
+
\ No newline at end of file diff --git a/app/templates/ajax/show_list_version.html b/app/templates/ajax/show_list_version.html index 04878c7b..9a528357 100644 --- a/app/templates/ajax/show_list_version.html +++ b/app/templates/ajax/show_list_version.html @@ -108,7 +108,6 @@ -

@@ -176,7 +175,6 @@ - Select