haproxy-wi/app/config.py

102 lines
2.4 KiB
Python
Raw Normal View History

2018-01-15 06:16:04 +00:00
#!/usr/bin/env python3
import cgi
import os
import http.cookies
import funct
2018-04-24 07:38:48 +00:00
import sql
from jinja2 import Environment, FileSystemLoader
2019-07-16 11:42:45 +00:00
env = Environment(loader=FileSystemLoader('templates/'), autoescape=True)
template = env.get_template('config.html')
2018-12-25 10:59:08 +00:00
print('Content-type: text/html\n')
funct.check_login()
2018-01-15 06:16:04 +00:00
form = cgi.FieldStorage()
serv = form.getvalue('serv')
config_read = ""
cfg = ""
stderr = ""
2018-05-06 19:58:52 +00:00
error = ""
aftersave = ""
2018-01-15 06:16:04 +00:00
try:
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
user_id = cookie.get('uuid')
user = sql.get_user_name_by_uuid(user_id.value)
servers = sql.get_dick_permit()
2018-06-01 12:27:58 +00:00
token = sql.get_token(user_id.value)
2018-12-25 10:59:08 +00:00
role = sql.get_user_role_by_uuid(user_id.value)
except:
pass
2018-01-15 06:16:04 +00:00
hap_configs_dir = funct.get_config_var('configs', 'haproxy_save_configs_dir')
2018-01-15 06:16:04 +00:00
if serv is not None:
cfg = hap_configs_dir + serv + "-" + funct.get_data('config') + ".cfg"
2018-09-27 04:28:10 +00:00
if serv is not None and form.getvalue('open') is not None :
2018-05-03 06:36:50 +00:00
try:
funct.logging(serv, "config.py open config")
except:
pass
2018-05-06 19:58:52 +00:00
error = funct.get_config(serv, cfg)
2018-01-15 06:16:04 +00:00
2018-04-26 04:24:23 +00:00
try:
conf = open(cfg, "r")
config_read = conf.read()
2018-05-06 19:58:52 +00:00
conf.close
2018-04-26 04:24:23 +00:00
except IOError:
2018-05-06 19:58:52 +00:00
error += '<br />Can\'t read import config file'
2018-01-15 06:16:04 +00:00
os.system("/bin/mv %s %s.old" % (cfg, cfg))
2018-01-15 06:16:04 +00:00
2018-09-27 04:28:10 +00:00
if serv is not None and form.getvalue('config') is not None:
2018-05-03 06:36:50 +00:00
try:
funct.logging(serv, "config.py edited config")
except:
pass
2018-01-15 06:16:04 +00:00
config = form.getvalue('config')
oldcfg = form.getvalue('oldconfig')
save = form.getvalue('save')
aftersave = 1
2018-01-15 06:16:04 +00:00
try:
with open(cfg, "a") as conf:
conf.write(config)
except IOError:
2018-05-06 19:58:52 +00:00
error = "Can't read import config file"
2018-01-15 06:16:04 +00:00
2018-04-24 07:38:48 +00:00
MASTERS = sql.is_master(serv)
for master in MASTERS:
if master[0] != None:
funct.upload_and_restart(master[0], cfg, just_save=save)
stderr = funct.upload_and_restart(serv, cfg, just_save=save)
2018-05-29 09:53:20 +00:00
funct.diff_config(oldcfg, cfg)
2018-09-27 04:28:10 +00:00
#if save:
# c = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
# c["restart"] = form.getvalue('serv')
# print(c)
2018-05-29 09:53:20 +00:00
os.system("/bin/rm -f " + hap_configs_dir + "*.old")
2018-01-15 06:16:04 +00:00
2018-12-25 10:59:08 +00:00
2018-09-27 04:28:10 +00:00
template = template.render(h2 = 1, title = "Working with HAProxy configs",
2018-12-25 10:59:08 +00:00
role = role,
2018-09-27 04:28:10 +00:00
action = "config.py",
user = user,
select_id = "serv",
serv = serv,
aftersave = aftersave,
config = config_read,
cfg = cfg,
selects = servers,
stderr = stderr,
error = error,
note = 1,
token = token)
print(template)