4 from routes import Mapper
12 from invirt.config import structs as config
13 from invirt import database
15 realpath = "/home/machines/"
17 class ConsoleFS(routefs.RouteFS):
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
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)
28 super(ConsoleFS, self).__init__(*args, **kw)
31 self.fuse_args.add("allow_other", True)
33 openlog('invirt-consolefs ', LOG_PID, LOG_DAEMON)
35 syslog(LOG_DEBUG, 'Init complete.')
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')
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.session.query(database.Machine).all())
51 def getMachines(self, **kw):
53 return self.machines.keys()
55 def getMirror(self, machine, path='', **kw):
56 """Translate the path into its realpath equivalent, and return that
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()
70 def getK5login(self, machine, **kw):
72 machine = self.machines[machine]
73 users = [acl.user for acl in machine.acl]
74 return "\n".join(map(self.userToPrinc, users) + [''])
76 def mirrorPath(self, path):
77 """Translate a virtual path to its real path counterpart"""
78 return realpath + "/".join(getParts(path)[1:])
80 def userToPrinc(self, user):
81 """Convert Kerberos v4-style names to v5-style and append a default
82 realm if none is specified
85 (princ, realm) = user.split('@')
88 realm = config.kerberos.realm
90 return princ.replace('.', '/') + '@' + realm
92 if __name__ == '__main__':
94 routefs.main(ConsoleFS)