Example #1
0
def storage(request, compute_id, pool):
    """
    :param request:
    :return:
    """

    if not request.user.is_superuser:
        return HttpResponseRedirect(reverse('index'))

    def handle_uploaded_file(path, f_name):
        target = path + '/' + str(f_name)
        destination = open(target, 'wb+')
        for chunk in f_name.chunks():
            destination.write(chunk)
        destination.close()

    error_messages = []
    compute = get_object_or_404(Compute, pk=compute_id)
    meta_prealloc = False

    try:
        conn = wvmStorage(compute.hostname,
                          compute.login,
                          compute.password,
                          compute.type,
                          pool)

        storages = conn.get_storages()
        state = conn.is_active()
        size, free = conn.get_size()
        used = (size - free)
        if state:
            percent = (used * 100) / size
        else:
            percent = 0
        status = conn.get_status()
        path = conn.get_target_path()
        type = conn.get_type()
        autostart = conn.get_autostart()

        if state:
            conn.refresh()
            volumes = conn.update_volumes()
        else:
            volumes = None
    except libvirtError as lib_err:
        error_messages.append(lib_err)

    if request.method == 'POST':
        if 'start' in request.POST:
            try:
                conn.start()
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as lib_err:
                error_messages.append(lib_err.message)
        if 'stop' in request.POST:
            try:
                conn.stop()
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as lib_err:
                error_messages.append(lib_err.message)
        if 'delete' in request.POST:
            try:
                conn.delete()
                return HttpResponseRedirect(reverse('storages', args=[compute_id]))
            except libvirtError as lib_err:
                error_messages.append(lib_err.message)
        if 'set_autostart' in request.POST:
            try:
                conn.set_autostart(1)
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as lib_err:
                error_messages.append(lib_err.message)
        if 'unset_autostart' in request.POST:
            try:
                conn.set_autostart(0)
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as lib_err:
                error_messages.append(lib_err.message)
        if 'add_volume' in request.POST:
            form = AddImage(request.POST)
            if form.is_valid():
                data = form.cleaned_data
                if data['meta_prealloc'] and data['format'] == 'qcow2':
                    meta_prealloc = True
                try:
                    conn.create_volume(data['name'], data['size'], data['format'], meta_prealloc)
                    messages.success(request, _("Image file {} is created successfully".format(data['name']+".img")))
                    return HttpResponseRedirect(request.get_full_path())
                except libvirtError as lib_err:
                    error_messages.append(lib_err)
            else:
                for msg_err in form.errors.values():
                    error_messages.append(msg_err.as_text())
        if 'del_volume' in request.POST:
            volname = request.POST.get('volname', '')
            try:
                vol = conn.get_volume(volname)
                vol.delete(0)
                messages.success(request, _('Volume: {} is deleted.'.format(volname)))
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as lib_err:
                error_messages.append(lib_err.message)
        if 'iso_upload' in request.POST:
            if str(request.FILES['file']) in conn.update_volumes():
                error_msg = _("ISO image already exist")
                error_messages.append(error_msg)
            else:
                handle_uploaded_file(path, request.FILES['file'])
                messages.success(request, _('ISO: {} is uploaded.'.format(request.FILES['file'])))
                return HttpResponseRedirect(request.get_full_path())
        if 'cln_volume' in request.POST:
            form = CloneImage(request.POST)
            if form.is_valid():
                data = form.cleaned_data
                img_name = data['name'] + '.img'
                meta_prealloc = 0
                if img_name in conn.update_volumes():
                    msg = _("Name of volume name already use")
                    error_messages.append(msg)
                if not error_messages:
                    if data['convert']:
                        format = data['format']
                        if data['meta_prealloc'] and data['format'] == 'qcow2':
                            meta_prealloc = True
                    else:
                        format = None
                    try:
                        conn.clone_volume(data['image'], data['name'], format, meta_prealloc)
                        messages.success(request, _("{} image cloned as {} successfully".format(data['image'], data['name'] + ".img")))
                        return HttpResponseRedirect(request.get_full_path())
                    except libvirtError as lib_err:
                        error_messages.append(lib_err)
            else:
                for msg_err in form.errors.values():
                    error_messages.append(msg_err.as_text())

    conn.close()

    return render(request, 'storage.html', locals())
