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}
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}
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 = StorageAPI() 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 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}
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}
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)
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}
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(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 } }
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}
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}
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}
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)
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()
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}