2019-09-20 06:42:19 +00:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
import os
|
2022-09-27 19:24:37 +00:00
|
|
|
|
import sys
|
2022-05-24 19:29:29 +00:00
|
|
|
|
|
2022-09-26 09:18:18 +00:00
|
|
|
|
from jinja2 import Environment, FileSystemLoader
|
|
|
|
|
|
2022-11-17 07:34:58 +00:00
|
|
|
|
import modules.db.sql as sql
|
|
|
|
|
import modules.common.common as common
|
|
|
|
|
import modules.roxywi.auth as roxywi_auth
|
|
|
|
|
import modules.roxywi.common as roxywi_common
|
|
|
|
|
import modules.config.section as section_mod
|
|
|
|
|
import modules.config.config as config_mod
|
2022-09-26 09:18:18 +00:00
|
|
|
|
import modules.roxy_wi_tools as roxy_wi_tools
|
|
|
|
|
|
2022-11-17 07:34:58 +00:00
|
|
|
|
time_zone = sql.get_setting('time_zone')
|
|
|
|
|
get_date = roxy_wi_tools.GetDate(time_zone)
|
2022-09-26 09:18:18 +00:00
|
|
|
|
get_config_var = roxy_wi_tools.GetConfigVar()
|
2020-11-06 04:39:44 +00:00
|
|
|
|
env = Environment(loader=FileSystemLoader('templates/'), autoescape=True, extensions=['jinja2.ext.loopcontrols'])
|
2019-09-20 06:42:19 +00:00
|
|
|
|
template = env.get_template('sections.html')
|
|
|
|
|
|
|
|
|
|
print('Content-type: text/html\n')
|
2022-09-27 19:06:34 +00:00
|
|
|
|
|
2022-11-17 07:34:58 +00:00
|
|
|
|
user_params = roxywi_common.get_users_params()
|
2022-09-27 19:06:34 +00:00
|
|
|
|
|
|
|
|
|
try:
|
2022-11-17 07:34:58 +00:00
|
|
|
|
roxywi_auth.check_login(user_params['user_uuid'], user_params['token'], service=1)
|
2022-09-27 19:06:34 +00:00
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f'error {e}')
|
|
|
|
|
sys.exit()
|
2019-09-20 06:42:19 +00:00
|
|
|
|
|
2022-11-17 07:34:58 +00:00
|
|
|
|
form = common.form
|
2019-09-20 06:42:19 +00:00
|
|
|
|
serv = form.getvalue('serv')
|
|
|
|
|
section = form.getvalue('section')
|
2021-08-15 09:02:12 +00:00
|
|
|
|
is_serv_protected = sql.is_serv_protected(serv)
|
2019-09-20 06:42:19 +00:00
|
|
|
|
sections = ""
|
|
|
|
|
config_read = ""
|
|
|
|
|
cfg = ""
|
|
|
|
|
stderr = ""
|
|
|
|
|
error = ""
|
|
|
|
|
aftersave = ""
|
|
|
|
|
start_line = ""
|
|
|
|
|
end_line = ""
|
2022-06-15 07:34:20 +00:00
|
|
|
|
warning = ''
|
2022-08-18 09:27:28 +00:00
|
|
|
|
is_restart = ''
|
2019-09-20 06:42:19 +00:00
|
|
|
|
|
2022-09-26 09:18:18 +00:00
|
|
|
|
hap_configs_dir = get_config_var.get_config_var('configs', 'haproxy_save_configs_dir')
|
2019-09-20 06:42:19 +00:00
|
|
|
|
|
|
|
|
|
if serv is not None and open is not None:
|
2022-11-17 07:34:58 +00:00
|
|
|
|
cfg = f"{hap_configs_dir}{serv}-{get_date.return_date('config')}.cfg"
|
|
|
|
|
error = config_mod.get_config(serv, cfg)
|
|
|
|
|
sections = section_mod.get_sections(cfg)
|
2019-09-20 06:42:19 +00:00
|
|
|
|
|
2020-11-06 04:39:44 +00:00
|
|
|
|
if serv is not None and section is not None:
|
2022-05-24 19:29:29 +00:00
|
|
|
|
|
2019-09-20 06:42:19 +00:00
|
|
|
|
try:
|
2022-11-17 07:34:58 +00:00
|
|
|
|
roxywi_common.logging(serv, "sections.py open config")
|
2020-11-06 04:39:44 +00:00
|
|
|
|
except Exception:
|
2019-09-20 06:42:19 +00:00
|
|
|
|
pass
|
2022-05-24 19:29:29 +00:00
|
|
|
|
|
2022-11-17 07:34:58 +00:00
|
|
|
|
start_line, end_line, config_read = section_mod.get_section_from_config(cfg, section)
|
2022-08-18 03:30:55 +00:00
|
|
|
|
server_id = sql.select_server_id_by_ip(serv)
|
|
|
|
|
is_restart = sql.select_service_setting(server_id, 'haproxy', 'restart')
|
2019-09-20 06:42:19 +00:00
|
|
|
|
|
2022-11-17 07:34:58 +00:00
|
|
|
|
os.system(f"/bin/mv {cfg} {cfg}.old")
|
2019-09-20 06:42:19 +00:00
|
|
|
|
|
|
|
|
|
if serv is not None and form.getvalue('config') is not None:
|
|
|
|
|
try:
|
2022-11-17 07:34:58 +00:00
|
|
|
|
roxywi_common.logging(serv, "sections.py edited config")
|
2020-11-06 04:39:44 +00:00
|
|
|
|
except Exception:
|
2019-09-20 06:42:19 +00:00
|
|
|
|
pass
|
2022-05-24 19:29:29 +00:00
|
|
|
|
|
2019-09-20 06:42:19 +00:00
|
|
|
|
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
|
2021-04-16 14:09:51 +00:00
|
|
|
|
|
|
|
|
|
if save == 'delete':
|
|
|
|
|
config = ''
|
2022-09-28 12:45:02 +00:00
|
|
|
|
save = 'reload'
|
2021-04-16 14:09:51 +00:00
|
|
|
|
|
2022-11-17 07:34:58 +00:00
|
|
|
|
config = section_mod.rewrite_section(start_line, end_line, oldcfg, config)
|
2022-05-24 19:29:29 +00:00
|
|
|
|
|
2019-09-20 06:42:19 +00:00
|
|
|
|
try:
|
|
|
|
|
with open(cfg, "w") as conf:
|
|
|
|
|
conf.write(config)
|
|
|
|
|
except IOError:
|
|
|
|
|
error = "Can't read import config file"
|
2022-05-24 19:29:29 +00:00
|
|
|
|
|
2022-11-17 07:34:58 +00:00
|
|
|
|
stderr = config_mod.master_slave_upload_and_restart(serv, cfg, just_save=save, oldcfg=oldcfg)
|
2022-05-22 09:09:04 +00:00
|
|
|
|
|
|
|
|
|
if "is valid" in stderr:
|
2022-06-15 07:34:20 +00:00
|
|
|
|
warning = stderr
|
2022-05-22 09:09:04 +00:00
|
|
|
|
stderr = ''
|
2022-05-24 19:29:29 +00:00
|
|
|
|
|
2022-11-17 07:34:58 +00:00
|
|
|
|
config_mod.diff_config(oldcfg, cfg)
|
2022-05-24 19:29:29 +00:00
|
|
|
|
|
2022-11-17 07:34:58 +00:00
|
|
|
|
os.system(f"/bin/rm -f {hap_configs_dir}*.old")
|
2019-09-20 06:42:19 +00:00
|
|
|
|
|
2023-02-13 14:45:45 +00:00
|
|
|
|
if user_params['lang'] == 'ru':
|
|
|
|
|
title = 'Работа с секциями HAProxy'
|
|
|
|
|
else:
|
|
|
|
|
title = 'Working with HAProxy config sections'
|
2019-09-20 06:42:19 +00:00
|
|
|
|
|
2022-05-24 19:29:29 +00:00
|
|
|
|
rendered_template = template.render(
|
2023-02-13 14:45:45 +00:00
|
|
|
|
h2=1, title=title, role=user_params['role'], action="sections.py", user=user_params['user'],
|
2022-11-17 07:34:58 +00:00
|
|
|
|
select_id="serv", serv=serv, aftersave=aftersave, config=config_read, cfg=cfg, selects=user_params['servers'],
|
|
|
|
|
stderr=stderr, error=error, start_line=start_line, end_line=end_line, section=section, sections=sections,
|
2022-11-17 10:18:04 +00:00
|
|
|
|
is_serv_protected=is_serv_protected, user_services=user_params['user_services'], token=user_params['token'],
|
2023-02-13 14:45:45 +00:00
|
|
|
|
warning=warning, is_restart=is_restart, lang=user_params['lang']
|
2022-05-24 19:29:29 +00:00
|
|
|
|
)
|
|
|
|
|
print(rendered_template)
|