Merge branch 'master' into 7255-manifests-in-datamanager
[arvados.git] / services / fuse / bin / arv-mount
1 #!/usr/bin/env python
2
3 import argparse
4 import arvados
5 import daemon
6 import logging
7 import os
8 import signal
9 import subprocess
10 import time
11
12 import arvados.commands._util as arv_cmd
13 from arvados_fuse import *
14 from arvados.safeapi import ThreadSafeApiCache
15 import arvados.keep
16
17 logger = logging.getLogger('arvados.arv-mount')
18
19 class Stat(object):
20     def __init__(self, prefix, interval,
21                  egr_name, ing_name,
22                  egr_func, ing_func):
23         self.prefix = prefix
24         self.interval = interval
25         self.egr_name = egr_name
26         self.ing_name = ing_name
27         self.egress = egr_func
28         self.ingress = ing_func
29         self.egr_prev = self.egress()
30         self.ing_prev = self.ingress()
31
32     def update(self):
33         egr = self.egress()
34         ing = self.ingress()
35
36         delta = " -- interval %.4f seconds %d %s %d %s" % (self.interval,
37                                                            egr - self.egr_prev,
38                                                            self.egr_name,
39                                                            ing - self.ing_prev,
40                                                            self.ing_name)
41
42         sys.stderr.write("crunchstat: %s %d %s %d %s%s\n" % (self.prefix,
43                                                              egr,
44                                                              self.egr_name,
45                                                              ing,
46                                                              self.ing_name,
47                                                              delta))
48
49         self.egr_prev = egr
50         self.ing_prev = ing
51
52
53 def statlogger(interval, keep, ops):
54     calls = Stat("keepcalls", interval, "put", "get",
55                  keep.put_counter.get,
56                  keep.get_counter.get)
57     net = Stat("net:keep0", interval, "tx", "rx",
58                keep.upload_counter.get,
59                keep.download_counter.get)
60     cache = Stat("keepcache", interval, "hit", "miss",
61                keep.hits_counter.get,
62                keep.misses_counter.get)
63     fuseops = Stat("fuseops", interval,"write", "read",
64                    ops.write_ops_counter.get,
65                    ops.read_ops_counter.get)
66     blk = Stat("blkio:0:0", interval, "write", "read",
67                ops.write_counter.get,
68                ops.read_counter.get)
69
70     while True:
71         time.sleep(interval)
72         calls.update()
73         net.update()
74         cache.update()
75         fuseops.update()
76         blk.update()
77
78
79 if __name__ == '__main__':
80     # Handle command line parameters
81     parser = argparse.ArgumentParser(
82         parents=[arv_cmd.retry_opt],
83         description='''Mount Keep data under the local filesystem.  Default mode is --home''',
84         epilog="""
85 Note: When using the --exec feature, you must either specify the
86 mountpoint before --exec, or mark the end of your --exec arguments
87 with "--".
88 """)
89     parser.add_argument('mountpoint', type=str, help="""Mount point.""")
90     parser.add_argument('--allow-other', action='store_true',
91                         help="""Let other users read the mount""")
92
93     mount_mode = parser.add_mutually_exclusive_group()
94
95     mount_mode.add_argument('--all', action='store_true', help="""Mount a subdirectory for each mode: home, shared, by_tag, by_id (default).""")
96     mount_mode.add_argument('--home', action='store_true', help="""Mount only the user's home project.""")
97     mount_mode.add_argument('--shared', action='store_true', help="""Mount only list of projects shared with the user.""")
98     mount_mode.add_argument('--by-tag', action='store_true',
99                             help="""Mount subdirectories listed by tag.""")
100     mount_mode.add_argument('--by-id', action='store_true',
101                             help="""Mount subdirectories listed by portable data hash or uuid.""")
102     mount_mode.add_argument('--by-pdh', action='store_true',
103                             help="""Mount subdirectories listed by portable data hash.""")
104     mount_mode.add_argument('--project', type=str, help="""Mount a specific project.""")
105     mount_mode.add_argument('--collection', type=str, help="""Mount only the specified collection.""")
106
107     parser.add_argument('--debug', action='store_true', help="""Debug mode""")
108     parser.add_argument('--logfile', help="""Write debug logs and errors to the specified file (default stderr).""")
109     parser.add_argument('--foreground', action='store_true', help="""Run in foreground (default is to daemonize unless --exec specified)""", default=False)
110     parser.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")
111
112     parser.add_argument('--file-cache', type=int, help="File data cache size, in bytes (default 256MiB)", default=256*1024*1024)
113     parser.add_argument('--directory-cache', type=int, help="Directory data cache size, in bytes (default 128MiB)", default=128*1024*1024)
114
115     parser.add_argument('--read-only', action='store_false', help="Mount will be read only (default)", dest="enable_write", default=False)
116     parser.add_argument('--read-write', action='store_true', help="Mount will be read-write", dest="enable_write", default=False)
117
118     parser.add_argument('--crunchstat-interval', type=float, help="Write stats to stderr every N seconds (default disabled)", default=0)
119
120     parser.add_argument('--exec', type=str, nargs=argparse.REMAINDER,
121                         dest="exec_args", metavar=('command', 'args', '...', '--'),
122                         help="""Mount, run a command, then unmount and exit""")
123
124     args = parser.parse_args()
125     args.mountpoint = os.path.realpath(args.mountpoint)
126     if args.logfile:
127         args.logfile = os.path.realpath(args.logfile)
128
129     # Daemonize as early as possible, so we don't accidentally close
130     # file descriptors we're using.
131     if not (args.exec_args or args.foreground):
132         os.chdir(args.mountpoint)
133         daemon_ctx = daemon.DaemonContext(working_directory='.')
134         daemon_ctx.open()
135     else:
136         daemon_ctx = None
137
138     # Configure a log handler based on command-line switches.
139     if args.logfile:
140         log_handler = logging.FileHandler(args.logfile)
141     elif daemon_ctx:
142         log_handler = logging.NullHandler()
143     else:
144         log_handler = None
145
146     if log_handler is not None:
147         arvados.logger.removeHandler(arvados.log_handler)
148         arvados.logger.addHandler(log_handler)
149
150     if args.debug:
151         arvados.logger.setLevel(logging.DEBUG)
152         logger.debug("arv-mount debugging enabled")
153
154     logger.info("enable write is %s", args.enable_write)
155
156     try:
157         # Create the request handler
158         operations = Operations(os.getuid(),
159                                 os.getgid(),
160                                 encoding=args.encoding,
161                                 inode_cache=InodeCache(cap=args.directory_cache),
162                                 enable_write=args.enable_write)
163         api = ThreadSafeApiCache(apiconfig=arvados.config.settings(),
164                                  keep_params={"block_cache": arvados.keep.KeepBlockCache(args.file_cache)})
165
166         if args.crunchstat_interval:
167             statsthread = threading.Thread(target=statlogger, args=(args.crunchstat_interval, api.keep, operations))
168             statsthread.daemon = True
169             statsthread.start()
170
171         usr = api.users().current().execute(num_retries=args.retries)
172         now = time.time()
173         dir_class = None
174         dir_args = [llfuse.ROOT_INODE, operations.inodes, api, args.retries]
175         if args.by_id or args.by_pdh:
176             # Set up the request handler with the 'magic directory' at the root
177             dir_class = MagicDirectory
178             dir_args.append(args.by_pdh)
179         elif args.by_tag:
180             dir_class = TagsDirectory
181         elif args.shared:
182             dir_class = SharedDirectory
183             dir_args.append(usr)
184         elif args.home:
185             dir_class = ProjectDirectory
186             dir_args.append(usr)
187             dir_args.append(True)
188         elif args.collection is not None:
189             # Set up the request handler with the collection at the root
190             dir_class = CollectionDirectory
191             dir_args.append(args.collection)
192         elif args.project is not None:
193             dir_class = ProjectDirectory
194             dir_args.append(api.groups().get(uuid=args.project).execute(
195                     num_retries=args.retries))
196
197         if dir_class is not None:
198             operations.inodes.add_entry(dir_class(*dir_args))
199         else:
200             e = operations.inodes.add_entry(Directory(llfuse.ROOT_INODE, operations.inodes))
201             dir_args[0] = e.inode
202
203             e._entries['by_id'] = operations.inodes.add_entry(MagicDirectory(*dir_args))
204
205             e._entries['by_tag'] = operations.inodes.add_entry(TagsDirectory(*dir_args))
206
207             dir_args.append(usr)
208             dir_args.append(True)
209             e._entries['home'] = operations.inodes.add_entry(ProjectDirectory(*dir_args))
210             e._entries['shared'] = operations.inodes.add_entry(SharedDirectory(*dir_args))
211
212             text = '''
213 Welcome to Arvados!  This directory provides file system access to files and objects
214 available on the Arvados installation located at '{}'
215 using credentials for user '{}'.
216
217 From here, the following directories are available:
218
219   by_id/     Access to Keep collections by uuid or portable data hash (see by_id/README for details).
220   by_tag/    Access to Keep collections organized by tag.
221   home/      The contents of your home project.
222   shared/    Projects shared with you.
223 '''.format(arvados.config.get('ARVADOS_API_HOST'), usr['email'])
224
225             e._entries["README"] = operations.inodes.add_entry(StringFile(e.inode, text, now))
226
227
228     except Exception:
229         logger.exception("arv-mount: exception during API setup")
230         exit(1)
231
232     # FUSE options, see mount.fuse(8)
233     opts = [optname for optname in ['allow_other', 'debug']
234             if getattr(args, optname)]
235
236     # Increase default read/write size from 4KiB to 128KiB
237     opts += ["big_writes", "max_read=131072"]
238
239     if args.exec_args:
240         # Initialize the fuse connection
241         llfuse.init(operations, args.mountpoint, opts)
242
243         # Subscribe to change events from API server
244         if not args.by_pdh:
245             operations.listen_for_events(api)
246
247         t = threading.Thread(None, lambda: llfuse.main())
248         t.start()
249
250         # wait until the driver is finished initializing
251         operations.initlock.wait()
252
253         rc = 255
254         try:
255             sp = subprocess.Popen(args.exec_args, shell=False)
256
257             # forward signals to the process.
258             signal.signal(signal.SIGINT, lambda signum, frame: sp.send_signal(signum))
259             signal.signal(signal.SIGTERM, lambda signum, frame: sp.send_signal(signum))
260             signal.signal(signal.SIGQUIT, lambda signum, frame: sp.send_signal(signum))
261
262             # wait for process to complete.
263             rc = sp.wait()
264
265             # restore default signal handlers.
266             signal.signal(signal.SIGINT, signal.SIG_DFL)
267             signal.signal(signal.SIGTERM, signal.SIG_DFL)
268             signal.signal(signal.SIGQUIT, signal.SIG_DFL)
269         except Exception as e:
270             logger.exception('arv-mount: exception during exec %s',
271                              args.exec_args)
272             try:
273                 rc = e.errno
274             except AttributeError:
275                 pass
276         finally:
277             subprocess.call(["fusermount", "-u", "-z", args.mountpoint])
278             operations.destroy()
279
280         exit(rc)
281     else:
282         try:
283             llfuse.init(operations, args.mountpoint, opts)
284
285             # Subscribe to change events from API server
286             operations.listen_for_events(api)
287
288             llfuse.main()
289         except Exception as e:
290             logger.exception('arv-mount: exception during mount')
291             exit(getattr(e, 'errno', 1))
292         finally:
293             operations.destroy()