Menu

[r1]: / backend / storage.py  Maximize  Restore  History

Download this file

571 lines (545 with data), 25.9 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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# -----------------------------------------------------------------------
# XenWebManager
#
# Copyright (C) 2009 Alberto Gonzalez Rodriguez alberto@pesadilla.org
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# -----------------------------------------------------------------------
import xmlrpclib, urllib
import asyncore, socket
import select
from os import chdir
import platform
import sys, shutil
import datetime
from threading import Thread
from configobj import ConfigObj
import xml.dom.minidom
from operator import itemgetter
import pdb
import rrdinfo
import time
from messages import messages, messages_header
import xml.sax.saxutils as saxutils
class storage:
stg_ref = None
stg_uuid = None
def fill_hw_hba(self, ref, list):
#<?xml version="1.0"?><methodCall><methodName>SR.probe</methodName><params><param><value><string>OpaqueRef:c9ea013c-cbce-0e85-6863-66d8e7b66ea7</string></value></param><param><value><string>OpaqueRef:5c0a69d1-7719-946b-7f3c-683a7058338d</string></value></param><param><value><struct /></value></param><param><value><string>lvmohba</string></value></param><param><value><struct /></value></param></params></methodCall>
list.clear()
res = self.connection.SR.probe(self.session_uuid, ref, {}, "lvmohba", {})
if len(res['ErrorDescription']) > 2:
result = res['ErrorDescription'][3]
dom = xml.dom.minidom.parseString(result)
nodes = dom.getElementsByTagName("BlockDevice")
disks = {}
for node in nodes:
size = self.convert_bytes(node.getElementsByTagName("size")[0].childNodes[0].data.strip())
serial = node.getElementsByTagName("serial")[0].childNodes[0].data.strip()
scsiid = node.getElementsByTagName("SCSIid")[0].childNodes[0].data.strip()
adapter = node.getElementsByTagName("adapter")[0].childNodes[0].data.strip()
channel = node.getElementsByTagName("channel")[0].childNodes[0].data.strip()
id = node.getElementsByTagName("id")[0].childNodes[0].data.strip()
lun = node.getElementsByTagName("lun")[0].childNodes[0].data.strip()
vendor = node.getElementsByTagName("vendor")[0].childNodes[0].data.strip()
path = node.getElementsByTagName("path")[0].childNodes[0].data.strip()
if vendor not in disks:
disks[vendor] = []
disks[vendor].append([" %s %s %s %s:%s:%s:%s" % (size, serial, scsiid, adapter, channel, id, lun),scsiid,path])
for ref in disks.keys():
list.append(["<b>" + ref + "</b>", False, "", ""])
for lun in disks[ref]:
list.append([lun[0], True, lun[1], lun[2]])
return 0
else:
self.wine.show_error_dlg("No LUNs were found. Please verify your hardware configuration")
return 1
def rescan_isos(self, ref):
res = self.connection.Async.SR.scan(self.session_uuid, ref)
if "Value" in res:
self.track_tasks[res['Value']] = ref
return "OK"
else:
return str(res["ErrorDescription"][0])
def detach_storage(self, ref):
for pbd in self.all_storage[ref]['PBDs']:
res = self.connection.Async.PBD.unplug(self.session_uuid, pbd)
if "Value" in res:
self.track_tasks[res['Value']] = ref
else:
print res
if "Value" in res:
value = res["Value"]
task = self.connection.task.get_record(self.session_uuid, value)['Value']
while task["status"] == "pending":
task = self.connection.task.get_record(self.session_uuid, value)['Value']
res = self.connection.PBD.destroy(self.session_uuid, pbd)
if "Value" in res:
self.track_tasks[res['Value']] = ref
else:
print res
def forget_storage(self, ref):
if self.all_storage[ref]['allowed_operations'].count("unplug"):
for pbd in self.all_storage[ref]['PBDs']:
res = self.connection.Async.PBD.unplug(self.session_uuid, pbd)
if "Value" in res:
self.track_tasks[res['Value']] = ref
value = res["Value"]
task = self.connection.task.get_record(self.session_uuid, value)['Value']
while task["status"] == "pending":
task = self.connection.task.get_record(self.session_uuid, value)['Value']
res = self.connection.Async.PBD.destroy(self.session_uuid, pbd)
if "Value" in res:
self.track_tasks[res['Value']] = ref
value = res["Value"]
task = self.connection.task.get_record(self.session_uuid, value)['Value']
while task["status"] == "pending":
task = self.connection.task.get_record(self.session_uuid, value)['Value']
else:
print res
else:
print res
res = self.connection.Async.SR.forget(self.session_uuid, ref)
if "Value" in res:
self.track_tasks[res['Value']] = ref
else:
print res
def delete_vdi(self, ref_vdi, ref_vm):
for ref_vbd in self.all_vdi[ref_vdi]['VBDs']:
res = self.connection.VBD.destroy(self.session_uuid, ref_vbd)
if "Value" in res:
self.track_tasks[res['Value']] = ref_vm
else:
print res
res = self.connection.VDI.destroy(self.session_uuid, ref_vdi)
if "Value" in res:
self.track_tasks[res['Value']] = ref_vm
return "OK"
else:
print res
def reattach_nfs_iso(self, sr, name, share, options):
# FIXME
ref = self.all_hosts.keys()[0]
pbd = {
"uuid" : "",
"host" : ref,
"SR" : sr,
"device_config" : {
"location" : share,
"options": options
},
"currentyle_attached" : False,
"other_config" : {}
}
self.connection.SR.set_name_label(self.session_uuid, sr, name)
self.connection.SR.set_name_description(self.session_uuid, sr, "NFS ISO Library [%s]" % (share))
res = self.connection.Async.PBD.create(self.session_uuid, pbd)
if "Value" in res:
self.track_tasks[res['Value']] = ref
value = res['Value']
task = self.connection.task.get_record(self.session_uuid, value)['Value']
while task["status"] == "pending":
task = self.connection.task.get_record(self.session_uuid, value)['Value']
result = saxutils.unescape(task['result']).replace("<value>","").replace("</value>","").replace("&quot;", '"')
res = self.connection.Async.PBD.plug(self.session_uuid, result)
value = res['Value']
task = self.connection.task.get_record(self.session_uuid, value)['Value']
while task["status"] == "pending":
task = self.connection.task.get_record(self.session_uuid, value)['Value']
if task["status"] == "success":
return 0
else:
self.wine.show_error_dlg(str(task["error_info"]))
return 1
else:
print res
def create_nfs_iso(self, ref, name, share, options):
sr = {
"location" : share,
"options" : options
}
value = self.connection.SR.create(self.session_uuid, ref, sr, "0", name, "NFS ISO Library [%s]" % (share), "iso", "iso", True, {})
if "ErrorDescription" in value:
return value["ErrorDescription"][2]
else:
return 0
def reattach_cifs_iso(self, sr, name, share, options, user="", password=""):
ref = self.all_hosts.keys()[0]
pbd = {
"uuid" : "",
"host" : ref,
"SR" : sr,
"device_config" : {
"location" : share,
"type": "cifs",
"options": options
},
"currentyle_attached" : False,
"other_config" : {}
}
self.connection.SR.set_name_label(self.session_uuid, sr, name)
self.connection.SR.set_name_description(self.session_uuid, sr, "CIFS ISO Library [%s]" % (share))
res = self.connection.Async.PBD.create(self.session_uuid, pbd)
if "Value" in res:
self.track_tasks[res['Value']] = ref
value = res['Value']
task = self.connection.task.get_record(self.session_uuid, value)['Value']
while task["status"] == "pending":
task = self.connection.task.get_record(self.session_uuid, value)['Value']
result = saxutils.unescape(task['result']).replace("<value>","").replace("</value>","").replace("&quot;", '"')
res = self.connection.Async.PBD.plug(self.session_uuid, result)
value = res['Value']
task = self.connection.task.get_record(self.session_uuid, value)['Value']
while task["status"] == "pending":
task = self.connection.task.get_record(self.session_uuid, value)['Value']
if task["status"] == "success":
return 0
else:
self.wine.show_error_dlg(str(task["error_info"]))
return 1
else:
print res
def create_cifs_iso(self, ref, name, share, options, user="", password=""):
sr = {
"location" : share,
"type" : "cifs",
"options" : options,
"username" : user,
"cifspassword" : password,
}
value = self.connection.SR.create(self.session_uuid, ref, sr, "0", name, "CIFS ISO Library [%s]" % (share), "iso", "iso", True, {})
if "ErrorDescription" in value:
return value["ErrorDescription"][2]
else:
return 0
def create_nfs_vhd(self, ref, name, host, path, options):
sr = {
"serverpath" : path,
"server" : host,
"options" : options
}
res = self.connection.SR.create(self.session_uuid, ref, sr, str(0), name, "NF SR [%s:%s]" % (host, path), "nfs", "", True, {})
if "Value" in res:
self.track_tasks[res['Value']] = ref
return ""
else:
return res
def reattach_nfs_vhd(self, ref, name, host, path, options, uuid):
sr = self.connection.SR.get_by_uuid(self.session_uuid, uuid)
res = self.connection.SR.introduce(self.session_uuid, uuid, name, "NFS SR [%s:%s]" % (host, path), "nfs", "", True, {})
if "Value" in res:
sr = res['Value']
pbd = {
"uuid" : "",
"host" : ref,
"SR" : sr,
"device_config" : {
"serverpath" : path,
"server" : host,
"options": options
},
"currentyle_attached" : False,
"other_config" : {}
}
ref = self.connection.PBD.create(self.session_uuid, pbd)['Value']
self.connection.PBD.plug(self.session_uuid, ref)
else:
print res
return res
def format_hardware_hba(self, ref, uuid, name, path):
sr = {
"SCSIid" : uuid,
}
res = self.connection.SR.create(self.session_uuid, ref, sr, "0", name, "Hardware HBA SR [%s]" % (path), "lvmohba", "", False, {})
if "Value" in res:
self.track_tasks[res['Value']] = self.host_vm[ref][0]
print self.connection.SR.set_other_config(self.session_uuid, res['Value'], {"auto-scan": "false"})
else:
print res
def reattach_and_introduce_hardware_hba(self, ref, uuid, name, path):
res = self.connection.SR.introduce(self.session_uuid, self.stg_uuid, name, "Hardware HBA SR [%s]" % (path), "lvmohba", "", False, {})
pbd = {
"uuid" : "",
"host" : ref,
"SR" : res['Value'],
"device_config" : {
"SCSIid" : uuid,
},
"currentyle_attached" : False,
"other_config" : {}
}
res = self.connection.Async.PBD.create(self.session_uuid, pbd)
if "Value" in res:
self.track_tasks[res['Value']] = self.host_vm[ref][0]
value = res['Value']
task = self.connection.task.get_record(self.session_uuid, value)['Value']
while task["status"] == "pending":
task = self.connection.task.get_record(self.session_uuid, value)['Value']
result = saxutils.unescape(task['result']).replace("<value>","").replace("</value>","").replace("&quot;", '"')
res = self.connection.Async.PBD.plug(self.session_uuid, result)
value = res['Value']
task = self.connection.task.get_record(self.session_uuid, value)['Value']
while task["status"] == "pending":
task = self.connection.task.get_record(self.session_uuid, value)['Value']
if task["status"] == "success":
return 0
else:
self.wine.show_error_dlg(str(task["error_info"]))
return 1
else:
print res
def reattach_hardware_hba(self, ref, uuid, name, path):
ref = self.all_hosts.keys()[0]
pbd = {
"uuid" : "",
"host" : ref,
"SR" : self.stg_ref,
"device_config" : {
"SCSIid" : uuid,
},
"currentyle_attached" : False,
"other_config" : {}
}
self.connection.SR.set_name_label(self.session_uuid, self.stg_ref, name)
self.connection.SR.set_name_description(self.session_uuid, self.stg_ref, "Hardware HBA SR [%s]" % (path))
res = self.connection.Async.PBD.create(self.session_uuid, pbd)
if "Value" in res:
self.track_tasks[res['Value']] = self.host_vm[ref][0]
value = res['Value']
task = self.connection.task.get_record(self.session_uuid, value)['Value']
while task["status"] == "pending":
task = self.connection.task.get_record(self.session_uuid, value)['Value']
result = saxutils.unescape(task['result']).replace("<value>","").replace("</value>","").replace("&quot;", '"')
res = self.connection.Async.PBD.plug(self.session_uuid, result)
value = res['Value']
task = self.connection.task.get_record(self.session_uuid, value)['Value']
while task["status"] == "pending":
task = self.connection.task.get_record(self.session_uuid, value)['Value']
if task["status"] == "success":
return 0
else:
self.wine.show_error_dlg(str(task["error_info"]))
return 1
else:
print res
pass
"""
sr = {
"SCSIid" : uuid,
}
res = self.connection.SR.create(self.session_uuid, ref, sr, "0", name, "Hardware HBA SR [IBM - %s]" % (path), "lvmohba", "", False, {})
if "Value" in res:
self.track_tasks[res['Value']] = self.host_vm[ref][0]
print self.connection.SR.set_other_config(self.session_uuid, res['Value'], {"auto-scan": "false"})
else:
print res
"""
def check_hardware_hba(self, ref, uuid, text):
result = self.connection.SR.probe(self.session_uuid, ref, {"SCSIid" : uuid }, "lvmohba", {})['Value']
dom = xml.dom.minidom.parseString(result)
nodes = dom.getElementsByTagName("UUID")
if len(nodes):
reattach = True
self.stg_uuid = nodes[0].childNodes[0].data.strip()
for storage_ref in self.all_storage.keys():
storage = self.all_storage[storage_ref]
if storage["uuid"] == self.stg_uuid:
self.stg_ref = storage_ref
if len(storage['PBDs']):
reattach = False
if reattach:
if self.stg_ref:
return [2, self.all_storage[self.stg_ref]['name_label'], self.all_hosts[ref]['name_label']]
else:
return [3, text, self.all_hosts[ref]['name_label']]
else:
return [1, self.all_storage[self.stg_ref]['name_label'], self.all_hosts[ref]['name_label']]
else:
return [0, None, None]
def check_iscsi(self, ref, name, host, port, scsiid, targetiqn, user, password):
sr = {
"port" : port,
"target" : host,
"SCSIid" : scsiid,
"targetIQN" : targetiqn
}
if user:
sr["chapuser"] = user
if password:
sr["chappassword"] = password
value = self.connection.Async.SR.probe(self.session_uuid, ref, sr, "lvmoiscsi", {})['Value']
task = self.connection.task.get_record(self.session_uuid, value)['Value']
while task["status"] == "pending":
task = self.connection.task.get_record(self.session_uuid, value)['Value']
result = saxutils.unescape(task['result']).replace("<value>","").replace("</value>","").replace("&quot;", '"')
dom = xml.dom.minidom.parseString(result)
nodes = dom.getElementsByTagName("UUID")
if len(nodes):
return nodes[0].childNodes[0].data.strip()
else:
return None
#ref = self.connection.SR.create(self.session_uuid, ref, sr, "0", name, "iSCSI SR [%s (%s)]" % (host, targetiqn), "lvmoiscsi", "", True, {})
#print ref
def create_iscsi(self, ref, name, host, port, scsiid, targetiqn, user, password):
sr = {
"port" : port,
"target" : host,
"SCSIid" : scsiid,
"targetIQN" : targetiqn
}
if user:
sr["chapuser"] = user
if password:
sr["chappassword"] = password
res = self.connection.Async.SR.create(self.session_uuid, ref, sr, "0", name, "iSCSI SR [%s (%s)]" % (host, targetiqn), "lvmoiscsi", "", True, {})
if "Value" in res:
return "OK"
else:
return res
def reattach_iscsi(self, ref, name, host, port, scsiid, targetiqn, user, password, lun):
res = self.connection.SR.introduce(self.session_uuid, lun, name, "iSCSI SR [%s (%s)]" % (host, targetiqn), "lvmoiscsi", "", True, {})
pbd = {
"uuid" : "",
"host" : ref,
"SR" : res['Value'],
"device_config" : {
"port" : port,
"target" : host,
"SCSIid" : scsiid,
"targetIQN" : targetiqn
},
"currently_attached" : False,
"other_config" : {}
}
if user:
pbd["device_config"]["chapuser"] = user
if password:
pbd["device_config"]["chappassword"] = password
res = self.connection.PBD.create(self.session_uuid, pbd)
res = self.connection.Async.PBD.plug(self.session_uuid, res['Value'])
if "Value" in res:
return "OK"
else:
return res
"""
sr = {
"port" : port,
"target" : host,
"SCSIid" : scsiid,
"targetIQN" : targetiqn
}
if user:
sr["chapuser"] = user
if password:
sr["chappassword"] = password
res = self.connection.Async.SR.create(self.session_uuid, ref, sr, "0", name, "iSCSI SR [%s (%s)]" % (host, targetiqn), "lvmoiscsi", "", True, {})
"""
def scan_nfs_vhd(self, ref, host, path, options):
mylist = []
sr = {
"serverpath" : path,
"server" : host,
"options" : options,
}
value = self.connection.Async.SR.probe(self.session_uuid, ref, sr, "nfs", {})['Value']
task = self.connection.task.get_record(self.session_uuid, value)['Value']
while task["status"] == "pending":
task = self.connection.task.get_record(self.session_uuid, value)['Value']
if task['result'].count("<value>"):
result = saxutils.unescape(task['result']).replace("<value>","").replace("</value>","").replace("&quot;", '"')
dom = xml.dom.minidom.parseString(result)
nodes = dom.getElementsByTagName("SRlist")
if len(nodes[0].childNodes):
for i in range(1,len(nodes[0].childNodes),2):
ref = nodes[0].childNodes[i].childNodes[1].childNodes[0].data.strip()
if self.search_storage_uuid(ref) == False:
mylist.append(ref)
if mylist.__len__() > 0:
return mylist, 2, ""
else:
return mylist, 1, ""
else:
error = task["error_info"][2]
self.connection.task.destroy(self.session_uuid, value)
return mylist, 0, error
def search_storage_uuid(self, uuid):
"""
Function to search a storage with specify uuid, returns True if found
"""
for stg in self.all_storage.keys():
if self.all_storage[stg]["uuid"] == uuid and len(self.all_storage[stg]['PBDs']):
return True
return False
def fill_iscsi_target_iqn(self, ref, target, port, user=None, password=None):
mylist = []
sr = {
"port" : port,
"target": target,
}
if user:
sr["chapuser"] = user
if password:
sr["chappassword"] = password
value = self.connection.Async.SR.create(self.session_uuid, ref, sr, "0", "__gui__", "SHOULD NEVER BE CREATED","lvmoiscsi","user", True, {})['Value']
task = self.connection.task.get_record(self.session_uuid, value)['Value']
while task["status"] == "pending":
task = self.connection.task.get_record(self.session_uuid, value)['Value']
if task["error_info"][3]:
dom = xml.dom.minidom.parseString(task["error_info"][3])
nodes = dom.getElementsByTagName("TGT")
ix = 1
for i in range(0, len(nodes)):
index = nodes[i].childNodes[1].childNodes[0].data.strip()
ip = nodes[i].childNodes[3].childNodes[0].data.strip()
target = nodes[i].childNodes[5].childNodes[0].data.strip()
mylist.append([target, "%s (%s)" % (target, ip)])
self.connection.task.destroy(self.session_uuid, value)
return True, mylist, ""
else:
self.connection.task.destroy(self.session_uuid, value)
return False, mylist, task["error_info"][2]
def fill_iscsi_target_lun(self, ref, target, targetiqn, port, user=None, password=None):
mylist = []
sr = {
"port" : port,
"target": target,
}
# chapuser
# chappassword
if user:
sr["chapuser"] = user
if password:
sr["chappassword"] = password
sr["targetIQN"] = targetiqn
value = self.connection.Async.SR.create(self.session_uuid, ref, sr, "0", "__gui__", "SHOULD NEVER BE CREATED","lvmoiscsi","user", True, {})['Value']
task = self.connection.task.get_record(self.session_uuid, value)['Value']
while task["status"] == "pending":
task = self.connection.task.get_record(self.session_uuid, value)['Value']
if task["error_info"][3]:
dom = xml.dom.minidom.parseString(task["error_info"][3])
nodes = dom.getElementsByTagName("LUN")
for i in range(0, len(nodes)):
vendor = nodes[i].getElementsByTagName("vendor")[0].childNodes[0].data.strip()
#serial = nodes[i].getElementsByTagName("serial")[0].childNodes[0].data.strip()
lunid = nodes[i].getElementsByTagName("LUNid")[0].childNodes[0].data.strip()
size = nodes[i].getElementsByTagName("size")[0].childNodes[0].data.strip()
scsiid = nodes[i].getElementsByTagName("SCSIid")[0].childNodes[0].data.strip()
mylist.append([scsiid, "LUN %s: %s (%s)" % (lunid, self.convert_bytes(size), vendor)])
self.connection.task.destroy(self.session_uuid, value)
return True, mylist, ""
else:
print task["error_info"][2]
self.connection.task.destroy(self.session_uuid, value)
return False, mylist, task["error_info"][2]
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.