Merge remote-tracking branch 'origin/master' into 2049-run-job-from-workbench
[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
7 if __name__ == '__main__':
8     api = arvados.api()
9
10     # Handle command line parameters
11     parser = argparse.ArgumentParser(
12         description='Mount Keep data under the local filesystem.',
13         epilog="""
14 Note: When using the --exec feature, you must either specify the
15 mountpoint before --exec, or mark the end of your --exec arguments
16 with "--".
17 """)
18     parser.add_argument('mountpoint', type=str, help="""Mount point.""")
19     parser.add_argument('--collection', type=str, help="""Collection locator""")
20     parser.add_argument('--debug', action='store_true', help="""Debug mode""")
21     parser.add_argument('--exec', type=str, nargs=argparse.REMAINDER,
22                         dest="exec_args", metavar=('command', 'args', '...', '--'),
23                         help="""Mount, run a command, then unmount and exit""")
24
25     args = parser.parse_args()
26
27     # Create the request handler
28     operations = Operations(os.getuid(), os.getgid())
29
30     if args.collection != None:
31         # Set up the request handler with the collection at the root
32         e = operations.inodes.add_entry(Directory(llfuse.ROOT_INODE))
33         operations.inodes.load_collection(e, arvados.CollectionReader(arvados.Keep.get(args.collection)))
34     else:
35         # Set up the request handler with the 'magic directory' at the root
36         operations.inodes.add_entry(MagicDirectory(llfuse.ROOT_INODE, operations.inodes))
37
38     # FUSE options, see mount.fuse(8)
39     opts = []
40
41     # Enable FUSE debugging (logs each FUSE request)
42     if args.debug:
43         opts += ['debug']    
44     
45     # Initialize the fuse connection
46     llfuse.init(operations, args.mountpoint, opts)
47
48     if args.exec_args:
49         t = threading.Thread(None, lambda: llfuse.main())
50         t.start()
51
52         # wait until the driver is finished initializing
53         operations.initlock.wait()
54
55         try:
56             rc = subprocess.call(args.exec_args, shell=False)
57         except:
58             rc = 255
59         finally:
60             subprocess.call(["fusermount", "-u", "-z", args.mountpoint])
61
62         exit(rc)
63     else:
64         llfuse.main()