Пример #1
0
def get_control_domain(host):
    """
    得到XenServer主机的控制域VM
    @host: XenServer主机的IP
    """
    session = global_xenserver_conn.get(host, None)
    main_host = None
    if session != None:
        try:
            all_vm = session.xenapi.VM.get_all()
        except Exception, e:
            # 如果出现异常,说明这是台slave
            # 需要从它的master上获取所有VM
            session = global_xenserver_conn.get(e.details[1], None)
            all_vm = session.xenapi.VM.get_all()

        # 并对比含有is_control_domain的VM
        # 它的resident_on是否等于传递的参数host
        all_host = session.xenapi.host.get_all()
        for xenhost in all_host:
            host_record = session.xenapi.host.get_record(xenhost)
            if host_record['address'] == host:
                main_host = xenhost
                break

        for vm_ref in all_vm:
            record = session.xenapi.VM.get_record(vm_ref)
            if record['is_control_domain'] and record[
                    'resident_on'] == main_host:
                return vm_ref
Пример #2
0
def get_control_domain(host):
    """
    得到XenServer主机的控制域VM
    @host: XenServer主机的IP
    """
    session = global_xenserver_conn.get(host, None)
    main_host = None
    if session != None:
        try:
            all_vm = session.xenapi.VM.get_all()
        except Exception, e:
            # 如果出现异常,说明这是台slave
            # 需要从它的master上获取所有VM
            session = global_xenserver_conn.get(e.details[1], None)
            all_vm = session.xenapi.VM.get_all()

        # 并对比含有is_control_domain的VM
        # 它的resident_on是否等于传递的参数host
        all_host = session.xenapi.host.get_all()
        for xenhost in all_host:
            host_record = session.xenapi.host.get_record(xenhost)
            if host_record["address"] == host:
                main_host = xenhost
                break

        for vm_ref in all_vm:
            record = session.xenapi.VM.get_record(vm_ref)
            if record["is_control_domain"] and record["resident_on"] == main_host:
                return vm_ref
Пример #3
0
def get_xenserver_vm_all(host):
    '''
    获取单台xenserver的所有虚拟机
    @host xenserver的IP地址
    '''
    final_vms_record = []
    session = global_xenserver_conn.get(host, None)
    if session != None:
        try:
            vms = session.xenapi.VM.get_all()
        except Exception, e:
            session = global_xenserver_conn.get(e.details[1])
            vms = session.xenapi.VM.get_all()

        # 并对比含有is_control_domain的VM
        # 它的resident_on是否等于传递的参数host
        all_host = session.xenapi.host.get_all()
        for xenhost in all_host:
            host_record = session.xenapi.host.get_record(xenhost)
            if host_record['address'] == host:
                main_host = xenhost
                break

        i = 1
        for vm in vms:
            record = session.xenapi.VM.get_record(vm)
            # 排除以下两种情况的VM
            # resident_on参数不等于当前需要查询的主机的VM
            # 并且电源状态是Running的

            # 这样在M/S架构中,Halted的VM不属于任何NODE
            # 使用这样的判断可以将这些VM添加到当前HOST的VM列表中,方便管理
            if record['resident_on'] != main_host and record[
                    'power_state'] == 'Running':
                continue
            if not record['is_a_template'] and not record['is_control_domain']:
                temp_record = {}
                temp_record['uuid'] = record['uuid']
                temp_record['vcpu_max'] = record['VCPUs_max']
                temp_record['memory_static_max'] = int(
                    record['memory_static_max']) / (1024**2)
                temp_record['name_label'] = record['name_label']
                temp_record['power_state'] = record['power_state']
                vm_ref = vm.split(":")[1]
                temp_record['vm_ref'] = vm_ref

                #get network information
                guest_metrics = record['guest_metrics']
                if guest_metrics != "OpaqueRef:NULL":
                    guest_metrics_record = session.xenapi.VM_guest_metrics.get_record(
                        guest_metrics)
                    temp_record['networks'] = guest_metrics_record['networks']
                else:
                    temp_record['networks'] = {}
                temp_record['id'] = i
                i += 1

                final_vms_record.append(temp_record)
