haproxy-wi/app/config.py

82 lines
2.4 KiB
Python
Raw Normal View History

2018-01-15 06:16:04 +00:00
#!/usr/bin/env python3
import html
import cgi
import os
import http.cookies
from configparser import ConfigParser, ExtendedInterpolation
2018-01-15 06:16:04 +00:00
import funct
2018-04-24 07:38:48 +00:00
import sql
2018-01-15 06:16:04 +00:00
form = cgi.FieldStorage()
serv = form.getvalue('serv')
servNew = form.getvalue('serNew')
funct.head("Edit HAproxy config")
funct.check_config()
funct.check_login()
2018-04-05 03:34:17 +00:00
funct.page_for_admin(level = 1)
2018-01-15 06:16:04 +00:00
path_config = "haproxy-webintarface.config"
config = ConfigParser(interpolation=ExtendedInterpolation())
2018-01-15 06:16:04 +00:00
config.read(path_config)
2018-04-10 06:10:07 +00:00
log_path = config.get('main', 'log_path')
2018-01-15 06:16:04 +00:00
hap_configs_dir = config.get('configs', 'haproxy_save_configs_dir')
2018-04-10 06:10:07 +00:00
funct.chooseServer("config.py", "Edit HAproxy config", "y")
2018-01-15 06:16:04 +00:00
if serv is not None:
cfg = hap_configs_dir + serv + "-" + funct.get_data('config') + ".cfg"
2018-01-15 06:16:04 +00:00
if form.getvalue('serv') is not None and form.getvalue('open') is not None :
2018-01-15 06:16:04 +00:00
funct.logging(serv, "config.py open config")
funct.get_config(serv, cfg)
2018-04-26 04:24:23 +00:00
try:
conf = open(cfg, "r")
except IOError:
print('<div class="alert alert-danger">Can\'t read import config file</div>')
2018-04-10 06:10:07 +00:00
print("<center><h3>Config from %s</h3>" % serv)
print('<form action="config.py" method="get">')
2018-01-15 06:16:04 +00:00
print('<input type="hidden" value="%s" name="serv">' % serv)
print('<input type="hidden" value="%s.old" name="oldconfig">' % cfg)
print('<textarea name="config" class="config" rows="35" cols="100">%s</textarea>' % conf.read())
print('<p>')
funct.get_button("Just save", value="save")
2018-04-05 03:34:17 +00:00
funct.get_button("Save and restart")
print('</p></form>')
2018-01-15 06:16:04 +00:00
conf.close
os.system("/bin/mv %s %s.old" % (cfg, cfg))
2018-01-15 06:16:04 +00:00
2018-04-10 06:10:07 +00:00
if form.getvalue('serv') is not None and form.getvalue('config') is not None:
funct.logging(serv, "config.py edited config")
2018-01-15 06:16:04 +00:00
config = form.getvalue('config')
oldcfg = form.getvalue('oldconfig')
save = form.getvalue('save')
2018-01-15 06:16:04 +00:00
try:
with open(cfg, "a") as conf:
conf.write(config)
except IOError:
print("Can't read import config file")
2018-04-27 04:07:59 +00:00
print('<center><div class="alert alert-info">New config was saved as: %s </div></center>' % cfg)
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)
funct.upload_and_restart(serv, cfg, just_save=save)
2018-01-15 06:16:04 +00:00
2018-04-26 04:24:23 +00:00
os.system("/bin/diff -ub %s %s >> %s/config_edit-%s.log" % (oldcfg, cfg, log_path, funct.get_data('logs')))
os.system("/bin/rm -f " + hap_configs_dir + "*.old")
2018-01-15 06:16:04 +00:00
2018-02-06 04:39:43 +00:00
print('</br><a href="viewsttats.py?serv=%s" target="_blank" title="View stats">Go to view stats</a> <br />' % serv)
2018-01-15 06:16:04 +00:00
funct.footer()