Example #2
0
def storage(request, host_id, pool):
    """
    Storage pool block
    """
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('login'))

    def handle_uploaded_file(path, f_name):
        target = path + '/' + str(f_name)
        destination = open(target, 'wb+')
        for chunk in f_name.chunks():
            destination.write(chunk)
        destination.close()

    errors = []
    compute = Compute.objects.get(id=host_id)
    meta_prealloc = False

    try:
        conn = wvmStorage(compute.hostname, compute.login, compute.password,
                          compute.type, pool)

        storages = conn.get_storages()
        state = conn.is_active()
        size, free = conn.get_size()
        used = (size - free)
        if state:
            percent = (used * 100) / size
        else:
            percent = 0
        status = conn.get_status()
        path = conn.get_target_path()
        type = conn.get_type()
        autostart = conn.get_autostart()

        if state:
            conn.refresh()
            volumes = conn.update_volumes()
        else:
            volumes = None
    except libvirtError as err:
        errors.append(err)

    if request.method == 'POST':
        if 'start' in request.POST:
            try:
                conn.start()
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as error_msg:
                errors.append(error_msg.message)
        if 'stop' in request.POST:
            try:
                conn.stop()
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as error_msg:
                errors.append(error_msg.message)
        if 'delete' in request.POST:
            try:
                conn.delete()
                return HttpResponseRedirect(reverse('storages',
                                                    args=[host_id]))
            except libvirtError as error_msg:
                errors.append(error_msg.message)
        if 'set_autostart' in request.POST:
            try:
                conn.set_autostart(1)
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as error_msg:
                errors.append(error_msg.message)
        if 'unset_autostart' in request.POST:
            try:
                conn.set_autostart(0)
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as error_msg:
                errors.append(error_msg.message)
        if 'add_volume' in request.POST:
            form = AddImage(request.POST)
            if form.is_valid():
                data = form.cleaned_data
                if data['meta_prealloc'] and data['format'] == 'qcow2':
                    meta_prealloc = True
                try:
                    conn.create_volume(data['name'], data['size'],
                                       data['format'], meta_prealloc)
                    return HttpResponseRedirect(request.get_full_path())
                except libvirtError as err:
                    errors.append(err)
        if 'del_volume' in request.POST:
            volname = request.POST.get('volname', '')
            try:
                vol = conn.get_volume(volname)
                vol.delete(0)
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as error_msg:
                errors.append(error_msg.message)
        if 'iso_upload' in request.POST:
            if str(request.FILES['file']) in conn.update_volumes():
                msg = _("ISO image already exist")
                errors.append(msg)
            else:
                handle_uploaded_file(path, request.FILES['file'])
                return HttpResponseRedirect(request.get_full_path())
        if 'cln_volume' in request.POST:
            form = CloneImage(request.POST)
            if form.is_valid():
                data = form.cleaned_data
                img_name = data['name'] + '.img'
                meta_prealloc = 0
                if img_name in conn.update_volumes():
                    msg = _("Name of volume name already use")
                    errors.append(msg)
                if not errors:
                    if data['convert']:
                        format = data['format']
                        if data['meta_prealloc'] and data['format'] == 'qcow2':
                            meta_prealloc = True
                    else:
                        format = None
                    try:
                        conn.clone_volume(data['image'], data['name'], format,
                                          meta_prealloc)
                        return HttpResponseRedirect(request.get_full_path())
                    except libvirtError as err:
                        errors.append(err)
    conn.close()

    return render_to_response('storage.html',
                              locals(),
                              context_instance=RequestContext(request))
