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

93 lines
2.2 KiB

#!/usr/bin/env python3
import cgi
import os
import http.cookies
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()
form = cgi.FieldStorage()
serv = form.getvalue('serv')
config_read = ""
cfg = ""
stderr = ""
error = ""
aftersave = ""
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()
token = sql.get_token(user_id.value)
6 years ago
role = sql.get_user_role_by_uuid(user_id.value)
except:
pass
hap_configs_dir = funct.get_config_var('configs', 'haproxy_save_configs_dir')
if serv is not None:
cfg = hap_configs_dir + serv + "-" + funct.get_data('config') + ".cfg"
6 years ago
if serv is not None and form.getvalue('open') is not None :
7 years ago
try:
funct.logging(serv, "config.py open config")
except:
pass
error = funct.get_config(serv, cfg)
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:
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"
stderr = funct.master_slave_upload_and_restart(serv, cfg, just_save=save)
funct.diff_config(oldcfg, cfg)
os.system("/bin/rm -f " + hap_configs_dir + "*.old")
6 years ago
6 years ago
template = template.render(h2 = 1, title = "Working with HAProxy configs",
6 years ago
role = role,
6 years ago
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,
versions = funct.versions(),
6 years ago
token = token)
print(template)