Ejemplo n.º 1
0
    def claim_from_host(self, host_id: int, vcpu: int, mem: int):
        '''
        向宿主机申请资源

        :param host_id: 宿主机id
        :param vcpu: 要申请的cpu数
        :param mem: 要申请的内存大小
        :return:
            Host()  # success
            None    #宿主机不存在
        :raise ComputeError
        '''
        with transaction.atomic():
            host = Host.objects.select_for_update().filter(id=host_id).first()
            if not host:
                return None

            # 宿主机是否满足资源需求
            if not host.meet_needs(vcpu=vcpu, mem=mem):
                raise ComputeError(msg='宿主机没有足够的资源')

            # 申请资源
            if not host.claim(vcpu=vcpu, mem=mem):
                raise ComputeError(msg='向宿主机申请资源时失败')

        return host
Ejemplo n.º 2
0
    def enforce_group_obj(self, group_or_id):
        """
        转换为机组对象

        :param group_or_id: Group() or id
        :return:
            Group()

        :raise: ComputeError    # 机组不存在,或参数有误
        """
        if isinstance(group_or_id, Group):
            return group_or_id

        if isinstance(group_or_id, int):
            if group_or_id <= 0:
                raise ComputeError(msg='无效的group id')

            group = self._cache_group_get(group_or_id)  # 是否有缓存
            if group:
                return group

            group = self.get_group_by_id(group_or_id)
            if group:
                self._cache_group_add(group)
                return group
            else:
                raise ComputeError(msg='机组不存在')

        raise ComputeError(msg='无效的group or id')
Ejemplo n.º 3
0
    def enforce_center_obj(self, center_or_id):
        """
        转换为分中心对象

        :param center_or_id: Center() or id
        :return:
            Center()

        :raise: ComputeError    # 分中心不存在,或参数有误
        """
        if isinstance(center_or_id, Center):
            return center_or_id

        if isinstance(center_or_id, int):
            if center_or_id <= 0:
                raise ComputeError(msg='无效的center id')

            c = self._cache_center_get(center_or_id)
            if c:
                return c

            c = self.get_center_by_id(center_or_id)
            if c:
                self._cache_center_add(c)
                return c
            else:
                raise ComputeError(msg='分中心不存在')

        raise ComputeError(msg='无效的center or id')
Ejemplo n.º 4
0
    def filter_meet_requirements(self,
                                 hosts: list,
                                 vcpu: int,
                                 mem: int,
                                 claim=False):
        '''
        筛选满足申请资源要求的宿主机

        :param hosts: 宿主机列表
        :param vcpu: 要申请的cpu数
        :param mem: 要申请的内存大小
        :param claim: True:立即申请资源
        :return:
            Host()  # success
            None    # 没有足够的资源的宿主机

        :raise ComputeError
        '''
        # 检查参数
        if not isinstance(hosts, list):
            raise ComputeError(msg='参数有误,请输入宿主机列表')

        if len(hosts) == 0:  # 没有满足条件的宿主机
            return None

        if not isinstance(hosts[0], Host):
            raise ComputeError(msg='参数有误,请输入宿主机列表')

        if not isinstance(vcpu, int) or vcpu <= 0:
            raise ComputeError(msg='参数有误,vcpu必须是一个正整数')

        if not isinstance(mem, int) or mem <= 0:
            raise ComputeError(msg='参数有误,mem必须是一个正整数')

        random.shuffle(hosts)  # 随机打乱
        for host in hosts:
            # 宿主机是否满足资源需求
            if not host.meet_needs(vcpu=vcpu, mem=mem):
                continue

            if not claim:  # 立即申请资源
                continue

            host = self.claim_from_host(host_id=host.id, vcpu=vcpu, mem=mem)
            if host:
                return host

        return None
Ejemplo n.º 5
0
    def filter_hosts_queryset(self, group_id: int = 0, enable: bool = True):
        '''
        过滤宿主机查询集

        :param group_id: 宿主机组id,默认为0,忽略此参数
        :param enable: True,过滤有效的宿主机;False,不过滤
        :return:
            QuerySet    # success

        :raises: ComputeError
        '''
        if group_id < 0:
            raise ComputeError(msg='group_id无效')

        qs = None
        if group_id > 0:
            qs = Host.objects.filter(group=group_id).all()

        if qs is None:
            qs = Host.objects.all()

        if enable:
            qs = qs.filter(enable=True).all()

        return qs
Ejemplo n.º 6
0
    def get_group_by_id(group_id: int):
        """
        通过id获取宿主机组

        :param group_id: 宿主机组id
        :return:
            Group() # success
            None    #不存在
        :raise ComputeError
        """
        if not isinstance(group_id, int) or group_id < 0:
            raise ComputeError(msg='宿主机组ID参数有误')

        try:
            return Group.objects.filter(id=group_id).first()
        except Exception as e:
            raise ComputeError(msg=f'查询宿主机组时错误,{str(e)}')
Ejemplo n.º 7
0
    def get_center_by_id(self, center_id: int):
        '''
        通过id获取分中心

        :param center_id: 分中心id
        :return:
            Image() # success
            None    #不存在
        :raise ComputeError
        '''
        if not isinstance(center_id, int) or center_id < 0:
            raise ComputeError(msg='分中心ID参数有误')

        try:
            return Center.objects.filter(id=center_id).first()
        except Exception as e:
            raise ComputeError(msg=f'查询分中心时错误,{str(e)}')
