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

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

Add real caching to consolefs, too

  • Property svn:executable set to *
File size: 2.8 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                """Refresh the local cache of VMs if the cache is more than 15 minutes old
47                """
48                if time() - self.lasttime > 15:
49                        self.lasttime = time()
50                        database.clear_cache()
51                        self.machines = dict((machine.name, machine) for machine in database.session.query(database.Machine).all())
52
53        def getMachines(self, **kw):
54                """Get the list of VMs in the database"""
55                self.recache()
56                return self.machines.keys()
57       
58        def getMirror(self, machine, path='', **kw):
59                """Translate the path into its realpath equivalent, and return that
60                """
61                real = realpath + path
62                if os.path.isdir(real):
63                        # The list is converted to a set so that we can handle the case
64                        # where there is already a .k5login in the realpath gracefully   
65                        return routefs.Directory(set(os.listdir(real) + ['.k5login']))
66                elif os.path.islink(real):
67                        return routefs.Symlink(os.readlink(real))
68                elif os.path.isfile(real):
69                        return open(real).read()
70                else:
71                        return -errno.EINVAL
72       
73        def getK5login(self, machine, **kw):
74                """Build the ACL for a machine and turn it into a .k5login file
75                """
76                self.recache()
77                machine = self.machines[machine]
78                users = [acl.user for acl in machine.acl]
79                return "\n".join(map(self.userToPrinc, users) + [''])
80       
81        def mirrorPath(self, path):
82                """Translate a virtual path to its real path counterpart"""
83                return realpath + "/".join(getParts(path)[1:])
84       
85        def userToPrinc(self, user):
86                """Convert Kerberos v4-style names to v5-style and append a default
87                realm if none is specified
88                """
89                if '@' in user:
90                        (princ, realm) = user.split('@')
91                else:
92                        princ = user
93                        realm = config.authn[0].realm
94               
95                return princ.replace('.', '/') + '@' + realm
96
97if __name__ == '__main__':
98        database.connect()
99        routefs.main(ConsoleFS)
Note: See TracBrowser for help on using the repository browser.