You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
haproxy-wi/app/config.py

124 lines
3.2 KiB

#!/usr/bin/env python3
import os
import funct
import sql
from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader('templates/'), autoescape=True)
template = env.get_template('config.html')
6 years ago
print('Content-type: text/html\n')
funct.check_login()
5 years ago
form = funct.form
serv = form.getvalue('serv')
service = form.getvalue('service')
config_read = ""
cfg = ""
stderr = ""
error = ""
aftersave = ""
try:
user, user_id, role, token, servers = funct.get_users_params()
except:
pass
if service == 'keepalived':
title = "Working with Keepalived configs"
action = "config.py?service=keepalived"
configs_dir = funct.get_config_var('configs', 'kp_save_configs_dir')
format = 'conf'
servers = sql.get_dick_permit(keepalived=1)
elif service == 'nginx':
title = "Working with Nginx configs"
action = "config.py?service=nginx"
configs_dir = funct.get_config_var('configs', 'nginx_save_configs_dir')
format = 'conf'
servers = sql.get_dick_permit(nginx=1)
else:
title = "Working with HAProxy configs"
action = "config.py"
configs_dir = funct.get_config_var('configs', 'haproxy_save_configs_dir')
format = 'cfg'
servers = sql.get_dick_permit()
if serv is not None:
cfg = configs_dir + serv + "-" + funct.get_data('config') + "."+format
6 years ago
if serv is not None and form.getvalue('open') is not None :
funct.check_is_server_in_group(serv)
if service == 'keepalived':
error = funct.get_config(serv, cfg, keepalived=1)
try:
funct.logging(serv, " Keepalived config has opened for ")
except:
pass
elif service == 'nginx':
error = funct.get_config(serv, cfg, nginx=1)
try:
funct.logging(serv, " Nginx config has opened ")
except:
pass
else:
error = funct.get_config(serv, cfg)
try:
funct.logging(serv, " HAProxy config has opened ")
except:
pass
7 years ago
try:
conf = open(cfg, "r")
config_read = conf.read()
conf.close
7 years ago
except IOError:
error += '<br />Can\'t read import config file'
os.system("/bin/mv %s %s.old" % (cfg, cfg))
6 years ago
if serv is not None and form.getvalue('config') is not None:
funct.check_is_server_in_group(serv)
7 years ago
try:
funct.logging(serv, "config.py edited config")
except:
pass
config = form.getvalue('config')
oldcfg = form.getvalue('oldconfig')
save = form.getvalue('save')
aftersave = 1
try:
with open(cfg, "a") as conf:
conf.write(config)
except IOError:
error = "Can't read import config file"
if service == 'keepalived':
stderr = funct.upload_and_restart(serv, cfg, just_save=save, keepalived=1)
elif service == 'nginx':
stderr = funct.master_slave_upload_and_restart(serv, cfg, just_save=save, nginx=1)
else:
stderr = funct.master_slave_upload_and_restart(serv, cfg, just_save=save)
funct.diff_config(oldcfg, cfg)
os.system("/bin/rm -f " + configs_dir + "*.old")
6 years ago
template = template.render(h2 = 1, title = title,
6 years ago
role = role,
action = action,
6 years ago
user = user,
select_id = "serv",
serv = serv,
aftersave = aftersave,
config = config_read,
cfg = cfg,
selects = servers,
stderr = stderr,
error = error,
note = 1,
versions = funct.versions(),
service = service,
6 years ago
token = token)
5 years ago
print(template)