Update RemConfFS to use RouteFS
[invirt/packages/invirt-remote.git] / files / usr / sbin / sipb-xen-remconffs
1 #!/usr/bin/python
2
3 import routefs
4 from routes import Mapper
5
6 from syslog import *
7 from time import time
8
9 from invirt import database
10 from invirt.config import structs as config
11
12 class RemConfFS(routefs.RouteFS):
13         """
14         RemConfFS creates a filesytem for configuring remctl, like this:
15         /
16         |-- acl
17         |   |-- machine1
18         |   ...
19         |   `-- machinen
20         `-- conf
21         
22         The machine list and the acls are drawn from a database.
23         
24         This filesystem only implements the getattr, getdir, read, and readlink
25         calls, because this is a read-only filesystem.
26         """
27         
28         def __init__(self, *args, **kw):
29                 """Initialize the filesystem and set it to allow_other access besides
30                 the user who mounts the filesystem (i.e. root)
31                 """
32                 super(RemConfFS, self).__init__(*args, **kw)
33                 self.lasttime = time()
34                 self.fuse_args.add("allow_other", True)
35                 
36                 openlog('sipb-xen-remconffs ', LOG_PID, LOG_DAEMON)
37                 
38                 syslog(LOG_DEBUG, 'Init complete.')
39         
40         def make_map(self):
41                 m = Mapper()
42                 m.connect('', controller='getroot')
43                 m.connect('acl/:machine', controller='getacl')
44                 m.connect('conf', controller='getconf')
45                 return m
46         
47         def getroot(self, **kw):
48                 return ['acl', 'conf']
49         
50         def getacl(self, machine, **kw):
51                 """Build the ACL file for a machine
52                 """
53                 machine = database.Machine.get_by(name=machine)
54                 users = [acl.user for acl in machine.acl]
55                 return "\n".join(map(self.userToPrinc, users)
56                                  + ['include /etc/remctl/acl/web',
57                                     ''])
58         
59         def getconf(self, **kw):
60                 """Build the master conf file, with all machines
61                 """
62                 return '\n'.join("control %s /usr/sbin/sipb-xen-remote-proxy-control"
63                                  " /etc/remctl/remconffs/acl/%s"
64                                  % (machine_name, machine_name)
65                                  for machine_name in self.getMachines())+'\n'
66         
67         def getMachines(self):
68                 """Get the list of VMs in the database, clearing the cache if it's 
69                 older than 15 seconds"""
70                 if time() - self.lasttime > 15:
71                         self.lasttime = time()
72                         database.clear_cache()
73                 return [machine.name for machine in database.Machine.select()]
74         
75         def userToPrinc(self, user):
76                 """Convert Kerberos v4-style names to v5-style and append a default
77                 realm if none is specified
78                 """
79                 if '@' in user:
80                         (princ, realm) = user.split('@')
81                 else:
82                         princ = user
83                         realm = config.authn[0].realm
84                 
85                 return princ.replace('.', '/') + '@' + realm
86
87 if __name__ == '__main__':
88         database.connect()
89         routefs.main(RemConfFS)