Пример #1
0
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()
Пример #2
0
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
Пример #3
0
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
Пример #4
0
    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)
Пример #5
0
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
Пример #6
0
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
Пример #7
0
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
Пример #8
0
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
Пример #9
0
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
Пример #10
0
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
Пример #11
0
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
Пример #12
0
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
Пример #13
0
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
Пример #14
0
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
Пример #15
0
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
Пример #16
0
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
Пример #17
0
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
Пример #18
0
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
Пример #19
0
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
Пример #20
0
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
Пример #21
0
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
Пример #22
0
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
Пример #23
0
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
Пример #24
0
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
Пример #25
0
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
Пример #26
0
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
Пример #27
0
    def get_vm(self):
        from XenXenXenSe.VM import VM

        try:
            vm = self.session.xenapi.VIF.get_VM(self.vif)

            if vm is not None:
                vm = VM(self.session, vm)
                return vm
            else:
                return None
        except Exception as e:
            print("VIF.get_vm Exception", e)
            return None
Пример #28
0
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
Пример #29
0
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
Пример #30
0
    def get_VM(self):
        """ get VM attached to the specified VBD """
        from XenXenXenSe.VM import VM

        try:
            vm = self.session.xenapi.VBD.get_VM(self.vbd)

            if vm is not None:
                return VM(self.session, vm)
            else:
                return None
        except Exception as e:
            print("VBD.get_VM Exception", e)
            return None