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