Esempio n. 1
0
def get_vmware_config(request):
    pointid = request.GET.get('pointid')
    ret = {'e': '操作成功', 'r': 0}
    allPlans = VirtualCenterConnectionView().get(request).data
    rowList = list()
    for planAttr in allPlans:
        one_info = {
            'id': planAttr['id'],
            'address': '{}({})'.format(planAttr['address'],
                                       planAttr['username'])
        }
        rowList.append(one_info)
        rowList.sort(key=lambda x: x['id'])
    ret['vcenterlist'] = rowList

    host_snapshot = HostSnapshot.objects.get(id=pointid.split('|')[1])
    host_snapshot_ext_config = json.loads(host_snapshot.ext_info)
    vm_cfg = host_snapshot_ext_config['vmware_config']

    ret['vmconfig'] = {
        "vmname": vm_cfg["name"],
        "hardware": {
            "numCPU": vm_cfg['hardware']["numCPU"],
            "numCoresPerSocket": vm_cfg['hardware']["numCoresPerSocket"],
            "memoryMB": vm_cfg['hardware']["memoryMB"]
        }
    }
    return HttpResponse(json.dumps(ret, ensure_ascii=False))
Esempio n. 2
0
def add_host(request):
    data = json.loads(request.POST['data'])
    address = data['address']
    username = data['username']
    password = data['password']
    ret = {'e': '', 'r': 0, 'debug': ''}
    api, e = _get_api_instance_ex(address, username, password, 443)  # 验证连接信息
    if not api:
        ret['e'] = _fmt_vmware_proxy_msg(e)
        ret['r'] = 1
        return HttpResponse(json.dumps(ret, ensure_ascii=False))
    else:
        api_request = {
            'address': address,
            'username': username,
            'password': password
        }
        rsp = VirtualCenterConnectionView().post(request, api_request)
        if status.is_success(rsp.status_code):
            _record_log(request, {
                '操作': '添加数据中心连接',
                '结果': '添加成功',
                '数据中心': address,
                '账号': username
            })
            return HttpResponse(json.dumps(ret))
        else:
            ret['e'] = '创建失败,内部异常。'
            ret['r'] = 1
            return HttpResponse(json.dumps(ret, ensure_ascii=False))
Esempio n. 3
0
def check_vmname(request):
    vmname = request.GET.get('vmname')
    vcenter_id = request.GET.get('vcenter_id')
    allPlans = VirtualCenterConnectionView().get(request, {
        'id': vcenter_id
    }).data
    address = None
    for planAttr in allPlans:
        address = planAttr['address']
        username = planAttr['username']
        password = planAttr['password']
        port = int(planAttr['port'])
        break

    api = _get_api_instance(address, username, password, port)
    if api is None:
        return HttpResponse(
            json.dumps({
                'r': 1,
                'e': '连接信息无效'
            }, ensure_ascii=False))
    tmpname = api.get_vm_by_name(vmname)
    if tmpname is not None:
        return HttpResponse(
            json.dumps({
                'r': 2,
                'e': '虚拟机:{},已存在'.format(vmname)
            },
                       ensure_ascii=False))
    return HttpResponse(json.dumps({'r': 0, 'e': '操作成功'}, ensure_ascii=False))
Esempio n. 4
0
def del_host(request):
    obj_ids = request.GET['taskid']
    ret = {'e': '', 'r': 0, 'debug': ''}
    for obj_id in obj_ids.split(','):
        api_request = {'id': obj_id}
        rsp = VirtualCenterConnectionView().delete(request, api_request)
        if status.is_success(rsp.status_code):
            _record_log(
                request, {
                    '操作': '删除数据中心连接',
                    '结果': '删除成功',
                    '数据中心': rsp.data['address'],
                    '账号': rsp.data['username']
                })
        else:
            ret['e'] = rsp.data
            ret['r'] = 1
            return HttpResponse(json.dumps(ret, ensure_ascii=False))

    return HttpResponse(json.dumps(ret))
