示例#1
0
def get_list(args):
    '''获取虚拟机列表'''
    ret_list = []
    api = VmAPI()
    group_api = GroupAPI()
    group = group_api.get_group_by_id(args['group_id'])
    if not group.managed_by(args['req_user']):
        return {'res': False, 'err': ERR_AUTH_PERM}

    vm_list = api.get_vm_list_by_group_id(args['group_id'], order = '-create_time')

    for vm in vm_list:
        if vm.create_time:
            create_time = vm.create_time.strftime('%Y-%m-%d %H:%M:%S')
        else:
            create_time = ''
        ret_list.append({
            'uuid':         vm.uuid,
            'name':         vm.name,
            'center_id':    vm.center_id,
            'center_name':  vm.center_name,
            'group_id':     vm.group_id,
            'group_name':   vm.group_name,
            'host_id':      vm.host_id,
            'host_ipv4':    vm.host_ipv4,
            'image_id':     vm.image_id,
            'image':        vm.image,
            'ipv4':         vm.ipv4,
            'vcpu':         vm.vcpu,
            'mem':          vm.mem,
            'creator':      vm.creator,
            'create_time':  create_time,
            'remarks':      vm.remarks
            })
    return {'res': True, 'list': ret_list}
示例#2
0
文件: host.py 项目: bobff/ev-cloud
def get_list(args):
    '''获取宿主机列表'''
    ret_list = []
    group_api = GroupAPI()
    group = group_api.get_group_by_id(args['group_id'])
    
    if not group.managed_by(args['req_user']):
        return {'res': False, 'err': ERR_AUTH_PERM}
    
    host_api = HostAPI()
    host_list = host_api.get_host_list_by_group_id(args['group_id'])
     
    for host in host_list:
        ret_list.append({
            'id':   host.id,
            'group_id': host.group_id,
            'ipv4': host.ipv4,
            'vcpu_total': host.vcpu_total,
            'vcpu_allocated': host.vcpu_allocated,
            'mem_total': host.mem_total,
            'mem_allocated': host.mem_allocated,
            'mem_reserved': host.mem_reserved,
            'vm_limit': host.vm_limit,
            'vm_created': host.vm_created,
            'enable': host.enable,
            'net_types':[vlan[1] for vlan in host.vlan_types]})
    return {'res': True, 'list': ret_list}
示例#3
0
def get_vlan_list(args=None):
    '''获取vlan列表'''
    group_api = GroupAPI()
    group = group_api.get_group_by_id(args['group_id'])

    if not group.managed_by(args['req_user']):
        return {'res': False, 'err': ERR_AUTH_PERM}

    network_api = NetworkAPI()
    vlans = network_api.get_vlan_list_by_group_id(args['group_id'])
    
    if vlans:
        vlan_list = []
        for vlan in vlans:
            vlan_list.append({
                'id': vlan.id,
                'vlan': vlan.vlan,
                'br': vlan.br,
                'type_code': vlan.type_code,
                'type': vlan.type_name,
                'enable': vlan.enable,
                'order': vlan.order,
                'ip_count': vlan.ip_count,
                'ip_used': vlan.ip_used,
                'ip_free': vlan.ip_count-vlan.ip_used,
                'subnetip': vlan.subnetip,
                'netmask': vlan.netmask,
                'gateway': vlan.gateway,
                'remarks': vlan.remarks
                })
        return {'res': True, 'list': vlan_list}
    return {'res': False, 'err': ERR_VLAN_NO_FIND}
示例#4
0
文件: net.py 项目: iselu/ev-cloud
def get_vlan_list(args=None):
    '''获取网络列表'''
    ret_list = []
    group_api = GroupAPI()
    group = group_api.get_group_by_id(args['group_id'])

    if not group.managed_by(args['req_user']):
        return {'res': False, 'err': ERR_AUTH_PERM}

    network_api = NetworkAPI()
    vlans = network_api.get_vlan_list_by_group_id(args['group_id'])
    
    if vlans:
        vlan_list = []
        for vlan in vlans:
            vlan_list.append({
                'id': vlan.id,
                'vlan': vlan.vlan,
                'br': vlan.br,
                'type_code': vlan.type_code,
                'type': vlan.type_name,
                'enable': vlan.enable,
                'order': vlan.order
                })
        return {'res': True, 'list': vlan_list}
    return {'res': False, 'err': ERR_VLAN_NO_FIND}
