source: trunk/packages/sipb-xen-www/code/cache_acls.py @ 412

Last change on this file since 412 was 410, checked in by ecprice, 17 years ago

Expose a useful function.

File size: 1.7 KB
Line 
1#!/usr/bin/python
2from sipb_xen_database import *
3import sys
4import getafsgroups
5import subprocess
6
7def 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
18def 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
26def 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
33def accessList(m):
34    people = set()
35    people.update(expandLocker(m.owner))
36    people.update(expandName(m.administrator))
37    return people
38
39def refreshMachine(m):
40    people = accessList(m)
41    old_people = set(a.user for a in m.acl)
42    for removed in old_people - people:
43        ma = [x for x in m.acl if x.user == removed][0]
44        ctx.current.delete(ma)
45    for p in people - old_people:
46        ma = MachineAccess(machine_id=m.machine_id, user=p)
47        ctx.current.save(ma)
48   
49def refreshCache():
50    transaction = ctx.current.create_transaction()
51
52    try:
53        machines = Machine.select()
54        for m in machines:
55            refreshMachine(m)
56        ctx.current.flush()
57           
58        # Atomically execute our changes
59        transaction.commit()
60    except:
61        # Failed! Rollback all the changes.
62        transaction.rollback()
63        raise
64
65if __name__ == '__main__':
66    connect('postgres://sipb-xen@sipb-xen-dev/sipb_xen')
67    refreshCache()
Note: See TracBrowser for help on using the repository browser.