source: trunk/packages/invirt-console/server/usr/bin/invirt-consolefs

Last change on this file was 2426, checked in by broder, 15 years ago

invirt-console:

  • Give the website access to all consoles in preparation for exposing AjaxTerm?.
  • Property svn:executable set to *
File size: 3.1 KB
Line 
1#!/usr/bin/python
2
3import routefs
4from routes import Mapper
5
6from syslog import *
7from time import time
8
9import os
10import errno
11
12from invirt.config import structs as config
13from invirt import database
14
15realpath = "/home/machines/"
16
17class ConsoleFS(routefs.RouteFS):
18    """
19    ConsoleFS creates a series of subdirectories each mirroring the same real
20    directory, except for a single file - the .k5login - which is dynamically
21    generated for each subdirectory
22    """
23   
24    def __init__(self, *args, **kw):
25        """Initialize the filesystem and set it to allow_other access besides
26        the user who mounts the filesystem (i.e. root)
27        """
28        super(ConsoleFS, self).__init__(*args, **kw)
29        self.lasttime = 0
30        self.machines = []
31        self.fuse_args.add("allow_other", True)
32       
33        openlog('invirt-consolefs ', LOG_PID, LOG_DAEMON)
34       
35        syslog(LOG_DEBUG, 'Init complete.')
36   
37    def make_map(self):
38        m = Mapper()
39        m.connect('', controller='getMachines')
40        m.connect(':machine', controller='getMirror')
41        m.connect(':machine/.k5login', controller='getK5login')
42        m.connect(':machine/*(path)', controller='getMirror')
43        return m
44   
45    def recache(self):
46        if time() - self.lasttime > 5:
47            self.lasttime = time()
48            database.clear_cache()
49            self.machines = dict((machine.name, machine) for machine in database.Machine.query.all())
50
51    def getMachines(self, **kw):
52        self.recache()
53        return self.machines.keys()
54   
55    def getMirror(self, machine, path='', **kw):
56        """Translate the path into its realpath equivalent, and return that
57        """
58        real = realpath + path
59        if os.path.isdir(real):
60            # The list is converted to a set so that we can handle the case
61            # where there is already a .k5login in the realpath gracefully   
62            return routefs.Directory(set(os.listdir(real) + ['.k5login']))
63        elif os.path.islink(real):
64            return routefs.Symlink(os.readlink(real))
65        elif os.path.isfile(real):
66            return open(real).read()
67        else:
68            return -errno.EINVAL
69   
70    def getK5login(self, machine, **kw):
71        self.recache()
72        machine = self.machines[machine]
73        users = (acl.user for acl in machine.acl)
74        users = [self.userToPrinc(u) for u in users]
75        users.append('daemon/%s@%s' % (config.web.hostname,
76                                       config.kerberos.realm))
77        users.append('')
78        return "\n".join(users)
79   
80    def mirrorPath(self, path):
81        """Translate a virtual path to its real path counterpart"""
82        return realpath + "/".join(getParts(path)[1:])
83   
84    def userToPrinc(self, user):
85        """Convert Kerberos v4-style names to v5-style and append a default
86        realm if none is specified
87        """
88        if '@' in user:
89            (princ, realm) = user.split('@')
90        else:
91            princ = user
92            realm = config.kerberos.realm
93       
94        return princ.replace('.', '/') + '@' + realm
95
96if __name__ == '__main__':
97    database.connect()
98    routefs.main(ConsoleFS)
Note: See TracBrowser for help on using the repository browser.