Example #1
0
def migrate(args):
    #被迁移虚拟机校验
    api = VmAPI()
    host_api = HostAPI()
    vm = api.get_vm_by_uuid(args['uuid'])
    if not vm:
        return {'res': False, 'err': ERR_VM_UUID}
    if not vm.can_operate_by(args['req_user']):
        return {'res': False, 'err': ERR_AUTH_PERM}

    #目标主机校验
    host = host_api.get_host_by_id(args['host_id'])
    if not host.managed_by(args['req_user']):
        return {'res': False, 'err': ERR_AUTH_PERM}

    #被迁移虚拟机与目标主机是否处于同一个分中心
    if not vm.center_id == host.center_id:
        return {'res': False, 'err': ERR_AUTH_PERM}

    if vm.host_id == host.id:
        return {'res': False, 'err': ERR_VM_MIGRATE_SAME_HOST}

    res = api.migrate_vm(args['uuid'], args['host_id'])
    if res:
        return {'res': True}
    return {'res': False, 'err': ERR_VM_MIGRATE}
Example #2
0
File: vm.py Project: bobff/ev-cloud
def migrate(args):
    #被迁移虚拟机校验
    api = VmAPI()
    host_api = HostAPI()
    vm = api.get_vm_by_uuid(args['uuid'])
    if not vm:
        return {'res': False, 'err': ERR_VM_UUID}
    if not vm.can_operate_by(args['req_user']):
        return {'res': False, 'err': ERR_AUTH_PERM}
    
    #目标主机校验
    host = host_api.get_host_by_id(args['host_id'])
    if not host.managed_by(args['req_user']):
        return {'res': False, 'err': ERR_AUTH_PERM}
    
    #被迁移虚拟机与目标主机是否处于同一个分中心
    if not vm.center_id == host.center_id:
        return {'res': False, 'err': ERR_AUTH_PERM}
    
    if vm.host_id == host.id:
        return {'res': False, 'err': ERR_VM_MIGRATE}
    
    res = api.migrate_vm(args['uuid'], args['host_id'])
    if res:
        return {'res': True}
    return {'res': False, 'err': ERR_VM_MIGRATE}

    
Example #3
0
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}
Example #4
0
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}
Example #5
0
 def handle(self, *args, **options):
     host_ip = options['host_ip']
     host_api = HostAPI()
     host = host_api.get_host_by_ipv4(host_ip)
     api = GPUAPI()
     gpu_list = api.get_gpu_list_by_host_id(host.id)
     for gpu in gpu_list:
         print(gpu.host_ipv4, gpu.address, gpu.vm)
Example #6
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()
Example #7
0
def get(args):
    host_api = HostAPI()
    host = host_api.get_host_by_id(args['host_id'])
    
    if not host.managed_by(args['req_user']):
        return {'res': False, 'err': ERR_AUTH_PERM}
     
    return {'res': True,
            'info': {
                     'id': host.id,
                     'group_id': host.group_id,
                     'vlan_list': host.vlans,
                     '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
                     }}
Example #8
0
 def __init__(self, manager=None, vm_api=None, host_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()
Example #9
0
def migrate(args):
    #被迁移虚拟机校验
    api = VmAPI()
    host_api = HostAPI()
    gpu_api = GPUAPI()
    volume_api = VolumeAPI()
    vm = api.get_vm_by_uuid(args['uuid'])
    if not vm:
        return {'res': False, 'err': ERR_VM_UUID}
    if not vm.can_operate_by(args['req_user']):
        return {'res': False, 'err': ERR_AUTH_PERM}

    #目标主机校验
    host = host_api.get_host_by_id(args['host_id'])
    if not host.managed_by(args['req_user']):
        return {'res': False, 'err': ERR_AUTH_PERM}

    #被迁移虚拟机与目标主机是否处于同一个分中心
    if not vm.center_id == host.center_id:
        return {'res': False, 'err': ERR_VM_MIGRATE_DIFF_CEPH}

    #检测目标主机是否为当前宿主机
    if vm.host_id == host.id:
        return {'res': False, 'err': ERR_VM_MIGRATE_SAME_HOST}

    #检测是否挂载GPU
    gpu_list = gpu_api.get_gpu_list_by_vm_uuid(args['uuid'])
    if len(gpu_list) > 0:
        return {'res': False, 'err': ERR_VM_MIGRATE_WITHGPU}

    #检测挂载云硬盘与目标主机是否在同一集群
    volume_list = volume_api.get_volume_list_by_vm_uuid(args['uuid'])
    if len(volume_list) > 0 and vm.group_id != host.group_id:
        return {'res': False, 'err': ERR_VM_MIGRATE_WITHVOL}

    res = api.migrate_vm(args['uuid'], args['host_id'])
    if res:
        return {'res': True}
    return {'res': False, 'err': ERR_VM_MIGRATE}
Example #10
0
def get(args):
    host_api = HostAPI()
    host = host_api.get_host_by_id(args['host_id'])

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

    return {
        'res': True,
        'info': {
            'id': host.id,
            'group_id': host.group_id,
            'vlan_list': host.vlans,
            '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
        }
    }
Example #11
0
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()

    vm_list = []
    vm_dic = {}
Example #12
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
Example #13
0
class GPUAPI(object):
    def __init__(self, manager=None, vm_api=None, host_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()

    def get_gpu_list_by_host_id(self, host_id):
        host = self.host_api.get_host_by_id(host_id)
        db_list = DBGPU.objects.filter(host_id = host_id)
        ret_list = []
        for db in db_list:
            ret_list.append(GPU(db=db))
        return ret_list

    def get_gpu_list_by_group_id(self, group_id):
        db_list = DBGPU.objects.filter(host__group_id = group_id)
        ret_list = []
        for db in db_list:
            ret_list.append(GPU(db=db))
        return ret_list

    def get_gpu_by_id(self, gpu_id):
        db = DBGPU.objects.filter(pk = gpu_id)
        if not db.exists():
            raise Error(ERR_GPU_ID)
        return GPU(db=db[0])

    def get_gpu_by_address(self, address):
        db = DBGPU.objects.filter(address = address)
        if not db.exists():
            raise Error(ERR_GPU_ADDRESS)
        return GPU(db=db[0])

    def get_gpu_list_by_vm_uuid(self, vm_uuid):
        db_list = DBGPU.objects.filter(vm = vm_uuid)
        ret_list = []
        for db in db_list:
            ret_list.append(GPU(db=db))
        return ret_list
        
    def set_remarks(self, gpu_id, content):
        gpu = self.get_gpu_by_id(gpu_id)
        return gpu.set_remarks(content)

    def mount(self, vm_id, gpu_id):
        gpu = self.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.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
Example #14
0
def create(args):  # TODO: 乱
    backend = 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}
Example #15
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