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