X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=host_thread.py;h=b9778af30882a1c2422abad487fe9a274d6fc887;hb=9678855e35ac6a03e7b9bf3fbe50f96937d9a686;hp=cae8c6053cb0b0171e7ce0d2f48ec61e2f8582ca;hpb=9a61c6b761065160d0889e7bd1e0f9fc37de5310;p=osm%2Fopenvim.git diff --git a/host_thread.py b/host_thread.py index cae8c60..b9778af 100644 --- a/host_thread.py +++ b/host_thread.py @@ -36,7 +36,8 @@ import time import Queue import paramiko from jsonschema import validate as js_v, exceptions as js_e -import libvirt +#import libvirt +import imp from vim_schema import localinfo_schema, hostinfo_schema import random #from logging import Logger @@ -44,7 +45,9 @@ import random #TODO: insert a logging system + class host_thread(threading.Thread): + lvirt_module = None # libvirt module is charged only if not in test mode def __init__(self, name, host, user, db, db_lock, test, image_path, host_id, version, develop_mode, develop_bridge_iface): '''Init a thread. Arguments: @@ -60,6 +63,14 @@ class host_thread(threading.Thread): self.db = db self.db_lock = db_lock self.test = test + if not test and host_thread.lvirt_module == None: + try: + module_info = imp.find_module("libvirt") + host_thread.lvirt_module = imp.load_module("libvirt", *module_info) + 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 @@ -120,7 +131,7 @@ class host_thread(threading.Thread): 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 host_thread.lvirt_module.libvirtError as e: text = e.get_error_message() print self.name, ": load_localinfo libvirt Exception:", text except yaml.YAMLError as exc: @@ -165,7 +176,7 @@ class host_thread(threading.Thread): 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 host_thread.lvirt_module.libvirtError as e: text = e.get_error_message() print self.name, ": load_hostinfo libvirt Exception:", text except yaml.YAMLError as exc: @@ -206,7 +217,7 @@ class host_thread(threading.Thread): print self.name, ": save_localinfo ssh Exception:", text if "SSH session not active" in text: self.ssh_connect() - except libvirt.libvirtError as e: + except host_thread.lvirt_module.libvirtError as e: text = e.get_error_message() print self.name, ": save_localinfo libvirt Exception:", text except yaml.YAMLError as exc: @@ -757,9 +768,14 @@ class host_thread(threading.Thread): raise paramiko.ssh_exception.SSHException("Error deleting file: " + error_msg) def copy_file(self, source, destination, perserve_time=True): - command = 'cp --no-preserve=mode ' - if perserve_time: command += '--preserve=timestamps ' - command += source + ' ' + destination + if source[0:4]=="http": + command = "wget --no-verbose -O '{dst}' '{src}' 2>'{dst_result}' || cat '{dst_result}' >&2 && rm '{dst_result}'".format( + dst=destination, src=source, dst_result=destination + ".result" ) + else: + command = 'cp --no-preserve=mode' + if perserve_time: + command += ' --preserve=timestamps' + command += " '{}' '{}'".format(source, destination) print self.name, ': command:', command (_, _, stderr) = self.ssh_conn.exec_command(command) error_msg = stderr.read() @@ -783,24 +799,29 @@ class host_thread(threading.Thread): use_incremental_out = use_incremental new_backing_file = None local_file = None + file_from_local = True #in case incremental use is not decided, take the decision depending on the image #avoid the use of incremental if this image is already incremental - qemu_remote_info = self.qemu_get_info(remote_file) + if remote_file[0:4] == "http": + file_from_local = False + if file_from_local: + qemu_remote_info = self.qemu_get_info(remote_file) if use_incremental_out==None: - use_incremental_out = not 'backing file' in qemu_remote_info + use_incremental_out = not ( file_from_local and 'backing file' in qemu_remote_info) #copy recursivelly the backing files - if 'backing file' in qemu_remote_info: + if file_from_local and 'backing file' in qemu_remote_info: new_backing_file, _, _ = self.copy_remote_file(qemu_remote_info['backing file'], True) #check if remote file is present locally if use_incremental_out and remote_file in self.localinfo['files']: local_file = self.localinfo['files'][remote_file] local_file_info = self.get_file_info(local_file) - remote_file_info = self.get_file_info(remote_file) + if file_from_local: + remote_file_info = self.get_file_info(remote_file) if local_file_info == None: local_file = None - elif local_file_info[4]!=remote_file_info[4] or local_file_info[5]!=remote_file_info[5]: + elif file_from_local and (local_file_info[4]!=remote_file_info[4] or local_file_info[5]!=remote_file_info[5]): #local copy of file not valid because date or size are different. #TODO DELETE local file if this file is not used by any active virtual machine try: @@ -923,7 +944,7 @@ class host_thread(threading.Thread): 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 = host_thread.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) @@ -938,7 +959,7 @@ class host_thread(threading.Thread): 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 host_thread.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: @@ -961,26 +982,26 @@ class host_thread(threading.Thread): return try: - conn = libvirt.open("qemu+ssh://"+self.user+"@"+self.host+"/system") + conn = host_thread.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 - if libvirt_status[0] == libvirt.VIR_DOMAIN_RUNNING or libvirt_status[0] == libvirt.VIR_DOMAIN_SHUTDOWN: + if libvirt_status[0] == host_thread.lvirt_module.VIR_DOMAIN_RUNNING or libvirt_status[0] == host_thread.lvirt_module.VIR_DOMAIN_SHUTDOWN: new_status = "ACTIVE" - elif libvirt_status[0] == libvirt.VIR_DOMAIN_PAUSED: + elif libvirt_status[0] == host_thread.lvirt_module.VIR_DOMAIN_PAUSED: new_status = "PAUSED" - elif libvirt_status[0] == libvirt.VIR_DOMAIN_SHUTOFF: + elif libvirt_status[0] == host_thread.lvirt_module.VIR_DOMAIN_SHUTOFF: new_status = "INACTIVE" - elif libvirt_status[0] == libvirt.VIR_DOMAIN_CRASHED: + elif libvirt_status[0] == host_thread.lvirt_module.VIR_DOMAIN_CRASHED: new_status = "ERROR" else: new_status = None domain_dict[uuid] = new_status conn.close - except libvirt.libvirtError as e: + except host_thread.lvirt_module.libvirtError as e: print self.name, ": get_state() Exception '", e.get_error_message() return @@ -1046,15 +1067,15 @@ class host_thread(threading.Thread): self.create_image(None, req) else: try: - conn = libvirt.open("qemu+ssh://"+self.user+"@"+self.host+"/system") + conn = host_thread.lvirt_module.open("qemu+ssh://"+self.user+"@"+self.host+"/system") try: dom = conn.lookupByUUIDString(server_id) - except libvirt.libvirtError as e: + except host_thread.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: - 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']: @@ -1175,7 +1196,7 @@ class host_thread(threading.Thread): conn.close() - except libvirt.libvirtError as e: + except host_thread.lvirt_module.libvirtError as e: if conn is not None: conn.close text = e.get_error_message() new_status = "ERROR" @@ -1232,7 +1253,7 @@ class host_thread(threading.Thread): return 0, None try: if not lib_conn: - conn = libvirt.open("qemu+ssh://"+self.user+"@"+self.host+"/system") + conn = host_thread.lvirt_module.open("qemu+ssh://"+self.user+"@"+self.host+"/system") else: conn = lib_conn @@ -1243,7 +1264,7 @@ class host_thread(threading.Thread): iface.destroy() iface.create() print self.name, ": restore_iface '%s' %s" % (name, mac) - except libvirt.libvirtError as e: + except host_thread.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 @@ -1333,12 +1354,12 @@ class host_thread(threading.Thread): try: conn=None - conn = libvirt.open("qemu+ssh://"+self.user+"@"+self.host+"/system") + conn = host_thread.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.detachDeviceFlags(text, flags=libvirt.VIR_DOMAIN_AFFECT_LIVE) + dom.detachDeviceFlags(text, flags=host_thread.lvirt_module.VIR_DOMAIN_AFFECT_LIVE) if new_net: xml[-1] =" " self.xml_level = 1 @@ -1346,9 +1367,9 @@ class host_thread(threading.Thread): xml.append('') 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=host_thread.lvirt_module.VIR_DOMAIN_AFFECT_LIVE) - except libvirt.libvirtError as e: + except host_thread.lvirt_module.libvirtError as e: text = e.get_error_message() print self.name, ": edit_iface(",port["instance_id"],") libvirt exception:", text