示例#5
0
文件: vm.py 项目: bobff/ev-cloud
def get_list(args):
    '''获取虚拟机列表'''
    ret_list = []
    api = VmAPI()
    group_api = GroupAPI()
    group = group_api.get_group_by_id(args['group_id'])
    if not group.managed_by(args['req_user']):
        return {'res': False, 'err': ERR_AUTH_PERM}

    vm_list = api.get_vm_list_by_group_id(args['group_id'], order = '-create_time')

    for vm in vm_list:
        if vm.create_time:
            create_time = vm.create_time.strftime('%Y-%m-%d %H:%M:%S')
        else:
            create_time = ''
        ret_list.append({
            'uuid':         vm.uuid,
            'name':         vm.name,
            'center_id':    vm.center_id,
            'center_name':  vm.center_name,
            'group_id':     vm.group_id,
            'group_name':   vm.group_name,
            'host_id':      vm.host_id,
            'host_ipv4':    vm.host_ipv4,
            'image_id':     vm.image_id,
            'image':        vm.image,
            'ipv4':         vm.ipv4,
            'vcpu':         vm.vcpu,
            'mem':          vm.mem,
            'creator':      vm.creator,
            'create_time':  create_time,
            'remarks':      vm.remarks
            })
    return {'res': True, 'list': ret_list}
示例#6
0
文件: api.py 项目: g711ab/ev-cloud
    def __init__(self,
                 manager=None,
                 storage_api=None,
                 vm_api=None,
                 group_api=None,
                 quota=None):
        if not manager:
            self.manager = CephManager()
        else:
            self.manager = manager
        if not storage_api:
            self.storage_api = CephStorageAPI()
        else:
            self.storage_api = storage_api
        if not vm_api:
            self.vm_api = VmAPI()
        else:
            self.vm_api = vm_api
        if not group_api:
            self.group_api = GroupAPI()
        else:
            self.group_api = group_api
        if not quota:
            self.quota = CephQuota()
        else:
            self.quota = quota

        super().__init__()
示例#7
0
文件: host.py 项目: g711ab/ev-cloud
def get_list(args):
    '''获取宿主机列表'''
    ret_list = []
    group_api = GroupAPI()
    group = group_api.get_group_by_id(args['group_id'])

    if not group.managed_by(args['req_user']):
        return {'res': False, 'err': ERR_AUTH_PERM}

    host_api = HostAPI()
    host_list = host_api.get_host_list_by_group_id(args['group_id'])

    for host in host_list:
        ret_list.append({
            'id': host.id,
            'group_id': host.group_id,
            'ipv4': host.ipv4,
            'vcpu_total': host.vcpu_total,
            'vcpu_allocated': host.vcpu_allocated,
            'mem_total': host.mem_total,
            'mem_allocated': host.mem_allocated,
            'mem_reserved': host.mem_reserved,
            'vm_limit': host.vm_limit,
            'vm_created': host.vm_created,
            'enable': host.enable,
            'net_types': [vlan[1] for vlan in host.vlan_types]
        })
    return {'res': True, 'list': ret_list}
示例#8
0
文件: net.py 项目: bobff/ev-cloud
def get_vlan_list(args=None):
    """获取网络列表"""
    ret_list = []
    group_api = GroupAPI()
    group = group_api.get_group_by_id(args["group_id"])

    if not group.managed_by(args["req_user"]):
        return {"res": False, "err": ERR_AUTH_PERM}

    network_api = NetworkAPI()
    vlans = network_api.get_vlan_list_by_group_id(args["group_id"])

    if vlans:
        vlan_list = []
        for vlan in vlans:
            vlan_list.append(
                {
                    "id": vlan.id,
                    "vlan": vlan.vlan,
                    "br": vlan.br,
                    "type_code": vlan.type_code,
                    "type": vlan.type_name,
                    "enable": vlan.enable,
                    "order": vlan.order,
                    "ip_count": vlan.ip_count,
                    "ip_used": vlan.ip_used,
                }
            )
        return {"res": True, "list": vlan_list}
    return {"res": False, "err": ERR_VLAN_NO_FIND}
示例#9
0
def get_list(args):
    '''获取集群列表'''
    ret_list = []
    group_api = GroupAPI()

    if 'center_id' in args:
        group_list = group_api.get_group_list_in_perm(
            args['req_user'].username, args['center_id'])
    else:
        group_list = group_api.get_group_list_in_perm(
            args['req_user'].username)

    for group in group_list:
        ret_list.append({
            'id':
            group.id,
            'center_id':
            group.center_id,
            'name':
            group.name,
            'desc':
            group.desc,
            'admin_user': [user.username for user in group.admin_user],
            'order':
            group.order
        })
    return {'res': True, 'list': ret_list}
