def __createVM(self): ''' Author : LHearen E-mail : [email protected] Time : 2015-12-21 10 : 49 Description : Make sure there is always a VM called 'test' Delete the VM first if it exists, and then create another VM called 'test'. ''' _id = '103adb75-f41a-4dae-8bdc-c61e92c85e58' name = self.name memory = 1024 vcpu = 2 mac = '' diskDir = '/home/res/images/test.qcow2' isoDir = '/home/res/iso/CentOS-7.1.iso' bridgeSrc = 'ovs0' try: self.__deleteVM(self.name) except: pass log.debug("After deletion, now let's create a new one.") return VM.create(_id, name, memory, vcpu, mac, diskDir, isoDir, bridgeSrc)
def logNotFound(objectName, NameOrId, message): """ Author : LHearen E-mail : [email protected] Time : 2016-01-07 11:09 Description : Used to log not found error in libvirt.log; """ log.debug("%s %s Not Found! Message: %s" % (objectName, NameOrId, message))
def listPools(): ''' Author : LHearen E-mail : [email protected] Time : 2015-12-30 10 : 25 Description : Used to list all known pools; ''' try: poolNames = conn.listAllStoragePoolsNames() except libvirtError, e: log.debug("pool listing error! Message: %s" % e) return None
def __shutoffVM(self, vm_name): ''' Author : LHearen E-mail : [email protected] Time : 2015-12-21 11 : 01 Description : If the VM is running, shut it off forcefully. ''' try: dom = VM.conn.lookupByName(self.name) if dom.isActive(): dom.destroyFlags(int(0)) except Exception, e: log.debug(str(e))
def testDelete(self): ''' Author : LHearen E-mail : [email protected] Time : 2015-12-21 11 : 11 Description : Make sure the deletion operation is effective; ''' self.__deleteVM() try: VM.conn.lookupByName(self.name) except Exception, e: log.debug(e) log.debug(type(e))
def __deleteVM(self): ''' Author : LHearen E-mail : [email protected] Time : 2015-12-21 10 : 45 Description : Remove the vm_name VM for later VM creation process. ''' try: log.debug('Delete domain first') dom = VM.conn.lookupByName(self.name) if dom.isActive(): dom.destroyFlags(int(0)) dom.undefineFlags(int(0)) except Exception, e: log.debug(str(e))
def testStart(self): ''' Author : LHearen E-mail : [email protected] Time : 2015-12-21 11 : 06 Description : Make sure the starting operation takes effect after at most 6 seconds. ''' dom = self.__startVM() for i in range(3): sleep(2) if dom.isActive(): break log.debug(dom.isActive()) self.assertTrue(dom.isActive())
def delete(_id): ''' Author : LHearen E-mail : [email protected] Time : 2016-01-05 10:23 Description : Used to delete or undefine a virtual network card; ''' try: vif = conn.networkLookupByUUIDString(_id) if vif.isActive(): vif.destroy() vif.undefine() VIFHelper.remove({"_id": _id}) except Exception, e: log.debug("VIF.delete Failed! Message: %s" % e) return None
def createPool(_id, name, target): ''' Author : LHearen E-mail : [email protected] Time : 2015-12-30 10 : 17 Description : Using indispensible factors to create a pool; ''' if len(_id) < 5: from utils.UUIDGenerator import createString _id = createString() name = name if name else _id global PoolName PoolName = name global PoolUUIDString PoolUUIDString = _id config = XmlConverter.toSRXml(_id, name, target) try: conn.poolCreateXML(config) except libvirtError, e: log.debug("pool %s creation failed! Message: %s" % (name, e)) return None
def testReboot(self): ''' Author : LHearen E-mail : [email protected] Time : 2015-12-21 11 : 08 Description : Detect two states changes from up to down then from down to up. ''' try: self.__startVM(self.name) except: pass dom_id0 = self.__get_vm_id(self.name) log.debug(dom_id0) _id = VM.conn.lookupByName(self.name).UUIDString() log.debug("reboot" + _id) VM.reboot(str(_id)) dom_id1 = self.__get_vm_id(self.name) log.debug(dom_id1) self.assertTrue(dom_id1 > dom_id0)
Time : 2015-12-16 14 : 00 Description : destory the VM first if it's still active and then undefine it and delete the document in db; ''' dom = None try: dom = conn.lookupByUUIDString(_id) except Exception, e: logNotFound("VM", _id, e) return None if dom: if dom.isActive(): dom.destroyFlags(int(flags)) try: dom.undefineFlags(int(flags)) except Exception, e: log.debug("VM %s deletion failed! Message: %s" % (_id, e)) try: conn.lookupByUUIDString(_id) except Exception, e: log.error("VM %s deletion failed! Message: %s" % (_id, e)) filterDict = {"_id": _id} ret = VMHelper.remove(filterDict) if ret['ok'] > 0: return True else: log.debug("mongodb deletion failed!") return None return None def reboot(_id, flags=0): '''
except Exception, e: logNotFound("VM", vm_id, e) return None ret = VIFHelper.retrieve({"_id": vif_id}) macString = ret['macString'] source = ret['source'] interfaceXmlConfig = XmlConverter.toVIFXml(source, macString) try: dom.attachDeviceFlags(interfaceXmlConfig, 0) dataDict = {"busy": True, "attachedVM": vm_id} VIFHelper.update({"_id": vif_id}, dataDict) dataDict = {"vifs.vif_id": vif_id, "vifs.macString": macString} VMHelper.update({"_id": vm_id}, dataDict) return True except Exception, e: log.debug("Attaching VIF %s to VM %s failed! Message: %s" % (vif_id, vm_id, e)) return None def detach(vm_id, vif_id): ''' Author : LHearen E-mail : [email protected] Time : 2016-01-06 15:35 Description : Detaching a vif from a VM; ''' dom = None try: dom = conn.lookupByUUIDString(vm_id) except Exception, e: logNotFound("VM", vm_id, e)
Time : 2015-12-30 10 : 17 Description : Delete the pool specified by a name; ''' try: pool = conn.storagePoolLookupByUUIDString(_id) except libvirtError, e: logNotFound("Pool", _id, e) return None if pool.isActive(): pool.destroy() try: pool.undefine() filterDict = {"_id": _id} VBDHelper.removePool(filterDict) except libvirtError, e: log.debug("pool %s cannot be removed! Message: %s" % (_id, e)) return None return True def listPools(): ''' Author : LHearen E-mail : [email protected] Time : 2015-12-30 10 : 25 Description : Used to list all known pools; ''' try: poolNames = conn.listAllStoragePoolsNames() except libvirtError, e: log.debug("pool listing error! Message: %s" % e) return None