-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalveofs.py
executable file
·126 lines (99 loc) · 4.49 KB
/
alveofs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
import sys
import logging
import argparse
import requests
import six
from fuse import FUSE, Operations, FuseOSError, ENOENT
from parser import Directory, File
class AlveoFS(Operations):
def __init__(self, root, x_api_key, verify_ssl=True):
self.root = root
self.x_api_key = x_api_key
self.log = logging.getLogger(__name__)
self.readdir_cache = {}
self.attr_cache = {}
self.file_cache = {}
# append headers
s = requests.Session()
s.headers.update({'X-API-Key': x_api_key, 'Accept': 'application/json'})
# s.headers.update({'X-API-Key': 'fxss5G7NxD472koixm7r', 'Accept': 'application/json'})
self.session = s
if not verify_ssl:
self.log.warn("Disabling SSL verification!")
self.session.verify = False
def readdir(self, path, fh):
path = path.strip("/")
path = six.text_type(path)
self.log.debug(u"[READDIR] Reading path {}".format(path))
if path not in self.readdir_cache.keys():
self.readdir_cache[path] = Directory(self.root, path, self.session).contents()
return [x[0] for x in self.readdir_cache[path]]
def read(self, path, length, offset, fh):
path = path.strip("/")
path = six.text_type(path)
self.log.debug(u"[READ] Reading path {}, {} bytes from {}".format(path, length, offset))
if path not in self.file_cache.keys():
self.file_cache[path] = File(self.root, path, self, self.session)
return self.file_cache[path].read(length, offset)
def getattr(self, path, fh=None):
path = path.strip("/")
path = six.text_type(path)
self.log.debug(u"[GETATTR] Path {}".format(path))
if path not in self.attr_cache.keys():
try:
if path not in self.file_cache.keys():
self.file_cache[path] = File(self.root, path, self, self.session)
self.attr_cache[path] = self.file_cache[path].attributes()
except FuseOSError:
self.attr_cache[path] = None
raise FuseOSError(ENOENT)
if self.attr_cache[path] is not None:
return self.attr_cache[path]
else:
raise FuseOSError(ENOENT)
# Disable unused operations:
access = None
flush = None
getxattr = None
listxattr = None
open = None
opendir = None
release = None
releasedir = None
statfs = None
if __name__ == '__main__':
FORMAT = "%(created)f - %(thread)d (%(name)s) - [%(levelname)s] %(message)s"
logging.basicConfig(level=logging.INFO, format=FORMAT)
p = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
p.add_argument("http_resource", help="Target web directory index")
p.add_argument("mountpoint", help="Target directory")
p.add_argument("x-api-key", help="Alveo API key")
p.add_argument("--foreground", action="store_true", help="Do not fork into background")
p.add_argument("--debug", action="store_true", help="Enable debug logging")
p.add_argument("--nothreads", action="store_true", help="Disable fuse threads")
p.add_argument("--no_ssl_verify", action="store_true", help="Disable SSL Verification")
p.add_argument("--allow_other", action="store_true", help="Allow users other than the one running the command ")
p.add_argument("-o", "--options", type=str, default="", help="Mount-style variant of the above options "
"(e.g. -o debug,allow_other")
args = vars(p.parse_args(sys.argv[1:]))
fsroot = six.text_type(args.pop("http_resource").strip("/"))
mountpoint = args.pop("mountpoint")
x_api_key = args.pop("x-api-key")
fuse_kwargs = {
'nothreads': True if args.pop("nothreads") else False,
'foreground': True if args.pop("foreground") else False,
'debug': True if args.pop("debug") else False,
'allow_other': True if args.pop("allow_other") else False,
}
# o_args_list = [x.strip() for x in args.pop("o").split(",")]
# o_args = {}
# for x in o_args_list:
# xs = [y.strip() for y in x.split("=")]
# if len(xs) > 1:
# fuse_kwargs[xs[0]] = xs[1:]
# else:
# fuse_kwargs[x] = True
if fuse_kwargs['debug']:
logging.basicConfig(level=logging.DEBUG, format=FORMAT)
FUSE(AlveoFS(fsroot, x_api_key, verify_ssl=False if args.pop("no_ssl_verify") else True), mountpoint, **fuse_kwargs)