示例#10
0
def create(args):
    api = CephVolumeAPI()
    group_api = GroupAPI()
    group = group_api.get_group_by_id(args['group_id'])
    if not group.managed_by(args['req_user']):
        return {'res': False, 'err': ERR_AUTH_PERM}
    volume_id = api.create(args['group_id'], args['size'])
    api.set_user_id(volume_id, args['req_user'].id)
    return {'res': True, 'volume_id': volume_id}
示例#11
0
文件: image.py 项目: g711ab/ev-cloud
 def managed_by(self, user):
     if type(user) != User:
         raise RuntimeError('user type error.')
     if user.is_superuser:
         return True
     from compute.api import GroupAPI
     return GroupAPI().has_center_perm(user,
                                       self.db_obj.cephpool.host.center.id)
示例#12
0
 def __init__(self, manager=None, vm_api=None, host_api=None, group_api=None):
     if manager:
         self.manager = manager
     else:
         self.manager = GPUManager()
     if vm_api:
         self.vm_api = vm_api
     else:
         self.vm_api = VmAPI()
     if host_api:
         self.host_api = host_api
     else:
         self.host_api = HostAPI()
     if group_api:
         self.group_api = group_api
     else:
         self.group_api = GroupAPI()
示例#13
0
文件: group.py 项目: bobff/ev-cloud
def get(args):    
    group_api = GroupAPI()
    group = group_api.get_group_by_id(args['group_id'])

    if not group.managed_by(args['req_user']):
        return {'res': False, 'err': ERR_AUTH_PERM}
    
    return {'res': True,
            'info': {
                'id':   group.id,
                'center_id': group.center_id,
                'name': group.name,
                'desc': group.desc,
                'admin_user': [user.username for user in group.admin_user],
                'order': group.order
                }
            }
示例#14
0
def get(args):
    group_api = GroupAPI()
    group = group_api.get_group_by_id(args['group_id'])

    if not group.managed_by(args['req_user']):
        return {'res': False, 'err': ERR_AUTH_PERM}

    return {
        'res': True,
        'info': {
            'id': group.id,
            'center_id': group.center_id,
            'name': group.name,
            'desc': group.desc,
            'admin_user': [user.username for user in group.admin_user],
            'order': group.order
        }
    }
示例#15
0
def get_quota_list(args):
    # if not args['req_user'].is_superuser:
    #     return {'res': False, 'err': ERR_AUTH_PERM}
    group_id = 'group_id' in args and args['group_id']
    group = GroupAPI().get_group_by_id(group_id)
    if not group.managed_by(args['req_user']):
        return {'res': False, 'err': ERR_AUTH_PERM}
    api = VolumeAPI()
    quota_list = api.quota.get_quota_list_by_group_id(group_id=group_id)
    ret_list = []
    for q in quota_list:
        ret_list.append({
            'id': q['id'],
            'group_id': q['group_id'],
            'cephpool_id': q['cephpool_id'],
            'total_g': q['total_g'],
            'volume_g': q['volume_g']
        })
    return {'res': True, 'list': ret_list}
示例#16
0
文件: group.py 项目: bobff/ev-cloud
def get_list(args):
    '''获取集群列表'''
    ret_list = []
    group_api = GroupAPI()
    
    if 'center_id' in args:
        group_list = group_api.get_group_list_in_perm(args['req_user'].username, args['center_id'])
    else:
        group_list = group_api.get_group_list_in_perm(args['req_user'].username)
            
    for group in group_list:
        ret_list.append({
            'id':   group.id,
            'center_id': group.center_id,
            'name': group.name,
            'desc': group.desc,
            'admin_user': [user.username for user in group.admin_user],
            'order': group.order})
    return {'res': True, 'list': ret_list}
示例#17
0
def get_gpu_list(args):
    group_api = GroupAPI()
    group_list = group_api.get_group_list_in_perm(args['req_user'])

    gpu_api = GPUAPI()
    ret_list = []

    for g in group_list:
        gpu_list = gpu_api.get_gpu_list_by_group_id(g.id)
        for gpu in gpu_list:
            ret_list.append({
                'id':   gpu.id,
                'host_ipv4': gpu.host_ipv4,
                'address': gpu.address,
                'vm': gpu.vm,
                'attach_time': gpu.attach_time,
                'enable': gpu.enable,
                'remarks': gpu.remarks})
    return {'res': True, 'list': ret_list}
示例#18
0
文件: device.py 项目: bobff/ev-cloud
def get_gpu_list(args):
    group_api = GroupAPI()
    group_list = group_api.get_group_list_in_perm(args['req_user'])
    
    gpu_api = GPUAPI()
    ret_list = []

    for g in group_list:
        gpu_list = gpu_api.get_gpu_list_by_group_id(g.id)
        for gpu in gpu_list:
            ret_list.append({
                'id':   gpu.id,
                'host_ipv4': gpu.host_ipv4,
                'address': gpu.address,
                'vm': gpu.vm,
                'attach_time': gpu.attach_time,
                'enable': gpu.enable,
                'remarks': gpu.remarks})
    return {'res': True, 'list': ret_list}
