2020-04-24 16:34:29 +00:00
|
|
|
import json
|
2015-04-02 12:39:40 +00:00
|
|
|
from django.shortcuts import render, get_object_or_404
|
2018-10-24 09:04:05 +00:00
|
|
|
from django.http import HttpResponseRedirect, HttpResponse
|
2015-02-27 09:28:22 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2020-01-24 11:30:37 +00:00
|
|
|
from django.urls import reverse
|
2015-02-27 09:28:22 +00:00
|
|
|
from computes.models import Compute
|
2020-05-28 21:43:26 +00:00
|
|
|
from appsettings.models import AppSettings
|
2015-02-27 09:28:22 +00:00
|
|
|
from storages.forms import AddStgPool, AddImage, CloneImage
|
|
|
|
from vrtManager.storage import wvmStorage, wvmStorages
|
|
|
|
from libvirt import libvirtError
|
2018-07-25 06:35:35 +00:00
|
|
|
from django.contrib import messages
|
2020-05-27 12:24:06 +00:00
|
|
|
from admin.decorators import superuser_only
|
2015-02-27 09:28:22 +00:00
|
|
|
|
2019-09-10 06:48:31 +00:00
|
|
|
|
2020-05-27 12:24:06 +00:00
|
|
|
@superuser_only
|
2015-02-27 09:28:22 +00:00
|
|
|
def storages(request, compute_id):
|
|
|
|
"""
|
|
|
|
:param request:
|
2019-09-10 06:48:31 +00:00
|
|
|
:param compute_id:
|
2015-02-27 09:28:22 +00:00
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
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:
|
2020-05-27 12:24:06 +00:00
|
|
|
conn = wvmStorages(compute.hostname, compute.login, compute.password, compute.type)
|
2015-02-27 09:28:22 +00:00
|
|
|
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':
|
2020-05-27 12:24:06 +00:00
|
|
|
conn.create_storage_ceph(data['stg_type'], data['name'], data['ceph_pool'], data['ceph_host'],
|
2015-02-27 09:28:22 +00:00
|
|
|
data['ceph_user'], data['secret'])
|
|
|
|
elif data['stg_type'] == 'netfs':
|
2020-05-27 12:24:06 +00:00
|
|
|
conn.create_storage_netfs(data['stg_type'], data['name'], data['netfs_host'], data['source'],
|
2015-02-27 09:28:22 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2020-05-27 12:24:06 +00:00
|
|
|
@superuser_only
|
2015-02-27 09:28:22 +00:00
|
|
|
def storage(request, compute_id, pool):
|
|
|
|
"""
|
|
|
|
:param request:
|
2019-09-10 06:48:31 +00:00
|
|
|
:param compute_id:
|
|
|
|
:param pool:
|
2015-02-27 09:28:22 +00:00
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
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:
|
2020-05-27 12:24:06 +00:00
|
|
|
conn = wvmStorage(compute.hostname, compute.login, compute.password, compute.type, pool)
|
2015-02-27 09:28:22 +00:00
|
|
|
|
|
|
|
storages = conn.get_storages()
|
|
|
|
state = conn.is_active()
|
|
|
|
size, free = conn.get_size()
|
|
|
|
used = (size - free)
|
|
|
|
if state:
|
2020-03-16 13:59:45 +00:00
|
|
|
percent = (used * 100) // size
|
2015-02-27 09:28:22 +00:00
|
|
|
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:
|
2020-03-16 13:59:45 +00:00
|
|
|
error_messages.append(lib_err)
|
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:
|
2020-03-16 13:59:45 +00:00
|
|
|
error_messages.append(lib_err)
|
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:
|
2020-03-16 13:59:45 +00:00
|
|
|
error_messages.append(lib_err)
|
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:
|
2020-03-16 13:59:45 +00:00
|
|
|
error_messages.append(lib_err)
|
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:
|
2020-03-16 13:59:45 +00:00
|
|
|
error_messages.append(lib_err)
|
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:
|
2020-05-28 21:43:26 +00:00
|
|
|
disk_owner = AppSettings.objects.filter(key__startswith="INSTANCE_VOLUME_DEFAULT_OWNER")
|
|
|
|
disk_owner_uid = int(disk_owner.get(key="INSTANCE_VOLUME_DEFAULT_OWNER_UID").value)
|
|
|
|
disk_owner_gid = int(disk_owner.get(key="INSTANCE_VOLUME_DEFAULT_OWNER_GID").value)
|
|
|
|
|
|
|
|
name = conn.create_volume(data['name'], data['size'], data['format'], meta_prealloc, disk_owner_uid, disk_owner_gid)
|
|
|
|
messages.success(request, _(f"Image file {name} is created successfully"))
|
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)
|
2018-09-26 14:18:30 +00:00
|
|
|
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:
|
2020-03-16 13:59:45 +00:00
|
|
|
error_messages.append(lib_err)
|
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'])
|
2018-07-25 06:35:35 +00:00
|
|
|
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
|
2019-04-29 11:10:54 +00:00
|
|
|
img_name = data['name']
|
2015-02-27 09:28:22 +00:00
|
|
|
meta_prealloc = 0
|
|
|
|
if img_name in conn.update_volumes():
|
2019-04-29 11:10:54 +00:00
|
|
|
msg = _("Name of volume already in 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:
|
2019-04-29 11:10:54 +00:00
|
|
|
name = conn.clone_volume(data['image'], data['name'], format, meta_prealloc)
|
|
|
|
messages.success(request, _("{} image cloned as {} successfully".format(data['image'], name)))
|
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())
|
2018-10-24 09:04:05 +00:00
|
|
|
|
2015-02-27 09:28:22 +00:00
|
|
|
conn.close()
|
|
|
|
|
2015-04-02 13:20:46 +00:00
|
|
|
return render(request, 'storage.html', locals())
|
2018-10-24 09:04:05 +00:00
|
|
|
|
2018-11-16 13:04:26 +00:00
|
|
|
|
|
|
|
def get_volumes(request, compute_id, pool):
|
2020-04-24 16:34:29 +00:00
|
|
|
"""
|
|
|
|
:param request:
|
|
|
|
:param compute_id: compute id
|
|
|
|
:param pool: pool name
|
|
|
|
:return: volumes list of pool
|
|
|
|
"""
|
2018-11-16 13:04:26 +00:00
|
|
|
data = {}
|
|
|
|
compute = get_object_or_404(Compute, pk=compute_id)
|
|
|
|
try:
|
2020-05-27 12:24:06 +00:00
|
|
|
conn = wvmStorage(compute.hostname, compute.login, compute.password, compute.type, pool)
|
2018-12-03 13:18:41 +00:00
|
|
|
conn.refresh()
|
2019-12-13 13:38:21 +00:00
|
|
|
data['vols'] = sorted(conn.get_volumes())
|
2019-02-14 13:49:12 +00:00
|
|
|
except libvirtError:
|
2018-11-16 13:04:26 +00:00
|
|
|
pass
|
2019-09-10 06:48:31 +00:00
|
|
|
return HttpResponse(json.dumps(data))
|