1cae001ef60712e71ad517e5e6a3248f7574f183
[invirt/packages/invirt-remote.git] / server / usr / sbin / invirt-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 import sqlalchemy as sa
9
10 from invirt import database
11 from invirt.config import structs as config
12
13 import sqlalchemy.orm.util as util
14
15 class RemConfFS(routefs.RouteFS):
16     """
17     RemConfFS creates a filesytem for configuring remctl, like this:
18     /
19     |-- adminacl
20     |-- acl
21     |   |-- machine1
22     |   ...
23     |   `-- machinen
24     `-- conf
25     
26     The machine list and the acls are drawn from a database.
27     """
28     
29     def __init__(self, *args, **kw):
30         """Initialize the filesystem and set it to allow_other access besides
31         the user who mounts the filesystem (i.e. root)
32         """
33         super(RemConfFS, self).__init__(*args, **kw)
34         self.fuse_args.add("allow_other", True)
35         
36         openlog('invirt-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', controller='getmachines')
44         m.connect('/acl/:machine', controller='getacl')
45         m.connect('/adminacl', controller='getadmin')
46         m.connect('/conf', controller='getconf')
47         return m
48     
49     def getroot(self, **kw):
50         return ['adminacl', 'acl', 'conf']
51     
52     def getacl(self, machine, **kw):
53         """Build the ACL file for a machine
54         """
55         s = sa.sql.select([database.machine_access_table.class_mapper(type(self)).mapped_table.c.user], # Field to select from
56                           sa.sql.and_( # where clause
57                 database.machine_table.class_mapper(type(self)).mapped_table.c.machine_id==database.machine_access_table.class_mapper(type(self)).mapped_table.c.machine_id, # join field
58                 database.machine_table.class_mapper(type(self)).mapped_table.c.name == machine), # filter field
59                           from_obj=[database.machine_access_table, database.machine_table]) # from tables
60         users = [self.userToPrinc(acl[0]) for acl in
61                  database.session.execute(s)]
62         return "\n".join(users
63                  + ['include /etc/remctl/acl/web',
64                     ''])
65     
66     def getconf(self, **kw):
67         """Build the master conf file, with all machines
68         """
69         return '\n'.join("control %s /usr/sbin/invirt-remote-proxy-control"
70                  " /etc/remctl/remconffs/acl/%s"
71                  % (machine_name, machine_name)
72                  for machine_name in self.getmachines())+'\n'
73     
74     def getmachines(self, **kw):
75         """Get the list of VMs in the database. Does not cache to prevent race conditions."""
76         return list(row[0] for row in database.session.execute(sa.sql.select([database.Machine.class_mapper(type(self)).mapped_table.c.name])))
77
78     def getadmin(self, **kw):
79         """
80         Get the list of administrators for the global ACL.
81         """
82         acl = [self.userToPrinc(row[0]) for row in database.session.execute(sa.sql.select([database.admins_table.class_mapper(type(self)).mapped_table.c.user]))]
83         acl.append('include /etc/remctl/acl/web\n')
84         return '\n'.join(acl)
85     
86     def userToPrinc(self, user):
87         """Convert Kerberos v4-style names to v5-style and append a default
88         realm if none is specified
89         """
90         if '@' in user:
91             (princ, realm) = user.split('@')
92         else:
93             princ = user
94             realm = config.kerberos.realm
95         
96         return princ.replace('.', '/') + '@' + realm
97
98 if __name__ == '__main__':
99     database.connect()
100     routefs.main(RemConfFS)