Example #3
0
def storage(request, host_id, pool):
    """
    Storage pool block
    """
    def handle_uploaded_file(path, f_name):
        target = path + '/' + str(f_name)
        destination = open(target, 'wb+')
        for chunk in f_name.chunks():
            destination.write(chunk)
        destination.close()

    errors = []
    compute = Compute.objects.get(id=host_id)
    meta_prealloc = False
    conn = None

    try:
        conn = wvmStorage(compute.hostname,
                          compute.login,
                          compute.password,
                          compute.type,
                          pool)

        storages = conn.get_storages()
        state = conn.is_active()
        size, free = conn.get_size()
        used = (size - free)
        if state:
            percent = (used * 100) / size
        else:
            percent = 0
        status = conn.get_status()
        path = conn.get_target_path()
        type = conn.get_type()
        autostart = conn.get_autostart()

        if state:
            conn.refresh()
            volumes = conn.update_volumes()
        else:
            volumes = None
    except libvirtError as err:
        errors.append(err)

    if request.method == 'POST':
        if 'start' in request.POST:
            try:
                conn.start()
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as error_msg:
                errors.append(error_msg.message)
        if 'stop' in request.POST:
            try:
                conn.stop()
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as error_msg:
                errors.append(error_msg.message)
        if 'delete' in request.POST:
            try:
                conn.delete()
                return HttpResponseRedirect('/%s/storages' % host_id)
            except libvirtError as error_msg:
                errors.append(error_msg.message)
        if 'set_autostart' in request.POST:
            try:
                conn.set_autostart(1)
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as error_msg:
                errors.append(error_msg.message)
        if 'unset_autostart' in request.POST:
            try:
                conn.set_autostart(0)
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as error_msg:
                errors.append(error_msg.message)
        if 'add_volume' in request.POST:
            form = AddImage(request.POST)
            if form.is_valid():
                data = form.cleaned_data
                if data['meta_prealloc'] and data['format'] == 'qcow2':
                    meta_prealloc = True
                try:
                    conn.create_volume(data['name'], data['size'], data['format'], meta_prealloc)
                    return HttpResponseRedirect(request.get_full_path())
                except libvirtError as err:
                    errors.append(err)
            else:
                errors.append('Invalid input.')
        if 'del_volume' in request.POST:
            volname = request.POST.get('volname', '')
            try:
                vol = conn.get_volume(volname)
                vol.delete(0)
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as error_msg:
                errors.append(error_msg.message)
        if 'iso_upload' in request.POST:
            if str(request.FILES['file']) in conn.update_volumes():
                msg = _("ISO image already exist")
                errors.append(msg)
            else:
                handle_uploaded_file(path, request.FILES['file'])
                return HttpResponseRedirect(request.get_full_path())
        if 'cln_volume' in request.POST:
            form = CloneImage(request.POST)
            if form.is_valid():
                data = form.cleaned_data
                img_name = data['name'] + '.img'
                meta_prealloc = 0
                if img_name in conn.update_volumes():
                    msg = _("Name of volume name already use")
                    errors.append(msg)
                if not errors:
                    if data['convert']:
                        format = data['format']
                        if data['meta_prealloc'] and data['format'] == 'qcow2':
                            meta_prealloc = True
                    else:
                        format = None
                    try:
                        conn.clone_volume(data['image'], data['name'], format, meta_prealloc)
                        return HttpResponseRedirect(request.get_full_path())
                    except libvirtError as err:
                        errors.append(err)

    if conn:
        conn.close()

    try:
        response = {
            'type': type,
            'path': path,
            'status': status,
            'usage': percent,
            'state': state,
            'autostart': autostart,
            'pool': pool
        }
    except:
        response = {}

    object = {
        'errors': [str(error) for error in errors],
        'response': response
    }
    return render(object, 'storage.html', locals(), request)
Example #4
0
def storage(request, host_id, pool):
    """
    Storage pool block
    """
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('login'))

    if not request.user.is_staff:
        raise PermissionDenied

    def handle_uploaded_file(path, f_name):
        target = path + '/' + str(f_name)
        destination = open(target, 'wb+')
        for chunk in f_name.chunks():
            destination.write(chunk)
        destination.close()

    errors = []
    compute = Compute.objects.get(id=host_id)
    meta_prealloc = False

    try:
        conn = wvmStorage(compute.hostname,
                          compute.login,
                          compute.password,
                          compute.type,
                          compute.hypervisor,
                          pool)

        storages = conn.get_storages()
        state = conn.is_active()
        size, free = conn.get_size()
        used = (size - free)
        if state:
            percent = (used * 100) / size
        else:
            percent = 0
        status = conn.get_status()
        path = conn.get_target_path()
        type = conn.get_type()
        autostart = conn.get_autostart()

        if state:
            conn.refresh()
            volumes = conn.update_volumes()
        else:
            volumes = None
    except libvirtError as err:
        errors.append(err)

    if request.method == 'POST':
        if 'start' in request.POST:
            try:
                conn.start()
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as error_msg:
                errors.append(error_msg.message)
        if 'stop' in request.POST:
            try:
                conn.stop()
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as error_msg:
                errors.append(error_msg.message)
        if 'delete' in request.POST:
            try:
                conn.delete()
                return HttpResponseRedirect(reverse('storages', args=[host_id]))
            except libvirtError as error_msg:
                errors.append(error_msg.message)
        if 'set_autostart' in request.POST:
            try:
                conn.set_autostart(1)
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as error_msg:
                errors.append(error_msg.message)
        if 'unset_autostart' in request.POST:
            try:
                conn.set_autostart(0)
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as error_msg:
                errors.append(error_msg.message)
        if 'add_volume' in request.POST:
            form = AddImage(request.POST)
            if form.is_valid():
                data = form.cleaned_data
                if data['meta_prealloc'] and data['format'] == 'qcow2':
                    meta_prealloc = True
                try:
                    conn.create_volume(data['name'], data['size'], data['format'], meta_prealloc)
                    return HttpResponseRedirect(request.get_full_path())
                except libvirtError as err:
                    errors.append(err)
        if 'del_volume' in request.POST:
            volname = request.POST.get('volname', '')
            try:
                vol = conn.get_volume(volname)
                vol.delete(0)
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as error_msg:
                errors.append(error_msg.message)
        if 'iso_upload' in request.POST:
            if str(request.FILES['file']) in conn.update_volumes():
                msg = _("ISO image already exist")
                errors.append(msg)
            else:
                handle_uploaded_file(path, request.FILES['file'])
                return HttpResponseRedirect(request.get_full_path())
        if 'cln_volume' in request.POST:
            form = CloneImage(request.POST)
            if form.is_valid():
                data = form.cleaned_data
                img_name = data['name'] + '.img'
                meta_prealloc = 0
                if img_name in conn.update_volumes():
                    msg = _("Name of volume name already use")
                    errors.append(msg)
                if not errors:
                    if data['convert']:
                        format = data['format']
                        if data['meta_prealloc'] and data['format'] == 'qcow2':
                            meta_prealloc = True
                    else:
                        format = None
                    try:
                        conn.clone_volume(data['image'], data['name'], format, meta_prealloc)
                        return HttpResponseRedirect(request.get_full_path())
                    except libvirtError as err:
                        errors.append(err)
    conn.close()

    return render_to_response('storage.html', locals(), context_instance=RequestContext(request))
