12 from arvados_fuse import *
14 logger = logging.getLogger('arvados.arv-mount')
16 if __name__ == '__main__':
17 # Handle command line parameters
18 parser = argparse.ArgumentParser(
19 description='''Mount Keep data under the local filesystem. Default mode is --home''',
21 Note: When using the --exec feature, you must either specify the
22 mountpoint before --exec, or mark the end of your --exec arguments
25 parser.add_argument('mountpoint', type=str, help="""Mount point.""")
26 parser.add_argument('--allow-other', action='store_true',
27 help="""Let other users read the mount""")
29 mount_mode = parser.add_mutually_exclusive_group()
31 mount_mode.add_argument('--all', action='store_true', help="""Mount a subdirectory for each mode: home, shared, by_tag, by_id (default).""")
32 mount_mode.add_argument('--home', action='store_true', help="""Mount only the user's home project.""")
33 mount_mode.add_argument('--shared', action='store_true', help="""Mount only list of projects shared with the user.""")
34 mount_mode.add_argument('--by-tag', action='store_true',
35 help="""Mount subdirectories listed by tag.""")
36 mount_mode.add_argument('--by-id', action='store_true',
37 help="""Mount subdirectories listed by portable data hash or uuid.""")
38 mount_mode.add_argument('--project', type=str, help="""Mount a specific project.""")
39 mount_mode.add_argument('--collection', type=str, help="""Mount only the specified collection.""")
41 parser.add_argument('--debug', action='store_true', help="""Debug mode""")
42 parser.add_argument('--logfile', help="""Write debug logs and errors to the specified file (default stderr).""")
43 parser.add_argument('--foreground', action='store_true', help="""Run in foreground (default is to daemonize unless --exec specified)""", default=False)
44 parser.add_argument('--exec', type=str, nargs=argparse.REMAINDER,
45 dest="exec_args", metavar=('command', 'args', '...', '--'),
46 help="""Mount, run a command, then unmount and exit""")
48 args = parser.parse_args()
49 args.mountpoint = os.path.realpath(args.mountpoint)
51 args.logfile = os.path.realpath(args.logfile)
53 # Daemonize as early as possible, so we don't accidentally close
54 # file descriptors we're using.
55 if not (args.exec_args or args.foreground):
56 os.chdir(args.mountpoint)
57 daemon_ctx = daemon.DaemonContext(working_directory='.')
62 # Configure a logger based on command-line switches.
63 # If we're using a contemporary Python SDK (mid-August 2014),
64 # configure the arvados hierarchy logger.
65 # Otherwise, configure the program root logger.
66 base_logger = getattr(arvados, 'logger', None)
69 log_handler = logging.FileHandler(args.logfile)
71 log_handler = logging.NullHandler()
73 log_handler = arvados.log_handler
75 log_handler = logging.StreamHandler()
77 if base_logger is None:
78 base_logger = logging.getLogger()
80 base_logger.removeHandler(arvados.log_handler)
81 base_logger.addHandler(log_handler)
84 base_logger.setLevel(logging.DEBUG)
85 logger.debug("arv-mount debugging enabled")
88 # Create the request handler
89 operations = Operations(os.getuid(), os.getgid())
90 api = SafeApi(arvados.config)
92 usr = api.users().current().execute()
95 # Set up the request handler with the 'magic directory' at the root
96 operations.inodes.add_entry(MagicDirectory(llfuse.ROOT_INODE, operations.inodes, api))
98 operations.inodes.add_entry(TagsDirectory(llfuse.ROOT_INODE, operations.inodes, api))
100 operations.inodes.add_entry(SharedDirectory(llfuse.ROOT_INODE, operations.inodes, api, usr))
102 operations.inodes.add_entry(ProjectDirectory(llfuse.ROOT_INODE, operations.inodes, api, usr))
103 elif args.collection != None:
104 # Set up the request handler with the collection at the root
105 operations.inodes.add_entry(CollectionDirectory(llfuse.ROOT_INODE, operations.inodes, api, args.collection))
106 elif args.project != None:
107 operations.inodes.add_entry(ProjectDirectory(llfuse.ROOT_INODE, operations.inodes, api, api.groups().get(uuid=args.project).execute()))
109 e = operations.inodes.add_entry(Directory(llfuse.ROOT_INODE))
111 e._entries['home'] = operations.inodes.add_entry(ProjectDirectory(e.inode, operations.inodes, api, usr))
112 e._entries['shared'] = operations.inodes.add_entry(SharedDirectory(e.inode, operations.inodes, api, usr))
113 e._entries['by_tag'] = operations.inodes.add_entry(TagsDirectory(e.inode, operations.inodes, api))
114 e._entries['by_id'] = operations.inodes.add_entry(MagicDirectory(e.inode, operations.inodes, api))
117 Welcome to Arvados! This directory provides file system access to files and objects
118 available on the Arvados installation located at '{}'
119 using credentials for user '{}'.
121 From here, the following directories are available:
123 by_id/ Access to Keep collections by uuid or portable data hash (see by_id/README for details).
124 by_tag/ Access to Keep collections organized by tag.
125 home/ The contents of your home project.
126 shared/ Projects shared with you.
127 '''.format(arvados.config.get('ARVADOS_API_HOST'), usr['email'])
129 e._entries["README"] = operations.inodes.add_entry(StringFile(e.inode, text, now))
133 logger.exception("arv-mount: exception during API setup")
136 # FUSE options, see mount.fuse(8)
137 opts = [optname for optname in ['allow_other', 'debug']
138 if getattr(args, optname)]
141 # Initialize the fuse connection
142 llfuse.init(operations, args.mountpoint, opts)
144 t = threading.Thread(None, lambda: llfuse.main())
147 # wait until the driver is finished initializing
148 operations.initlock.wait()
152 sp = subprocess.Popen(args.exec_args, shell=False)
154 # forward signals to the process.
155 signal.signal(signal.SIGINT, lambda signum, frame: sp.send_signal(signum))
156 signal.signal(signal.SIGTERM, lambda signum, frame: sp.send_signal(signum))
157 signal.signal(signal.SIGQUIT, lambda signum, frame: sp.send_signal(signum))
159 # wait for process to complete.
162 # restore default signal handlers.
163 signal.signal(signal.SIGINT, signal.SIG_DFL)
164 signal.signal(signal.SIGTERM, signal.SIG_DFL)
165 signal.signal(signal.SIGQUIT, signal.SIG_DFL)
166 except Exception as e:
167 logger.exception('arv-mount: exception during exec %s',
171 except AttributeError:
174 subprocess.call(["fusermount", "-u", "-z", args.mountpoint])
179 llfuse.init(operations, args.mountpoint, opts)
181 except Exception as e:
182 logger.exception('arv-mount: exception during mount')
183 exit(getattr(e, 'errno', 1))