From e90862076e9783a49b38969e64cc22a1afd50a81 Mon Sep 17 00:00:00 2001 From: Rory McNamara Date: Sun, 5 Mar 2017 23:03:48 +0000 Subject: [PATCH 1/2] add SIGUSR1 handler to reload leases --- pypxe/server.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/pypxe/server.py b/pypxe/server.py index e4dea8d..2708658 100755 --- a/pypxe/server.py +++ b/pypxe/server.py @@ -5,6 +5,7 @@ import json import logging import logging.handlers +import signal try: import argparse @@ -120,8 +121,37 @@ def do_verbose(service): or 'all' in args.MODE_VERBOSE.lower()) and '-{0}'.format(service) not in args.MODE_VERBOSE.lower()) +def signalhandler(signum, frame): + if args.USE_DHCP and args.STATIC_CONFIG: + # so we can skip out, sort of like a goto, without loads of nesting + for _ in (True,): + try: + # if we're called before the object is initialised + dhcp_server + except NameError: + continue + + sys_logger.info("Reloading DHCP static leases") + + try: + static_config = open(args.STATIC_CONFIG, 'rb') + except IOError: + print("Failed to open {0}".format(args.STATIC_CONFIG)) + continue + + try: + loaded_statics = json.load(static_config) + static_config.close() + except ValueError: + print("{0} does not contain valid json".format(args.STATIC_CONFIG)) + continue + + # add and overwrite the old config + # set as global inside main + dhcp_server.static_config = loaded_statics + def main(): - global SETTINGS, args + global SETTINGS, args, dhcp_server, sys_logger try: # configure args = parse_cli_arguments() @@ -311,4 +341,5 @@ def main(): sys.exit('\nShutting down PyPXE...\n') if __name__ == '__main__': + signal.signal(signal.SIGUSR1, signalhandler) main() From d342890bc1b094573e94a901c900df7871f9a340 Mon Sep 17 00:00:00 2001 From: Rory McNamara Date: Sun, 5 Mar 2017 23:10:26 +0000 Subject: [PATCH 2/2] error log invalid json --- pypxe/server.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pypxe/server.py b/pypxe/server.py index 2708658..c78eb63 100755 --- a/pypxe/server.py +++ b/pypxe/server.py @@ -136,14 +136,14 @@ def signalhandler(signum, frame): try: static_config = open(args.STATIC_CONFIG, 'rb') except IOError: - print("Failed to open {0}".format(args.STATIC_CONFIG)) + sys_logger.error("Failed to open {0}".format(args.STATIC_CONFIG)) continue try: loaded_statics = json.load(static_config) static_config.close() except ValueError: - print("{0} does not contain valid json".format(args.STATIC_CONFIG)) + sys_logger.error("{0} does not contain valid json".format(args.STATIC_CONFIG)) continue # add and overwrite the old config