Example #5
0
def storage(request, host_id, pool):
    """
    Storage pool block
    """
    if not request.user.is_authenticated():
        return HttpResponseRedirect("/login")

    def handle_uploaded_file(path, f_name):
        target = path + "/" + str(f_name)
        destination = open(target, "wb+")
        for chunk in f_name.chunks():
            destination.write(chunk)
        destination.close()

    errors = []
    compute = Compute.objects.get(id=host_id)

    try:
        conn = wvmStorage(compute.hostname, compute.login, compute.password, compute.type, pool)

        storages = conn.get_storages()
        state = conn.is_active()
        size, free, usage = conn.get_size()
        if state:
            percent = (free * 100) / size
        else:
            percent = 0
        path = conn.get_target_path()
        type = conn.get_type()
        autostart = conn.get_autostart()

        if state:
            conn.refresh()
            volumes = conn.update_volumes()
        else:
            volumes = None
    except libvirtError as err:
        errors.append(err.message)

    if request.method == "POST":
        if "start" in request.POST:
            try:
                conn.start()
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as error_msg:
                errors.append(error_msg.message)
        if "stop" in request.POST:
            try:
                conn.stop()
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as error_msg:
                errors.append(error_msg.message)
        if "delete" in request.POST:
            try:
                conn.delete()
                return HttpResponseRedirect("/storages/%s/" % host_id)
            except libvirtError as error_msg:
                errors.append(error_msg.message)
        if "set_autostart" in request.POST:
            try:
                conn.set_autostart(1)
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as error_msg:
                errors.append(error_msg.message)
        if "unset_autostart" in request.POST:
            try:
                conn.set_autostart(0)
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as error_msg:
                errors.append(error_msg.message)
        if "add_volume" in request.POST:
            form = AddImage(request.POST)
            if form.is_valid():
                data = form.cleaned_data
                img_name = data["name"] + ".img"
                if img_name in conn.update_volumes():
                    msg = _("Volume name already use")
                    errors.append(msg)
                if not errors:
                    conn.create_volume(data["name"], data["size"], data["format"])
                    return HttpResponseRedirect(request.get_full_path())
        if "del_volume" in request.POST:
            volname = request.POST.get("volname", "")
            try:
                vol = conn.get_volume(volname)
                vol.delete(0)
                return HttpResponseRedirect(request.get_full_path())
            except libvirtError as error_msg:
                errors.append(error_msg.message)
        if "iso_upload" in request.POST:
            if str(request.FILES["file"]) in conn.update_volumes():
                msg = _("ISO image already exist")
                errors.append(msg)
            else:
                handle_uploaded_file(path, request.FILES["file"])
                return HttpResponseRedirect(request.get_full_path())
        if "cln_volume" in request.POST:
            form = CloneImage(request.POST)
            if form.is_valid():
                data = form.cleaned_data
                img_name = data["name"] + ".img"
                vol_name = request.POST.get("image", "")
                if img_name in conn.update_volumes():
                    msg = _("Name of volume name already use")
                    errors.append(msg)
                if not errors:
                    overlay = data["overlay"]
                    if data["convert"]:
                        format = data["format"]
                    else:
                        format = None
                    try:
                        conn.clone_volume(vol_name, data["name"], overlay, format)
                        return HttpResponseRedirect(request.get_full_path())
                    except libvirtError as error_msg:
                        errors.append(error_msg.message)
    conn.close()

    return render_to_response("storage.html", locals(), context_instance=RequestContext(request))