From da0d3b99c48a49db7c0bdb9ea18087e4d689c9e2 Mon Sep 17 00:00:00 2001
From: Evan Broder <broder@mit.edu>
Date: Mon, 17 Nov 2008 13:20:02 -0500
Subject: [PATCH 1/1] Add real caching to consolefs, too

svn path=/trunk/packages/invirt-console-server/; revision=1699
---
 debian/changelog               |    6 ++++++
 files/usr/bin/invirt-consolefs |   21 ++++++++++++++-------
 2 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index 9ccb877..27e67bf 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,9 @@
+invirt-console-server (0.1.0) unstable; urgency=low
+
+  * Add real caching to consolefs
+
+ -- Evan Broder <broder@mit.edu>  Mon, 17 Nov 2008 13:16:09 -0500
+
 invirt-console-server (0.0.13) unstable; urgency=low
 
   * Actually get the password fields right for libnss-pgsql
diff --git a/files/usr/bin/invirt-consolefs b/files/usr/bin/invirt-consolefs
index 53c37d5..e092c21 100755
--- a/files/usr/bin/invirt-consolefs
+++ b/files/usr/bin/invirt-consolefs
@@ -26,13 +26,14 @@ class ConsoleFS(routefs.RouteFS):
 		the user who mounts the filesystem (i.e. root)
 		"""
 		super(ConsoleFS, self).__init__(*args, **kw)
-		self.lasttime = time()
+		self.lasttime = 0
+		self.machines = []
 		self.fuse_args.add("allow_other", True)
 		
 		openlog('invirt-consolefs ', LOG_PID, LOG_DAEMON)
 		
 		syslog(LOG_DEBUG, 'Init complete.')
-
+	
 	def make_map(self):
 		m = Mapper()
 		m.connect('', controller='getMachines')
@@ -41,13 +42,18 @@ class ConsoleFS(routefs.RouteFS):
 		m.connect(':machine/*(path)', controller='getMirror')
 		return m
 	
-	def getMachines(self, **kw):
-		"""Get the list of VMs in the database, clearing the cache if it's 
-		older than 15 seconds"""
+	def recache(self):
+		"""Refresh the local cache of VMs if the cache is more than 15 minutes old
+		"""
 		if time() - self.lasttime > 15:
 			self.lasttime = time()
 			database.clear_cache()
-		return [machine.name for machine in database.Machine.query()]
+			self.machines = dict((machine.name, machine) for machine in database.session.query(database.Machine).all())
+
+	def getMachines(self, **kw):
+		"""Get the list of VMs in the database"""
+		self.recache()
+		return self.machines.keys()
 	
 	def getMirror(self, machine, path='', **kw):
 		"""Translate the path into its realpath equivalent, and return that
@@ -67,7 +73,8 @@ class ConsoleFS(routefs.RouteFS):
  	def getK5login(self, machine, **kw):
 		"""Build the ACL for a machine and turn it into a .k5login file
 		"""
-		machine = database.Machine.query().filter_by(name=machine).one()
+		self.recache()
+		machine = self.machines[machine]
 		users = [acl.user for acl in machine.acl]
 		return "\n".join(map(self.userToPrinc, users) + [''])
 	
-- 
1.7.9.5