Modularize cache_acls.py more
[invirt/packages/invirt-web.git] / cache_acls.py
1 #!/usr/bin/python
2 from sipb_xen_database import *
3 import sys
4 import getafsgroups
5 import subprocess
6
7 def expandLocker(name):
8     groups = getafsgroups.getLockerAcl(name)
9     cell = getafsgroups.getCell(name)
10     ans = set()
11     for group in groups:
12         if ':' in group:
13             ans.update(getafsgroups.getAfsGroupMembers(group, cell))
14         else:
15             ans.add(group)
16     return ans
17
18 def isUser(name):
19     p = subprocess.Popen(['vos', 'examine', 'user.'+name],
20                          stdout=subprocess.PIPE, stderr=subprocess.PIPE)
21     if p.wait():
22         return False
23     return True
24     
25
26 def expandName(name):
27     if ':' not in name:
28         if isUser(name):
29             return [name]
30         name = 'system:'+name
31     return getafsgroups.getAfsGroupMembers(name, 'athena.mit.edu')
32
33 def refreshCache():
34     transaction = ctx.current.create_transaction()
35
36     try:
37         machines = Machine.select()
38         for m in machines:
39             people = set()
40             people.update(expandLocker(m.owner))
41             people.update(expandName(m.administrator))
42             print '%s: %s' % (m.name, ' '.join(people))
43             old_people = set(a.user for a in m.acl)
44             for removed in old_people - people:
45                 ma = [x for x in m.acl if x.user == removed][0]
46                 ctx.current.delete(ma)
47             for p in people - old_people:
48                 ma = MachineAccess(machine_id=m.machine_id, user=p)
49                 ctx.current.save(ma)
50             ctx.current.flush()
51             
52         # Atomically execute our changes
53         transaction.commit()
54     except:
55         # Failed! Rollback all the changes.
56         transaction.rollback()
57         raise
58
59 if __name__ == '__main__':
60     connect('postgres://sipb-xen@sipb-xen-dev/sipb_xen')
61     refreshCache()