public inbox for ipfire-scm@lists.ipfire.org
 help / color / mirror / Atom feed
From: git@ipfire.org
To: ipfire-scm@lists.ipfire.org
Subject: [git.ipfire.org] IPFire 2.x development tree branch, next, updated. 86c9deb2ea39001c488b7b65f6dfac6fe88a1a35
Date: Tue, 11 Oct 2016 20:24:57 +0100	[thread overview]
Message-ID: <20161011192503.40F151078E81@git01.ipfire.org> (raw)

[-- Attachment #1: Type: text/plain, Size: 5508 bytes --]

This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "IPFire 2.x development tree".

The branch, next has been updated
       via  86c9deb2ea39001c488b7b65f6dfac6fe88a1a35 (commit)
      from  998e880b61b4c201a9483a0beafc6d139a29f0db (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 86c9deb2ea39001c488b7b65f6dfac6fe88a1a35
Author: Michael Tremer <michael.tremer(a)ipfire.org>
Date:   Tue Oct 11 19:14:33 2016 +0200

    unbound: Public static leases in DNS, too
    
    Signed-off-by: Michael Tremer <michael.tremer(a)ipfire.org>

-----------------------------------------------------------------------

Summary of changes:
 config/unbound/unbound-dhcp-leases-bridge | 81 +++++++++++++++++++++++++++++--
 1 file changed, 76 insertions(+), 5 deletions(-)

Difference in files:
diff --git a/config/unbound/unbound-dhcp-leases-bridge b/config/unbound/unbound-dhcp-leases-bridge
index 862b581..91bdb4f 100644
--- a/config/unbound/unbound-dhcp-leases-bridge
+++ b/config/unbound/unbound-dhcp-leases-bridge
@@ -64,8 +64,9 @@ def reverse_pointer_to_ip_address(rr):
 	return ".".join(parts)
 
 class UnboundDHCPLeasesBridge(object):
-	def __init__(self, dhcp_leases_file, unbound_leases_file):
+	def __init__(self, dhcp_leases_file, fix_leases_file, unbound_leases_file):
 		self.leases_file = dhcp_leases_file
+		self.fix_leases_file = fix_leases_file
 
 		self.unbound = UnboundConfigWriter(unbound_leases_file)
 		self.running = False
@@ -77,7 +78,7 @@ class UnboundDHCPLeasesBridge(object):
 		# Initially read leases file
 		self.update_dhcp_leases()
 
-		i = inotify.adapters.Inotify([self.leases_file])
+		i = inotify.adapters.Inotify([self.leases_file, self.fix_leases_file])
 
 		for event in i.event_gen():
 			# End if we are requested to terminate
@@ -93,12 +94,21 @@ class UnboundDHCPLeasesBridge(object):
 			if "IN_MODIFY" in type_names:
 				self.update_dhcp_leases()
 
+			# If the file is deleted, we re-add the watcher
+			if "IN_IGNORED" in type_names:
+				i.add_watch(watch_path)
+
 		log.info("Unbound DHCP Leases Bridge terminated")
 
 	def update_dhcp_leases(self):
-		log.info("Reading DHCP leases from %s" % self.leases_file)
+		leases = []
+
+		for lease in DHCPLeases(self.leases_file):
+			leases.append(lease)
+
+		for lease in FixLeases(self.fix_leases_file):
+			leases.append(lease)
 
-		leases = DHCPLeases(self.leases_file)
 		self.unbound.update_dhcp_leases(leases)
 
 	def terminate(self):
@@ -117,6 +127,8 @@ class DHCPLeases(object):
 		return iter(self._leases)
 
 	def _parse(self):
+		log.info("Reading DHCP leases from %s" % self.path)
+
 		leases = []
 
 		with open(self.path) as f:
@@ -179,6 +191,58 @@ class DHCPLeases(object):
 		return properties
 
 
+class FixLeases(object):
+	cache = {}
+
+	def __init__(self, path):
+		self.path = path
+
+		self._leases = self.cache[self.path] = self._parse()
+
+	def __iter__(self):
+		return iter(self._leases)
+
+	def _parse(self):
+		log.info("Reading fix leases from %s" % self.path)
+
+		leases = []
+		now = datetime.datetime.utcnow()
+
+		with open(self.path) as f:
+			for line in f.readlines():
+				line = line.rstrip()
+
+				try:
+					hwaddr, ipaddr, enabled, a, b, c, hostname = line.split(",")
+				except ValueError:
+					log.warning("Could not parse line: %s" % line)
+					continue
+
+				# Skip any disabled leases
+				if not enabled == "on":
+					continue
+
+				l = Lease(ipaddr, {
+					"binding"         : "state active",
+					"client-hostname" : hostname,
+					"hardware"        : "ethernet %s" % hwaddr,
+					"starts"          : now.strftime("%w %Y/%m/%d %H:%M:%S"),
+					"ends"            : "never",
+				})
+				leases.append(l)
+
+		# Try finding any deleted leases
+		for lease in self.cache.get(self.path, []):
+			if lease in leases:
+				continue
+
+			# Free the deleted lease
+			lease.free()
+			leases.append(lease)
+
+		return leases
+
+
 class Lease(object):
 	def __init__(self, ipaddr, properties):
 		self.ipaddr = ipaddr
@@ -208,6 +272,11 @@ class Lease(object):
 			state = state.split(" ", 1)
 			return state[1]
 
+	def free(self):
+		self._properties.update({
+			"binding" : "state free",
+		})
+
 	@property
 	def active(self):
 		return self.binding_state == "active"
@@ -430,6 +499,8 @@ if __name__ == "__main__":
 		metavar="PATH", help="Path to the DHCPd leases file")
 	parser.add_argument("--unbound-leases", default="/etc/unbound/dhcp-leases.conf",
 		metavar="PATH", help="Path to the unbound configuration file")
+	parser.add_argument("--fix-leases", default="/var/ipfire/dhcp/fixleases",
+		metavar="PATH", help="Path to the fix leases file")
 
 	# Parse command line arguments
 	args = parser.parse_args()
@@ -444,7 +515,7 @@ if __name__ == "__main__":
 
 	setup_logging(loglevel)
 
-	bridge = UnboundDHCPLeasesBridge(args.dhcp_leases, args.unbound_leases)
+	bridge = UnboundDHCPLeasesBridge(args.dhcp_leases, args.fix_leases, args.unbound_leases)
 
 	ctx = daemon.DaemonContext(detach_process=args.daemon)
 	ctx.signal_map = {


hooks/post-receive
--
IPFire 2.x development tree

                 reply	other threads:[~2016-10-11 19:24 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20161011192503.40F151078E81@git01.ipfire.org \
    --to=git@ipfire.org \
    --cc=ipfire-scm@lists.ipfire.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox