kumistatus/servertools/helpers.py
2016-08-03 22:52:34 +02:00

42 lines
1.4 KiB
Python

import ast, syslog, sys, setuptools, servertools, encodings.idna, os
logging = exec(setuptools.getSetting("Log", "sink")) or servertools.STDOUT
def logger(message, prio=syslog.LOG_INFO, sink=logging):
if sink in (servertools.STDOUT, servertools.STDDEB):
if prio not in (syslog.LOG_NOTICE, syslog.LOG_INFO, syslog.LOG_DEBUG):
print(message)
sys.stderr.write(message)
elif prio != syslog.LOG_DEBUG or sink == servertools.STDDEB:
print(message)
elif sink == servertools.SYSLOG:
syslog.openlog("KumiStatusServer", syslog.LOG_PID)
syslog.syslog(prio, message)
elif sink != servertools.SILENT:
try:
sys.stderr.write("Unknown logging level %s, assuming STDOUT from now on." % str(sink))
except:
pass
logging = servertools.STDOUT
logger(message, prio, logging)
def listIncluded(host, section):
if not setuptools.getListSetting("Server", "ignorelist"):
for i in setuptools.getListSetting("SSL" if section == servertools.SSL else "Ports", "hosts"):
if encodings.idna.ToASCII(i[0].lower()).decode("UTF-8") == encodings.idna.ToASCII(host.lower()).decode("UTF-8"):
return True
return False
return True
def normalizeHost(host):
return encodings.idna.ToASCII(str(host)).decode("UTF-8")
def shutdown(reboot = False, status = 0):
if reboot:
args = sys.argv[:]
args.insert(0, sys.executable)
try:
os.execv(sys.executable, args)
except:
logger("Restart failed. Shutting down.")
exit(status)