示例#19
0
def _get_pool_list_by_group_id(args):
    '''获取指定计算集群(group)可使用的ceph资源池列表,即根据group可用的quota进行筛选'''
    ret_list = []
    storage_api = StorageAPI()
    center_api = CenterAPI()
    volume_api = VolumeAPI()

    group_id = 'group_id' in args and args['group_id']
    print(group_id)
    group = GroupAPI().get_group_by_id(group_id)
    if not group.managed_by(args['req_user']):
        return  {'res': False, 'err': ERR_AUTH_PERM}
    
    quota_list = volume_api.quota.get_quota_list_by_group_id(group_id=group_id)
    print(quota_list)
    pool_list = []
    for q in quota_list:
        if not q['cephpool_id']:
            continue 
        try:
            pool = storage_api.get_pool_by_id(q['cephpool_id'])            
            pool_list.append(pool)            
        except Exception as e:
            print(e)
    
    for pool in pool_list:
        ret_list.append({
            'id':   pool.id,
            'pool': pool.pool,
            'type': pool.type,
            'center_id': pool.center_id,
            'host': pool.host,
            'port': pool.port,
            'uuid': pool.uuid
            })

    return {'res': True, 'list': ret_list}
示例#20
0
文件: api.py 项目: bobff/ev-cloud
 def __init__(self, manager=None, vm_api=None, host_api=None, group_api=None):
     if manager:
         self.manager = manager
     else:
         self.manager = GPUManager()
     if vm_api:
         self.vm_api = vm_api
     else:
         self.vm_api = VmAPI()
     if host_api:
         self.host_api = host_api
     else:
         self.host_api = HostAPI()
     if group_api:
         self.group_api = group_api
     else:
         self.group_api = GroupAPI()
示例#21
0
文件: api.py 项目: bobff/ev-cloud
    def __init__(self, manager=None, storage_api=None, vm_api=None, group_api=None, quota=None):
        if not manager:
            self.manager = CephManager()
        else:
            self.manager = manager
        if not storage_api:
            self.storage_api = CephStorageAPI()
        else:
            self.storage_api = storage_api
        if not vm_api:
            self.vm_api = VmAPI()
        else:
            self.vm_api = vm_api
        if not group_api:
            self.group_api = GroupAPI()
        else:
            self.group_api = group_api
        if not quota:
            self.quota = CephQuota()
        else:
            self.quota = quota

        super().__init__()
示例#22
0
def create(args):  # TODO: 乱
    backend = 'CEPH'  #args.get('backend', 'CEPH')
    host_id = args.get('host_id')
    size = args.get('size')
    req_user = args['req_user']
    pool_id = args.get('pool_id')
    storage_api = get_storage_api(backend)
    api = VolumeAPI(storage_api=storage_api)

    res = True
    err = ''

    if backend == 'CEPH':
        group_id = args.get('group_id')
        group = GroupAPI().get_group_by_id(group_id)
        if not group.managed_by(req_user):
            err = '无权限操作'
            res = False
        if pool_id:
            pool = storage_api.get_pool_by_id(pool_id)
        else:
            pool = storage_api.get_volume_pool_by_center_id(group.center_id)
    # elif backend == 'GFS':
    #     host = HostAPI().get_host_by_id(host_id)
    #     if not host.managed_by(req_user):
    #         err = '无权限操作'
    #         res = False
    #     group_id = host.group_id

    #     # TODO: bad
    #     from storage.models import CephHost, CephPool
    #     h = CephHost.objects.filter(host=host.ipv4).first()
    #     p = CephPool.objects.filter(host=h).first()
    #     if p:
    #         pool = storage_api.get_pool_by_id(p.id)
    #     else:
    #         err = '宿主机无存储资源池'
    #         res = False
    else:
        err = '存储后端参数有误'
        res = False

    if res:
        # if not api.quota.group_quota_validate(group_id, size):
        #     err = '集群存储用量达到上限'
        #     res = False
        if not api.quota.group_pool_quota_validate(group_id, pool.id, size):
            err = '集群在存储卷上的存储用量达到上限'
            res = False

        # if not api.quota.volume_quota_validate(group_id, size):
        #     err = '超过单个云硬盘最大容量限制'
        #     res = False
        if not api.quota.volume_pool_quota_validate(group_id, pool.id, size):
            err = '超过存储卷允许的单个云硬盘最大容量限制'
            res = False

    if res:
        volume_id = api.create(pool.id, size, group_id)
        api.set_user_id(volume_id, args['req_user'].id)
        return {'res': True, 'volume_id': volume_id}

    return {'res': False, 'err': err}
