-
Notifications
You must be signed in to change notification settings - Fork 6
/
cloudstorage.py
executable file
·370 lines (291 loc) · 11.4 KB
/
cloudstorage.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/env python
import os
import sys
import stat
import errno
import logging
import StringIO
try:
import _find_fuse_parts
except ImportError:
pass
import fuse
from libcloud.common.types import InvalidCredsError
from libcloud.storage.types import (Provider,
ContainerDoesNotExistError,
ObjectDoesNotExistError,
)
from libcloud.storage.providers import get_driver
import libcloud.security
libcloud.security.VERIFY_SSL_CERT = True
fuse.fuse_python_api = (0, 2)
write_cache = {}
class CloudStat(fuse.Stat):
def __init__(self):
self.st_mode = 0
self.st_ino = 0
self.st_dev = 0
self.st_nlink = 0
self.st_uid = 0
self.st_gid = 0
self.st_size = 0
self.st_atime = 0
self.st_mtime = 0
self.st_ctime = 0
class CloudStorageFS(fuse.Fuse):
_storage_handle = None
_objects_to_create = []
def __init__(self, *args, **kwargs):
fuse.Fuse.__init__(self, *args, **kwargs)
logging.basicConfig(filename='storage.log', level=logging.DEBUG)
logging.debug("Starting CloudStorageFS")
def make_connection(self):
CloudFiles = get_driver(getattr(Provider, self.driver))
self._storage_handle = CloudFiles(self.access_id, self.secret)
@property
def storage_handle(self):
if not self._storage_handle:
self.make_connection
return self._storage_handle
def _read_container_names(self):
return [container.name for container in
self.storage_handle.list_containers()]
def _get_object(self, path_tokens):
"""Return an object instance from path_tokens (i.e. result
of path.split('/') or None if object doesn't exist"""
container_name, object_name = path_tokens[1], path_tokens[2]
try:
container = self.storage_handle.get_container(container_name)
return container.get_object(object_name)
except (ContainerDoesNotExistError, ObjectDoesNotExistError):
return None
def getattr(self, path):
logging.debug("getattr(path='%s')" % path)
st = CloudStat()
if path == '/':
st.st_mode = stat.S_IFDIR | 0755
st.st_nlink = 2
return st
elif path in self._objects_to_create:
logging.debug("getattr(path='%s'): file is scheduled for creation" % (path))
st.st_mode = stat.S_IFREG | 0644
st.st_nlink = 1
st.st_size = 0
return st
path_tokens = path.split('/')
if 2 == len(path_tokens):
container_names = self._read_container_names()
if path_tokens[1] in container_names:
st.st_mode = stat.S_IFDIR | 0755
st.st_nlink = 2
return st
else:
return -errno.ENOENT
elif 3 == len(path_tokens):
obj = self._get_object(path_tokens)
if obj:
st.st_mode = stat.S_IFREG | 0444
st.st_nlink = 1
st.st_size = obj.size
else:
# getattr() might be called for a new file which doesn't
# exist yet, so we need to make it writable in such case
#st.st_mode = stat.S_IFREG | 0644
#st.st_nlink = 1
#st.st_size = 0
return -errno.ENOENT
return st
return -errno.ENOENT
def readdir(self, path, offset):
logging.debug("readdir(path='%s', offset='%s')" % (path, offset))
if "/" == path:
try:
container_names = [str(container.name) for container in
self.storage_handle.list_containers()]
logging.debug("container names = %s" % container_names)
dirs = [".", ".."] + container_names
logging.debug("dirs = %s" % dirs)
for r in dirs:
logging.debug("yielding %s" % r)
yield fuse.Direntry(r)
#return dirs
except Exception:
logging.exception("exception in readdir()")
else:
path_tokens = path.split("/")
if 2 != len(path_tokens):
# we should only have 1 level depth
logging.warning("Path '%s' is deeper than it should" % path)
return
try:
container_name = path_tokens[1]
container = self.storage_handle.get_container(container_name)
dirs = [".", ".."] + [str(obj.name) for obj in container.list_objects()]
logging.debug("dirs = %s" % dirs)
for r in dirs:
yield fuse.Direntry(r)
except Exception:
logging.exception("exception while trying to list container objects")
def mkdir(self, path, mode):
logging.debug("mkdir(path='%s', mode='%s')" % (path, mode))
path_tokens = path.split('/')
if 2 != len(path_tokens):
logging.warning("attempting to create a non-container dir %s" % path)
return -errno.EOPNOTSUPP
container_name = path_tokens[1]
self.storage_handle.create_container(container_name)
return 0
def rmdir(self, path):
logging.debug("rmdir(path='%s')" % (path,))
path_tokens = path.split('/')
if 1 == len(path_tokens):
return -errno.EPERM
elif 2 == len(path_tokens):
container_name = path_tokens[1]
try:
container = self.storage_handle.get_container(container_name)
except ContainerDoesNotExistError:
return -errno.ENOENT
if 0 != len(container.list_objects()):
return -errno.ENOTEMPTY
container.delete()
return 0
elif 3 <= len(path_tokens):
return -errno.EOPNOTSUPP
def mknod(self, path, mode, dev):
logging.debug("mknod(path='%s', mode='%s', dev='%s')" % (path, mode, dev))
try:
path_tokens = path.split('/')
if 3 != len(path_tokens):
return -errno.EPERM
container_name = path_tokens[1]
object_name = path_tokens[2]
self.storage_handle.upload_object_via_stream(StringIO.StringIO('\n'),
self.storage_handle.get_container(container_name),
object_name,
extra={"content_type": "application/octet-stream"})
return 0
except Exception:
logging.exception("exception in mknod()")
def open(self, path, flags):
logging.debug("open(path='%s', flags='%s')" % (path, flags))
return 0
path_tokens = path.split('/')
if 3 != len(path_tokens):
logging.warning("path_tokens != 3")
return -errno.EOPNOTSUPP
try:
# obj = self._get_object(path_tokens)
# # we allow opening existing files in read-only mode
# if obj:
# accmode = os.O_RDONLY | os.O_WRONLY | os.O_RDWR
# if (flags & accmode) != os.O_RDONLY:
# return -errno.EACCES
return 0
except Exception:
logging.exception("exception in open()")
def read(self, path, size, offset):
logging.debug("read(path='%s', size=%s, offset=%s)" % (path, size, offset))
path_tokens = path.split('/')
if 3 != len(path_tokens):
return -errno.EOPNOTSUPP
container_name, object_name = path_tokens[1], path_tokens[2]
try:
container = self.storage_handle.get_container(container_name)
obj = container.get_object(object_name)
except (ContainerDoesNotExistError, ObjectDoesNotExistError):
return -errno.ENOENT
try:
content = ''.join([line for line in obj.as_stream()])
except:
logging.exception("error reading file content")
return
slen = len(content)
if offset < slen:
if offset + size > slen:
size = slen - offset
response = content[offset:offset+size]
else:
response = ''
return response
def write(self, path, buff, offset):
# as cloudstorage does not provide object
# modification facilities, we need to delete an old one
# and create a new one with the old content
logging.debug("write(path='%s', buff=<skip>, offset='%s')" % (path, offset))
try:
if path not in write_cache:
write_cache[path] = [buff,]
else:
write_cache.append(buff)
return len(buff)
except Exception:
logging.exception("exception in write()")
def unlink(self, path):
logging.debug("unlink(path='%s')" % (path,))
try:
path_tokens = path.split('/')
if 3 != len(path_tokens):
return
obj = self._get_object(path_tokens)
if not obj:
return -errno.ENOENT
obj.delete()
return 0
except Exception:
logging.exception("error while processing unlink()")
def release(self, path, flags):
logging.debug("release(path='%s', flags='%s')" % (path, flags))
# XXX: what's the nature of this?
if "-" == path:
return 0
try:
path_tokens = path.split("/")
container_name, object_name = path_tokens[1], path_tokens[2]
if len(write_cache[path]) > 0:
self.unlink(path)
self.storage_handle.upload_object_via_stream(StringIO.StringIO(''.join(write_cache[path])),
self.storage_handle.get_container(container_name),
object_name,
extra={"content_type": "application/octet-stream"})
del write_cache[path]
return 0
except KeyError:
logging.warning("no cached entry for path: %s" % path)
return 0
except Exception:
logging.exception("exception in release()")
def truncate(self, path, size):
return 0
def utime(self, path, times):
return 0
def fsync(self, path, isfsyncfile):
return 0
def main():
usage="""
cloud storage filesystem
""" + fuse.Fuse.fusage
server = CloudStorageFS(version="%prog " + fuse.__version__,
usage=usage,
dash_s_do='setsingle')
server.parser.add_option(mountopt='driver', metavar="DRIVER",
help=("Cloud storage driver to use.\n"
"Supported values: %s") % ' '.join([attr for attr in dir(Provider)
if attr.isupper()]))
server.parser.add_option(mountopt='access_id', metavar='ACCESS_ID',
help=("Access id, i.e. account id or name"))
server.parser.add_option(mountopt='secret', metavar='SECRET',
help=("Account secret key or password"))
server.parse(values=server, errex=1)
if not (hasattr(server, 'driver') and hasattr(server, 'access_id') and \
hasattr(server, 'secret')):
print >>sys.stderr, "Please specify driver, access_id and secret."
sys.exit(1)
try:
server.make_connection()
except Exception, err:
print >>sys.stderr, "Cannot connect to cloud storage: %s" % str(err)
sys.exit(1)
server.main()
if __name__ == '__main__':
main()