From f6f344b1e92391e535512e7ec69ea8c9aa6308ab Mon Sep 17 00:00:00 2001 From: Pavel Loginov Date: Sun, 16 Aug 2020 09:22:09 +0200 Subject: [PATCH] v4.4.2.0 Changelog: https://haproxy-wi.org/changelog.py#4_4_2 --- api/api.py | 181 ----------------- api/api_funct.py | 302 ---------------------------- api/app.wsgi | 9 - app/scripts/ansible/roles/nginx.yml | 6 +- 4 files changed, 3 insertions(+), 495 deletions(-) delete mode 100644 api/api.py delete mode 100644 api/api_funct.py delete mode 100644 api/app.wsgi diff --git a/api/api.py b/api/api.py deleted file mode 100644 index aadff4e5..00000000 --- a/api/api.py +++ /dev/null @@ -1,181 +0,0 @@ -import os -import sys -os.chdir(os.path.dirname(__file__)) -sys.path.append(os.path.dirname(__file__)) -sys.path.append(os.path.join(sys.path[0], '/var/www/haproxy-wi/app/')) -os.chdir(os.path.dirname(__file__)) - -from bottle import route, run, template, hook, response, request, error -import sql -import funct -import api_funct -import json - - -_error_auth = '403 Auth before' -_allow_origin = '*' -_allow_methods = 'PUT, GET, POST, DELETE, OPTIONS' -_allow_headers = 'Authorization, Origin, Accept, Content-Type, X-Requested-With' - - -@hook('before_request') -def check_login(): - try: - login = request.headers.get('login') - password_from_user = request.headers.get('password') - USERS = sql.select_users(user=login) - password = funct.get_hash(password_from_user) - except: - return False - - for users in USERS: - if users[7] == 0: - return False - if login in users[1] and password == users[3]: - return True - else: - return False - - -@hook('after_request') -def enable_cors(): - '''Add headers to enable CORS''' - - response.headers['Access-Control-Allow-Origin'] = _allow_origin - response.headers['Access-Control-Allow-Methods'] = _allow_methods - response.headers['Access-Control-Allow-Headers'] = _allow_headers - - -@error(500) -def error_handler_500(error): - return json.dumps({"status": "error", "message": str(error.exception)}) - - -@route('/', method=['GET', 'POST']) -@route('/help', method=['GET', 'POST']) -def index(): - if not check_login(): - return dict(error=_error_auth) - - data = { - 'help': 'show all available endpoints', - 'servers':'show info about all servers', - 'servers/status':'show status all servers', - 'server/':'show info about the server by id or hostname or ip', - 'server//status':'show HAProxy status by id or hostname or ip', - 'server//runtime':'exec HAProxy runtime commands by id or hostname or ip', - 'server//backends':'show backends by id or hostname or ip', - 'server//action/start':'start HAProxy service by id or hostname or ip', - 'server//action/stop':'stop HAProxy service by id or hostname or ip', - 'server//action/restart':'restart HAProxy service by id or hostname or ip', - 'server//config/get':'get HAProxy config from the server by id or hostname or ip', - 'server//config/send':'send HAProxy config to the server by id or hostname or ip. Has to have config header with config and action header for action after upload. Action header accepts next value: save, test, reload and restart. May be empty for just save', - 'server//config/add':'add section to the HAProxy config by id or hostname or ip. Has to have config header with section and action header for action after upload. Action header accepts next value: save, test, reload and restart. May be empty for just save', - 'server//log':'show HAProxy log by id or hostname or ip. May to have config next headers: rows(format INT) default: 10 grep, waf(if needs WAF log) deault: 0, start_hour(format: 24) default: 00, start_minut, end_hour(format: 24) default: 24, end_minut' - } - return dict(help=data) - - -@route('/servers', method=['GET', 'POST']) -def get_servers(): - if not check_login(): - return dict(error=_error_auth) - try: - login = request.headers.get('login') - servers = sql.get_dick_permit(username=login) - data = {} - for s in servers: - data[s[0]] = { - 'id':s[0], - 'hostname':s[1], - 'ip':s[2], - 'group':s[3], - 'virt':s[4], - 'enable':s[5], - 'is_master':s[6], - 'creds':s[7], - 'alert':s[8], - 'metrics':s[9] - } - except: - pass - return dict(servers=data) - - -@route('/servers/status', method=['GET', 'POST']) -def callback(): - if not check_login(): - return dict(error=_error_auth) - return api_funct.get_all_statuses() - -@route('/server/', method=['GET', 'POST']) -@route('/server/', method=['GET', 'POST']) -def callback(id): - if not check_login(): - return dict(error=_error_auth) - return api_funct.get_server(id) - - -@route('/server//status', method=['GET', 'POST']) -@route('/server//status', method=['GET', 'POST']) -def callback(id): - if not check_login(): - return dict(error=_error_auth) - return api_funct.get_status(id) - - -@route('/server//action/', method=['GET', 'POST']) -@route('/server//action/', method=['GET', 'POST']) -def callback(id, action): - if not check_login(): - return dict(error=_error_auth) - return api_funct.actions(id, action) - - -@route('/server//runtime', method=['GET', 'POST']) -@route('/server//runtime', method=['GET', 'POST']) -def callback(id): - if not check_login(): - return dict(error=_error_auth) - return api_funct.runtime(id) - - -@route('/server//backends', method=['GET', 'POST']) -@route('/server//backends', method=['GET', 'POST']) -def callback(id): - if not check_login(): - return dict(error=_error_auth) - return api_funct.show_backends(id) - - -@route('/server//config/get', method=['GET', 'POST']) -@route('/server//config/get', method=['GET', 'POST']) -def callback(id): - if not check_login(): - return dict(error=_error_auth) - return api_funct.get_config(id) - - -@route('/server//config/send', method=['GET', 'POST']) -@route('/server//config/send', method=['GET', 'POST']) -def callback(id): - if not check_login(): - return dict(error=_error_auth) - return api_funct.upload_config(id) - - -@route('/server//config/add', method=['GET', 'POST']) -@route('/server//config/add', method=['GET', 'POST']) -def callback(id): - if not check_login(): - return dict(error=_error_auth) - return api_funct.add_to_config(id) - - -@route('/server//log', method=['GET', 'POST']) -@route('/server//log', method=['GET', 'POST']) -def callback(id): - if not check_login(): - return dict(error=_error_auth) - return api_funct.show_log(id) - \ No newline at end of file diff --git a/api/api_funct.py b/api/api_funct.py deleted file mode 100644 index 7dc21792..00000000 --- a/api/api_funct.py +++ /dev/null @@ -1,302 +0,0 @@ -import os -import sys -os.chdir(os.path.dirname(__file__)) -sys.path.append(os.path.dirname(__file__)) -sys.path.append(os.path.join(sys.path[0], '/var/www/haproxy-wi/app/')) - -from bottle import route, run, template, hook, response, request, post -import sql -import funct - - -def return_dict_from_out(id, out): - data = {} - data[id] = {} - for k in out: - if "Ncat:" not in k: - k = k.split(':') - data[id][k[0]] = k[1].strip() - else: - data[id] = {"error":"Can\'t connect to HAproxy"} - - return data - - -def check_permit_to_server(id): - servers = sql.select_servers(id_hostname=id) - login = request.headers.get('login') - - for s in servers: - servers = sql.get_dick_permit(username=login, ip=s[2]) - - return servers - - -def get_server(id): - data = {} - try: - servers = check_permit_to_server(id) - - for s in servers: - data = { - 'id':s[0], - 'hostname':s[1], - 'ip':s[2], - 'group':s[3], - 'virt':s[4], - 'enable':s[5], - 'master':s[6], - 'creds':s[7] - } - except: - server = data - return dict(server=data) - - -def get_status(id): - try: - servers = check_permit_to_server(id) - - for s in servers: - cmd = 'echo "show info" |nc %s %s -w 1|grep -e "Ver\|CurrConns\|Maxco\|MB\|Uptime:"' % (s[2], sql.get_setting('haproxy_sock_port')) - - out = funct.subprocess_execute(cmd) - data = return_dict_from_out(id, out[0]) - - except: - data = {} - data[id] = {"error":"Cannot find the server"} - return dict(error=data) - - return dict(status=data) - - -def get_all_statuses(): - data = {} - try: - servers = sql.select_servers() - login = request.headers.get('login') - sock_port = sql.get_setting('haproxy_sock_port') - - for s in servers: - servers = sql.get_dick_permit(username=login) - - for s in servers: - cmd = 'echo "show info" |nc %s %s -w 1|grep -e "Ver\|CurrConns\|Maxco\|MB\|Uptime:"' % (s[2], sock_port) - data[s[2]] = {} - out = funct.subprocess_execute(cmd) - data[s[2]] = return_dict_from_out(s[1], out[0]) - except: - data = {"error":"Cannot find the server"} - return dict(error=data) - - return dict(status=data) - - -def actions(id, action): - if action == 'start' or action == 'stop' or action == 'restart': - try: - servers = check_permit_to_server(id) - - for s in servers: - cmd = [ "sudo systemctl %s haproxy" % action ] - error = funct.ssh_command(s[2], cmd) - done = error if error else 'done' - - data = {'id':s[0],'ip':s[2],'action':action,'hostname':s[1],'status':done} - - return dict(status=data) - except: - return dict(status='error') - else: - return dict(status='wrong action') - - - -def runtime(id): - data = {} - try: - action = request.headers.get('action') - haproxy_sock = sql.get_setting('haproxy_sock') - servers = check_permit_to_server(id) - cmd = [ 'echo "%s" |sudo socat stdio %s' % (action, haproxy_sock) ] - - for s in servers: - out = funct.ssh_command(s[2], cmd) - - data = {} - data[id] = {} - sep_data = out.split('\r\n') - data[id] = {'ouput':sep_data} - - return dict(status=data) - except: - return dict(status='error') - - -def show_backends(id): - data = {} - try: - servers = check_permit_to_server(id) - - for s in servers: - out = funct.show_backends(s[2], ret=1) - - data = {id: out} - - except: - data = {} - data[id] = {"error":"Cannot find the server"} - return dict(error=data) - - return dict(backends=data) - - -def get_config(id): - data = {} - try: - servers = check_permit_to_server(id) - - for s in servers: - cfg = '/tmp/'+s[2]+'.cfg' - out = funct.get_config(s[2], cfg) - os.system("sed -i 's/\\n/\n/g' "+cfg) - try: - conf = open(cfg, "r") - config_read = conf.read() - conf.close - - except IOError: - conf = '
Can\'t read import config file' - - data = {id: config_read} - - except: - data = {} - data[id] = {"error":"Cannot find the server"} - return dict(error=data) - - return dict(config=data) - - -def upload_config(id): - data = {} - body = request.body.getvalue().decode('utf-8') - save = request.headers.get('action') - login = request.headers.get('login') - - if save == '': - save = 'save' - elif save == 'restart': - save = '' - - try: - servers = check_permit_to_server(id) - - for s in servers: - ip = s[2] - cfg = '/tmp/'+ip+'.cfg' - cfg_for_save = hap_configs_dir + ip + "-" + funct.get_data('config') + ".cfg" - - try: - with open(cfg, "w") as conf: - conf.write(body) - return_mess = 'config was uploaded' - os.system("/bin/cp %s %s" % (cfg, cfg_for_save)) - out = funct.upload_and_restart(ip, cfg, just_save=save) - funct.logging('localhost', " config was uploaded via REST API", login=login) - - if out: - return_mess == out - except IOError: - return_mess = "cannot upload config" - - data = {id: return_mess} - except: - data = {} - data[id] = {"error":"Cannot find the server"} - return dict(error=data) - - return dict(config=data) - - -def add_to_config(id): - data = {} - body = request.body.getvalue().decode('utf-8') - save = request.headers.get('action') - hap_configs_dir = funct.get_config_var('configs', 'haproxy_save_configs_dir') - login = request.headers.get('login') - - if save == '': - save = 'save' - elif save == 'restart': - save = '' - - try: - servers = check_permit_to_server(id) - - for s in servers: - ip = s[2] - cfg = '/tmp/'+ip+'.cfg' - cfg_for_save = hap_configs_dir + ip + "-" + funct.get_data('config') + ".cfg" - out = funct.get_config(ip, cfg) - try: - with open(cfg, "a") as conf: - conf.write('\n'+body+'\n') - return_mess = 'section was added to the config' - os.system("/bin/cp %s %s" % (cfg, cfg_for_save)) - funct.logging('localhost', " section was added via REST API", login=login) - out = funct.upload_and_restart(ip, cfg, just_save=save) - - if out: - return_mess = out - except IOError: - return_mess = "cannot upload config" - - data = {id: return_mess} - except: - data[id] = {"error":"Cannot find the server"} - return dict(error=data) - - return dict(config=data) - - -def show_log(id): - data = {} - rows = request.headers.get('rows') - waf = request.headers.get('waf') - grep = request.headers.get('grep') - hour = request.headers.get('starthour') - minut = request.headers.get('startminut') - hour1 = request.headers.get('endhour') - minut1 = request.headers.get('endminut') - - if rows is None: - rows = '10' - if waf is None: - waf = '0' - if hour is None: - hour = '00' - if minut is None: - minut = '00' - if hour1 is None: - hour1 = '24' - if minut1 is None: - minut1 = '00' - - try: - servers = check_permit_to_server(id) - - for s in servers: - ip = s[2] - except: - - data[id] = {"error":"Cannot find the server"} - return dict(error=data) - - out = funct.show_haproxy_log(ip, rows=rows, waf=str(waf), grep=grep, hour=str(hour), minut=str(minut), hour1=str(hour1), minut1=str(minut1), html=0) - data = {id: out} - - return dict(log=data) - - \ No newline at end of file diff --git a/api/app.wsgi b/api/app.wsgi deleted file mode 100644 index 38360063..00000000 --- a/api/app.wsgi +++ /dev/null @@ -1,9 +0,0 @@ -import os, sys -sys.path.append(os.path.join(sys.path[0], '/var/www/haproxy-wi/app/')) -sys.path.append(os.path.dirname(__file__)) -import api -import bottle -bottle.debug(True) - -application = bottle.default_app() -application.catchall = False \ No newline at end of file diff --git a/app/scripts/ansible/roles/nginx.yml b/app/scripts/ansible/roles/nginx.yml index 3149ac33..ccbc3b24 100644 --- a/app/scripts/ansible/roles/nginx.yml +++ b/app/scripts/ansible/roles/nginx.yml @@ -33,14 +33,14 @@ - name: Set passlib version set_fact: passlib_ver: "python3-passlib" - when: ansible_facts['distribution_major_version'] == 8 and (ansible_facts['os_family'] == "RedHat" or ansible_facts['os_family'] == 'CentOS') + when: ansible_facts['distribution_major_version'] == '8' and (ansible_facts['os_family'] == "RedHat" or ansible_facts['os_family'] == 'CentOS') ignore_errors: True - name: Set passlib version set_fact: - passlib_ver: "python3-passlib" - when: ansible_facts['distribution_major_version'] == 7 or (ansible_facts['os_family'] == 'Debian' or ansible_facts['os_family'] == 'Ubuntu') + passlib_ver: "python-passlib" + when: ansible_facts['distribution_major_version'] == '7' or (ansible_facts['os_family'] == 'Debian' or ansible_facts['os_family'] == 'Ubuntu') ignore_errors: True