示例#23
0
文件: api.py 项目: bobff/ev-cloud
class CephVolumeAPI(object):
    def __init__(self, manager=None, storage_api=None, vm_api=None, group_api=None, quota=None):
        if not manager:
            self.manager = CephManager()
        else:
            self.manager = manager
        if not storage_api:
            self.storage_api = CephStorageAPI()
        else:
            self.storage_api = storage_api
        if not vm_api:
            self.vm_api = VmAPI()
        else:
            self.vm_api = vm_api
        if not group_api:
            self.group_api = GroupAPI()
        else:
            self.group_api = group_api
        if not quota:
            self.quota = CephQuota()
        else:
            self.quota = quota

        super().__init__()

    def create(self, group_id, size):
        group = self.group_api.get_group_by_id(group_id)
        if not self.quota.group_quota_validate(group_id, size):
            raise Error(ERR_VOLUME_QUOTA_G)
        if not self.quota.volume_quota_validate(group_id, size):
            raise Error(ERR_VOLUME_QUOTA_V)
        cephpool = self.storage_api.get_volume_pool_by_center_id(group.center_id)
        if not cephpool:
            raise Error(ERR_VOLUME_CREATE_NOPOOL)
        if type(size) != int or size <= 0:
            raise Error(ERR_INT_VOLUME_SIZE)
        return self.manager.create_volume(cephpool, size, group_id)

    def delete(self, volume_id, force=False):
        volume = self.get_volume_by_id(volume_id)
        if volume.vm:
            raise Error(ERR_DEL_MOUNTED_VOLUME)
        cephpool_id = volume.cephpool_id
        tmp_volume_name = 'x_' + volume_id
        if self.storage_api.mv(cephpool_id, volume_id, tmp_volume_name):
            if not volume.delete():
                self.storage_api.mv(cephpool_id, tmp_volume_name, volume_id)
                return False

            if force == True:
                if not self.storage_api.rm(cephpool_id, tmp_volume_name):
                    print(ERR_CEPH_RM)
        else:
            raise Error(ERR_CEPH_MV)
        return True
        
    def resize(self, volume_id, size):
        volume = self.get_volume_by_id(volume_id)
        if not self.quota.group_quota_validate(volume.group_id, size, volume.size):
            raise Error(ERR_VOLUME_QUOTA_G)
        if not self.quota.volume_quota_validate(volume.group_id, size):
            raise Error(ERR_VOLUME_QUOTA_V)
        if self.storage_api.resize(volume.cephpool_id, volume.id, size):
            return volume.resize(size)
        else:
            raise Error(ERR_CEPH_RESIZE)
        

    def _get_disk_xml(self, volume, dev):
        cephpool = self.storage_api.get_pool_by_id(volume.cephpool_id)

        xml = volume.xml_tpl % {
            'type': 'network',
            'device': 'disk',
            'driver': 'qemu',
            'auth_user': cephpool.username,
            'auth_type': 'ceph',
            'auth_uuid': cephpool.uuid,
            'source_protocol': 'rbd',
            'pool': cephpool.pool,
            'name': volume.id,
            'host': cephpool.host,
            'port': cephpool.port,
            'dev': dev
        }
        return xml

    def mount(self, vm_uuid, volume_id):
        volume = self.get_volume_by_id(volume_id)
        vm = self.vm_api.get_vm_by_uuid(vm_uuid)
        if vm.group_id != volume.group_id:
            return False

        disk_list = vm.get_disk_list()
        mounted_volume_list = self.get_volume_list(vm_uuid=vm_uuid)
        for v in mounted_volume_list:
            disk_list.append(v.dev)
        print(disk_list)
        i = 0
        while True:
            j = i
            d = ''
            while True:
                d += chr(ord('a') + j % 26)
                j //= 26
                if j <= 0:
                    break
            if not 'vd' + d in disk_list:
                dev = 'vd' + d
                break
            i+=1

        xml = self._get_disk_xml(volume, dev)
        print(xml)
        if volume.mount(vm_uuid, dev):
            try:
                if self.vm_api.attach_device(vm_uuid, xml):
                    return True
            except Error as e:
                volume.umount()
                raise e
        return False

    def umount(self, volume_id):
        volume = self.get_volume_by_id(volume_id)
        vm_uuid = volume.vm
        if self.vm_api.vm_uuid_exists(vm_uuid):
            vm = self.vm_api.get_vm_by_uuid(vm_uuid)
            disk_list = vm.get_disk_list()
            if not volume.dev in disk_list:
                if volume.umount():
                    return True
                return False
            xml = self._get_disk_xml(volume, volume.dev)
            if self.vm_api.detach_device(vm_uuid, xml):
                try:
                    if volume.umount():
                        return True
                except Error as e:
                    self.vm_api.attach_device(vm_uuid, xml)
                    raise e
        else:
            if volume.umount():
                return True
        return False

    def set_remark(self, volume_id, content):
        volume = self.get_volume_by_id(volume_id)
        return volume.set_remark(content)

    def get_volume_list_by_pool_id(self, cephpool_id):
        cephpool = self.storage_api.get_pool_by_id(cephpool_id)
        return self.manager.get_volume_list(cephpool_id=cephpool_id)
        
    def get_volume_list_by_user_id(self, user_id):
        return self.manager.get_volume_list(user_id=user_id)

    def get_volume_list_by_group_id(self, group_id):
        return self.manager.get_volume_list(group_id=group_id)

    def get_volume_list_by_vm_uuid(self, vm_uuid):
        return self.manager.get_volume_list(vm_uuid=vm_uuid)
        
    def get_volume_list(self, user_id=None, creator=None, cephpool_id=None, group_id=None, vm_uuid=None):
        return self.manager.get_volume_list(user_id=user_id, creator=creator, cephpool_id=cephpool_id, group_id=group_id, vm_uuid=vm_uuid)

    def get_volume_by_id(self, volume_id):
        return self.manager.get_volume_by_id(volume_id)

    def set_user_id(self, volume_id, user_id):
        volume = self.get_volume_by_id(volume_id)
        return volume.set_user_id(user_id)

    def set_group_id(self, volume_id, group_id):
        volume = self.get_volume_by_id(volume_id)
        return volume.set_group_id(group_id)
