diff --git a/app/create_db.py b/app/create_db.py index 8489e037..c727881c 100644 --- a/app/create_db.py +++ b/app/create_db.py @@ -670,7 +670,7 @@ def update_db_v_5_2_0(**kwargs): def update_db_v_5_2_4(**kwargs): cursor = conn.cursor() - sql = """ALTER TABLE `user` ADD COLUMN user_services varchar(20) DEFAULT '1 2 3';""" + sql = """ALTER TABLE `user` ADD COLUMN user_services varchar(20) DEFAULT '1 2 3 4';""" try: cursor.execute(sql) except Exception as e: @@ -892,7 +892,7 @@ def update_db_v_5_4_02(**kwargs): def update_ver(): - query = Version.update(version='5.4.0.0') + query = Version.update(version='5.4.1.0') try: query.execute() except: diff --git a/app/funct.py b/app/funct.py index 86b46f88..8f9c4f0b 100644 --- a/app/funct.py +++ b/app/funct.py @@ -157,10 +157,7 @@ def logging(server_ip, action, **kwargs): except Exception: pass - if kwargs.get('alerting') == 1: - mess = get_data('date_in_log') + action + "\n" - log = open(log_path + "/checker-"+get_data('logs')+".log", "a") - elif kwargs.get('metrics') == 1: + if kwargs.get('metrics') == 1: mess = get_data('date_in_log') + action + "\n" log = open(log_path + "/metrics-"+get_data('logs')+".log", "a") elif kwargs.get('keep_alive') == 1: @@ -908,10 +905,32 @@ def upload_and_restart(server_ip, cfg, **kwargs): if kwargs.get("nginx"): service = 'nginx' + # 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 kwargs.get("keepalived"): service = 'keepalived' + config_path = "/etc/keepalived/keepalived.conf" + tmp_file = sql.get_setting('tmp_config_path') + "/" + get_data('config') + ".cfg" else: service = 'haproxy' + config_path = sql.get_setting('haproxy_config_path') + tmp_file = sql.get_setting('tmp_config_path') + "/" + get_data('config') + ".cfg" + + is_docker = sql.select_service_setting(server_id, service, 'dockerized') + if is_docker == '1': + service_cont_name = service + '_container_name' + container_name = sql.get_setting(service_cont_name) + reload_command = " && sudo docker kill -s HUP " + container_name + restart_command = " && sudo docker restart " + container_name + else: + service_name = service + if service == 'haproxy': + haproxy_enterprise = sql.select_service_setting(server_id, 'haproxy', 'haproxy_enterprise') + if haproxy_enterprise == '1': + service_name = "hapee-2.0-lb" + reload_command = " && sudo systemctl reload " + service_name + restart_command = " && sudo systemctl restart " + service_name if kwargs.get("just_save") == 'save': action = 'save' @@ -927,17 +946,6 @@ def upload_and_restart(server_ip, cfg, **kwargs): else: login = 1 - if service == "nginx": - # 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" - try: os.system("dos2unix "+cfg) except OSError: @@ -948,48 +956,30 @@ def upload_and_restart(server_ip, cfg, **kwargs): if action == "save": commands = [move_config] elif action == "reload": - commands = [ - move_config + " && sudo systemctl reload keepalived"] + commands = [move_config + reload_command] else: - commands = [move_config + " && sudo systemctl restart keepalived"] + commands = [move_config + restart_command] elif service == "nginx": - is_docker = sql.select_service_setting(server_id, 'nginx', 'dockerized') if is_docker == '1': - container_name = sql.get_setting('nginx_container_name') check_config = "sudo docker exec -it exec " + container_name + " nginx -t -q " else: check_config = "sudo nginx -t -q -p " + tmp_file - check_and_move = "sudo mv -f " + tmp_file + " " + config_path + " && sudo nginx -t -q" + check_and_move = "sudo mv -f " + tmp_file + " " + config_path + " && " + check_config if action == "test": commands = [check_config + " && sudo rm -f " + tmp_file] elif action == "save": commands = [check_and_move] elif action == "reload": - if is_docker == '1': - commands = [ check_and_move + " && sudo docker kill -s HUP "+container_name ] - else: - commands = [ check_and_move + " && sudo systemctl reload nginx" ] + commands = [ check_and_move + reload_command ] else: - if is_docker == '1': - commands = [check_and_move + " && sudo docker restart " + container_name] - else: - commands = [check_and_move + " && sudo systemctl restart nginx"] + commands = [check_and_move + restart_command] if sql.return_firewall(server_ip): commands[0] += open_port_firewalld(cfg, server_ip=server_ip, service='nginx') else: - is_docker = sql.select_service_setting(server_id, 'haproxy', 'dockerized') - haproxy_service_name = "haproxy" - if is_docker == '1': - container_name = sql.get_setting('haproxy_container_name') check_config = "sudo docker exec -it " + container_name + " haproxy -q -c -f " + tmp_file else: - haproxy_enterprise = sql.select_service_setting(server_id, 'haproxy', 'haproxy_enterprise') - - if haproxy_enterprise == '1': - haproxy_service_name = "hapee-2.0-lb" - - check_config = "sudo " + haproxy_service_name + " -q -c -f " + tmp_file + check_config = "sudo " + service_name + " -q -c -f " + tmp_file move_config = " && sudo mv -f " + tmp_file + " " + config_path if action == "test": @@ -997,15 +987,9 @@ def upload_and_restart(server_ip, cfg, **kwargs): elif action == "save": commands = [check_config + move_config] elif action == "reload": - if is_docker == '1': - commands = [check_config + move_config + " && sudo docker kill -s HUP "+container_name ] - else: - commands = [check_config + move_config + " && sudo systemctl reload "+haproxy_service_name ] + commands = [check_config + move_config + reload_command ] else: - if is_docker == '1': - commands = [check_config + move_config + " && sudo docker restart "+container_name ] - else: - commands = [check_config + move_config + " && sudo systemctl restart "+haproxy_service_name ] + commands = [check_config + move_config + restart_command ] if sql.return_firewall(server_ip): commands[0] += open_port_firewalld(cfg, server_ip=server_ip) @@ -1533,21 +1517,25 @@ def check_user_group(**kwargs): if kwargs.get('token') is not None: return True - import http.cookies - import os import sql - cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) - user_uuid = cookie.get('uuid') - group = cookie.get('group') - group_id = group.value - user_id = sql.get_user_id_by_uuid(user_uuid.value) + if kwargs.get('user_uuid'): + group_id = kwargs.get('user_group_id') + user_uuid = kwargs.get('user_uuid') + user_id = sql.get_user_id_by_uuid(user_uuid) + else: + import http.cookies + import os + cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) + user_uuid = cookie.get('uuid') + group = cookie.get('group') + group_id = group.value + user_id = sql.get_user_id_by_uuid(user_uuid.value) if sql.check_user_group(user_id, group_id): return True else: logging('localhost', ' has tried to actions in not his group ', haproxywi=1, login=1) print('Atata!') - sys.exit() def check_is_server_in_group(server_ip): diff --git a/app/options.py b/app/options.py index ca9b1d9d..df10494a 100644 --- a/app/options.py +++ b/app/options.py @@ -1207,6 +1207,7 @@ if form.getvalue('master'): IP = form.getvalue('vrrpip') syn_flood = form.getvalue('syn_flood') virt_server = form.getvalue('virt_server') + return_to_master = form.getvalue('return_to_master') haproxy = form.getvalue('hap') nginx = form.getvalue('nginx') script = "install_keepalived.sh" @@ -1229,7 +1230,8 @@ if form.getvalue('master'): os.system("cp scripts/%s ." % script) commands = ["chmod +x " + script + " && ./" + script + " PROXY=" + proxy_serv + " SSH_PORT=" + ssh_port + - " ETH=" + ETH + " IP=" + str(IP) + " MASTER=MASTER" + " SYN_FLOOD=" + syn_flood + " HOST=" + str(master) + + " ETH=" + ETH + " IP=" + str(IP) + " MASTER=MASTER" + " RETURN_TO_MASTER=" + return_to_master + + " SYN_FLOOD=" + syn_flood + " HOST=" + str(master) + " USER=" + str(ssh_user_name) + " PASS='" + str(ssh_user_password) + "' KEY=" + str(ssh_key_name)] output, error = funct.subprocess_execute(commands[0]) @@ -1286,6 +1288,7 @@ if form.getvalue('masteradd'): ETH = form.getvalue('interfaceadd') IP = form.getvalue('vrrpipadd') kp = form.getvalue('kp') + return_to_master = form.getvalue('return_to_master') script = "install_keepalived.sh" proxy = sql.get_setting('proxy') ssh_port = 22 @@ -1306,7 +1309,7 @@ if form.getvalue('masteradd'): ssh_port = str(server[10]) commands = ["chmod +x " + script + " && ./" + script + " PROXY=" + proxy_serv + - " SSH_PORT=" + ssh_port + " ETH=" + ETH + + " SSH_PORT=" + ssh_port + " ETH=" + ETH + " RETURN_TO_MASTER=" + return_to_master + " IP=" + str(IP) + " MASTER=MASTER" + " RESTART=" + kp + " ADD_VRRP=1 HOST=" + str(master) + " USER=" + str(ssh_user_name) + " PASS='" + str(ssh_user_password) + "' KEY=" + str(ssh_key_name)] diff --git a/app/templates/ajax/config_show.html b/app/templates/ajax/config_show.html index 419ee591..3ba1f9d1 100644 --- a/app/templates/ajax/config_show.html +++ b/app/templates/ajax/config_show.html @@ -4,6 +4,11 @@ {% if role <= 3 %} {% if not is_serv_protected or role <= 2 %} Edit + {% if service == 'haproxy' %} + Add + {% elif service == 'keepalived' %} + Add + {% endif %} {% endif %} {% endif %} Expand all @@ -111,6 +116,14 @@
{% continue %} {% endif %} + {%- if "virtual_server " in line -%} + {% if i > 1 %} +
+ {% endif %} + {{ line }} +
+ {% continue %} + {% endif %} {%- if "vrrp_script " in line -%} {% if i > 1 %}
diff --git a/app/templates/ha.html b/app/templates/ha.html index a7107a66..8d93326b 100644 --- a/app/templates/ha.html +++ b/app/templates/ha.html @@ -7,22 +7,20 @@ - +
+ + - - - - - + + - + + + + + + + + + + + + + + + + + + + + - - +

