예제 #1
0
def api(request):
    if request.method == 'POST':
        try:
            received_json_data = json.loads(request.body)
        except Exception:
            return HttpResponse('ValueError')
        try:
            cluster_ip = received_json_data['cluster']
        except Exception:
            return HttpResponse('clusterip Error')
        try:
            method = received_json_data['method']
        except Exception:
            return HttpResponse('method Error')
        if method == 'getstatus':
            show_list = get_vms_status(cluster_ip)
        elif method == 'getinfo':
            show_list = get_vms_info(cluster_ip)
        elif method == 'resetinfo':
            show_list = reset_vms_info(cluster_ip)
        elif method == 'shutdown':
            show_list = shutdown_vms(cluster_ip)
        elif method == 'poweron':
            show_list = start_up_vms(cluster_ip)
        elif method == 'createvmsinfo':
            show_list = create_vms_info(cluster_ip)
        else:
            return HttpResponse('ERROR * 2!')
        return HttpResponse(json.dumps(show_list))
예제 #2
0
def create_vms_info(cluster_ip):
    info_list = get_vms_status(cluster_ip)

    for i in info_list:
        i['Cluster'] = cluster_ip
        VmInfo.objects.get_or_create(**i)

    return {'succeed': 'Created!'}
예제 #3
0
def vms_status(request):
    if request.method == 'GET':
        cluster_ip = request.GET.get('cluster_ip')
        if cluster_ip == '' or cluster_ip is None:
            return HttpResponse(
                'Cluster IP import Error, Please Check - from vms_status.GET',
                status=400)
        else:
            show_list = get_vms_status(cluster_ip)
            if isinstance(show_list, list):
                return HttpResponse(json.dumps(show_list))
            elif isinstance(show_list, dict) and len(
                    show_list.values()) == 1 and 'error' in show_list.keys():
                return HttpResponse(json.dumps(show_list), status=400)
            else:
                return HttpResponse('rua! rua! rua! - from vms_status.GET')
    else:
        return HttpResponse(
            'Error : Can Only Use GET Request - from vms_status.GET',
            status=400)
예제 #4
0
def reset_vms_info(cluster_ip, reset=True):
    # reset the vminfo from status to db
    real_status_list = get_vms_status(cluster_ip)
    if isinstance(real_status_list, list):
        for i in real_status_list:
            i['Cluster'] = cluster_ip
        t_info_list = get_vms_info(cluster_ip)
        if isinstance(t_info_list, list):
            old_info = OldVmInfo.objects.filter(Cluster=cluster_ip)

            if reset:
                if type(real_status_list) is list and type(t_info_list) is list:
                    for i in t_info_list:
                        i.pop('id')
                # if different, clean the OldVmInfo in cluster_ip, then reset with the real_status_list
                if datadiff.diff(sorted(real_status_list), sorted(t_info_list)):
                    # clean OldVmInfo table
                    old_info_list = list(old_info.values())
                    old_info.delete()
                    # get OldVmInfo data
                    old_cluster_vms = VmInfo.objects.filter(Cluster=cluster_ip)
                    # reset the OldVmInfo table
                    for j in old_cluster_vms.values():
                        OldVmInfo.objects.get_or_create(**j)
                    # clean VmInfo table
                    old_cluster_vms.delete()
                    # reset the VmInfo table with real_status_list
                    for k in real_status_list:
                        VmInfo.objects.get_or_create(**k)
                    return {'now': real_status_list,
                            'old': t_info_list,
                            'deleted': old_info_list
                            }
                else:
                    return {'warning': 'The information is same, Nothing to do! - from reset_vms_info'}
            else:
                return {'warning': 'Reset is False, Nothing to do! - from reset_vms_info'}
        elif isinstance(t_info_list, dict):
            return t_info_list
    else:
        return {'error': 'The import Cluster IP not found in system - from reset_vms_info'}
예제 #5
0
def index(request):
    if request.method == 'POST':
        cluster_form = ClusterForm(request.POST)
        if cluster_form.is_valid():
            cluster_ip = cluster_form.cleaned_data["Cluster"]
            show_list = []
            if 'getstatus' in request.POST:
                show_list = get_vms_status(cluster_ip)
            elif 'getinfo' in request.POST:
                show_list = get_vms_info(cluster_ip)
            elif 'resetinfo' in request.POST:
                show_list = reset_vms_info(cluster_ip)
            elif 'shutdown' in request.POST:
                show_list = shutdown_vms(cluster_ip)
            elif 'poweron' in request.POST:
                show_list = start_up_vms(cluster_ip)
            elif 'createvmsinfo' in request.POST:
                show_list = create_vms_info(cluster_ip)
            # return HttpResponse(json.dumps(show_list))
            return HttpResponse(show_list)
    else:
        cluster_form = ClusterForm()
    return render(request, 'index.html', {'cluster_form': cluster_form})