Beispiel #1
0
def image_upload(request):
    if request.session.get('token', 'none') != 'none':
        session_created = create_session_from_token(
            request.session.get('token', 'none'))
        if not session_created.is_authenticated():
            return redirect('logout_view')
        api_ops = opsbase(auth_session=session_created.session_auth)
    else:
        return redirect('logout_view')

    list_format = FORMAT_IMAGE
    form = CreateImageForm(request.POST or None)
    # if request.method == 'POST':
    #     print(request.FILES['uploadFromPC'])
    if form.is_valid() and request.FILES['uploadFromPC']:
        key = form.cleaned_data['name']
        disk_format = form.cleaned_data['disk_format']
        file_image = request.FILES['uploadFromPC']
        # print(request.FILES['uploadFromPC'])
        # print(type(request.FILES['uploadFromPC']))
        path_file = os.path.join(settings.PATH_CACHE, file_image.name)
        #print(path_file)
        path_save = default_storage.save(path_file,
                                         ContentFile(file_image.read()))
        #print(path)
        api_ops.image_upload_image(name=key,
                                   path=path_save,
                                   disk_format='qcow2')
        messages.add_message(
            request, messages.INFO,
            'Tạo động thành công, đợi 5-10s để hệ thống cập nhật trạng thái')
        return redirect('image_list')
    print(form.errors)
    return render(request, 'dashboard/image/upload.html',
                  {'formats': list_format})
Beispiel #2
0
def server_delete(request, key='', name=''):
    if request.session.get('token', 'none') != 'none':
        session_created = create_session_from_token(
            request.session.get('token', 'none'))
        if not session_created.is_authenticated():
            return redirect('logout_view')
        api_ops = opsbase(auth_session=session_created.session_auth)
    else:
        return redirect('logout_view')

    if request.method == "POST":
        form = StateServerForm(request.POST or None)
        if form.is_valid():
            key_id = form.cleaned_data['key']
            #print(key_id)
            api_ops.compute_delete_server(key_id)
            messages.add_message(
                request, messages.INFO,
                'Xóa thành công, đợi 5-10s để hệ thống cập nhật trạng thái')
            return redirect('server_list')
        return render(request, 'dashboard/server/delete.html')
    return render(request, 'dashboard/server/delete.html', {
        'id': key,
        'name': name
    })
Beispiel #3
0
def flavor_create(request):
    if request.session.get('token', 'none') != 'none':
        session_created = create_session_from_token(
            request.session.get('token', 'none'))
        if not session_created.is_authenticated():
            return redirect('logout_view')
        api_ops = opsbase(auth_session=session_created.session_auth)
    else:
        return redirect('logout_view')

    if request.method == "POST":
        form = CreateFlavorForm(request.POST or None)
        if form.is_valid():
            key = form.cleaned_data['name']
            vcpus = form.cleaned_data['vcpus']
            ram = form.cleaned_data['ram']
            disk = form.cleaned_data['disk']

            api_ops.compute_create_flavor(name=key,
                                          vcpu=vcpus,
                                          ram=ram,
                                          disk=disk)
            messages.add_message(
                request, messages.INFO,
                'Tạo động thành công, đợi 5-10s để hệ thống cập nhật trạng thái'
            )
            return redirect('flavor_list')
        return render(request, 'dashboard/flavor/create.html')

    return render(request, 'dashboard/flavor/create.html')
Beispiel #4
0
def image_list(request):
    if request.session.get('token', 'none') != 'none':
        session_created = create_session_from_token(
            request.session.get('token', 'none'))
        if not session_created.is_authenticated():
            return redirect('logout_view')
        api_ops = opsbase(auth_session=session_created.session_auth)
    else:
        return redirect('logout_view')

    list_object = api_ops.image_list_images()
    return render(request, 'dashboard/image_list.html',
                  {'images': list_object})
Beispiel #5
0
def server_list(request):
    #print(request.session.get('token','none'))
    if request.session.get('token', 'none') != 'none':
        session_created = create_session_from_token(
            request.session.get('token', 'none'))
        #print(session_created.is_authenticated())
        if not session_created.is_authenticated():
            return redirect('logout_view')
        api_ops = opsbase(auth_session=session_created.session_auth)
        list_object = api_ops.compute_list_servers
        return render(request, 'dashboard/server_list.html',
                      {'servers': list_object})
    return redirect('login_view')
Beispiel #6
0
def server_get_vnc_console(request, key='', name=''):
    if request.session.get('token', 'none') != 'none':
        session_created = create_session_from_token(
            request.session.get('token', 'none'))
        if not session_created.is_authenticated():
            return redirect('logout_view')
        api_ops = opsbase(auth_session=session_created.session_auth)
        sess = session.Session(auth=session_created.session_auth)
        nova_session = nova_client.Client(version=2, session=sess)
        url = nova_session.servers.get_vnc_console(key,
                                                   'novnc')['console']['url']
        return redirect(url)
    else:
        return redirect('logout_view')
Beispiel #7
0
def server_get_console(request, key='', name=''):
    if request.session.get('token', 'none') != 'none':
        session_created = create_session_from_token(
            request.session.get('token', 'none'))
        if not session_created.is_authenticated():
            return redirect('logout_view')
        api_ops = opsbase(auth_session=session_created.session_auth)
    else:
        return redirect('logout_view')

    server_console = api_ops.compute_get_console_server(key)
    return render(request, 'dashboard/server/get_console.html', {
        'id': key,
        'name': name,
        'server_console': server_console
    })
Beispiel #8
0
def server_create(request, key='', name=''):
    if request.session.get('token', 'none') != 'none':
        session_created = create_session_from_token(
            request.session.get('token', 'none'))
        if not session_created.is_authenticated():
            return redirect('logout_view')
        api_ops = opsbase(auth_session=session_created.session_auth)
    else:
        return redirect('logout_view')

    if request.method == "POST":
        form = CreateServerForm(request.POST or None)
        if form.is_valid():
            key_id = form.cleaned_data['name']
            flavor_id = form.cleaned_data['flavor_id']
            image_id = form.cleaned_data['image_id']
            network_id = form.cleaned_data['network_id']

            api_ops.compute_create_vm(name_vm=key_id,
                                      name_image=image_id,
                                      name_flavor=flavor_id,
                                      name_network=network_id)
            messages.add_message(
                request, messages.INFO,
                'Tạo động thành công, đợi 5-10s để hệ thống cập nhật trạng thái'
            )
            return redirect('server_list')
        return render(request, 'dashboard/server/create.html')

    list_image = api_ops.image_list_images()
    list_flavor = api_ops.compute_list_flavors
    list_network = api_ops.network_list_networks

    return render(request, 'dashboard/server/create.html', {
        'images': list_image,
        'flavors': list_flavor,
        'networks': list_network
    })