Fix bug during create/delete clearing the cache
[invirt/packages/invirt-web.git] / cache_acls.py
index e0094e8..34d5e1e 100644 (file)
@@ -30,27 +30,26 @@ def expandName(name):
         name = 'system:'+name
     return getafsgroups.getAfsGroupMembers(name, 'athena.mit.edu')
 
-if __name__ == '__main__':
-    connect('postgres://sipb-xen@sipb-xen-dev/sipb_xen')
+def refreshMachine(m):
+    people = set()
+    people.update(expandLocker(m.owner))
+    people.update(expandName(m.administrator))
+    old_people = set(a.user for a in m.acl)
+    for removed in old_people - people:
+        ma = [x for x in m.acl if x.user == removed][0]
+        ctx.current.delete(ma)
+    for p in people - old_people:
+        ma = MachineAccess(machine_id=m.machine_id, user=p)
+        ctx.current.save(ma)
     
+def refreshCache():
     transaction = ctx.current.create_transaction()
 
-    print repr(ctx), repr(ctx.current), repr(transaction)
-    # Remove existing machine access entries
-    machine_access_table.delete()
-    ctx.current.flush()
-
     try:
         machines = Machine.select()
         for m in machines:
-            people = set()
-            people.update(expandLocker(m.owner))
-            people.update(expandName(m.administrator))
-            print '%s: %s' % (m.name, ' '.join(people))
-            for p in people:
-                ma = MachineAccess(machine_id=m.machine_id, user=p)
-                ctx.current.save(ma)
-            ctx.current.flush()
+            refreshMachine(m)
+        ctx.current.flush()
             
         # Atomically execute our changes
         transaction.commit()
@@ -58,3 +57,7 @@ if __name__ == '__main__':
         # Failed! Rollback all the changes.
         transaction.rollback()
         raise
+
+if __name__ == '__main__':
+    connect('postgres://sipb-xen@sipb-xen-dev/sipb_xen')
+    refreshCache()