1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-11-01 03:54:15 +00:00
webvirtcloud/storages/views.py

211 lines
8.7 KiB
Python
Raw Normal View History

2015-04-02 12:39:40 +00:00
from django.shortcuts import render, get_object_or_404
2015-02-27 09:28:22 +00:00
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
2015-02-27 09:28:22 +00:00
from computes.models import Compute
from storages.forms import AddStgPool, AddImage, CloneImage
from vrtManager.storage import wvmStorage, wvmStorages
from libvirt import libvirtError
from django.contrib import messages
2015-02-27 09:28:22 +00:00
@login_required
2015-02-27 09:28:22 +00:00
def storages(request, compute_id):
"""
:param request:
:return:
"""
2015-03-03 09:34:09 +00:00
if not request.user.is_superuser:
return HttpResponseRedirect(reverse('index'))
2015-02-27 09:51:33 +00:00
error_messages = []
2015-04-02 12:39:40 +00:00
compute = get_object_or_404(Compute, pk=compute_id)
2015-02-27 09:28:22 +00:00
try:
conn = wvmStorages(compute.hostname,
compute.login,
compute.password,
compute.type)
storages = conn.get_storages_info()
secrets = conn.get_secrets()
if request.method == 'POST':
if 'create' in request.POST:
form = AddStgPool(request.POST)
if form.is_valid():
data = form.cleaned_data
if data['name'] in storages:
msg = _("Pool name already use")
2015-02-27 09:51:33 +00:00
error_messages.append(msg)
2015-02-27 09:28:22 +00:00
if data['stg_type'] == 'rbd':
if not data['secret']:
msg = _("You need create secret for pool")
2015-02-27 09:51:33 +00:00
error_messages.append(msg)
2015-02-27 09:28:22 +00:00
if not data['ceph_pool'] and not data['ceph_host'] and not data['ceph_user']:
msg = _("You need input all fields for creating ceph pool")
2015-02-27 09:51:33 +00:00
error_messages.append(msg)
if not error_messages:
2015-02-27 09:28:22 +00:00
if data['stg_type'] == 'rbd':
conn.create_storage_ceph(data['stg_type'], data['name'],
data['ceph_pool'], data['ceph_host'],
data['ceph_user'], data['secret'])
elif data['stg_type'] == 'netfs':
conn.create_storage_netfs(data['stg_type'], data['name'],
data['netfs_host'], data['source'],
data['source_format'], data['target'])
else:
conn.create_storage(data['stg_type'], data['name'], data['source'], data['target'])
2015-02-27 09:51:33 +00:00
return HttpResponseRedirect(reverse('storage', args=[compute_id, data['name']]))
2015-03-10 14:24:10 +00:00
else:
for msg_err in form.errors.values():
error_messages.append(msg_err.as_text())
2015-02-27 09:28:22 +00:00
conn.close()
2015-02-27 09:51:33 +00:00
except libvirtError as lib_err:
error_messages.append(lib_err)
2015-02-27 09:28:22 +00:00
2015-04-02 13:20:46 +00:00
return render(request, 'storages.html', locals())
2015-02-27 09:28:22 +00:00
@login_required
2015-02-27 09:28:22 +00:00
def storage(request, compute_id, pool):
"""
:param request:
:return:
"""
if not request.user.is_superuser:
return HttpResponseRedirect(reverse('index'))
2015-02-27 09:28:22 +00:00
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()
2015-02-27 09:51:33 +00:00
error_messages = []
2015-04-02 12:39:40 +00:00
compute = get_object_or_404(Compute, pk=compute_id)
2015-02-27 09:28:22 +00:00
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
2015-02-27 09:51:33 +00:00
except libvirtError as lib_err:
error_messages.append(lib_err)
2015-02-27 09:28:22 +00:00
if request.method == 'POST':
if 'start' in request.POST:
try:
conn.start()
return HttpResponseRedirect(request.get_full_path())
2015-02-27 09:51:33 +00:00
except libvirtError as lib_err:
error_messages.append(lib_err.message)
2015-02-27 09:28:22 +00:00
if 'stop' in request.POST:
try:
conn.stop()
return HttpResponseRedirect(request.get_full_path())
2015-02-27 09:51:33 +00:00
except libvirtError as lib_err:
error_messages.append(lib_err.message)
2015-02-27 09:28:22 +00:00
if 'delete' in request.POST:
try:
conn.delete()
2015-03-27 15:12:15 +00:00
return HttpResponseRedirect(reverse('storages', args=[compute_id]))
2015-02-27 09:51:33 +00:00
except libvirtError as lib_err:
error_messages.append(lib_err.message)
2015-02-27 09:28:22 +00:00
if 'set_autostart' in request.POST:
try:
conn.set_autostart(1)
return HttpResponseRedirect(request.get_full_path())
2015-02-27 09:51:33 +00:00
except libvirtError as lib_err:
error_messages.append(lib_err.message)
2015-02-27 09:28:22 +00:00
if 'unset_autostart' in request.POST:
try:
conn.set_autostart(0)
return HttpResponseRedirect(request.get_full_path())
2015-02-27 09:51:33 +00:00
except libvirtError as lib_err:
error_messages.append(lib_err.message)
2015-02-27 09:28:22 +00:00
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"))
2015-02-27 09:28:22 +00:00
return HttpResponseRedirect(request.get_full_path())
2015-02-27 09:51:33 +00:00
except libvirtError as lib_err:
error_messages.append(lib_err)
2015-03-10 14:24:10 +00:00
else:
for msg_err in form.errors.values():
error_messages.append(msg_err.as_text())
2015-02-27 09:28:22 +00:00
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)))
2015-02-27 09:28:22 +00:00
return HttpResponseRedirect(request.get_full_path())
2015-02-27 09:51:33 +00:00
except libvirtError as lib_err:
error_messages.append(lib_err.message)
2015-02-27 09:28:22 +00:00
if 'iso_upload' in request.POST:
if str(request.FILES['file']) in conn.update_volumes():
2015-02-27 09:51:33 +00:00
error_msg = _("ISO image already exist")
error_messages.append(error_msg)
2015-02-27 09:28:22 +00:00
else:
handle_uploaded_file(path, request.FILES['file'])
messages.success(request, _('ISO: {} is uploaded.'.format(request.FILES['file'])))
2015-02-27 09:28:22 +00:00
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")
2015-02-27 09:51:33 +00:00
error_messages.append(msg)
if not error_messages:
2015-02-27 09:28:22 +00:00
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")))
2015-02-27 09:28:22 +00:00
return HttpResponseRedirect(request.get_full_path())
2015-02-27 09:51:33 +00:00
except libvirtError as lib_err:
error_messages.append(lib_err)
2015-03-10 14:24:10 +00:00
else:
for msg_err in form.errors.values():
error_messages.append(msg_err.as_text())
2015-02-27 09:28:22 +00:00
conn.close()
2015-04-02 13:20:46 +00:00
return render(request, 'storage.html', locals())