Пример #4
0
def get_xenserver_vm_all(host):
    """
    获取单台xenserver的所有虚拟机
    @host xenserver的IP地址
    """
    final_vms_record = []
    session = global_xenserver_conn.get(host, None)
    if session != None:
        try:
            vms = session.xenapi.VM.get_all()
        except Exception, e:
            session = global_xenserver_conn.get(e.details[1])
            vms = session.xenapi.VM.get_all()

        # 并对比含有is_control_domain的VM
        # 它的resident_on是否等于传递的参数host
        all_host = session.xenapi.host.get_all()
        for xenhost in all_host:
            host_record = session.xenapi.host.get_record(xenhost)
            if host_record["address"] == host:
                main_host = xenhost
                break

        i = 1
        for vm in vms:
            record = session.xenapi.VM.get_record(vm)
            # 排除以下两种情况的VM
            # resident_on参数不等于当前需要查询的主机的VM
            # 并且电源状态是Running的

            # 这样在M/S架构中,Halted的VM不属于任何NODE
            # 使用这样的判断可以将这些VM添加到当前HOST的VM列表中,方便管理
            if record["resident_on"] != main_host and record["power_state"] == "Running":
                continue
            if not record["is_a_template"] and not record["is_control_domain"]:
                temp_record = {}
                temp_record["uuid"] = record["uuid"]
                temp_record["vcpu_max"] = record["VCPUs_max"]
                temp_record["memory_static_max"] = int(record["memory_static_max"]) / (1024 ** 2)
                temp_record["name_label"] = record["name_label"]
                temp_record["power_state"] = record["power_state"]
                vm_ref = vm.split(":")[1]
                temp_record["vm_ref"] = vm_ref

                # get network information
                guest_metrics = record["guest_metrics"]
                if guest_metrics != "OpaqueRef:NULL":
                    guest_metrics_record = session.xenapi.VM_guest_metrics.get_record(guest_metrics)
                    temp_record["networks"] = guest_metrics_record["networks"]
                else:
                    temp_record["networks"] = {}
                temp_record["id"] = i
                i += 1

                final_vms_record.append(temp_record)
Пример #5
0
def get_vm_ref_by_name(host_ip, vm_name):
    session = global_xenserver_conn.get(host_ip, None)
    if session != None:
        try:
            vm_ref = session.xenapi.VM.get_by_name_label(vm_name)
        except Exception, e:
            session = global_xenserver_conn.get(e.details[1], None)
            vm_ref = session.xenapi.VM.get_by_name_label(vm_name)
        try:
            return vm_ref[0], True
        except Exception, e:
            logger.exception(e)
            return ("Can not get refid for %s" % vm_name, False)
Пример #6
0
def get_vm_ref_by_name(host_ip, vm_name):
    session = global_xenserver_conn.get(host_ip, None)
    if session != None:
        try:
            vm_ref = session.xenapi.VM.get_by_name_label(vm_name)
        except Exception, e:
            session = global_xenserver_conn.get(e.details[1], None)
            vm_ref = session.xenapi.VM.get_by_name_label(vm_name)
        try:
            return vm_ref[0], True
        except Exception, e:
            logger.exception(e)
            return ("Can not get refid for %s" % vm_name, False)
Пример #7
0
def get_xenserver_host(xenhost):
    '''
    获取单台xenserver的相信信息
    @xenhost: xenserver的IP
    '''
    session = global_xenserver_conn.get(xenhost, None)
    if session != None:
        try:
            hosts = session.xenapi.host.get_all()
        except Exception, e:
            session = global_xenserver_conn.get(e.details[1])
            hosts = session.xenapi.host.get_all()
        for host_record in hosts:
            record = session.xenapi.host.get_record(host_record)
            if record['address'] == xenhost:
                temp_host = get_host_info(session, host_record, verbose=True)
                return temp_host
Пример #8
0
def get_vm_info(host, vm_ref):
    """
    得到单台VM的简略信息
    @host: XenServer主机
    @vm_ref: VM的reference
    """
    vm_info = {}
    session = global_xenserver_conn.get(host, None)
    if session != None:
        try:
            vm_record = session.xenapi.VM.get_record("OpaqueRef:" + vm_ref)
        except Exception, e:
            session = global_xenserver_conn.get(e.details[1], None)
            vm_record = session.xenapi.VM.get_record("OpaqueRef:" + vm_ref)
        vm_info['name_label'] = vm_record['name_label']
        vm_info['power_state'] = vm_record['power_state']
        return vm_info
Пример #9
0
def get_xenserver_host(xenhost):
    """
    获取单台xenserver的相信信息
    @xenhost: xenserver的IP
    """
    session = global_xenserver_conn.get(xenhost, None)
    if session != None:
        try:
            hosts = session.xenapi.host.get_all()
        except Exception, e:
            session = global_xenserver_conn.get(e.details[1])
            hosts = session.xenapi.host.get_all()
        for host_record in hosts:
            record = session.xenapi.host.get_record(host_record)
            if record["address"] == xenhost:
                temp_host = get_host_info(session, host_record, verbose=True)
                return temp_host
