Signed-off-by: tierno <alfonso.tiernosepulveda@telefonica.com>
import Queue
import paramiko
from jsonschema import validate as js_v, exceptions as js_e
import Queue
import paramiko
from jsonschema import validate as js_v, exceptions as js_e
+#import libvirt
+import imp
from vim_schema import localinfo_schema, hostinfo_schema
import random
#from logging import Logger
from vim_schema import localinfo_schema, hostinfo_schema
import random
#from logging import Logger
#TODO: insert a logging system
#TODO: insert a logging system
+global lvirt_module
+lvirt_module=None #libvirt module is charged only if not in test mode
+
class host_thread(threading.Thread):
def __init__(self, name, host, user, db, db_lock, test, image_path, host_id, version, develop_mode, develop_bridge_iface):
'''Init a thread.
class host_thread(threading.Thread):
def __init__(self, name, host, user, db, db_lock, test, image_path, host_id, version, develop_mode, develop_bridge_iface):
'''Init a thread.
self.db = db
self.db_lock = db_lock
self.test = test
self.db = db
self.db_lock = db_lock
self.test = test
+ if not test:
+ try:
+ lvirt_module = imp.find_module("libvirt")
+ except (IOError, ImportError) as e:
+ raise ImportError("Cannot import python-libvirt. Openvim not properly installed" +str(e))
+
+
self.develop_mode = develop_mode
self.develop_bridge_iface = develop_bridge_iface
self.image_path = image_path
self.develop_mode = develop_mode
self.develop_bridge_iface = develop_bridge_iface
self.image_path = image_path
except paramiko.ssh_exception.SSHException as e:
text = e.args[0]
print self.name, ": load_localinfo ssh Exception:", text
except paramiko.ssh_exception.SSHException as e:
text = e.args[0]
print self.name, ": load_localinfo ssh Exception:", text
- except libvirt.libvirtError as e:
+ except lvirt_module.libvirtError as e:
text = e.get_error_message()
print self.name, ": load_localinfo libvirt Exception:", text
except yaml.YAMLError as exc:
text = e.get_error_message()
print self.name, ": load_localinfo libvirt Exception:", text
except yaml.YAMLError as exc:
except paramiko.ssh_exception.SSHException as e:
text = e.args[0]
print self.name, ": load_hostinfo ssh Exception:", text
except paramiko.ssh_exception.SSHException as e:
text = e.args[0]
print self.name, ": load_hostinfo ssh Exception:", text
- except libvirt.libvirtError as e:
+ except lvirt_module.libvirtError as e:
text = e.get_error_message()
print self.name, ": load_hostinfo libvirt Exception:", text
except yaml.YAMLError as exc:
text = e.get_error_message()
print self.name, ": load_hostinfo libvirt Exception:", text
except yaml.YAMLError as exc:
print self.name, ": save_localinfo ssh Exception:", text
if "SSH session not active" in text:
self.ssh_connect()
print self.name, ": save_localinfo ssh Exception:", text
if "SSH session not active" in text:
self.ssh_connect()
- except libvirt.libvirtError as e:
+ except lvirt_module.libvirtError as e:
text = e.get_error_message()
print self.name, ": save_localinfo libvirt Exception:", text
except yaml.YAMLError as exc:
text = e.get_error_message()
print self.name, ": save_localinfo libvirt Exception:", text
except yaml.YAMLError as exc:
print self.name, ": create xml server error:", xml
return -2, xml
print self.name, ": create xml:", xml
print self.name, ": create xml server error:", xml
return -2, xml
print self.name, ": create xml:", xml
- atribute = libvirt.VIR_DOMAIN_START_PAUSED if paused == "yes" else 0
+ atribute = lvirt_module.VIR_DOMAIN_START_PAUSED if paused == "yes" else 0
#4 Start the domain
if not rebuild: #ensures that any pending destroying server is done
self.server_forceoff(True)
#4 Start the domain
if not rebuild: #ensures that any pending destroying server is done
self.server_forceoff(True)
print self.name, ": launch_server(%s) ssh Exception: %s" %(server_id, text)
if "SSH session not active" in text:
self.ssh_connect()
print self.name, ": launch_server(%s) ssh Exception: %s" %(server_id, text)
if "SSH session not active" in text:
self.ssh_connect()
- except libvirt.libvirtError as e:
+ except lvirt_module.libvirtError as e:
text = e.get_error_message()
print self.name, ": launch_server(%s) libvirt Exception: %s" %(server_id, text)
except Exception as e:
text = e.get_error_message()
print self.name, ": launch_server(%s) libvirt Exception: %s" %(server_id, text)
except Exception as e:
- conn = libvirt.open("qemu+ssh://"+self.user+"@"+self.host+"/system")
+ conn = lvirt_module.open("qemu+ssh://"+self.user+"@"+self.host+"/system")
domains= conn.listAllDomains()
domain_dict={}
for domain in domains:
uuid = domain.UUIDString() ;
libvirt_status = domain.state()
#print libvirt_status
domains= conn.listAllDomains()
domain_dict={}
for domain in domains:
uuid = domain.UUIDString() ;
libvirt_status = domain.state()
#print libvirt_status
- if libvirt_status[0] == libvirt.VIR_DOMAIN_RUNNING or libvirt_status[0] == libvirt.VIR_DOMAIN_SHUTDOWN:
+ if libvirt_status[0] == lvirt_module.VIR_DOMAIN_RUNNING or libvirt_status[0] == lvirt_module.VIR_DOMAIN_SHUTDOWN:
- elif libvirt_status[0] == libvirt.VIR_DOMAIN_PAUSED:
+ elif libvirt_status[0] == lvirt_module.VIR_DOMAIN_PAUSED:
- elif libvirt_status[0] == libvirt.VIR_DOMAIN_SHUTOFF:
+ elif libvirt_status[0] == lvirt_module.VIR_DOMAIN_SHUTOFF:
- elif libvirt_status[0] == libvirt.VIR_DOMAIN_CRASHED:
+ elif libvirt_status[0] == lvirt_module.VIR_DOMAIN_CRASHED:
new_status = "ERROR"
else:
new_status = None
domain_dict[uuid] = new_status
conn.close
new_status = "ERROR"
else:
new_status = None
domain_dict[uuid] = new_status
conn.close
- except libvirt.libvirtError as e:
+ except lvirt_module.libvirtError as e:
print self.name, ": get_state() Exception '", e.get_error_message()
return
print self.name, ": get_state() Exception '", e.get_error_message()
return
self.create_image(None, req)
else:
try:
self.create_image(None, req)
else:
try:
- conn = libvirt.open("qemu+ssh://"+self.user+"@"+self.host+"/system")
+ conn = lvirt_module.open("qemu+ssh://"+self.user+"@"+self.host+"/system")
try:
dom = conn.lookupByUUIDString(server_id)
try:
dom = conn.lookupByUUIDString(server_id)
- except libvirt.libvirtError as e:
+ except lvirt_module.libvirtError as e:
text = e.get_error_message()
if 'LookupByUUIDString' in text or 'Domain not found' in text or 'No existe un dominio coincidente' in text:
dom = None
else:
text = e.get_error_message()
if 'LookupByUUIDString' in text or 'Domain not found' in text or 'No existe un dominio coincidente' in text:
dom = None
else:
- print self.name, ": action_on_server(",server_id,") libvirt exception:", text
+ print self.name, ": action_on_server(",server_id,") libvirt exception:", text
raise e
if 'forceOff' in req['action']:
raise e
if 'forceOff' in req['action']:
- except libvirt.libvirtError as e:
+ except lvirt_module.libvirtError as e:
if conn is not None: conn.close
text = e.get_error_message()
new_status = "ERROR"
if conn is not None: conn.close
text = e.get_error_message()
new_status = "ERROR"
return 0, None
try:
if not lib_conn:
return 0, None
try:
if not lib_conn:
- conn = libvirt.open("qemu+ssh://"+self.user+"@"+self.host+"/system")
+ conn = lvirt_module.open("qemu+ssh://"+self.user+"@"+self.host+"/system")
iface.destroy()
iface.create()
print self.name, ": restore_iface '%s' %s" % (name, mac)
iface.destroy()
iface.create()
print self.name, ": restore_iface '%s' %s" % (name, mac)
- except libvirt.libvirtError as e:
+ except lvirt_module.libvirtError as e:
error_text = e.get_error_message()
print self.name, ": restore_iface '%s' '%s' libvirt exception: %s" %(name, mac, error_text)
ret=-1
error_text = e.get_error_message()
print self.name, ": restore_iface '%s' '%s' libvirt exception: %s" %(name, mac, error_text)
ret=-1
- conn = libvirt.open("qemu+ssh://"+self.user+"@"+self.host+"/system")
+ conn = lvirt_module.open("qemu+ssh://"+self.user+"@"+self.host+"/system")
dom = conn.lookupByUUIDString(port["instance_id"])
if old_net:
text="\n".join(xml)
print self.name, ": edit_iface detaching SRIOV interface", text
dom = conn.lookupByUUIDString(port["instance_id"])
if old_net:
text="\n".join(xml)
print self.name, ": edit_iface detaching SRIOV interface", text
- dom.detachDeviceFlags(text, flags=libvirt.VIR_DOMAIN_AFFECT_LIVE)
+ dom.detachDeviceFlags(text, flags=lvirt_module.VIR_DOMAIN_AFFECT_LIVE)
if new_net:
xml[-1] =" <vlan> <tag id='" + str(port['vlan']) + "'/> </vlan>"
self.xml_level = 1
if new_net:
xml[-1] =" <vlan> <tag id='" + str(port['vlan']) + "'/> </vlan>"
self.xml_level = 1
xml.append('</interface>')
text="\n".join(xml)
print self.name, ": edit_iface attaching SRIOV interface", text
xml.append('</interface>')
text="\n".join(xml)
print self.name, ": edit_iface attaching SRIOV interface", text
- dom.attachDeviceFlags(text, flags=libvirt.VIR_DOMAIN_AFFECT_LIVE)
+ dom.attachDeviceFlags(text, flags=lvirt_module.VIR_DOMAIN_AFFECT_LIVE)
- except libvirt.libvirtError as e:
+ except lvirt_module.libvirtError as e:
text = e.get_error_message()
print self.name, ": edit_iface(",port["instance_id"],") libvirt exception:", text
text = e.get_error_message()
print self.name, ": edit_iface(",port["instance_id"],") libvirt exception:", text
import datetime
import hashlib
import os
import datetime
import hashlib
import os
+import imp
+#import only if needed because not needed in test mode. To allow an easier installation import RADclass
from jsonschema import validate as js_v, exceptions as js_e
import host_thread as ht
from vim_schema import host_new_schema, host_edit_schema, tenant_new_schema, \
from jsonschema import validate as js_v, exceptions as js_e
import host_thread as ht
from vim_schema import host_new_schema, host_edit_schema, tenant_new_schema, \
global my
global url_base
global config_dic
global my
global url_base
global config_dic
+global RADclass_module
+RADclass=None #RADclass module is charged only if not in test mode
ip_name=host['ip_name']
user=host['user']
password=host.get('password', None)
ip_name=host['ip_name']
user=host['user']
password=host.get('password', None)
+ if not RADclass_module:
+ try:
+ RADclass_module = imp.find_module("RADclass")
+ except (IOError, ImportError) as e:
+ raise ImportError("Cannot import RADclass.py Openvim not properly installed" +str(e))
- rad = RADclass.RADclass()
+ rad = RADclass_module.RADclass()
(return_status, code) = rad.obtain_RAD(user, password, ip_name)
#return
(return_status, code) = rad.obtain_RAD(user, password, ip_name)
#return