"filters" is now propagated through from pipeline component to the job
[arvados.git] / services / fuse / bin / arv-mount
1 #!/usr/bin/env python
2
3 import argparse
4 import arvados
5 import daemon
6 import os
7 import signal
8 import subprocess
9
10 from arvados_fuse import *
11
12 if __name__ == '__main__':
13     # Handle command line parameters
14     parser = argparse.ArgumentParser(
15         description='''Mount Keep data under the local filesystem.  By default, if neither
16         --collection or --tags is specified, this mounts as a virtual directory
17         under which all Keep collections are available as subdirectories named
18         with the Keep locator; however directories will not be visible to 'ls'
19         until a program tries to access them.''',
20         epilog="""
21 Note: When using the --exec feature, you must either specify the
22 mountpoint before --exec, or mark the end of your --exec arguments
23 with "--".
24 """)
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""")
28     parser.add_argument('--collection', type=str, help="""Mount only the specified collection at the mount point.""")
29     parser.add_argument('--tags', action='store_true', help="""Mount as a virtual directory consisting of subdirectories representing tagged
30 collections on the server.""")
31     parser.add_argument('--groups', action='store_true', help="""Mount as a virtual directory consisting of subdirectories representing groups on the server.""")
32     parser.add_argument('--debug', action='store_true', help="""Debug mode""")
33     parser.add_argument('--foreground', action='store_true', help="""Run in foreground (default is to daemonize unless --exec specified)""", default=False)
34     parser.add_argument('--exec', type=str, nargs=argparse.REMAINDER,
35                         dest="exec_args", metavar=('command', 'args', '...', '--'),
36                         help="""Mount, run a command, then unmount and exit""")
37
38     args = parser.parse_args()
39
40     # Create the request handler
41     operations = Operations(os.getuid(), os.getgid(), args.debug)
42
43     if args.debug:
44         arvados.config.settings()['ARVADOS_DEBUG'] = 'true'
45
46     try:
47         api = arvados.api('v1')
48
49         if args.groups:
50             e = operations.inodes.add_entry(GroupsDirectory(llfuse.ROOT_INODE, operations.inodes, api))
51         elif args.tags:
52             e = operations.inodes.add_entry(TagsDirectory(llfuse.ROOT_INODE, operations.inodes, api))
53         elif args.collection != None:
54             # Set up the request handler with the collection at the root
55             e = operations.inodes.add_entry(CollectionDirectory(llfuse.ROOT_INODE, operations.inodes, args.collection))
56         else:
57             # Set up the request handler with the 'magic directory' at the root
58             operations.inodes.add_entry(MagicDirectory(llfuse.ROOT_INODE, operations.inodes))
59     except Exception as ex:
60         print("arv-mount: %s" % ex)
61         exit(1)
62
63     # FUSE options, see mount.fuse(8)
64     opts = [optname for optname in ['allow_other', 'debug']
65             if getattr(args, optname)]
66
67     if args.exec_args:
68         # Initialize the fuse connection
69         llfuse.init(operations, args.mountpoint, opts)
70
71         t = threading.Thread(None, lambda: llfuse.main())
72         t.start()
73
74         # wait until the driver is finished initializing
75         operations.initlock.wait()
76
77         rc = 255
78         try:
79             sp = subprocess.Popen(args.exec_args, shell=False)
80
81             # forward signals to the process.
82             signal.signal(signal.SIGINT, lambda signum, frame: sp.send_signal(signum))
83             signal.signal(signal.SIGTERM, lambda signum, frame: sp.send_signal(signum))
84             signal.signal(signal.SIGQUIT, lambda signum, frame: sp.send_signal(signum))
85
86             # wait for process to complete.
87             rc = sp.wait()
88
89             # restore default signal handlers.
90             signal.signal(signal.SIGINT, signal.SIG_DFL)
91             signal.signal(signal.SIGTERM, signal.SIG_DFL)
92             signal.signal(signal.SIGQUIT, signal.SIG_DFL)
93         except OSError as e:
94             sys.stderr.write('arv-mount: %s -- exec %s\n' % (str(e), args.exec_args))
95             rc = e.errno
96         except Exception as e:
97             sys.stderr.write('arv-mount: %s\n' % str(e))
98         finally:
99             subprocess.call(["fusermount", "-u", "-z", args.mountpoint])
100
101         exit(rc)
102     else:
103         os.chdir(args.mountpoint)
104         if not args.foreground:
105             daemon_ctx = daemon.DaemonContext(working_directory='.')
106             daemon_ctx.open()
107         llfuse.init(operations, '.', opts)
108         llfuse.main()