11 from arvados_fuse import *
13 logger = logging.getLogger('arvados.arv-mount')
15 if __name__ == '__main__':
16 # Handle command line parameters
17 parser = argparse.ArgumentParser(
18 description='''Mount Keep data under the local filesystem. By default, if neither
19 --collection or --tags is specified, this mounts as a virtual directory
20 under which all Keep collections are available as subdirectories named
21 with the Keep locator; however directories will not be visible to 'ls'
22 until a program tries to access them.''',
24 Note: When using the --exec feature, you must either specify the
25 mountpoint before --exec, or mark the end of your --exec arguments
28 parser.add_argument('mountpoint', type=str, help="""Mount point.""")
29 parser.add_argument('--allow-other', action='store_true',
30 help="""Let other users read the mount""")
31 parser.add_argument('--collection', type=str, help="""Mount only the specified collection at the mount point.""")
32 parser.add_argument('--tags', action='store_true', help="""Mount as a virtual directory consisting of subdirectories representing tagged
33 collections on the server.""")
34 parser.add_argument('--groups', action='store_true', help="""Mount as a virtual directory consisting of subdirectories representing groups on the server.""")
35 parser.add_argument('--debug', action='store_true', help="""Debug mode""")
36 parser.add_argument('--logfile', help="""Write debug logs and errors to the specified file (default stderr).""")
37 parser.add_argument('--foreground', action='store_true', help="""Run in foreground (default is to daemonize unless --exec specified)""", default=False)
38 parser.add_argument('--exec', type=str, nargs=argparse.REMAINDER,
39 dest="exec_args", metavar=('command', 'args', '...', '--'),
40 help="""Mount, run a command, then unmount and exit""")
42 args = parser.parse_args()
43 args.mountpoint = os.path.realpath(args.mountpoint)
45 args.logfile = os.path.realpath(args.logfile)
47 # Daemonize as early as possible, so we don't accidentally close
48 # file descriptors we're using.
49 if not (args.exec_args or args.foreground):
50 os.chdir(args.mountpoint)
51 daemon_ctx = daemon.DaemonContext(working_directory='.')
56 # Configure a logger based on command-line switches.
57 # If we're using a contemporary Python SDK (mid-August 2014),
58 # configure the arvados hierarchy logger.
59 # Otherwise, configure the program root logger.
60 base_logger = getattr(arvados, 'logger', None)
63 log_handler = logging.FileHandler(args.logfile)
65 log_handler = logging.NullHandler()
67 log_handler = arvados.log_handler
69 log_handler = logging.StreamHandler()
71 if base_logger is None:
72 base_logger = logging.getLogger()
74 base_logger.removeHandler(arvados.log_handler)
75 base_logger.addHandler(log_handler)
78 base_logger.setLevel(logging.DEBUG)
79 logger.debug("arv-mount debugging enabled")
82 # Create the request handler
83 operations = Operations(os.getuid(), os.getgid())
84 api = arvados.api('v1')
87 e = operations.inodes.add_entry(GroupsDirectory(llfuse.ROOT_INODE, operations.inodes, api))
89 e = operations.inodes.add_entry(TagsDirectory(llfuse.ROOT_INODE, operations.inodes, api))
90 elif args.collection != None:
91 # Set up the request handler with the collection at the root
92 e = operations.inodes.add_entry(CollectionDirectory(llfuse.ROOT_INODE, operations.inodes, args.collection))
94 # Set up the request handler with the 'magic directory' at the root
95 operations.inodes.add_entry(MagicDirectory(llfuse.ROOT_INODE, operations.inodes))
97 logger.exception("arv-mount: exception during API setup")
100 # FUSE options, see mount.fuse(8)
101 opts = [optname for optname in ['allow_other', 'debug']
102 if getattr(args, optname)]
105 # Initialize the fuse connection
106 llfuse.init(operations, args.mountpoint, opts)
108 t = threading.Thread(None, lambda: llfuse.main())
111 # wait until the driver is finished initializing
112 operations.initlock.wait()
116 sp = subprocess.Popen(args.exec_args, shell=False)
118 # forward signals to the process.
119 signal.signal(signal.SIGINT, lambda signum, frame: sp.send_signal(signum))
120 signal.signal(signal.SIGTERM, lambda signum, frame: sp.send_signal(signum))
121 signal.signal(signal.SIGQUIT, lambda signum, frame: sp.send_signal(signum))
123 # wait for process to complete.
126 # restore default signal handlers.
127 signal.signal(signal.SIGINT, signal.SIG_DFL)
128 signal.signal(signal.SIGTERM, signal.SIG_DFL)
129 signal.signal(signal.SIGQUIT, signal.SIG_DFL)
130 except Exception as e:
131 logger.exception('arv-mount: exception during exec %s',
135 except AttributeError:
138 subprocess.call(["fusermount", "-u", "-z", args.mountpoint])
143 llfuse.init(operations, args.mountpoint, opts)
145 except Exception as e:
146 logger.exception('arv-mount: exception during mount')
147 exit(getattr(e, 'errno', 1))