示例#24
0
 def managed_by(self, user):
     if user.is_superuser:
         return True
     from compute.api import GroupAPI
     return GroupAPI().has_center_perm(user, self.center_id)
示例#25
0
文件: api.py 项目: bobff/ev-cloud
class GPUAPI(object):
    def __init__(self, manager=None, vm_api=None, host_api=None, group_api=None):
        if manager:
            self.manager = manager
        else:
            self.manager = GPUManager()
        if vm_api:
            self.vm_api = vm_api
        else:
            self.vm_api = VmAPI()
        if host_api:
            self.host_api = host_api
        else:
            self.host_api = HostAPI()
        if group_api:
            self.group_api = group_api
        else:
            self.group_api = GroupAPI()

    def get_gpu_list_by_host_id(self, host_id):
        host = self.host_api.get_host_by_id(host_id)
        return self.manager.get_gpu_list(host_id = host_id)

    def get_gpu_list_by_group_id(self, group_id):
        group = self.group_api.get_group_by_id(group_id)
        return self.manager.get_gpu_list(group_id=group_id)

    def get_gpu_by_id(self, gpu_id):
        return self.manager.get_gpu_by_id(gpu_id)

    def get_gpu_by_address(self, address):
        return self.manager.get_gpu_by_address(address)

    def get_gpu_list_by_vm_uuid(self, vm_uuid):
        return self.manager.get_gpu_list(vm_uuid=vm_uuid)
        
    def set_remarks(self, gpu_id, content):
        gpu = self.manager.get_gpu_by_id(gpu_id)
        return gpu.set_remarks(content)

    def mount(self, vm_id, gpu_id):
        gpu = self.manager.get_gpu_by_id(gpu_id)
        vm = self.vm_api.get_vm_by_uuid(vm_id)
        if vm.host_id != gpu.host_id:
            return False
        if gpu.mount(vm_id):
            if self.vm_api.attach_device(vm_id, gpu.xml_desc):
                return True
            gpu.umount()
        return False

    def umount(self, gpu_id):
        gpu = self.manager.get_gpu_by_id(gpu_id)
        if self.vm_api.vm_uuid_exists(gpu.vm):
            vm = self.vm_api.get_vm_by_uuid(gpu.vm)
            if vm and vm.host_id != gpu.host_id:
                return False
            if self.vm_api.detach_device(vm.uuid, gpu.xml_desc):
                if gpu.umount():
                    return True
                self.vm_api.attach_device(vm.uuid, gpu.xml_desc)
        else:
            if gpu.umount():
                return True
        return False
