From patchwork Tue Jul 11 13:29:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Michael Tremer X-Patchwork-Id: 6988 Return-Path: Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by web04.haj.ipfire.org (Postfix) with ESMTPS id 4R0hYY6RBDz3wfS for ; Tue, 11 Jul 2023 13:29:37 +0000 (UTC) Received: from mail02.haj.ipfire.org (mail02.haj.ipfire.org [172.28.1.201]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) client-signature ECDSA (secp384r1)) (Client CN "mail02.haj.ipfire.org", Issuer "R3" (verified OK)) by mail01.ipfire.org (Postfix) with ESMTPS id 4R0hYW5wyPzgC; Tue, 11 Jul 2023 13:29:35 +0000 (UTC) Received: from mail02.haj.ipfire.org (localhost [127.0.0.1]) by mail02.haj.ipfire.org (Postfix) with ESMTP id 4R0hYW5CCQz2xgq; Tue, 11 Jul 2023 13:29:35 +0000 (UTC) Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by mail02.haj.ipfire.org (Postfix) with ESMTPS id 4R0hYW0PHzz2xfn for ; Tue, 11 Jul 2023 13:29:35 +0000 (UTC) Received: from michael.haj.ipfire.org (michael.haj.ipfire.org [172.28.1.242]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384 client-signature ECDSA (secp384r1) client-digest SHA384) (Client CN "michael.haj.ipfire.org", Issuer "R3" (verified OK)) by mail01.ipfire.org (Postfix) with ESMTPS id 4R0hYV40cDzL7; Tue, 11 Jul 2023 13:29:34 +0000 (UTC) Received: by michael.haj.ipfire.org (Postfix, from userid 0) id 4R0hYV2PgMzTgg9; Tue, 11 Jul 2023 13:29:34 +0000 (UTC) From: Michael Tremer To: development@lists.ipfire.org Subject: [PATCH] unbound-dhcp-leases-bridge: Reload unbound to import leases Date: Tue, 11 Jul 2023 13:29:32 +0000 Message-Id: <20230711132932.786202-1-michael.tremer@ipfire.org> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-BeenThere: development@lists.ipfire.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: IPFire development talk List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Tremer Errors-To: development-bounces@lists.ipfire.org Sender: "Development" This changes the old "diff" algorithm that we needed to have before Unbound was able to reload its own configuration. Now, it can do this even without dropping the cache. This should hopefully perform much better and be more reliable than the old way. Signed-off-by: Michael Tremer Acked-by: Peter Müller --- config/unbound/unbound-dhcp-leases-bridge | 52 ++++------------------- 1 file changed, 8 insertions(+), 44 deletions(-) diff --git a/config/unbound/unbound-dhcp-leases-bridge b/config/unbound/unbound-dhcp-leases-bridge index e89e0446b..e9f022aff 100644 --- a/config/unbound/unbound-dhcp-leases-bridge +++ b/config/unbound/unbound-dhcp-leases-bridge @@ -514,56 +514,19 @@ class UnboundConfigWriter(object): def __init__(self, path): self.path = path - self._cached_leases = [] - def update_dhcp_leases(self, leases): - # Find any leases that have expired or do not exist any more - # but are still in the unbound local data - removed_leases = [l for l in self._cached_leases if not l in leases] - - # Find any leases that have been added - new_leases = [l for l in leases if l not in self._cached_leases] - - # End here if nothing has changed - if not new_leases and not removed_leases: - return - # Write out all leases self.write_dhcp_leases(leases) - # Update unbound about changes - for l in removed_leases: - try: - for name, ttl, type, content in l.rrset: - log.debug("Removing records for %s" % name) - self._control("local_data_remove", name) - - # If the lease cannot be removed we will try the next one - except: - continue - - # If the removal was successful, we will remove it from the cache - else: - self._cached_leases.remove(l) - - for l in new_leases: - try: - for rr in l.rrset: - log.debug("Adding new record %s" % " ".join(rr)) - self._control("local_data", *rr) - - # If the lease cannot be added we will try the next one - except: - continue + log.debug("Reloading Unbound...") - # Add lease to cache when successfully added - else: - self._cached_leases.append(l) + # Reload the configuration without dropping the cache + self._control("reload_keep_cache") def write_dhcp_leases(self, leases): - with tempfile.NamedTemporaryFile(mode="w", delete=False) as f: - filename = f.name + log.debug("Writing DHCP leases...") + with tempfile.NamedTemporaryFile(mode="w", delete=False) as f: for l in leases: for rr in l.rrset: f.write("local-data: \"%s\"\n" % " ".join(rr)) @@ -571,7 +534,8 @@ class UnboundConfigWriter(object): # Make file readable for everyone os.fchmod(f.fileno(), stat.S_IRUSR|stat.S_IWUSR|stat.S_IRGRP|stat.S_IROTH) - os.rename(filename, self.path) + # Move the file to its destination + os.rename(f.name, self.path) def _control(self, *args): command = ["unbound-control"] @@ -585,7 +549,7 @@ class UnboundConfigWriter(object): log.critical("Could not run %s, error code: %s: %s" % ( " ".join(command), e.returncode, e.output)) - raise + raise e if __name__ == "__main__":