Create a new HA cluster

Current installation MasterCurrent installation Slave Interface VRRP IPHAProxyNginx Add VIRTSYN-flood protection
{{ input('interface', size='7', title='Interface for VRRP address') }} {{ input('vrrp-ip', size='14') }}{{ checkbox('hap', title='Roxy-WI will try to install HAProxy') }}{{ checkbox('virt_server', title='Roxy-WI will add VRRP address as a separated server', checked='checked') }}
Return to Master stateSYN-flood protectionHAProxyNginx
{{ checkbox('return_to_master', checked='checked') }}{{ checkbox('syn_flood') }}{{ checkbox('hap', title='Roxy-WI will try to install HAProxy') }} {{ checkbox('nginx', title='Roxy-WI will try to install Nginx') }} {{ checkbox('virt_server', title='Roxy-WI will add VRRP address as a separated server', checked='checked') }}{{ checkbox('syn_flood') }} - +
+ + - - - - + + + - - - - - + +

Add VRRP to an existing cluster

Current installation MasterCurrent installation Slave Interface VRRP IP RestartReturn to Master state
{{ input('interface-add', size='7', title='Interface for VRRP address') }} {{ input('vrrp-ip-add', size='14') }}{{ checkbox('kp', title='If checked Roxy-WI will restart Keepalived') }}{{ checkbox('kp', title='If checked Roxy-WI will restart Keepalived') }}{{ checkbox('add_return_to_master', checked='checked') }} - +
diff --git a/inc/ha.js b/inc/ha.js index c42a9fa9..a97c90b8 100644 --- a/inc/ha.js +++ b/inc/ha.js @@ -104,8 +104,108 @@ $( function() { $('#nginx_docker_td_header').hide(); } }); + $( "#master" ).on('selectmenuchange',function() { + $.ajax( { + url: "options.py", + data: { + get_keepalived_v: 1, + serv: $('#master option:selected').val(), + token: $('#token').val() + }, + type: "POST", + success: function( data ) { + data = data.replace(/^\s+|\s+$/g,''); + if(data.indexOf('keepalived:') != '-1') { + $('#cur_master_ver').text('Keepalived has not installed'); + $('#create').prop('disabled', false); + $('#create').attr('title', 'Create HA cluster'); + } else { + $('#cur_master_ver').text(data); + $('#cur_master_ver').css('font-weight', 'bold'); + $('#create').prop('disabled', true); + $('#create').attr('title', 'Keepalived has been alredy installed. You cannot create a new HA Cluster'); + } + } + } ); + }); + $( "#slave" ).on('selectmenuchange',function() { + $.ajax( { + url: "options.py", + data: { + get_keepalived_v: 1, + serv: $('#slave option:selected').val(), + token: $('#token').val() + }, + type: "POST", + success: function( data ) { + data = data.replace(/^\s+|\s+$/g,''); + if(data.indexOf('keepalived:') != '-1') { + $('#cur_slave_ver').text('Keepalived has not installed'); + $('#create').prop('disabled', false); + $('#create').attr('title', 'Create HA cluster'); + } else { + $('#cur_slave_ver').text(data); + $('#cur_slave_ver').css('font-weight', 'bold'); + $('#create').prop('disabled', true); + $('#create').attr('title', 'Keepalived has been alredy installed. You cannot create a new HA Cluster'); + } + } + } ); + }); + $( "#master-add" ).on('selectmenuchange',function() { + $.ajax( { + url: "options.py", + data: { + get_keepalived_v: 1, + serv: $('#master-add option:selected').val(), + token: $('#token').val() + }, + type: "POST", + success: function( data ) { + data = data.replace(/^\s+|\s+$/g,''); + if(data.indexOf('keepalived:') != '-1') { + $('#cur_master_ver-add').text('Keepalived has not installed'); + $('#add-vrrp').prop('disabled', true); + $('#add-vrrp').attr('title', 'Add a HA configuration'); + } else { + $('#cur_master_ver-add').text(data); + $('#cur_master_ver-add').css('font-weight', 'bold'); + $('#add-vrrp').prop('disabled', false); + $('#add-vrrp').attr('title', 'Keepalived has been alredy installed. You cannot add a new HA configuration'); + } + } + } ); + }); + $( "#slave-add" ).on('selectmenuchange',function() { + $.ajax( { + url: "options.py", + data: { + get_keepalived_v: 1, + serv: $('#slave-add option:selected').val(), + token: $('#token').val() + }, + type: "POST", + success: function( data ) { + data = data.replace(/^\s+|\s+$/g,''); + if(data.indexOf('keepalived:') != '-1') { + $('#cur_slave_ver-add').text('Keepalived has not installed'); + $('#add-vrrp').prop('disabled', true); + $('#add-vrrp').attr('title', 'Add a HA configuration'); + } else { + $('#cur_slave_ver-add').text(data); + $('#cur_slave_ver-add').css('font-weight', 'bold'); + $('#add-vrrp').prop('disabled', false); + $('#add-vrrp').attr('title', 'Keepalived has been alredy installed. You cannot add a new HA configuration'); + } + } + } ); + }); }); function add_master_addr(kp) { + return_to_master = 0 + if ($('#add_return_to_master').is(':checked')) { + return_to_master = '1'; + } $.ajax( { url: "options.py", data: { @@ -113,6 +213,7 @@ function add_master_addr(kp) { slaveadd: $('#slave-add').val(), interfaceadd: $("#interface-add").val(), vrrpipadd: $('#vrrp-ip-add').val(), + return_to_master: return_to_master, kp: kp, token: $('#token').val() }, @@ -164,6 +265,7 @@ function create_master_keepalived(hap, nginx, syn_flood) { var virt_server = 0; var haproxy_docker = 0; var nginx_docker = 0; + var return_to_master = 0; if ($('#virt_server').is(':checked')) { virt_server = '1'; } @@ -173,6 +275,9 @@ function create_master_keepalived(hap, nginx, syn_flood) { if ($('#nginx_docker').is(':checked')) { nginx_docker = '1'; } + if ($('#return_to_master').is(':checked')) { + return_to_master = '1'; + } $.ajax( { url: "options.py", data: { @@ -180,6 +285,7 @@ function create_master_keepalived(hap, nginx, syn_flood) { slave: $('#slave').val(), interface: $("#interface").val(), vrrpip: $('#vrrp-ip').val(), + return_to_master: return_to_master, hap: hap, nginx: nginx, syn_flood: syn_flood, @@ -195,6 +301,7 @@ function create_master_keepalived(hap, nginx, syn_flood) { showProvisioningWarning(step_id, 'master Keepalived', '#creating-warning', '#wait_mess'); } else if (data.indexOf('success') != '-1' ){ showProvisioningProccess('

'+data+'

', '#creating-master', progress_value, '#creating-progress', '#created-mess', '#wait-mess'); + $( "#master" ).trigger( "selectmenuchange" ); if (hap === '1') { create_keep_alived_hap(nginx, 'master', haproxy_docker); } @@ -237,6 +344,7 @@ function create_slave_keepalived(hap, nginx, syn_flood) { showProvisioningWarning(step_id, 'slave Keepalived', '#creating-warning', '#wait_mess'); } else if (data.indexOf('success') != '-1' ){ showProvisioningProccess('

'+data+'

', '#creating-slave', progress_value, '#creating-progress', '#created-mess', '#wait-mess'); + $( "#slave" ).trigger( "selectmenuchange" ); } else { toastr.clear(); toastr.info(data);