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