4 from routes import Mapper
8 import sqlalchemy as sa
10 from invirt import database
11 from invirt.config import structs as config
13 import sqlalchemy.orm.util as util
15 class RemConfFS(routefs.RouteFS):
17 RemConfFS creates a filesytem for configuring remctl, like this:
26 The machine list and the acls are drawn from a database.
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)
33 super(RemConfFS, self).__init__(*args, **kw)
34 self.fuse_args.add("allow_other", True)
36 openlog('invirt-remconffs ', LOG_PID, LOG_DAEMON)
38 syslog(LOG_DEBUG, 'Init complete.')
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')
49 def getroot(self, **kw):
50 return ['adminacl', 'acl', 'conf']
52 def getacl(self, machine, **kw):
53 """Build the ACL file for a machine
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',
66 def getconf(self, **kw):
67 """Build the master conf file, with all machines
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'
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])))
78 def getadmin(self, **kw):
80 Get the list of administrators for the global ACL.
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')
86 def userToPrinc(self, user):
87 """Convert Kerberos v4-style names to v5-style and append a default
88 realm if none is specified
91 (princ, realm) = user.split('@')
94 realm = config.kerberos.realm
96 return princ.replace('.', '/') + '@' + realm
98 if __name__ == '__main__':
100 routefs.main(RemConfFS)