]> git.ipfire.org Git - ipfire-2.x.git/commitdiff
unbound-dhcp-leases-bridge: Only reload if leases have actually changed
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 26 Apr 2024 15:09:18 +0000 (15:09 +0000)
committerArne Fitzenreiter <arne_f@ipfire.org>
Sun, 28 Apr 2024 17:23:41 +0000 (17:23 +0000)
This patches changes that leases will always be written in
alphanumerical order so that we can later compare the newly generated
file with the previous version. If it has not changed, we skip reload
Unbound.

Suggested-by: Nick Howitt <nick@howitts.co.uk>
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Signed-off-by: Arne Fitzenreiter <arne_f@ipfire.org>
config/unbound/unbound-dhcp-leases-bridge

index 5d5696af0c50669ea5cfd2179fa7f5a3ad7cbf3b..80c8267e80b361aa257ce915bdc5cc3d8995fa91 100644 (file)
@@ -22,6 +22,7 @@
 import argparse
 import datetime
 import daemon
+import filecmp
 import functools
 import ipaddress
 import logging
@@ -516,24 +517,29 @@ class UnboundConfigWriter(object):
 
        def update_dhcp_leases(self, leases):
                # Write out all leases
-               self.write_dhcp_leases(leases)
+               if self.write_dhcp_leases(leases):
+                       log.debug("Reloading Unbound...")
 
-               log.debug("Reloading Unbound...")
-
-               # Reload the configuration without dropping the cache
-               self._control("reload_keep_cache")
+                       # Reload the configuration without dropping the cache
+                       self._control("reload_keep_cache")
 
        def write_dhcp_leases(self, leases):
                log.debug("Writing DHCP leases...")
 
                with tempfile.NamedTemporaryFile(mode="w") as f:
-                       for l in leases:
+                       for l in sorted(leases, key=lambda x: x.ipaddr):
                                for rr in l.rrset:
                                        f.write("local-data: \"%s\"\n" % " ".join(rr))
 
                        # Flush the file
                        f.flush()
 
+                       # Compare if the new leases file has changed from the previous version
+                       if filecmp.cmp(f.name, self.path, shallow=False):
+                               log.debug("The generated leases file has not changed")
+
+                               return False
+
                        # Make file readable for everyone
                        os.fchmod(f.fileno(), stat.S_IRUSR|stat.S_IWUSR|stat.S_IRGRP|stat.S_IROTH)
 
@@ -543,6 +549,8 @@ class UnboundConfigWriter(object):
                        # Move the file to its destination
                        os.link(f.name, self.path)
 
+               return True
+
        def _control(self, *args):
                command = ["unbound-control"]
                command.extend(args)