]> git.arvados.org - arvados.git/blob - sdk/python/bin/arv-mount
Improved fresh/stale handling with base class, added property fuse inode cache
[arvados.git] / sdk / python / 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('--collection', type=str, help="""Mount only the specified collection at the mount point.""")
24     parser.add_argument('--tags', action='store_true', help="""Mount as a virtual directory consisting of subdirectories representing tagged
25 collections on the server.""")
26     parser.add_argument('--groups', action='store_true', help="""Mount as a virtual directory consisting of subdirectories representing groups on the server.""")
27     parser.add_argument('--debug', action='store_true', help="""Debug mode""")
28     parser.add_argument('--foreground', action='store_true', help="""Run in foreground (default is to daemonize unless --exec specified)""", default=False)
29     parser.add_argument('--exec', type=str, nargs=argparse.REMAINDER,
30                         dest="exec_args", metavar=('command', 'args', '...', '--'),
31                         help="""Mount, run a command, then unmount and exit""")
32
33     args = parser.parse_args()
34
35     # Create the request handler
36     operations = Operations(os.getuid(), os.getgid())
37
38     if args.groups:
39         api = arvados.api('v1')
40         e = operations.inodes.add_entry(GroupsDirectory(llfuse.ROOT_INODE, operations.inodes, api))
41     elif args.tags:
42         api = arvados.api('v1')
43         e = operations.inodes.add_entry(TagsDirectory(llfuse.ROOT_INODE, operations.inodes, api))
44     elif args.collection != None:
45         # Set up the request handler with the collection at the root
46         e = operations.inodes.add_entry(CollectionDirectory(llfuse.ROOT_INODE, operations.inodes, args.collection))
47     else:
48         # Set up the request handler with the 'magic directory' at the root
49         operations.inodes.add_entry(MagicDirectory(llfuse.ROOT_INODE, operations.inodes))
50
51     # FUSE options, see mount.fuse(8)
52     opts = []
53
54     # Enable FUSE debugging (logs each FUSE request)
55     if args.debug:
56         opts += ['debug']
57
58     if args.exec_args:
59         # Initialize the fuse connection
60         llfuse.init(operations, args.mountpoint, opts)
61
62         t = threading.Thread(None, lambda: llfuse.main())
63         t.start()
64
65         # wait until the driver is finished initializing
66         operations.initlock.wait()
67
68         rc = 255
69         try:
70             rc = subprocess.call(args.exec_args, shell=False)
71         except OSError as e:
72             sys.stderr.write('arv-mount: %s -- exec %s\n' % (str(e), args.exec_args))
73             rc = e.errno
74         except Exception as e:
75             sys.stderr.write('arv-mount: %s\n' % str(e))
76         finally:
77             subprocess.call(["fusermount", "-u", "-z", args.mountpoint])
78
79         exit(rc)
80     else:
81         if args.foreground:
82             # Initialize the fuse connection
83             llfuse.init(operations, args.mountpoint, opts)
84             llfuse.main()
85         else:
86             with daemon.DaemonContext():
87                 # Initialize the fuse connection
88                 llfuse.init(operations, args.mountpoint, opts)
89                 llfuse.main()