diff --git a/pypxe/dhcp.py b/pypxe/dhcp.py index 0dbaddf..46b5b60 100644 --- a/pypxe/dhcp.py +++ b/pypxe/dhcp.py @@ -8,7 +8,6 @@ import struct import os import logging -import signal import json from collections import defaultdict from time import time @@ -130,12 +129,7 @@ def __init__(self, **server_settings): except ValueError: pass - signal.signal(signal.SIGINT, self.export_leases) - signal.signal(signal.SIGTERM, self.export_leases) - signal.signal(signal.SIGALRM, self.export_leases) - signal.signal(signal.SIGHUP, self.export_leases) - - def export_leases(self, signum, frame): + def export_leases(self): if self.save_leases_file: export_safe = dict() for lease in self.leases: @@ -145,10 +139,6 @@ def export_leases(self, signum, frame): json.dump(export_safe, leases_file) self.logger.info('Exported leases to {0}'.format(self.save_leases_file)) - # if keyboard interrupt, propagate upwards - if signum == signal.SIGINT: - raise KeyboardInterrupt - def get_namespaced_static(self, path, fallback = {}): statics = self.static_config for child in path.split('.'): diff --git a/pypxe/server.py b/pypxe/server.py index 4c1767f..34fc5d0 100755 --- a/pypxe/server.py +++ b/pypxe/server.py @@ -6,6 +6,7 @@ import json import logging import logging.handlers +import signal try: import argparse @@ -237,6 +238,13 @@ def main(): # configure/start DHCP server if args.USE_DHCP: + def dhcp_export_leases(signum, frame): + dhcp_server.export_leases() + + # if keyboard interrupt, propagate upwards + if signum == signal.SIGINT: + raise KeyboardInterrupt + def load_dhcp_static_config(filename): result = ['', dict()] @@ -290,6 +298,10 @@ def load_dhcp_static_config(filename): static_config = loaded_statics, logger = dhcp_logger, saveleases = args.LEASES_FILE) + signal.signal(signal.SIGINT, dhcp_export_leases) + signal.signal(signal.SIGTERM, dhcp_export_leases) + signal.signal(signal.SIGALRM, dhcp_export_leases) + signal.signal(signal.SIGHUP, dhcp_export_leases) dhcpd = threading.Thread(target = dhcp_server.listen) dhcpd.daemon = True dhcpd.start()