2018-01-15 06:16:04 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import os
|
|
|
|
import funct
|
2018-04-24 07:38:48 +00:00
|
|
|
import sql
|
2018-05-05 12:40:41 +00:00
|
|
|
from jinja2 import Environment, FileSystemLoader
|
2019-07-16 11:42:45 +00:00
|
|
|
env = Environment(loader=FileSystemLoader('templates/'), autoescape=True)
|
2018-05-05 12:40:41 +00:00
|
|
|
template = env.get_template('config.html')
|
2018-05-17 08:13:54 +00:00
|
|
|
|
2018-12-25 10:59:08 +00:00
|
|
|
print('Content-type: text/html\n')
|
2018-05-05 12:40:41 +00:00
|
|
|
funct.check_login()
|
2018-05-17 08:13:54 +00:00
|
|
|
|
2019-12-04 13:38:07 +00:00
|
|
|
form = funct.form
|
2018-01-15 06:16:04 +00:00
|
|
|
serv = form.getvalue('serv')
|
2020-02-02 14:23:00 +00:00
|
|
|
service = form.getvalue('service')
|
2022-04-15 09:49:00 +00:00
|
|
|
is_serv_protected = False
|
2022-01-16 13:50:53 +00:00
|
|
|
try:
|
|
|
|
config_file_name = form.getvalue('config_file_name').replace('92', '/')
|
2022-05-23 19:55:08 +00:00
|
|
|
except Exception:
|
2022-01-16 13:50:53 +00:00
|
|
|
config_file_name = ''
|
2018-05-05 12:40:41 +00:00
|
|
|
config_read = ""
|
|
|
|
cfg = ""
|
|
|
|
stderr = ""
|
2018-05-06 19:58:52 +00:00
|
|
|
error = ""
|
2018-05-05 12:40:41 +00:00
|
|
|
aftersave = ""
|
2018-01-15 06:16:04 +00:00
|
|
|
|
2018-05-05 12:40:41 +00:00
|
|
|
try:
|
2021-08-15 09:02:12 +00:00
|
|
|
user, user_id, role, token, servers, user_services = funct.get_users_params()
|
|
|
|
except Exception as e:
|
|
|
|
print(str(e))
|
2018-01-15 06:16:04 +00:00
|
|
|
|
2020-02-02 14:23:00 +00:00
|
|
|
if service == 'keepalived':
|
2021-08-15 09:02:12 +00:00
|
|
|
if funct.check_login(service=3):
|
|
|
|
title = "Working with Keepalived configuration files"
|
|
|
|
action = "config.py?service=keepalived"
|
|
|
|
configs_dir = funct.get_config_var('configs', 'kp_save_configs_dir')
|
|
|
|
file_format = 'conf'
|
|
|
|
servers = sql.get_dick_permit(keepalived=1)
|
2020-02-05 08:15:57 +00:00
|
|
|
elif service == 'nginx':
|
2021-08-15 09:02:12 +00:00
|
|
|
if funct.check_login(service=2):
|
2022-04-20 08:08:36 +00:00
|
|
|
title = "Working with NGINX configuration files"
|
2021-08-15 09:02:12 +00:00
|
|
|
action = "config.py?service=nginx"
|
|
|
|
configs_dir = funct.get_config_var('configs', 'nginx_save_configs_dir')
|
|
|
|
file_format = 'conf'
|
|
|
|
servers = sql.get_dick_permit(nginx=1)
|
2022-04-15 09:49:00 +00:00
|
|
|
elif service == 'apache':
|
2022-05-12 12:59:45 +00:00
|
|
|
if funct.check_login(service=4):
|
2022-04-15 09:49:00 +00:00
|
|
|
title = "Working with Apache configuration files"
|
|
|
|
action = "config.py?service=apache"
|
|
|
|
configs_dir = funct.get_config_var('configs', 'apache_save_configs_dir')
|
|
|
|
file_format = 'conf'
|
|
|
|
servers = sql.get_dick_permit(apache=1)
|
2020-02-02 14:23:00 +00:00
|
|
|
else:
|
2021-08-15 09:02:12 +00:00
|
|
|
if funct.check_login(service=1):
|
|
|
|
title = "Working with HAProxy configuration files"
|
|
|
|
action = "config.py"
|
|
|
|
configs_dir = funct.get_config_var('configs', 'haproxy_save_configs_dir')
|
|
|
|
file_format = 'cfg'
|
2022-03-24 08:23:29 +00:00
|
|
|
servers = sql.get_dick_permit(haproxy=1)
|
2018-01-15 06:16:04 +00:00
|
|
|
|
2018-04-21 14:40:59 +00:00
|
|
|
if serv is not None:
|
2022-04-15 09:49:00 +00:00
|
|
|
if service == 'nginx' or service == 'apache':
|
2022-03-02 20:09:39 +00:00
|
|
|
conf_file_name_short = config_file_name.split('/')[-1]
|
|
|
|
cfg = configs_dir + serv + "-" + conf_file_name_short + "-" + funct.get_data('config') + "." + file_format
|
|
|
|
else:
|
2022-05-23 19:55:08 +00:00
|
|
|
cfg = configs_dir + serv + "-" + funct.get_data('config') + "." + file_format
|
2018-04-21 14:40:59 +00:00
|
|
|
|
2022-03-02 20:09:39 +00:00
|
|
|
if serv is not None and form.getvalue('open') is not None and form.getvalue('new_config') is None:
|
2020-09-17 05:26:54 +00:00
|
|
|
funct.check_is_server_in_group(serv)
|
2022-04-15 09:49:00 +00:00
|
|
|
is_serv_protected = sql.is_serv_protected(serv)
|
|
|
|
|
2020-02-02 14:23:00 +00:00
|
|
|
if service == 'keepalived':
|
|
|
|
error = funct.get_config(serv, cfg, keepalived=1)
|
|
|
|
try:
|
2021-09-24 05:09:46 +00:00
|
|
|
funct.logging(serv, " Keepalived config has been opened for ")
|
2020-11-02 17:01:08 +00:00
|
|
|
except Exception:
|
2020-02-05 08:15:57 +00:00
|
|
|
pass
|
|
|
|
elif service == 'nginx':
|
2022-01-16 13:50:53 +00:00
|
|
|
error = funct.get_config(serv, cfg, nginx=1, config_file_name=config_file_name)
|
2020-02-05 08:15:57 +00:00
|
|
|
try:
|
2022-04-20 08:08:36 +00:00
|
|
|
funct.logging(serv, " NGINX config has been opened ")
|
2020-11-02 17:01:08 +00:00
|
|
|
except Exception:
|
2020-02-02 14:23:00 +00:00
|
|
|
pass
|
2022-04-15 09:49:00 +00:00
|
|
|
elif service == 'apache':
|
|
|
|
error = funct.get_config(serv, cfg, apache=1, config_file_name=config_file_name)
|
|
|
|
try:
|
|
|
|
funct.logging(serv, " Apache config has been opened ")
|
|
|
|
except Exception:
|
|
|
|
pass
|
2020-02-02 14:23:00 +00:00
|
|
|
else:
|
|
|
|
error = funct.get_config(serv, cfg)
|
|
|
|
try:
|
2021-09-24 05:09:46 +00:00
|
|
|
funct.logging(serv, " HAProxy config has been opened ")
|
2020-11-02 17:01:08 +00:00
|
|
|
except Exception:
|
2020-02-02 14:23:00 +00:00
|
|
|
pass
|
2022-05-23 19:55:08 +00:00
|
|
|
|
2018-04-26 04:24:23 +00:00
|
|
|
try:
|
|
|
|
conf = open(cfg, "r")
|
2018-05-05 12:40:41 +00:00
|
|
|
config_read = conf.read()
|
2020-11-02 17:01:08 +00:00
|
|
|
conf.close()
|
2018-04-26 04:24:23 +00:00
|
|
|
except IOError:
|
2022-01-16 13:50:53 +00:00
|
|
|
error += '<br />Cannot read imported config file'
|
2018-01-15 06:16:04 +00:00
|
|
|
|
2021-01-03 14:49:18 +00:00
|
|
|
os.system("/bin/mv %s %s.old" % (cfg, cfg))
|
2018-01-15 06:16:04 +00:00
|
|
|
|
2022-03-02 20:09:39 +00:00
|
|
|
if form.getvalue('new_config') is not None:
|
|
|
|
config_read = ' '
|
|
|
|
|
2018-09-27 04:28:10 +00:00
|
|
|
if serv is not None and form.getvalue('config') is not None:
|
2021-01-03 14:49:18 +00:00
|
|
|
import sys
|
2020-09-17 05:26:54 +00:00
|
|
|
funct.check_is_server_in_group(serv)
|
2021-01-03 14:49:18 +00:00
|
|
|
|
2018-01-15 06:16:04 +00:00
|
|
|
config = form.getvalue('config')
|
|
|
|
oldcfg = form.getvalue('oldconfig')
|
2018-04-06 03:53:30 +00:00
|
|
|
save = form.getvalue('save')
|
2021-01-03 14:49:18 +00:00
|
|
|
|
2018-01-15 06:16:04 +00:00
|
|
|
try:
|
|
|
|
with open(cfg, "a") as conf:
|
|
|
|
conf.write(config)
|
|
|
|
except IOError:
|
2022-01-16 13:50:53 +00:00
|
|
|
print("error: Cannot read imported config file")
|
2021-01-03 14:49:18 +00:00
|
|
|
|
2020-02-02 14:23:00 +00:00
|
|
|
if service == 'keepalived':
|
2021-10-17 05:57:51 +00:00
|
|
|
stderr = funct.upload_and_restart(serv, cfg, just_save=save, keepalived=1, oldcfg=oldcfg)
|
2020-02-05 08:15:57 +00:00
|
|
|
elif service == 'nginx':
|
2022-01-16 13:50:53 +00:00
|
|
|
stderr = funct.master_slave_upload_and_restart(serv, cfg, just_save=save, nginx=1, oldcfg=oldcfg, config_file_name=config_file_name)
|
2022-04-15 09:49:00 +00:00
|
|
|
elif service == 'apache':
|
|
|
|
stderr = funct.master_slave_upload_and_restart(serv, cfg, just_save=save, apache=1, oldcfg=oldcfg, config_file_name=config_file_name)
|
2020-02-02 14:23:00 +00:00
|
|
|
else:
|
2021-10-17 05:57:51 +00:00
|
|
|
stderr = funct.master_slave_upload_and_restart(serv, cfg, just_save=save, oldcfg=oldcfg)
|
2021-01-03 14:49:18 +00:00
|
|
|
|
2018-05-29 09:53:20 +00:00
|
|
|
funct.diff_config(oldcfg, cfg)
|
2021-01-03 14:49:18 +00:00
|
|
|
|
2022-06-15 07:34:20 +00:00
|
|
|
try:
|
|
|
|
os.system("/bin/rm -f " + configs_dir + "*.old")
|
|
|
|
except Exception as e:
|
|
|
|
print('error: ' + str(e))
|
2018-01-15 06:16:04 +00:00
|
|
|
|
2021-01-03 14:49:18 +00:00
|
|
|
if stderr:
|
|
|
|
print(stderr)
|
2022-06-15 07:34:20 +00:00
|
|
|
|
2021-01-03 14:49:18 +00:00
|
|
|
sys.exit()
|
2018-12-25 10:59:08 +00:00
|
|
|
|
2022-05-23 19:55:08 +00:00
|
|
|
template = template.render(
|
|
|
|
h2=1, title=title, role=role, action=action, user=user, select_id="serv", serv=serv, aftersave=aftersave,
|
|
|
|
config=config_read, cfg=cfg, selects=servers, stderr=stderr, error=error, service=service,
|
|
|
|
user_services=user_services, config_file_name=config_file_name, is_serv_protected=is_serv_protected, token=token
|
|
|
|
)
|
2019-12-04 13:38:07 +00:00
|
|
|
print(template)
|