Fixes reference to $js->{killtime} instead of $procinfo->{killtime}
[arvados.git] / services / fuse / arvados_fuse / command.py
1 import argparse
2 import arvados
3 import daemon
4 import llfuse
5 import logging
6 import os
7 import resource
8 import signal
9 import subprocess
10 import sys
11 import time
12
13 import arvados.commands._util as arv_cmd
14 from arvados_fuse import crunchstat
15 from arvados_fuse import *
16
17 class ArgumentParser(argparse.ArgumentParser):
18     def __init__(self):
19         super(ArgumentParser, self).__init__(
20             parents=[arv_cmd.retry_opt],
21             description='''Mount Keep data under the local filesystem.  Default mode is --home''',
22             epilog="""
23     Note: When using the --exec feature, you must either specify the
24     mountpoint before --exec, or mark the end of your --exec arguments
25     with "--".
26             """)
27         self.add_argument('mountpoint', type=str, help="""Mount point.""")
28         self.add_argument('--allow-other', action='store_true',
29                             help="""Let other users read the mount""")
30
31         mode = self.add_mutually_exclusive_group()
32
33         mode.add_argument('--all', action='store_const', const='all', dest='mode',
34                                 help="""Mount a subdirectory for each mode: home, shared, by_tag, by_id (default if no --mount-* arguments are given).""")
35         mode.add_argument('--custom', action='store_const', const=None, dest='mode',
36                                 help="""Mount a top level meta-directory with subdirectories as specified by additional --mount-* arguments (default if any --mount-* arguments are given).""")
37         mode.add_argument('--home', action='store_const', const='home', dest='mode',
38                                 help="""Mount only the user's home project.""")
39         mode.add_argument('--shared', action='store_const', const='shared', dest='mode',
40                                 help="""Mount only list of projects shared with the user.""")
41         mode.add_argument('--by-tag', action='store_const', const='by_tag', dest='mode',
42                                 help="""Mount subdirectories listed by tag.""")
43         mode.add_argument('--by-id', action='store_const', const='by_id', dest='mode',
44                                 help="""Mount subdirectories listed by portable data hash or uuid.""")
45         mode.add_argument('--by-pdh', action='store_const', const='by_pdh', dest='mode',
46                                 help="""Mount subdirectories listed by portable data hash.""")
47         mode.add_argument('--project', type=str, metavar='UUID',
48                                 help="""Mount the specified project.""")
49         mode.add_argument('--collection', type=str, metavar='UUID_or_PDH',
50                                 help="""Mount only the specified collection.""")
51
52         mounts = self.add_argument_group('Custom mount options')
53         mounts.add_argument('--mount-by-pdh',
54                             type=str, metavar='PATH', action='append', default=[],
55                             help="Mount each readable collection at mountpoint/PATH/P where P is the collection's portable data hash.")
56         mounts.add_argument('--mount-by-id',
57                             type=str, metavar='PATH', action='append', default=[],
58                             help="Mount each readable collection at mountpoint/PATH/UUID and mountpoint/PATH/PDH where PDH is the collection's portable data hash and UUID is its UUID.")
59         mounts.add_argument('--mount-by-tag',
60                             type=str, metavar='PATH', action='append', default=[],
61                             help="Mount all collections with tag TAG at mountpoint/PATH/TAG/UUID.")
62         mounts.add_argument('--mount-home',
63                             type=str, metavar='PATH', action='append', default=[],
64                             help="Mount the current user's home project at mountpoint/PATH.")
65         mounts.add_argument('--mount-shared',
66                             type=str, metavar='PATH', action='append', default=[],
67                             help="Mount projects shared with the current user at mountpoint/PATH.")
68         mounts.add_argument('--mount-tmp',
69                             type=str, metavar='PATH', action='append', default=[],
70                             help="Create a new collection, mount it in read/write mode at mountpoint/PATH, and delete it when unmounting.")
71
72         self.add_argument('--debug', action='store_true', help="""Debug mode""")
73         self.add_argument('--logfile', help="""Write debug logs and errors to the specified file (default stderr).""")
74         self.add_argument('--foreground', action='store_true', help="""Run in foreground (default is to daemonize unless --exec specified)""", default=False)
75         self.add_argument('--encoding', type=str, help="Character encoding to use for filesystem, default is utf-8 (see Python codec registry for list of available encodings)", default="utf-8")
76
77         self.add_argument('--file-cache', type=int, help="File data cache size, in bytes (default 256MiB)", default=256*1024*1024)
78         self.add_argument('--directory-cache', type=int, help="Directory data cache size, in bytes (default 128MiB)", default=128*1024*1024)
79
80         self.add_argument('--read-only', action='store_false', help="Mount will be read only (default)", dest="enable_write", default=False)
81         self.add_argument('--read-write', action='store_true', help="Mount will be read-write", dest="enable_write", default=False)
82
83         self.add_argument('--crunchstat-interval', type=float, help="Write stats to stderr every N seconds (default disabled)", default=0)
84
85         self.add_argument('--exec', type=str, nargs=argparse.REMAINDER,
86                             dest="exec_args", metavar=('command', 'args', '...', '--'),
87                             help="""Mount, run a command, then unmount and exit""")
88
89
90 class Mount(object):
91     def __init__(self, args, logger=logging.getLogger('arvados.arv-mount')):
92         self.logger = logger
93         self.args = args
94
95         self.args.mountpoint = os.path.realpath(self.args.mountpoint)
96         if self.args.logfile:
97             self.args.logfile = os.path.realpath(self.args.logfile)
98
99         try:
100             self._setup_logging()
101             self._setup_api()
102             self._setup_mount()
103         except Exception as e:
104             self.logger.exception("arv-mount: exception during setup: %s", e)
105             exit(1)
106
107     def __enter__(self):
108         llfuse.init(self.operations, self.args.mountpoint, self._fuse_options())
109         if self.args.mode != 'by_pdh':
110             self.operations.listen_for_events()
111         t = threading.Thread(None, lambda: llfuse.main())
112         t.start()
113         self.operations.initlock.wait()
114
115     def __exit__(self, exc_type, exc_value, traceback):
116         subprocess.call(["fusermount", "-u", "-z", self.args.mountpoint])
117         self.operations.destroy()
118
119     def run(self):
120         if self.args.exec_args:
121             self._run_exec()
122         else:
123             self._run_standalone()
124
125     def _fuse_options(self):
126         """FUSE mount options; see mount.fuse(8)"""
127         opts = [optname for optname in ['allow_other', 'debug']
128                 if getattr(self.args, optname)]
129         # Increase default read/write size from 4KiB to 128KiB
130         opts += ["big_writes", "max_read=131072"]
131         return opts
132
133     def _setup_logging(self):
134         # Configure a log handler based on command-line switches.
135         if self.args.logfile:
136             log_handler = logging.FileHandler(self.args.logfile)
137         else:
138             log_handler = None
139
140         if log_handler is not None:
141             arvados.logger.removeHandler(arvados.log_handler)
142             arvados.logger.addHandler(log_handler)
143
144         if self.args.debug:
145             arvados.logger.setLevel(logging.DEBUG)
146             self.logger.debug("arv-mount debugging enabled")
147
148         self.logger.info("enable write is %s", self.args.enable_write)
149
150     def _setup_api(self):
151         self.api = arvados.safeapi.ThreadSafeApiCache(
152             apiconfig=arvados.config.settings(),
153             keep_params={
154                 'block_cache': arvados.keep.KeepBlockCache(self.args.file_cache),
155                 'num_retries': self.args.retries,
156             })
157         # Do a sanity check that we have a working arvados host + token.
158         self.api.users().current().execute()
159
160     def _setup_mount(self):
161         self.operations = Operations(
162             os.getuid(),
163             os.getgid(),
164             api_client=self.api,
165             encoding=self.args.encoding,
166             inode_cache=InodeCache(cap=self.args.directory_cache),
167             enable_write=self.args.enable_write)
168
169         if self.args.crunchstat_interval:
170             statsthread = threading.Thread(
171                 target=crunchstat.statlogger,
172                 args=(self.args.crunchstat_interval,
173                       self.api.keep,
174                       self.operations))
175             statsthread.daemon = True
176             statsthread.start()
177
178         usr = self.api.users().current().execute(num_retries=self.args.retries)
179         now = time.time()
180         dir_class = None
181         dir_args = [llfuse.ROOT_INODE, self.operations.inodes, self.api, self.args.retries]
182         mount_readme = False
183
184         if self.args.collection is not None:
185             # Set up the request handler with the collection at the root
186             # First check that the collection is readable
187             self.api.collections().get(uuid=self.args.collection).execute()
188             self.args.mode = 'collection'
189             dir_class = CollectionDirectory
190             dir_args.append(self.args.collection)
191         elif self.args.project is not None:
192             self.args.mode = 'project'
193             dir_class = ProjectDirectory
194             dir_args.append(
195                 self.api.groups().get(uuid=self.args.project).execute(
196                     num_retries=self.args.retries))
197
198         if (self.args.mount_by_id or
199             self.args.mount_by_pdh or
200             self.args.mount_by_tag or
201             self.args.mount_home or
202             self.args.mount_shared or
203             self.args.mount_tmp):
204             if self.args.mode is not None:
205                 sys.exit(
206                     "Cannot combine '{}' mode with custom --mount-* options.".
207                     format(self.args.mode))
208         elif self.args.mode is None:
209             # If no --mount-custom or custom mount args, --all is the default
210             self.args.mode = 'all'
211
212         if self.args.mode in ['by_id', 'by_pdh']:
213             # Set up the request handler with the 'magic directory' at the root
214             dir_class = MagicDirectory
215             dir_args.append(self.args.mode == 'by_pdh')
216         elif self.args.mode == 'by_tag':
217             dir_class = TagsDirectory
218         elif self.args.mode == 'shared':
219             dir_class = SharedDirectory
220             dir_args.append(usr)
221         elif self.args.mode == 'home':
222             dir_class = ProjectDirectory
223             dir_args.append(usr)
224             dir_args.append(True)
225         elif self.args.mode == 'all':
226             self.args.mount_by_id = ['by_id']
227             self.args.mount_by_tag = ['by_tag']
228             self.args.mount_home = ['home']
229             self.args.mount_shared = ['shared']
230             mount_readme = True
231
232         if dir_class is not None:
233             self.operations.inodes.add_entry(dir_class(*dir_args))
234             return
235
236         e = self.operations.inodes.add_entry(Directory(
237             llfuse.ROOT_INODE, self.operations.inodes))
238         dir_args[0] = e.inode
239
240         for name in self.args.mount_by_id:
241             self._add_mount(e, name, MagicDirectory(*dir_args, pdh_only=False))
242         for name in self.args.mount_by_pdh:
243             self._add_mount(e, name, MagicDirectory(*dir_args, pdh_only=True))
244         for name in self.args.mount_by_tag:
245             self._add_mount(e, name, TagsDirectory(*dir_args))
246         for name in self.args.mount_home:
247             self._add_mount(e, name, ProjectDirectory(*dir_args, project_object=usr, poll=True))
248         for name in self.args.mount_shared:
249             self._add_mount(e, name, SharedDirectory(*dir_args, exclude=usr, poll=True))
250         for name in self.args.mount_tmp:
251             self._add_mount(e, name, TmpCollectionDirectory(*dir_args))
252
253         if mount_readme:
254             text = self._readme_text(
255                 arvados.config.get('ARVADOS_API_HOST'),
256                 usr['email'])
257             self._add_mount(e, 'README', StringFile(e.inode, text, now))
258
259     def _add_mount(self, tld, name, ent):
260         if name in ['', '.', '..'] or '/' in name:
261             sys.exit("Mount point '{}' is not supported.".format(name))
262         tld._entries[name] = self.operations.inodes.add_entry(ent)
263
264     def _readme_text(self, api_host, user_email):
265         return '''
266 Welcome to Arvados!  This directory provides file system access to
267 files and objects available on the Arvados installation located at
268 '{}' using credentials for user '{}'.
269
270 From here, the following directories are available:
271
272   by_id/     Access to Keep collections by uuid or portable data hash (see by_id/README for details).
273   by_tag/    Access to Keep collections organized by tag.
274   home/      The contents of your home project.
275   shared/    Projects shared with you.
276
277 '''.format(api_host, user_email)
278
279     def _run_exec(self):
280         # Initialize the fuse connection
281         llfuse.init(self.operations, self.args.mountpoint, self._fuse_options())
282
283         # Subscribe to change events from API server
284         if self.args.mode != 'by_pdh':
285             self.operations.listen_for_events()
286
287         t = threading.Thread(None, lambda: llfuse.main())
288         t.start()
289
290         # wait until the driver is finished initializing
291         self.operations.initlock.wait()
292
293         rc = 255
294         try:
295             sp = subprocess.Popen(self.args.exec_args, shell=False)
296
297             # forward signals to the process.
298             signal.signal(signal.SIGINT, lambda signum, frame: sp.send_signal(signum))
299             signal.signal(signal.SIGTERM, lambda signum, frame: sp.send_signal(signum))
300             signal.signal(signal.SIGQUIT, lambda signum, frame: sp.send_signal(signum))
301
302             # wait for process to complete.
303             rc = sp.wait()
304
305             # restore default signal handlers.
306             signal.signal(signal.SIGINT, signal.SIG_DFL)
307             signal.signal(signal.SIGTERM, signal.SIG_DFL)
308             signal.signal(signal.SIGQUIT, signal.SIG_DFL)
309         except Exception as e:
310             self.logger.exception(
311                 'arv-mount: exception during exec %s', self.args.exec_args)
312             try:
313                 rc = e.errno
314             except AttributeError:
315                 pass
316         finally:
317             subprocess.call(["fusermount", "-u", "-z", self.args.mountpoint])
318             self.operations.destroy()
319         exit(rc)
320
321     def _run_standalone(self):
322         try:
323             llfuse.init(self.operations, self.args.mountpoint, self._fuse_options())
324
325             if not (self.args.exec_args or self.args.foreground):
326                 self.daemon_ctx = daemon.DaemonContext(working_directory=os.path.dirname(self.args.mountpoint),
327                                                        files_preserve=range(3, resource.getrlimit(resource.RLIMIT_NOFILE)[1]))
328                 self.daemon_ctx.open()
329
330             # Subscribe to change events from API server
331             self.operations.listen_for_events()
332
333             llfuse.main()
334         except Exception as e:
335             self.logger.exception('arv-mount: exception during mount: %s', e)
336             exit(getattr(e, 'errno', 1))
337         finally:
338             self.operations.destroy()
339         exit(0)