Esempio n. 5
0
def list_host(request):
    paramsQD = request.GET
    perPage = paramsQD.get('rows', '10')  # 设置每页条数
    targPage = paramsQD.get('page', '1')  # 返回第几页
    search_key = request.GET.get('s_key', None)

    allPlans = VirtualCenterConnectionView().get(request).data
    rowList = list()
    for planAttr in allPlans:
        one_info = {
            'id': planAttr['id'],
            'cell':
            [planAttr['id'], planAttr['address'], planAttr['username']]
        }
        is_need = serversmgr.filter_hosts(search_key, one_info['cell'][1],
                                          one_info['cell'][2])
        if is_need:
            rowList.append(one_info)
        else:
            pass
        rowList.sort(key=lambda x: x['id'])

    paginator = Paginator(object_list=rowList, per_page=perPage)
    plansNum = paginator.count
    pagesNum = paginator.num_pages
    getPlans = paginator.page(targPage).object_list

    retInfo = {
        'r': 0,
        'a': 'list',
        'page': targPage,
        'total': pagesNum,
        'records': plansNum,
        'rows': getPlans
    }
    functions.sort_gird_rows(request, retInfo)
    jsonStr = json.dumps(retInfo, ensure_ascii=False)

    return HttpResponse(jsonStr)
Esempio n. 6
0
def getdatastore(request):
    ret = {'e': '操作成功', 'r': 0}
    vcenter_id = request.GET.get('vcenter_id')
    ret['datastorelist'] = list()
    filter = {'id': vcenter_id}
    allPlans = VirtualCenterConnectionView().get(request, filter).data
    address = None
    for planAttr in allPlans:
        if str(vcenter_id) == str(planAttr['id']):
            address = planAttr['address']
            username = planAttr['username']
            password = planAttr['password']
            port = int(planAttr['port'])
            break

    if not address:
        ret['e'] = '没找到数据存储或数据存储集群。'
        ret['r'] = 2
        return HttpResponse(json.dumps(ret, ensure_ascii=False))

    api = _get_api_instance(address, username, password, port)  # 验证连接信息
    if not api:
        ret['e'] = '创建失败,连接信息无效。'
        ret['r'] = 1
        return HttpResponse(json.dumps(ret, ensure_ascii=False))
    else:
        datastore_list = api.list_data_stores()
        for datastore in datastore_list:
            ret['datastorelist'].append({
                "id":
                datastore['name'],
                "name":
                "{name}(可用空间:{freeSpace})".format(name=datastore['name'],
                                                  freeSpace=_fmt_size(
                                                      datastore['freeSpace']))
            })
    return HttpResponse(json.dumps(ret, ensure_ascii=False))
Esempio n. 7
0
def restorevm(request):
    pointid = request.POST.get('pointid')
    vmname = request.POST.get('vmname')
    vcenter_id = request.POST.get('vcenter_id')
    memoryMB = request.POST.get('memoryMB')
    vm_datastore = request.POST.get('vm_datastore')
    numCPU = request.POST.get('numCPU')
    numCoresPerSocket = request.POST.get('numCoresPerSocket')
    ret = {'e': '操作成功', 'r': 0}

    allPlans = VirtualCenterConnectionView().get(request, {
        'id': vcenter_id
    }).data
    address = None
    for planAttr in allPlans:
        address = planAttr['address']
        username = planAttr['username']
        password = planAttr['password']
        break

    ext_config = {
        "vmname": vmname,
        "vcenter_id": vcenter_id,
        "memoryMB": memoryMB,
        "vm_datastore": vm_datastore,
        "numCPU": numCPU,
        "numCoresPerSocket": numCoresPerSocket
    }
    api_request = {
        "host_snapshot": pointid.split('|')[1],
        "ext_config": json.dumps(ext_config, ensure_ascii=False)
    }

    api_response = VirtualHostRestore().post(None, api_request)
    if not status.is_success(api_response.status_code):
        ret['r'] = 1
        e = get_response_error_string(api_response)
        ret['e'] = e
        debug = "TakeOverKVMCreate().post() failed {}".format(
            api_response.status_code)
        _record_log(
            request, {
                '操作': 'vmware恢复',
                '结果': '操作失败,e={}'.format(ret['e']),
                'vCenter Server/ESXi': address,
                '虚拟机名称': vmname,
                'debug': debug
            })
        return HttpResponse(
            json.dumps({
                "r": 1,
                "e": e,
                "debug": debug
            }, ensure_ascii=False))

    _record_log(
        request, {
            '操作': 'vmware恢复',
            '结果': ret['e'],
            'vCenter Server/ESXi': address,
            '虚拟机名称': vmname
        })
    return HttpResponse(json.dumps(ret, ensure_ascii=False))