import string from vrtManager import util from vrtManager.connection import wvmConnect from webvirtcloud.settings import QEMU_CONSOLE_DEFAULT_TYPE from webvirtcloud.settings import INSTANCE_VOLUME_DEFAULT_FILE_EXTENSION from webvirtcloud.settings import INSTANCE_VOLUME_DEFAULT_FORMAT def get_rbd_storage_data(stg): xml = stg.XMLDesc(0) ceph_user = util.get_xml_path(xml, "/pool/source/auth/@username") def get_ceph_hosts(ctx): hosts = [] for host in ctx.xpathEval("/pool/source/host"): name = host.prop("name") if name: hosts.append({'name': name, 'port': host.prop("port")}) return hosts ceph_hosts = util.get_xml_path(xml, func=get_ceph_hosts) secret_uuid = util.get_xml_path(xml, "/pool/source/auth/secret/@uuid") return ceph_user, secret_uuid, ceph_hosts class wvmCreate(wvmConnect): image_extension = INSTANCE_VOLUME_DEFAULT_FILE_EXTENSION image_format = INSTANCE_VOLUME_DEFAULT_FORMAT def get_storages_images(self): """ Function return all images on all storages """ images = [] storages = self.get_storages(only_actives=True) for storage in storages: stg = self.get_storage(storage) try: stg.refresh(0) except: pass for img in stg.listVolumes(): if img.endswith('.iso'): pass else: images.append(img) return images def get_os_type(self): """Get guest capabilities""" return util.get_xml_path(self.get_cap_xml(), "/capabilities/guest/os_type") def get_host_arch(self): """Get guest capabilities""" return util.get_xml_path(self.get_cap_xml(), "/capabilities/host/cpu/arch") def create_volume(self, storage, name, size, format=image_format, metadata=False, image_extension=image_extension): size = int(size) * 1073741824 stg = self.get_storage(storage) storage_type = util.get_xml_path(stg.XMLDesc(0), "/pool/@type") if storage_type == 'dir': name += '.' + image_extension alloc = 0 else: alloc = size metadata = False xml = """ %s %s %s """ % (name, size, alloc, format) stg.createXML(xml, metadata) try: stg.refresh(0) except: pass vol = stg.storageVolLookupByName(name) return vol.path() def get_volume_type(self, path): vol = self.get_volume_by_path(path) vol_type = util.get_xml_path(vol.XMLDesc(0), "/volume/target/format/@type") if vol_type == 'unknown': return 'raw' if vol_type: return vol_type else: return 'raw' def get_volume_path(self, volume): storages = self.get_storages(only_actives=True) for storage in storages: stg = self.get_storage(storage) if stg.info()[0] != 0: stg.refresh(0) for img in stg.listVolumes(): if img == volume: vol = stg.storageVolLookupByName(img) return vol.path() def get_storage_by_vol_path(self, vol_path): vol = self.get_volume_by_path(vol_path) return vol.storagePoolLookupByVolume() def clone_from_template(self, clone, template, metadata=False): vol = self.get_volume_by_path(template) stg = vol.storagePoolLookupByVolume() storage_type = util.get_xml_path(stg.XMLDesc(0), "/pool/@type") format = util.get_xml_path(vol.XMLDesc(0), "/volume/target/format/@type") if storage_type == 'dir': clone += '.img' else: metadata = False xml = """ %s 0 0 """ % (clone, format) stg.createXMLFrom(xml, vol, metadata) clone_vol = stg.storageVolLookupByName(clone) return clone_vol.path() def _defineXML(self, xml): self.wvm.defineXML(xml) def delete_volume(self, path): vol = self.get_volume_by_path(path) vol.delete() def create_instance(self, name, memory, vcpu, host_model, uuid, images, cache_mode, networks, virtio, mac=None): """ Create VM function """ memory = int(memory) * 1024 if self.is_kvm_supported(): hypervisor_type = 'kvm' else: hypervisor_type = 'qemu' xml = """ %s None %s %s %s""" % (hypervisor_type, name, uuid, memory, vcpu) if host_model: xml += """""" xml += """ %s """ % (self.get_host_arch(), self.get_os_type()) xml += """ destroy restart restart """ disk_letters = list(string.lowercase) for image, img_type in images.items(): stg = self.get_storage_by_vol_path(image) stg_type = util.get_xml_path(stg.XMLDesc(0), "/pool/@type") if stg_type == 'rbd': ceph_user, secret_uuid, ceph_hosts = get_rbd_storage_data(stg) xml += """ """ % (img_type, cache_mode, ceph_user, secret_uuid, image) if isinstance(ceph_hosts, list): for host in ceph_hosts: if host.get('port'): xml += """ """ % (host.get('name'), host.get('port')) else: xml += """ """ % host.get('name') xml += """ """ else: xml += """ """ % (img_type, cache_mode, image) if virtio: xml += """""" % (disk_letters.pop(0),) else: xml += """""" % (disk_letters.pop(0),) xml += """""" xml += """
""" for net in networks.split(','): xml += """""" if mac: xml += """""" % mac xml += """ """ % net if virtio: xml += """""" xml += """""" xml += """ """ % (QEMU_CONSOLE_DEFAULT_TYPE, util.randomPasswd()) self._defineXML(xml)