forked from xenserver/python-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccessor.py
More file actions
395 lines (340 loc) · 12.7 KB
/
accessor.py
File metadata and controls
395 lines (340 loc) · 12.7 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/usr/bin/env python
# Copyright (c) 2013, Citrix Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""accessor - provide common interface to access methods"""
import ftplib
import os
import tempfile
import urllib
import urllib2
import urlparse
import errno
import xcp.mount as mount
import xcp.logger as logger
# maps errno codes to HTTP error codes
# needed for error code consistency
def mapError(errorCode):
if errorCode == errno.EPERM:
return 401
elif errorCode == errno.ENOENT:
return 404
elif errorCode == errno.EACCES:
return 403
else:
return 500
class Accessor(object):
def __init__(self, ro):
self.read_only = ro
self.lastError = 0
def access(self, name):
""" Return boolean determining where 'name' is an accessible object
in the target. """
try:
f = self.openAddress(name)
if not f:
return False
f.close()
except Exception as e:
return False
return True
def openAddress(self, name):
"""should be overloaded"""
pass
def canEject(self):
return False
def start(self):
pass
def finish(self):
pass
@staticmethod
def _writeFile(in_fh, out_fh):
while out_fh:
data = in_fh.read(256 * 512)
if len(data) == 0:
break
out_fh.write(data)
out_fh.close()
return True
class FilesystemAccessor(Accessor):
def __init__(self, location, ro):
super(FilesystemAccessor, self).__init__(ro)
self.location = location
def openAddress(self, addr):
try:
file = open(os.path.join(self.location, addr), 'r')
except OSError as e:
if e.errno == errno.EIO:
self.lastError = 5
else:
self.lastError = mapError(e.errno)
return False
except IOError as e:
if e.errno == errno.EIO:
self.lastError = 5
else:
self.lastError = mapError(e.errno)
return False
except Exception as e:
self.lastError = 500
return False
return file
class MountingAccessor(FilesystemAccessor):
def __init__(self, mount_types, mount_source, mount_options = None):
ro = isinstance(mount_options, list) and 'ro' in mount_options
super(MountingAccessor, self).__init__(None, ro)
self.mount_types = mount_types
self.mount_source = mount_source
self.mount_options = mount_options
self.start_count = 0
def start(self):
if self.start_count == 0:
self.location = tempfile.mkdtemp(prefix="media-", dir="/tmp")
# try each filesystem in turn:
success = False
for fs in self.mount_types:
try:
opts = self.mount_options
if fs == 'iso9660':
if isinstance(opts, list):
if 'ro' not in opts:
opts.append('ro')
else:
opts = ['ro']
mount.mount(self.mount_source, self.location,
options = opts,
fstype = fs)
except mount.MountException:
continue
else:
success = True
break
if not success:
os.rmdir(self.location)
raise mount.MountException
self.start_count += 1
def finish(self):
if self.start_count == 0:
return
self.start_count -= 1
if self.start_count == 0:
mount.umount(self.location)
os.rmdir(self.location)
self.location = None
def writeFile(self, in_fh, out_name):
logger.info("Copying to %s" % os.path.join(self.location, out_name))
out_fh = open(os.path.join(self.location, out_name), 'w')
return self._writeFile(in_fh, out_fh)
def __del__(self):
while self.start_count > 0:
self.finish()
class DeviceAccessor(MountingAccessor):
def __init__(self, device, ro, fs = None):
""" Return a MountingAccessor for a device 'device', which should
be a fully qualified path to a device node. """
if device.startswith('dev://'):
device = device[6:]
if fs is None:
fs = ['iso9660', 'vfat', 'ext3']
opts = None
if ro:
opts = ['ro']
super(DeviceAccessor, self).__init__(fs, device, opts)
self.device = device
def __repr__(self):
return "<DeviceAccessor: %s>" % self.device
# def canEject(self):
# return diskutil.removable(self.device):
# def eject(self):
# assert self.canEject()
# self.finish()
# util.runCmd2(['/usr/bin/eject', self.device])
class NFSAccessor(MountingAccessor):
def __init__(self, nfspath, ro):
if nfspath.startswith('nfs://'):
nfspath = nfspath[6:]
opts = ['tcp,timeo=100,retrans=1,retry=0']
if ro:
opts.append('ro')
super(NFSAccessor, self).__init__(['nfs'], nfspath, opts)
self.nfspath = nfspath
def __repr__(self):
return "<NFSAccessor: %s>" % self.nfspath
class FileAccessor(Accessor):
def __init__(self, baseAddress, ro):
if baseAddress.startswith('file://'):
baseAddress = baseAddress[7:]
assert baseAddress.endswith('/')
super(FileAccessor, self).__init__(ro)
self.baseAddress = baseAddress
def openAddress(self, address):
try:
file = open(os.path.join(self.baseAddress, address))
except IOError as e:
if e.errno == errno.EIO:
self.lastError = 5
else:
self.lastError = mapError(e.errno)
return False
except OSError as e:
if e.errno == errno.EIO:
self.lastError = 5
else:
self.lastError = mapError(e.errno)
return False
except Exception as e:
self.lastError = 500
return False
return file
def writeFile(self, in_fh, out_name):
logger.info("Copying to %s" % os.path.join(self.baseAddress, out_name))
out_fh = open(os.path.join(self.baseAddress, out_name), 'w')
return self._writeFile(in_fh, out_fh)
def __repr__(self):
return "<FileAccessor: %s>" % self.baseAddress
def rebuild_url(url_parts):
'''Rebuild URL without auth components'''
host = url_parts.hostname
if url_parts.port:
host += ':' + str(url_parts.port)
return urlparse.urlunsplit(
(url_parts.scheme, host,
url_parts.path, '', ''))
class FTPAccessor(Accessor):
def __init__(self, baseAddress, ro):
super(FTPAccessor, self).__init__(ro)
self.url_parts = urlparse.urlsplit(baseAddress, allow_fragments=False)
self.start_count = 0
self.cleanup = False
self.ftp = None
self.baseAddress = rebuild_url(self.url_parts)
def _cleanup(self):
if self.cleanup:
# clean up after RETR
self.ftp.voidresp()
self.cleanup = False
def start(self):
if self.start_count == 0:
self.ftp = ftplib.FTP()
#self.ftp.set_debuglevel(1)
port = ftplib.FTP_PORT
if self.url_parts.port:
port = self.url_parts.port
self.ftp.connect(self.url_parts.hostname, port)
username = self.url_parts.username
password = self.url_parts.password
if username:
username = urllib.unquote(username)
if password:
password = urllib.unquote(password)
self.ftp.login(username, password)
directory = urllib.unquote(self.url_parts.path[1:])
if directory != '':
logger.debug("Changing to " + directory)
self.ftp.cwd(directory)
self.start_count += 1
def finish(self):
if self.start_count == 0:
return
self.start_count -= 1
if self.start_count == 0:
self.ftp.quit()
self.cleanup = False
self.ftp = None
def access(self, path):
try:
logger.debug("Testing "+path)
self._cleanup()
url = urllib.unquote(path)
if self.ftp.size(url) is not None:
return True
lst = self.ftp.nlst(os.path.dirname(url))
return os.path.basename(url) in map(os.path.basename, lst)
except IOError as e:
if e.errno == errno.EIO:
self.lastError = 5
else:
self.lastError = mapError(e.errno)
return False
except OSError as e:
if e.errno == errno.EIO:
self.lastError = 5
else:
self.lastError = mapError(e.errno)
return False
except Exception as e:
self.lastError = 500
return False
def openAddress(self, address):
logger.debug("Opening "+address)
self._cleanup()
url = urllib.unquote(address)
self.ftp.voidcmd('TYPE I')
s = self.ftp.transfercmd('RETR ' + url).makefile('rb')
self.cleanup = True
return s
def writeFile(self, in_fh, out_name):
self._cleanup()
fname = urllib.unquote(out_name)
logger.debug("Storing as " + fname)
self.ftp.storbinary('STOR ' + fname, in_fh)
def __repr__(self):
return "<FTPAccessor: %s>" % self.baseAddress
class HTTPAccessor(Accessor):
def __init__(self, baseAddress, ro):
assert ro
super(HTTPAccessor, self).__init__(ro)
self.url_parts = urlparse.urlsplit(baseAddress, allow_fragments=False)
if self.url_parts.username:
username = self.url_parts.username
if username is not None:
username = urllib.unquote(self.url_parts.username)
password = self.url_parts.password
if password is not None:
password = urllib.unquote(self.url_parts.password)
self.passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
self.passman.add_password(None, self.url_parts.hostname,
username, password)
self.authhandler = urllib2.HTTPBasicAuthHandler(self.passman)
self.opener = urllib2.build_opener(self.authhandler)
urllib2.install_opener(self.opener)
self.baseAddress = rebuild_url(self.url_parts)
def openAddress(self, address):
try:
urlFile = urllib2.urlopen(os.path.join(self.baseAddress, address))
except urllib2.HTTPError as e:
self.lastError = e.code
return False
return urlFile
def __repr__(self):
return "<HTTPAccessor: %s>" % self.baseAddress
SUPPORTED_ACCESSORS = {'nfs': NFSAccessor,
'http': HTTPAccessor,
'https': HTTPAccessor,
'ftp': FTPAccessor,
'file': FileAccessor,
'dev': DeviceAccessor,
}
def createAccessor(baseAddress, *args):
url_parts = urlparse.urlsplit(baseAddress, allow_fragments=False)
assert url_parts.scheme in SUPPORTED_ACCESSORS.keys()
return SUPPORTED_ACCESSORS[url_parts.scheme](baseAddress, *args)