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