示例#26
0
文件: api.py 项目: g711ab/ev-cloud
class CephVolumeAPI(object):
    def __init__(self,
                 manager=None,
                 storage_api=None,
                 vm_api=None,
                 group_api=None,
                 quota=None):
        if not manager:
            self.manager = CephManager()
        else:
            self.manager = manager
        if not storage_api:
            self.storage_api = CephStorageAPI()
        else:
            self.storage_api = storage_api
        if not vm_api:
            self.vm_api = VmAPI()
        else:
            self.vm_api = vm_api
        if not group_api:
            self.group_api = GroupAPI()
        else:
            self.group_api = group_api
        if not quota:
            self.quota = CephQuota()
        else:
            self.quota = quota

        super().__init__()

    def create(self, group_id, size):
        group = self.group_api.get_group_by_id(group_id)
        if not self.quota.group_quota_validate(group_id, size):
            raise Error(ERR_VOLUME_QUOTA_G)
        if not self.quota.volume_quota_validate(group_id, size):
            raise Error(ERR_VOLUME_QUOTA_V)
        cephpool = self.storage_api.get_volume_pool_by_center_id(
            group.center_id)
        if not cephpool:
            raise Error(ERR_VOLUME_CREATE_NOPOOL)
        if type(size) != int or size <= 0:
            raise Error(ERR_INT_VOLUME_SIZE)
        return self.manager.create_volume(cephpool, size, group_id)

    def delete(self, volume_id, force=False):
        volume = self.get_volume_by_id(volume_id)
        if volume.vm:
            raise Error(ERR_DEL_MOUNTED_VOLUME)
        cephpool_id = volume.cephpool_id
        tmp_volume_name = 'x_' + volume_id
        if self.storage_api.mv(cephpool_id, volume_id, tmp_volume_name):
            if not volume.delete():
                self.storage_api.mv(cephpool_id, tmp_volume_name, volume_id)
                return False

            if force == True:
                if not self.storage_api.rm(cephpool_id, tmp_volume_name):
                    print(ERR_CEPH_RM)
        else:
            raise Error(ERR_CEPH_MV)
        return True

    def resize(self, volume_id, size):
        volume = self.get_volume_by_id(volume_id)
        if not self.quota.group_quota_validate(volume.group_id, size,
                                               volume.size):
            raise Error(ERR_VOLUME_QUOTA_G)
        if not self.quota.volume_quota_validate(volume.group_id, size):
            raise Error(ERR_VOLUME_QUOTA_V)
        if self.storage_api.resize(volume.cephpool_id, volume.id, size):
            return volume.resize(size)
        else:
            raise Error(ERR_CEPH_RESIZE)

    def _get_disk_xml(self, volume, dev):
        cephpool = self.storage_api.get_pool_by_id(volume.cephpool_id)

        xml = volume.xml_tpl % {
            'type': 'network',
            'device': 'disk',
            'driver': 'qemu',
            'auth_user': cephpool.username,
            'auth_type': 'ceph',
            'auth_uuid': cephpool.uuid,
            'source_protocol': 'rbd',
            'pool': cephpool.pool,
            'name': volume.id,
            'host': cephpool.host,
            'port': cephpool.port,
            'dev': dev
        }
        return xml

    def mount(self, vm_uuid, volume_id):
        volume = self.get_volume_by_id(volume_id)
        vm = self.vm_api.get_vm_by_uuid(vm_uuid)
        if vm.group_id != volume.group_id:
            return False

        disk_list = vm.get_disk_list()
        mounted_volume_list = self.get_volume_list(vm_uuid=vm_uuid)
        for v in mounted_volume_list:
            disk_list.append(v.dev)
        print(disk_list)
        i = 0
        while True:
            j = i
            d = ''
            while True:
                d += chr(ord('a') + j % 26)
                j //= 26
                if j <= 0:
                    break
            if not 'vd' + d in disk_list:
                dev = 'vd' + d
                break
            i += 1

        xml = self._get_disk_xml(volume, dev)
        print(xml)
        if volume.mount(vm_uuid, dev):
            try:
                if self.vm_api.attach_device(vm_uuid, xml):
                    return True
            except Error as e:
                volume.umount()
                raise e
        return False

    def umount(self, volume_id):
        volume = self.get_volume_by_id(volume_id)
        vm_uuid = volume.vm
        if self.vm_api.vm_uuid_exists(vm_uuid):
            vm = self.vm_api.get_vm_by_uuid(vm_uuid)
            disk_list = vm.get_disk_list()
            if not volume.dev in disk_list:
                if volume.umount():
                    return True
                return False
            xml = self._get_disk_xml(volume, volume.dev)
            if self.vm_api.detach_device(vm_uuid, xml):
                try:
                    if volume.umount():
                        return True
                except Error as e:
                    self.vm_api.attach_device(vm_uuid, xml)
                    raise e
        else:
            if volume.umount():
                return True
        return False

    def set_remark(self, volume_id, content):
        volume = self.get_volume_by_id(volume_id)
        return volume.set_remark(content)

    def get_volume_list_by_pool_id(self, cephpool_id):
        cephpool = self.storage_api.get_pool_by_id(cephpool_id)
        return self.manager.get_volume_list(cephpool_id=cephpool_id)

    def get_volume_list_by_user_id(self, user_id):
        return self.manager.get_volume_list(user_id=user_id)

    def get_volume_list_by_group_id(self, group_id):
        return self.manager.get_volume_list(group_id=group_id)

    def get_volume_list_by_vm_uuid(self, vm_uuid):
        return self.manager.get_volume_list(vm_uuid=vm_uuid)

    def get_volume_list(self,
                        user_id=None,
                        creator=None,
                        cephpool_id=None,
                        group_id=None,
                        vm_uuid=None):
        return self.manager.get_volume_list(user_id=user_id,
                                            creator=creator,
                                            cephpool_id=cephpool_id,
                                            group_id=group_id,
                                            vm_uuid=vm_uuid)

    def get_volume_by_id(self, volume_id):
        return self.manager.get_volume_by_id(volume_id)

    def set_user_id(self, volume_id, user_id):
        volume = self.get_volume_by_id(volume_id)
        return volume.set_user_id(user_id)

    def set_group_id(self, volume_id, group_id):
        volume = self.get_volume_by_id(volume_id)
        return volume.set_group_id(group_id)
