[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 | 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 | """ |
---|
[982] | 32 | super(RemConfFS, self).__init__(*args, **kw) |
---|
[518] | 33 | self.lasttime = time() |
---|
[982] | 34 | self.fuse_args.add("allow_other", True) |
---|
[518] | 35 | |
---|
| 36 | openlog('sipb-xen-remconffs ', LOG_PID, LOG_DAEMON) |
---|
| 37 | |
---|
| 38 | syslog(LOG_DEBUG, 'Init complete.') |
---|
[982] | 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): |
---|
[518] | 51 | """Build the ACL file for a machine |
---|
| 52 | """ |
---|
[982] | 53 | machine = database.Machine.get_by(name=machine) |
---|
[518] | 54 | users = [acl.user for acl in machine.acl] |
---|
| 55 | return "\n".join(map(self.userToPrinc, users) |
---|
| 56 | + ['include /etc/remctl/acl/web', |
---|
| 57 | '']) |
---|
[982] | 58 | |
---|
| 59 | def getconf(self, **kw): |
---|
[519] | 60 | """Build the master conf file, with all machines |
---|
[518] | 61 | """ |
---|
[519] | 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' |
---|
[518] | 66 | |
---|
[982] | 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 | |
---|
[518] | 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 |
---|
[830] | 83 | realm = config.authn[0].realm |
---|
[518] | 84 | |
---|
| 85 | return princ.replace('.', '/') + '@' + realm |
---|
| 86 | |
---|
| 87 | if __name__ == '__main__': |
---|
[835] | 88 | database.connect() |
---|
[982] | 89 | routefs.main(RemConfFS) |
---|