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}
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}
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 }}
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}
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 } }
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
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