示例#27
0
import logging

from compute.api import CenterAPI, GroupAPI, VmAPI, HostAPI
from compute.vm.vm import VIR_DOMAIN_HOST_DOWN

from device.api import GPUAPI
from volume.api import VolumeAPI

from api.error import Error
from api.error import ERROR_CN

from .api import MonitoringAPI

api = MonitoringAPI()
center_api = CenterAPI()
group_api = GroupAPI()
vm_api = VmAPI()
host_api = HostAPI()
gpuapi = GPUAPI()
volumeapi = VolumeAPI()


def run_ha_monitoring():
    """
    虚拟机高可用定时监控
    lzx: 2018-09-25
    """
    global center_api, group_api, vm_api, gpuapi, volumeapi

    group_list = group_api.get_group_list()
示例#28
0
class GPUAPI(object):
    def __init__(self, manager=None, vm_api=None, host_api=None, group_api=None):
        if manager:
            self.manager = manager
        else:
            self.manager = GPUManager()
        if vm_api:
            self.vm_api = vm_api
        else:
            self.vm_api = VmAPI()
        if host_api:
            self.host_api = host_api
        else:
            self.host_api = HostAPI()
        if group_api:
            self.group_api = group_api
        else:
            self.group_api = GroupAPI()

    def get_gpu_list_by_host_id(self, host_id):
        host = self.host_api.get_host_by_id(host_id)
        return self.manager.get_gpu_list(host_id = host_id)

    def get_gpu_list_by_group_id(self, group_id):
        group = self.group_api.get_group_by_id(group_id)
        return self.manager.get_gpu_list(group_id=group_id)

    def get_gpu_by_id(self, gpu_id):
        return self.manager.get_gpu_by_id(gpu_id)

    def get_gpu_by_address(self, address):
        return self.manager.get_gpu_by_address(address)

    def get_gpu_list_by_vm_uuid(self, vm_uuid):
        return self.manager.get_gpu_list(vm_uuid=vm_uuid)
        
    def set_remarks(self, gpu_id, content):
        gpu = self.manager.get_gpu_by_id(gpu_id)
        return gpu.set_remarks(content)

    def mount(self, vm_id, gpu_id):
        gpu = self.manager.get_gpu_by_id(gpu_id)
        vm = self.vm_api.get_vm_by_uuid(vm_id)
        if vm.host_id != gpu.host_id:
            return False

        if gpu.mount(vm_id):
            if self.vm_api.attach_device(vm_id, gpu.xml_desc):
                return True
            gpu.umount()
        return False

    def umount(self, gpu_id):
        gpu = self.manager.get_gpu_by_id(gpu_id)
        if self.vm_api.vm_uuid_exists(gpu.vm):
            vm = self.vm_api.get_vm_by_uuid(gpu.vm)
            if vm and vm.host_id != gpu.host_id:
                return False
            if self.vm_api.detach_device(vm.uuid, gpu.xml_desc):
                if gpu.umount():
                    return True
                self.vm_api.attach_device(vm.uuid, gpu.xml_desc)
        else:
            if gpu.umount():
                return True
        return False