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
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 `vms`" cursor.execute(cls.sql) result = cursor.fetchall() for vm_v in result: cluster_id = vm_v['cluster_id'] vm_uuid = vm_v['vm_uuid'] print(cluster_id, vm_uuid) session = create_session(cluster_id) _vm = VM.get_by_uuid(session, vm_uuid) if _vm is None: cls.sql = "DELETE FROM `vms` WHERE `cluster_id`=%s AND `vm_uuid`=%s" cursor.execute(cls.sql, (cluster_id, vm_uuid)) connection.commit() except Exception as e: print("MySQL Sync: remove_orphaned failed.", e)
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
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 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 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 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 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_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_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 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 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 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 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 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 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 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 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
async def vm_delete(cluster_id: str, vm_uuid: str): """ Delete VM """ session = create_session(cluster_id) _vm: VM = VM.get_by_uuid(session, vm_uuid) if _vm is not None: ret = {"success": _vm.delete()} else: ret = {"success": False} XenVm.remove_orphaned(cluster_id) session.xenapi.session.logout() return ret
async def vm_set_memory(cluster_id: str, vm_uuid: str, args: MemoryArgs): """ Set 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": _vm.set_memory(args.memory)} else: ret = {"success": False} XenVm.update(cluster_id, _vm) session.xenapi.session.logout() return ret
async def instance_set_name(cluster_id: str, vm_uuid: str, args: NameArgs): """ Set 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": _vm.set_name(args.name)} else: ret = {"success": False} XenVm.update(cluster_id, _vm) session.xenapi.session.logout() return ret
async def vm_set_vCPU_inurl(cluster_id: str, vm_uuid: str, vCPU_count: int): """ Set VM vCPU count """ session = create_session(cluster_id) _vm: VM = VM.get_by_uuid(session, vm_uuid) if _vm is not None: ret = {"success": _vm.set_vCPUs(vCPU_count)} else: ret = {"success": False} XenVm.update(cluster_id, _vm) session.xenapi.session.logout() return ret
async def instance_set_description_inurl(cluster_id: str, vm_uuid: str, new_description: str): """ 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(new_description)} else: ret = {"success": False} XenVm.update(cluster_id, _vm) session.xenapi.session.logout() return ret
async def vm_power_restart(cluster_id: str, vm_uuid: str): """ Restart VM, can return false if the system does not support ACPI shutdown. System powerstate must be checked beforehand """ session = create_session(cluster_id) _vm: VM = VM.get_by_uuid(session, vm_uuid) if _vm is not None: ret = {"success": _vm.reboot()} else: ret = {"success": False} XenVm.update(cluster_id, _vm) session.xenapi.session.logout() return ret
async def vm_start(cluster_id: str, vm_uuid: str): """ Start the VM System powerstate must be checked beforehand """ session = create_session(cluster_id) _vm: VM = VM.get_by_uuid(session, vm_uuid) if XenVm is not None: ret = {"success": _vm.start()} else: ret = {"success": False} XenVm.update(cluster_id, _vm) session.xenapi.session.logout() return ret
async def instance_clone(cluster_id: str, vm_uuid: str, args: NameArgs): """ Clone Instance (VM/Template) """ session = create_session(cluster_id) _vm: VM = VM.get_by_uuid(session, vm_uuid) if _vm is not None: newVM = _vm.clone(args.name) if newVM is not None: ret = {"success": True, "data": newVM.serialize()} else: ret = {"success": False} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def instance_set_bios_property_byname_inurl(cluster_id: str, vm_uuid: str, name: str, var: str): """ Set Instance (VM/Template) BIOS Property by Name """ session = create_session(cluster_id) vm: VM = VM.get_by_uuid(session, vm_uuid) if vm is not None: data = {} data[name] = var ret = {"success": vm.set_bios_strings(data)} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def get_cd(cluster_id: str, vm_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: ret = {"success": True, "data": newVBD.serialize()} else: ret = {"success": False} else: ret = {"success": False} session.xenapi.session.logout() return ret
async def vm_consoles(cluster_id: str, vm_uuid: str): """ Get all consoles are available to 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() consoleList = [] for console in consoles: consoleList.append(console.serialize()) ret = {"success": True, "data": consoleList} else: ret = {"success": False} session.xenapi.session.logout() return ret