11 from arvados_fuse import *
13 logger = logging.getLogger('arvados.arv-mount')
15 if __name__ == '__main__':
16 # Handle command line parameters
17 parser = argparse.ArgumentParser(
18 description='''Mount Keep data under the local filesystem. Default mode is --home''',
20 Note: When using the --exec feature, you must either specify the
21 mountpoint before --exec, or mark the end of your --exec arguments
24 parser.add_argument('mountpoint', type=str, help="""Mount point.""")
25 parser.add_argument('--allow-other', action='store_true',
26 help="""Let other users read the mount""")
28 mount_mode = parser.add_mutually_exclusive_group()
30 mount_mode.add_argument('--all', action='store_true', help="""Mount a subdirectory for each mode: home, shared, tags, portable data hash (default).""")
31 mount_mode.add_argument('--home', action='store_true', help="""Mount only the user's home project.""")
32 mount_mode.add_argument('--shared', action='store_true', help="""Mount only list of projects shared with the user.""")
33 mount_mode.add_argument('--by-tag', action='store_true',
34 help="""Mount subdirectories listed by tag.""")
35 mount_mode.add_argument('--by-hash', action='store_true',
36 help="""Mount subdirectories listed by portable data hash.""")
37 mount_mode.add_argument('--project', type=str, help="""Mount a specific project.""")
38 mount_mode.add_argument('--collection', type=str, help="""Mount only the specified collection.""")
40 parser.add_argument('--debug', action='store_true', help="""Debug mode""")
41 parser.add_argument('--logfile', help="""Write debug logs and errors to the specified file (default stderr).""")
42 parser.add_argument('--foreground', action='store_true', help="""Run in foreground (default is to daemonize unless --exec specified)""", default=False)
43 parser.add_argument('--exec', type=str, nargs=argparse.REMAINDER,
44 dest="exec_args", metavar=('command', 'args', '...', '--'),
45 help="""Mount, run a command, then unmount and exit""")
47 args = parser.parse_args()
48 args.mountpoint = os.path.realpath(args.mountpoint)
50 args.logfile = os.path.realpath(args.logfile)
52 # Daemonize as early as possible, so we don't accidentally close
53 # file descriptors we're using.
54 if not (args.exec_args or args.foreground):
55 os.chdir(args.mountpoint)
56 daemon_ctx = daemon.DaemonContext(working_directory='.')
61 # Configure a logger based on command-line switches.
62 # If we're using a contemporary Python SDK (mid-August 2014),
63 # configure the arvados hierarchy logger.
64 # Otherwise, configure the program root logger.
65 base_logger = getattr(arvados, 'logger', None)
68 log_handler = logging.FileHandler(args.logfile)
70 log_handler = logging.NullHandler()
72 log_handler = arvados.log_handler
74 log_handler = logging.StreamHandler()
76 if base_logger is None:
77 base_logger = logging.getLogger()
79 base_logger.removeHandler(arvados.log_handler)
80 base_logger.addHandler(log_handler)
83 base_logger.setLevel(logging.DEBUG)
84 logger.debug("arv-mount debugging enabled")
87 # Create the request handler
88 operations = Operations(os.getuid(), os.getgid())
89 api = arvados.api('v1')
91 usr = api.users().current().execute()
93 # Set up the request handler with the 'magic directory' at the root
94 operations.inodes.add_entry(MagicDirectory(llfuse.ROOT_INODE, operations.inodes, api))
96 operations.inodes.add_entry(TagsDirectory(llfuse.ROOT_INODE, operations.inodes, api))
98 operations.inodes.add_entry(SharedDirectory(llfuse.ROOT_INODE, operations.inodes, api, usr))
100 operations.inodes.add_entry(ProjectDirectory(llfuse.ROOT_INODE, operations.inodes, api, usr))
101 elif args.collection != None:
102 # Set up the request handler with the collection at the root
103 operations.inodes.add_entry(CollectionDirectory(llfuse.ROOT_INODE, operations.inodes, api, args.collection))
104 elif args.project != None:
105 operations.inodes.add_entry(ProjectDirectory(llfuse.ROOT_INODE, operations.inodes, api, api.groups().get(uuid=args.project).execute()))
107 e = operations.inodes.add_entry(Directory(llfuse.ROOT_INODE))
108 e._entries['home'] = operations.inodes.add_entry(ProjectDirectory(e.inode, operations.inodes, api, usr))
109 e._entries['shared'] = operations.inodes.add_entry(SharedDirectory(e.inode, operations.inodes, api, usr))
110 e._entries['by_tag'] = operations.inodes.add_entry(TagsDirectory(e.inode, operations.inodes, api))
111 e._entries['by_hash'] = operations.inodes.add_entry(MagicDirectory(e.inode, operations.inodes, api))
114 logger.exception("arv-mount: exception during API setup")
117 # FUSE options, see mount.fuse(8)
118 opts = [optname for optname in ['allow_other', 'debug']
119 if getattr(args, optname)]
122 # Initialize the fuse connection
123 llfuse.init(operations, args.mountpoint, opts)
125 t = threading.Thread(None, lambda: llfuse.main())
128 # wait until the driver is finished initializing
129 operations.initlock.wait()
133 sp = subprocess.Popen(args.exec_args, shell=False)
135 # forward signals to the process.
136 signal.signal(signal.SIGINT, lambda signum, frame: sp.send_signal(signum))
137 signal.signal(signal.SIGTERM, lambda signum, frame: sp.send_signal(signum))
138 signal.signal(signal.SIGQUIT, lambda signum, frame: sp.send_signal(signum))
140 # wait for process to complete.
143 # restore default signal handlers.
144 signal.signal(signal.SIGINT, signal.SIG_DFL)
145 signal.signal(signal.SIGTERM, signal.SIG_DFL)
146 signal.signal(signal.SIGQUIT, signal.SIG_DFL)
147 except Exception as e:
148 logger.exception('arv-mount: exception during exec %s',
152 except AttributeError:
155 subprocess.call(["fusermount", "-u", "-z", args.mountpoint])
160 llfuse.init(operations, args.mountpoint, opts)
162 except Exception as e:
163 logger.exception('arv-mount: exception during mount')
164 exit(getattr(e, 'errno', 1))