def create(_id, name, source, macString): ''' Author : LHearen E-mail : [email protected] Time : 2016-01-05 09:40 Description : Used to create a virtual network card; ''' if len(_id) < 5: from utils.UUIDGenerator import createString global VIFUUIDString _id = createString() name = name if name else _id VIFUUIDString = _id xmlConfig = XmlConverter.toNetXml(_id, name, source, macString) try: conn.networkDefineXML(xmlConfig) except Exception: log.exception(traceback.print_exc()) return None return True
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 create(_id, name, memory, vcpu, mac, diskDir, isoDir, bridgeSrc): ''' Author : LHearen E-mail : [email protected] Time : 2015-12-16 09 : 43 Description : Using limited parameters to create a VM and return its UUIDString; ''' uuid = _id if len(uuid) < 5: uuid = createString() name = name if name else uuid global UUIDString UUIDString = uuid global Name Name = name hvm = {"loader": "/usr/lib/xen/boot/hvmloader"} hvm["boot"] = "cdrom" hvm["device_model"] = "/usr/lib/xen/bin/qemu-system-i386" image = {"hvm": hvm} tap2 = {"dev": "hdc:cdrom"} tap2["uname"] = "tap:aio:" + isoDir tap2["mode"] = "r" vif = {"bridge": bridgeSrc} if mac is not None: vif["mac"] = mac; vbd = {"dev": "hda:disk"} vbd["uname"] = "tap:aio:" + diskDir vbd["mode"] = "w" vfb = {"location": "0.0.0.0:5900"} vfb["vnclisten"] = "0.0.0.0" console = {"location": "0"} xmlConfig = XmlConverter.toVMXml(uuid, name, memory, vcpu, image, tap2, vif, vbd, vfb, console) # print xmlConfig return define_VM_by_xml(xmlConfig)
''' Author : LHearen E-mail : [email protected] Time : 2016-01-06 15:35 Description : Attaching a vif to a VM; ''' dom = None try: dom = conn.lookupByUUIDString(vm_id) 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): '''
Description : Create a volume in a existed pool specified by a pool name; ''' if len(_id) < 5: from utils.UUIDGenerator import createString _id = createString() volName = volName if volName else _id global VolumeUUIDString VolumeUUIDString = _id global VolName VolName = volName try: pool = conn.storagePoolLookupByName(poolName) except libvirtError, e: logNotFound("Pool", poolName, e) return None config = XmlConverter.toVolumeXml(volName, volSize) if not pool.isActive(): pool.create() try: pool.createXML(config) return VolumeUUIDString except libvirtError, e: log.debug("Volume %s creation failed! Message: %s" % (volName, e)) return None return None def deleteVolume(_id): ''' Author : LHearen E-mail : [email protected] Time : 2015-12-30 10 : 31