def get_section(server_id): section_name = request.headers.get('section-name') servers = check_permit_to_server(server_id) for s in servers: cfg = '/tmp/' + s[2] + '.cfg' out = funct.get_config(s[2], cfg) start_line, end_line, config_read = funct.get_section_from_config(cfg, section_name) data = {server_id: {section_name:{'start_line':start_line, 'end_line':end_line, 'config_read':config_read}}} return dict(section=data)
def edit_section(server_id): body = request.body.getvalue().decode('utf-8') section_name = request.headers.get('section-name') save = request.headers.get('action') token = request.headers.get('token') servers = check_permit_to_server(server_id) hap_configs_dir = funct.get_config_var('configs', 'haproxy_save_configs_dir') login, group_id = sql.get_username_groupid_from_api_token(token) if save == '': save = 'save' elif save == 'restart': save = '' elif save == 'reload': save = 'reload' for s in servers: ip = s[2] cfg = '/tmp/' + ip + '.cfg' out = funct.get_config(ip, cfg) start_line, end_line, config_read = funct.get_section_from_config(cfg, section_name) returned_config = funct.rewrite_section(start_line, end_line, cfg, body) try: cfg_for_save = hap_configs_dir + ip + "-" + funct.get_data('config') + ".cfg" try: with open(cfg, "w") as conf: conf.write(returned_config) return_mess = 'section has been updated' os.system("/bin/cp %s %s" % (cfg, cfg_for_save)) out = funct.master_slave_upload_and_restart(ip, cfg, save, login=login) funct.logging('localhost', " section " + section_name + " has been edited via API", login=login) funct.logging(ip, 'Section ' + section_name + ' has been edited via API', haproxywi=1, login=login, keep_history=1, service='haproxy') if out: return_mess = out except IOError: return_mess = "cannot upload config" data = {server_id: return_mess} except Exception as e: data = {server_id: {"error": str(e)}} return dict(error=data) return dict(config=data)
def del_acl(server_id): body = request.body.getvalue().decode('utf-8') json_loads = json.loads(body) save = json_loads['action'] section_name = json_loads['section-name'] acl = generate_acl() servers = check_permit_to_server(server_id) status = '' for s in servers: cfg = '/tmp/' + s[2] + '.cfg' server_ip = s[2] try: out = funct.get_config(server_ip, cfg) start_line, end_line, config_read = funct.get_section_from_config(cfg, section_name) except Exception as e: status = str(e) try: config_new_read = '' for line in config_read.split('\n'): if not line.startswith(acl): if line != '': config_new_read += line + '\n' except Exception as e: status = 'Cannot delete ACL: ' + str(e) try: config = funct.rewrite_section(start_line, end_line, cfg, config_new_read) try: with open(cfg, "w") as conf: conf.write(config) except IOError as e: status = "Cannot read import config file: " + str(e) except Exception as e: status = 'Cannot delete ACL: ' + str(e) try: out = funct.master_slave_upload_and_restart(server_ip, cfg, just_save=save) if out != '': status = out else: status = 'ACL has been deleted' except Exception as e: status = str(e) return dict(acl=status)
def add_acl(server_id): body = request.body.getvalue().decode('utf-8') json_loads = json.loads(body) save = json_loads['action'] section_name = json_loads['section-name'] acl = generate_acl(with_newline=1) servers = check_permit_to_server(server_id) status = '' for s in servers: cfg = '/tmp/' + s[2] + '.cfg' server_ip = s[2] try: out = funct.get_config(server_ip, cfg) start_line, end_line, config_read = funct.get_section_from_config(cfg, section_name) except Exception as e: status = "Cannot read section: " + str(e) try: config_read += acl config = funct.rewrite_section(start_line, end_line, cfg, config_read) try: with open(cfg, "w") as conf: conf.write(config) except IOError as e: status = "Cannot read import config file: " + str(e) except Exception as e: status = str(e) try: out = funct.master_slave_upload_and_restart(server_ip, cfg, just_save=save) if out != '': status = out else: status = 'ACL has been added' except Exception as e: status = str(e) data = {'acl':status} return dict(data)
hap_configs_dir = funct.get_config_var('configs', 'haproxy_save_configs_dir') if serv is not None and open is not None: cfg = hap_configs_dir + serv + "-" + funct.get_data('config') + ".cfg" error = funct.get_config(serv, cfg) sections = funct.get_sections(cfg) if serv is not None and section is not None: try: funct.logging(serv, "sections.py open config") except Exception: pass start_line, end_line, config_read = funct.get_section_from_config(cfg, section) os.system("/bin/mv %s %s.old" % (cfg, cfg)) if serv is not None and form.getvalue('config') is not None: try: funct.logging(serv, "config.py edited config") except Exception: pass config = form.getvalue('config') oldcfg = form.getvalue('oldconfig') save = form.getvalue('save') start_line = form.getvalue('start_line') end_line = form.getvalue('end_line') aftersave = 1