def vm_status_ajax(req): vmid = req.POST.get('vmid') status = api_vm_status({'req_user': req.user, 'uuid': vmid}) if status['res']: status['vmid'] = vmid else: if status['err'] in ERROR_CN: status['error'] = ERROR_CN[status['err']] return HttpResponse(json.dumps(status), content_type='application/json')
def vm_reset_view(req): dicts = {} arg_vmid = req.GET.get('vmid') if req.method == 'POST': arg_vmid = req.POST.get('vmid') arg_image = req.POST.get('image') status_res = api_vm_status({'req_user': req.user, 'uuid': arg_vmid}) if status_res['res'] and not status_res['status'] in (1, 2, 3, 7): reset_res = api_vm_reset({ 'req_user': req.user, 'uuid': arg_vmid, 'image_id': arg_image }) if reset_res['res']: dicts['result'] = '重置成功。' else: err_msg = ERROR_CN[reset_res['err']] if reset_res[ 'err'] in ERROR_CN else reset_res['err'] dicts['result'] = '重置失败: %s' % err_msg else: dicts['result'] = '重置失败:只能在关机状态重置。' vm_res = api_vm_get({'req_user': req.user, 'uuid': arg_vmid}) if vm_res['res']: dicts['vmobj'] = vm_res['info'] else: return HttpResponseRedirect(reverse("vm_list")) image_res = api_image_get_list({ 'req_user': req.user, 'ceph_id': dicts['vmobj']['ceph_id'], 'enable': True }) image_dic = {} if image_res['res']: for image in image_res['list']: if image['type'] in image_dic: image_dic[image['type']].append(image) else: image_dic[image['type']] = [image] image_type_list_info = api_image_get_type_list() if image_type_list_info['res']: image_type_list = [t['name'] for t in image_type_list_info['list']] else: image_type_list = list(image_dic.keys()) image_list_ordered = [] for image_type in image_type_list: if image_type in image_dic: image_list_ordered.append( (image_type, image_list_sort(image_dic[image_type]))) dicts['image_list_ordered'] = image_list_ordered return render(req, 'vmadmin_reset.html', dicts)
def vm_migrate_view(req): dicts = {} arg_vmid = req.GET.get('vmid') if req.method == 'POST': arg_vmid = req.POST.get('vmid') arg_host = req.POST.get('host') status_res = api_vm_status({'req_user': req.user, 'uuid': arg_vmid}) if status_res['res'] and not status_res['status'] in (1, 2, 3, 7): migrate_res = api_vm_migrate({ 'req_user': req.user, 'uuid': arg_vmid, 'host_id': arg_host }) if migrate_res['res']: dicts['result'] = '迁移成功。' else: err_msg = ERROR_CN[migrate_res['err']] if migrate_res[ 'err'] in ERROR_CN else migrate_res['err'] dicts['result'] = '迁移失败: %s' % err_msg else: dicts['result'] = '迁移失败:只能在关机状态迁移。' vm_res = api_vm_get({'req_user': req.user, 'uuid': arg_vmid}) if vm_res['res']: dicts['vmobj'] = vm_res['info'] else: return HttpResponseRedirect(reverse("vm_list")) group_res = api_group_get_list({ 'req_user': req.user, 'center_id': dicts['vmobj']['center_id'] }) if group_res['res']: host_list = [] for group in group_res['list']: host_res = api_host_get_list({ 'req_user': req.user, 'group_id': group['id'] }) if host_res['res']: for host in host_res['list']: if dicts['vmobj']['host_id'] == host['id'] or host[ 'enable'] == False: continue host['group_name'] = group['name'] host_list.append(host) dicts['host_list'] = host_list return render(req, 'vmadmin_migrate.html', dicts)
def vm_edit_view(req): dicts = {} arg_vmid = req.GET.get('vmid') if req.method == 'POST': arg_cpu = req.POST.get('cpu') arg_mem = req.POST.get('mem') status_res = api_vm_status({'req_user': req.user, 'uuid': arg_vmid}) if status_res['res'] and status_res['status'] != 1: if arg_cpu.isdigit() and arg_mem.isdigit(): edit_res = api_vm_edit({ 'req_user': req.user, 'uuid': arg_vmid, 'vcpu': arg_cpu, 'mem': arg_mem }) if not edit_res['res']: dicts['warning'] = '修改失败。' else: dicts['warning'] = '只能修改处于关机状态的虚拟机。' obj_res = api_vm_get({'req_user': req.user, 'uuid': arg_vmid}) if obj_res['res']: obj = obj_res['info'] else: return HttpResponseRedirect('../list/') dicts['vmobj'] = obj host_res = api_host_get({'req_user': req.user, 'host_id': obj['host_id']}) if host_res['res']: host = host_res['info'] dicts['remain_mem'] = host['mem_total'] - \ host['mem_allocated'] - \ host['mem_reserved'] if dicts['remain_mem'] < 0: dicts['remain_mem'] = 0 dicts['remain_cpu'] = host['vcpu_total'] - host['vcpu_allocated'] if dicts['remain_cpu'] < 0: dicts['remain_cpu'] = 0 return render_to_response('vmadmin_edit.html', dicts, context_instance=RequestContext(req))
def vm_edit_view(req): dicts = {} arg_vmid = req.GET.get('vmid') if req.method == 'POST': arg_cpu = req.POST.get('cpu') arg_mem = req.POST.get('mem') status_res = api_vm_status({'req_user': req.user, 'uuid': arg_vmid}) if status_res['res'] and status_res['status'] != 1: if arg_cpu.isdigit() and arg_mem.isdigit(): edit_res = api_vm_edit({'req_user': req.user, 'uuid': arg_vmid, 'vcpu': arg_cpu, 'mem': arg_mem}) if not edit_res['res']: dicts['warning'] = '修改失败。' else: dicts['warning'] = '只能修改处于关机状态的虚拟机。' obj_res = api_vm_get({'req_user': req.user, 'uuid': arg_vmid}) if obj_res['res']: obj = obj_res['info'] else: return HttpResponseRedirect('../list/') dicts['vmobj'] = obj host_res = api_host_get({'req_user': req.user, 'host_id': obj['host_id']}) if host_res['res']: host = host_res['info'] dicts['remain_mem'] = host['mem_total'] - \ host['mem_allocated'] - \ host['mem_reserved'] if dicts['remain_mem'] < 0: dicts['remain_mem'] = 0 dicts['remain_cpu'] = host['vcpu_total'] - host['vcpu_allocated'] if dicts['remain_cpu'] < 0: dicts['remain_cpu'] = 0 return render_to_response('vmadmin_edit.html', dicts, context_instance=RequestContext(req))
def vm_snap_rollback_ajax_view(req): vmid = req.POST.get('vmid', None) snap_id = req.POST.get('snap_id', None) dicts = {} dicts['res'] = False status_res = api_vm_status({'req_user': req.user, 'uuid': vmid}) if status_res['res'] and status_res['status'] != 1: rollback_res = api_vm_rollback_snap({ 'req_user': req.user, 'uuid': vmid, 'snap_id': snap_id }) if rollback_res['res']: dicts['res'] = True else: err_msg = ERROR_CN[rollback_res['err']] if rollback_res[ 'err'] in ERROR_CN else rollback_res['err'] dicts['error'] = '快照回滚失败: %s' % err_msg else: dicts['error'] = '快照回滚失败:只能在关机状态执行回滚操作。' return HttpResponse(json.dumps(dicts), content_type='application/json')
def vm_migrate_view(req): dicts = {} arg_vmid = req.GET.get('vmid') if req.method == 'POST': arg_vmid = req.POST.get('vmid') arg_host = req.POST.get('host') status_res = api_vm_status({'req_user': req.user, 'uuid': arg_vmid}) if status_res['res'] and not status_res['status'] in (1, 2, 3, 7): migrate_res = api_vm_migrate({'req_user': req.user, 'uuid': arg_vmid, 'host_id': arg_host}) if migrate_res['res']: dicts['result'] = '迁移成功。' else: dicts['result'] = '迁移失败: %s' % migrate_res['err'] else: dicts['result'] = '迁移失败:只能在关机状态迁移。' vm_res = api_vm_get({'req_user': req.user, 'uuid': arg_vmid}) if vm_res['res']: dicts['vmobj'] = vm_res['info'] else: return HttpResponseRedirect('../list/') group_res = api_group_get_list({'req_user': req.user, 'center_id': dicts['vmobj']['center_id']}) if group_res['res']: host_list = [] for group in group_res['list']: host_res = api_host_get_list({'req_user': req.user, 'group_id': group['id']}) if host_res['res']: for host in host_res['list']: if dicts['vmobj']['host_id'] == host['id'] or host['enable'] == False: continue host['group_name'] = group['name'] host_list.append(host) dicts['host_list'] = host_list return render_to_response('vmadmin_migrate.html', dicts, context_instance=RequestContext(req))