fix gen_config -> gen_files in two initscripts
[invirt/packages/invirt-remote.git] / files / usr / sbin / invirt-remconffs
index f4d88b8..6389391 100755 (executable)
@@ -5,6 +5,7 @@ from routes import Mapper
 
 from syslog import *
 from time import time
+import sqlalchemy as sa
 
 from invirt import database
 from invirt.config import structs as config
@@ -27,7 +28,6 @@ class RemConfFS(routefs.RouteFS):
         the user who mounts the filesystem (i.e. root)
         """
         super(RemConfFS, self).__init__(*args, **kw)
-        self.lasttime = 0
         self.fuse_args.add("allow_other", True)
         
         openlog('invirt-remconffs ', LOG_PID, LOG_DAEMON)
@@ -42,22 +42,20 @@ class RemConfFS(routefs.RouteFS):
         m.connect('conf', controller='getconf')
         return m
     
-    def recache(self):
-        if time() - self.lasttime > 5:
-            self.lasttime = time()
-            database.clear_cache()
-            self.machines = dict((machine.name, machine) for machine in database.session.query(database.Machine).all())
-    
     def getroot(self, **kw):
         return ['acl', 'conf']
     
     def getacl(self, machine, **kw):
         """Build the ACL file for a machine
         """
-        self.recache()
-        machine = self.machines[machine]
-        users = [acl.user for acl in machine.acl]
-        return "\n".join(map(self.userToPrinc, users)
+        s = sa.sql.select([database.machine_access_table.c.user], # Field to select from
+                          sa.sql.and_( # where clause
+                database.machine_table.c.machine_id==database.machine_access_table.c.machine_id, # join field
+                database.machine_table.c.name == machine), # filter field
+                          from_obj=[database.machine_access_table, database.machine_table]) # from tables
+        users = [self.userToPrinc(acl[0]) for acl in
+                 database.session.execute(s)]
+        return "\n".join(users
                  + ['include /etc/remctl/acl/web',
                     ''])
     
@@ -70,10 +68,8 @@ class RemConfFS(routefs.RouteFS):
                  for machine_name in self.getmachines())+'\n'
     
     def getmachines(self, **kw):
-        """Get the list of VMs in the database, clearing the cache if it's 
-        older than 15 seconds"""
-        self.recache()
-        return self.machines.keys()
+        """Get the list of VMs in the database. Does not cache to prevent race conditions."""
+        return list(row[0] for row in database.session.execute(sa.sql.select([database.Machine.c.name])))
     
     def userToPrinc(self, user):
         """Convert Kerberos v4-style names to v5-style and append a default