def sync_mysql_database(): if status.get_enabled(): print() print("MySQL Sync: MySQL Sync Triggered!") from .VM import XenVm from .Host import XenHost from XenXenXenSe.VM import VM from XenXenXenSe.Host import Host from XenXenXenSe.session import create_session for cluster_id in xen_credentials: session = create_session(cluster_id) # ================================== print("MySQL Sync: MySQL Host Sync Triggered!") hosts = Host.list_host(session) for host in hosts: host.update(cluster_id, host) XenHost.remove_orphaned(cluster_id) # =================================== print("MySQL Sync: MySQL VM Sync Triggered!") vms = VM.list_vm(session) for _vm in vms: XenVm.update(cluster_id, _vm) XenVm.remove_orphaned(cluster_id) print("MySQL Sync: MySQL Sync Completed!") print()
async def instance_vifs(cluster_id: str, vm_uuid: str): """ Show Instnace VIFs """ from XenXenXenSe.VIF import VIF session = create_session(cluster_id) vm: VM = VM.get_by_uuid(session, vm_uuid) if vm is not None: newVIFs = vm.get_VIFs() vif_serialized = [] if newVIFs is not None: for vif in newVIFs: if vif is not None: vif_serialized.append(vif.serialize()) ret = {"success": True, "data": vif_serialized} else: ret = {"success": False} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def get_cd_insert_inurl(cluster_id: str, vm_uuid: str, vdi_uuid: str): session = create_session(cluster_id) vm: VM = VM.get_by_uuid(session, vm_uuid) if vm is not None: newVBD = vm.get_CD() if newVBD is not None: from XenXenXenSe.VDI import VDI vdi: VDI = VDI.get_by_uuid(session, vdi_uuid) if vdi is not None: success = newVBD.insert(vdi) if success: ret = {"success": success, "data": vdi.serialize()} else: ret = {"success": success} else: ret = {"success": False} else: ret = {"success": False} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def vif_set_qos_speed_by_uuid(cluster_id: str, vif_uuid: str, speed: str): """ Set VIF QoS Speed by UUID """ session = create_session(cluster_id) vif: VIF = VIF.get_by_uuid(session, vif_uuid) speedNum = speed try: speedNum = int(speed) except ValueError: return {"success": False} if speedNum <= 0: a = vif.set_qos_type("") b = vif.set_qos_info({}) ret = {"success": a and b} else: if vif is not None: if vif.get_qos_type() != "ratelimit": vif.set_qos_type("ratelimit") ret = {"success": vif.set_qos_info({"kbps": speed})} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def vif_get_qos_by_uuid(cluster_id: str, vm_uuid: str): """ Set VIF QoS by VM """ session = create_session(cluster_id) vm: VM = VM.get_by_uuid(session, vm_uuid) if vm is not None: vif = vm.get_VIF() if vif is not None: ret = { "success": True, "data": { "type": vif.get_qos_type(), "info": vif.get_qos_info() } } else: ret = {"success": False} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def vif_set_qos_speed_by_vm(cluster_id: str, vm_uuid: str, speed: str): """ Set VIF QoS Speed by VM """ from XenXenXenSe.VIF import VIF session = create_session(cluster_id) vm: VM = VM.get_by_uuid(session, vm_uuid) if vm is not None: vif = vm.get_VIF() try: speedNum = int(speed) except ValueError: return {"success": False} if speedNum <= 0: a = vif.set_qos_type("") b = vif.set_qos_info({}) ret = {"success": a and b} else: if vif is not None: if vif.get_qos_type() != "ratelimit": vif.set_qos_type("ratelimit") ret = {"success": vif.set_qos_info({"kbps": speed})} else: ret = {"success": False} else: ret = {"success": False} session.xenapi.session.logout() return ret
def remove_orphaned(cls, cluster_id): if status.get_enabled(): try: connection = pymysql.connect( **mysql_credentials, cursorclass=pymysql.cursors.DictCursor) with connection.cursor() as cursor: from XenXenXenSe.session import create_session cls.sql = "SELECT * FROM `hosts`" cursor.execute(cls.sql) result = cursor.fetchall() for host_v in result: cluster_id = host_v['cluster_id'] host_uuid = host_v['host_uuid'] session = create_session(cluster_id) _host = Host.get_by_uuid(session, host_uuid) if _host is None: cls.sql = "DELETE FROM `hosts` WHERE `cluster_id`=%s AND `host_uuid`=%s" cursor.execute(cls.sql, (cluster_id, host_uuid)) connection.commit() except Exception as e: print("MySQL Sync: remove_orphaned failed.", e)
async def instance_vbds(cluster_id: str, vm_uuid: str): """ Show Instance VBDs """ from XenXenXenSe.VBD import VBD session = create_session(cluster_id) vm: VM = VM.get_by_uuid(session, vm_uuid) if vm is not None: newVBDs = vm.get_VBDs() vbd_serialized = [] if newVBDs is not None: for vbd in newVBDs: if vbd is not None: vbd_serialized.append(vbd.serialize()) print(vbd_serialized) ret = {"success": True, "data": vbd_serialized} else: ret = {"success": False} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def instance_get_name(cluster_id: str, vm_uuid: str): """ Get Instance (VM/Template) Name """ session = create_session(cluster_id) _vm: VM = VM.get_by_uuid(session, vm_uuid) if _vm is not None: ret = {"success": True, "data": _vm.get_name()} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def vm_get_memory(cluster_id: str, vm_uuid: str): """ Get VM Memory (needs troubleshooting) """ session = create_session(cluster_id) _vm: VM = VM.get_by_uuid(session, vm_uuid) if _vm is not None: ret = {"success": True, "data": _vm.get_memory()} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def instance_set_description(cluster_id: str, vm_uuid: str, args: DescriptionArgs): """ Set Instance (VM/Template) Description """ session = create_session(cluster_id) _vm: VM = VM.get_by_uuid(session, vm_uuid) if _vm is not None: ret = {"success": _vm.set_description(args.description)} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def vm_get_vCPU_platform(cluster_id: str, vm_uuid: str): """ Get vCPU Platform """ session = create_session(cluster_id) vm: VM = VM.get_by_uuid(session, vm_uuid) if vm is not None: ret = {"success": True, "data": vm.get_platform()} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def instance_get_bios(cluster_id: str, vm_uuid: str): """ Get Instance (VM/Template) BIOS """ session = create_session(cluster_id) vm: VM = VM.get_by_uuid(session, vm_uuid) if vm is not None: ret = {"success": True, "data": vm.get_bios_strings()} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def instance_get_description(cluster_id: str, vm_uuid: str): """ Get Instance (VM/Template) Description (needs troubleshooting) """ session = create_session(cluster_id) _vm: VM = VM.get_by_uuid(session, vm_uuid) if _vm is not None: ret = {"success": True, "data": _vm.get_description()} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def vm_get_vCPU_params(cluster_id: str, vm_uuid: str): """ Get vCPU Parameters """ session = create_session(cluster_id) _vm: VM = VM.get_by_uuid(session, vm_uuid) if _vm is not None: ret = {"success": True, "data": _vm.get_vCPU_params()} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def vbd_eject_vdi(cluster_id: str, vbd_uuid: str): """ Eject VDI from VBD """ session = create_session(cluster_id) vbd: VBD = VBD.get_by_uuid(session, vbd_uuid) if vbd is not None: ret = {"success": vbd.eject()} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def vm_power(cluster_id: str, vm_uuid: str): """ Get VM's power status, Can be "Paused", "Halted", "Running" """ session = create_session(cluster_id) _vm: VM = VM.get_by_uuid(session, vm_uuid) if _vm is not None: ret = {"success": True, "data": _vm.get_power_state()} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def instance_info(cluster_id: str, vm_uuid: str): """ Get an Info of VM or Template """ session = create_session(cluster_id) vm: VM = VM.get_by_uuid(session, vm_uuid) if vm is not None: ret = {"success": True, "data": vm.serialize()} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def vdi_get_by_uuid(cluster_id: str, vdi_uuid: str): """ Get VDI by UUID """ session = create_session(cluster_id) vdi: VDI = VDI.get_by_uuid(session, vdi_uuid) if vdi is not None: ret = {"success": True, "data": vdi.serialize()} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def vdi_get_by_uuid(cluster_id: str, vdi_uuid: str): """ Delete SR by UUID """ session = create_session(cluster_id) vdi: VDI = VDI.get_by_uuid(session, vdi_uuid) if vdi is not None: ret = {"success": vdi.destroy()} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def vif_get_qos_speed_by_uuid(cluster_id: str, vif_uuid: str): """ Set VIF QoS Type by UUID """ session = create_session(cluster_id) vif: VIF = VIF.get_by_uuid(session, vif_uuid) if vif is not None: ret = {"success": True, "data": vif.get_qos_info()['kbps']} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def vif_get_by_uuid(cluster_id: str, vif_uuid: str): """ Get VIF by UUID """ session = create_session(cluster_id) vif: VIF = VIF.get_by_uuid(session, vif_uuid) if vif is not None: ret = {"success": True, "data": vif.serialize()} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def vm_guest(cluster_id: str, vm_uuid: str): """ Get VM Guest Info """ session = create_session(cluster_id) vm: VM = VM.get_by_uuid(session, vm_uuid) if vm is not None: ret = {"success": True, "data": vm.get_guest_metrics().serialize()} else: session.xenapi.session.logout() ret = {"success": False} session.xenapi.session.logout() return ret
async def instance_set_platform_property_byname(cluster_id: str, vm_uuid: str, name: str): """ Get Instance (VM/Template) Platform Property by Name """ session = create_session(cluster_id) vm: VM = VM.get_by_uuid(session, vm_uuid) if vm is not None: ret = {"success": True, "data": vm.get_platform()[name]} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def vbd_delete(cluster_id: str, vbd_uuid: str): """ Destroy VBD by UUID """ session = create_session(cluster_id) vbd: VBD = VBD.get_by_uuid(session, vbd_uuid) if vbd is not None: ret = {"success": True, "data": vbd.destroy()} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def vm_console(cluster_id: str, vm_uuid: str): """ Get the first console of the VM """ session = create_session(cluster_id) vm: VM = VM.get_by_uuid(session, vm_uuid) if vm is not None: consoles = vm.get_consoles() ret = {"success": True, "data": consoles[0].serialize()} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def host_get_by_uuid(cluster_id: str, host_uuid: str): """ Get Host by UUID """ session = create_session(cluster_id) host: Host = Host.get_by_uuid(session, host_uuid) if host is not None: ret = {"success": True, "data": host.serialize()} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def template_list(cluster_id: str): """ Gets Templates available on Xen Server """ session = create_session(cluster_id) vms = VM.list_templates(session) sat = [] for vm in vms: sat.append(vm.serialize()) ret = {"success": True, "data": sat} session.xenapi.session.logout() return ret
async def vif_list(cluster_id: str): """ Get All from Storage Repos """ session = create_session(cluster_id) vifs = VIF.get_all(session) santilized_vifs = [] for vif in vifs: santilized_vifs.append(vif.serialize()) ret = {"success": True, "data": santilized_vifs} session.xenapi.session.logout() return ret
async def vm_power_suspend(cluster_id: str, vm_uuid: str): """ Suspend the VM """ session = create_session(cluster_id) _vm: VM = VM.get_by_uuid(session, vm_uuid) if _vm is not None: ret = {"success": _vm.suspend()} else: ret = {"success": False} XenVm.update(cluster_id, _vm) session.xenapi.session.logout() return ret