Пример #10
0
def get_vm_info(host, vm_ref):
    """
    得到单台VM的简略信息
    @host: XenServer主机
    @vm_ref: VM的reference
    """
    vm_info = {}
    session = global_xenserver_conn.get(host, None)
    if session != None:
        try:
            vm_record = session.xenapi.VM.get_record("OpaqueRef:" + vm_ref)
        except Exception, e:
            session = global_xenserver_conn.get(e.details[1], None)
            vm_record = session.xenapi.VM.get_record("OpaqueRef:" + vm_ref)
        vm_info["name_label"] = vm_record["name_label"]
        vm_info["power_state"] = vm_record["power_state"]
        return vm_info
Пример #11
0
def get_vm_info_by_uuid(host_ip, vm_uuid):
    """
    得到单台VM的详细
    @host: XenServer主机
    @vm_uuid: VM的UUID
    """
    session = global_xenserver_conn.get(host_ip, None)
    if session != None:
        try:
            vm_ref = session.xenapi.VM.get_by_uuid(vm_uuid)
        except Exception, e:
            session = global_xenserver_conn.get(e.details[1], None)
            vm_ref = session.xenapi.VM.get_by_uuid(vm_uuid)
            
        try:
            vm_record = session.xenapi.VM.get_record(vm_ref)
        except Exception, e:
            logger.exception(e)
            return
Пример #12
0
def get_vm_info_by_uuid(host_ip, vm_uuid):
    """
    得到单台VM的详细
    @host: XenServer主机
    @vm_uuid: VM的UUID
    """
    session = global_xenserver_conn.get(host_ip, None)
    if session != None:
        try:
            vm_ref = session.xenapi.VM.get_by_uuid(vm_uuid)
        except Exception, e:
            session = global_xenserver_conn.get(e.details[1], None)
            vm_ref = session.xenapi.VM.get_by_uuid(vm_uuid)

        try:
            vm_record = session.xenapi.VM.get_record(vm_ref)
        except Exception, e:
            logger.exception(e)
            return
Пример #13
0
def get_vm_uuid_by_name(host_ip, vm_name):
    """
    得到单台VM的UUID
    @host: XenServer主机
    @vm_uuid: VM的name label
    """
    session = global_xenserver_conn.get(host_ip, None)
    if session != None:
        try:
            vm_ref = session.xenapi.VM.get_by_name_label(vm_name)
        except Exception, e:
            session = global_xenserver_conn.get(e.details[1], None)
            vm_ref = session.xenapi.VM.get_by_name_label(vm_name)
            
        try:
            vm_record = session.xenapi.VM.get_record(vm_ref[0])
        except Exception, e:
            logger.exception(e)
            return ("Can not get uuid for %s" % vm_name, False)
Пример #14
0
def get_vm_uuid_by_name(host_ip, vm_name):
    """
    得到单台VM的UUID
    @host: XenServer主机
    @vm_uuid: VM的name label
    """
    session = global_xenserver_conn.get(host_ip, None)
    if session != None:
        try:
            vm_ref = session.xenapi.VM.get_by_name_label(vm_name)
        except Exception, e:
            session = global_xenserver_conn.get(e.details[1], None)
            vm_ref = session.xenapi.VM.get_by_name_label(vm_name)

        try:
            vm_record = session.xenapi.VM.get_record(vm_ref[0])
        except Exception, e:
            logger.exception(e)
            return ("Can not get uuid for %s" % vm_name, False)
Пример #15
0
def get_vm_info_by_uuid(host, vm_uuid):
    """
    得到单台VM的详细
    @host: XenServer主机
    @vm_uuid: VM的UUID
    """
    for ip, session in global_xenserver_conn.items():
        if ip == host:
            try:
                vms = session.xenapi.VM.get_all()
            except Exception, e:
                session = global_xenserver_conn.get(e.details[1])
                vms = session.xenapi.VM.get_all()
            for vm in vms:
                record = session.xenapi.VM.get_record(vm)
                if not record["is_a_template"] and not record["is_control_domain"]:
                    if record["uuid"] == vm_uuid:
                        return record
Пример #16
0
def get_xenserver_conn(xenhost):
    return global_xenserver_conn.get(xenhost, None)
Пример #17
0
def get_xenserver_conn(xenhost):
    return global_xenserver_conn.get(xenhost, None)