Ejemplo n.º 8
0
    def get_hosts_by_group_id(self, group_id: int):
        '''
        获取宿主机组的所有宿主机元数据模型对象

        :param group_id: 宿主机组id
        :return:
            [Host(),]    # success
            raise ComputeError #发生错误

        :raise ComputeError
        '''
        if not isinstance(group_id, int) or group_id < 0:
            raise ComputeError(msg='宿主机组ID参数有误')
        try:
            hosts_qs = Host.objects.filter(group=group_id).all()
            return list(hosts_qs)
        except Exception as e:
            raise ComputeError(msg=f'查询宿主机组的宿主机列表时错误,{str(e)}')
Ejemplo n.º 9
0
    def get_host_by_id(self, host_id: int):
        '''
        通过id获取宿主机元数据模型对象

        :param host_id: 宿主机id
        :return:
            Host() # success
            None    #不存在
        :raise ComputeError
        '''
        if not isinstance(host_id, int) or host_id < 0:
            raise ComputeError(msg='宿主机ID参数有误')

        try:
            return Host.objects.select_related('group').filter(
                id=host_id).first()
        except Exception as e:
            raise ComputeError(msg=f'查询宿主机时错误,{str(e)}')
Ejemplo n.º 10
0
    def get_host_ids_by_group_ids(self, ids: list):
        '''
        通过宿主机组id list获取宿主机id list

        :param ids: 宿主机组id list
        :return:
            ids: list   # success
        :raise ComputeError
        '''
        hosts = self.get_host_queryset_by_group_ids(ids)
        try:
            ids = list(hosts.values_list('id', flat=True).all())
        except Exception as e:
            raise ComputeError(msg=f'查询宿主机id错误,{str(e)}')
        return ids
Ejemplo n.º 11
0
    def enforce_host_obj(self, host_or_id):
        """
        转换为宿主机对象

        :param host_or_id: Host() or id
        :return:
            Host()

        :raise: ComputeError    # 不存在,或参数有误
        """
        if isinstance(host_or_id, Host):
            return host_or_id

        if isinstance(host_or_id, int):
            if host_or_id <= 0:
                raise ComputeError(msg='无效的host id')

            h = self.get_host_by_id(host_or_id)
            if h:
                return h
            else:
                raise ComputeError(msg='宿主机不存在')

        raise ComputeError(msg='无效的host or id')
Ejemplo n.º 12
0
    def get_user_group_queryset_by_center(self, center_or_id, user):
        '''
        获取分中心下的宿主机组查询集

        :param center_or_id: 分中心对象或id
        :param user: 用户对象
        :return:
            groups: QuerySet   # success
        :raise ComputeError
        '''
        if isinstance(center_or_id, Center) or isinstance(center_or_id, int):
            qs = self.get_user_group_queryset(user)
            return qs.filter(center=center_or_id)

        raise ComputeError(msg='无效的center id')
Ejemplo n.º 13
0
    def get_host_ids_by_group_or_ids(self, group_or_ids):
        '''
        通过宿主机组对象或id,或id list获取宿主机id list

        :param group_or_ids: 机组对象,机组id,或机组id list
        :return:
            ids: list   # success
        :raise ComputeError
        '''
        if isinstance(group_or_ids, list):
            return self.get_host_ids_by_group_ids(group_or_ids)
        elif isinstance(group_or_ids, int) or isinstance(group_or_ids, Group):
            return self.get_all_host_ids_by_group(group_or_ids)

        raise ComputeError(msg='无效的宿主机组参数')
Ejemplo n.º 14
0
    def get_user_host_ids(self, user):
        """
        获取用户有权限访问的宿主机id list

        :param user: 用户对象
        :return:
            ids: list   # success

        :raise ComputeError
        """
        hosts = self.get_user_host_queryset(user=user)
        try:
            ids = list(hosts.values_list('id', flat=True).all())
        except Exception as e:
            raise ComputeError(msg=f'查询宿主机id错误,{str(e)}')
        return ids
Ejemplo n.º 15
0
    def get_user_group_ids_by_center(self, center, user):
        """
        获取分中心下的,用户有访问权限的宿主机组查询集

        :param center: 分中心对象或ID
        :param user: 用户对象
        :return:
            QuerySet()
        """
        qs = self.get_user_group_queryset_by_center(center_or_id=center,
                                                    user=user)
        try:
            ids = list(qs.values_list('id', flat=True).all())
        except Exception as e:
            raise ComputeError(msg=f'查询宿主机组id错误,{str(e)}')
        return ids
Ejemplo n.º 16
0
    def get_all_host_ids_by_group(self, group_or_id):
        """
        通过宿主机组对象和id获取所有(包括未激活的)宿主机id list

        :param group_or_id: 宿主机组对象和id
        :return:
            ids: list   # success
        :raise ComputeError
        """
        hosts = self.get_all_host_queryset_by_group(group_or_id)

        try:
            ids = list(hosts.values_list('id', flat=True).all())
        except Exception as e:
            raise ComputeError(msg=f'查询宿主机id错误,{str(e)}')
        return ids
Ejemplo n.º 17
0
    def get_hosts_list_by_group(self, group_or_id):
        '''
        获取宿指定主机组,并且包含指定vlan的所有宿主机元数据模型对象

        :param group_or_id: 宿主机组对象Group()或id
        :return:
            [Host(),]    # success
            raise ComputeError #发生错误

        :raise ComputeError
        '''
        if isinstance(group_or_id, int):
            group_id = group_or_id
        else:
            group_id = group_or_id.id

        hosts_qs = self.get_hosts_by_group_id(group_id=group_id)
        try:
            return list(hosts_qs)
        except Exception as e:
            raise ComputeError(msg=f'查询宿主机组的宿主机列表时错误,{str(e)}')