From eb8aae957eed186b6103c4f1a489bacf54cf0dcc Mon Sep 17 00:00:00 2001 From: catborise Date: Fri, 23 Nov 2018 15:18:32 +0300 Subject: [PATCH] Bundle: Add/Delete Cdrom function added. Add disk device type/bus choice for custom instance creation. Change some cosmetics on create instance. Change some functions signature for compatibility. Add getting disk device types function. Fixed some bugs --- computes/urls.py | 3 +- computes/views.py | 31 +++++++- create/views.py | 45 +++++++++--- instances/templates/add_instance_volume.html | 4 +- instances/templates/instance.html | 74 +++++++++++-------- instances/views.py | 75 +++++++++++++------- vrtManager/create.py | 46 +++++++----- 7 files changed, 192 insertions(+), 86 deletions(-) diff --git a/computes/urls.py b/computes/urls.py index ec9ab5d..51276f6 100644 --- a/computes/urls.py +++ b/computes/urls.py @@ -4,7 +4,7 @@ from networks.views import networks, network from secrets.views import secrets from create.views import create_instance from interfaces.views import interfaces, interface -from computes.views import overview, compute_graph, computes +from computes.views import overview, compute_graph, computes, get_compute_disk_buses from instances.views import instances from nwfilters.views import nwfilter, nwfilters @@ -24,4 +24,5 @@ urlpatterns = [ url(r'^(?P[0-9]+)/nwfilter/(?P[\w\-\.\:]+)/$', nwfilter, name='nwfilter'), url(r'^(?P[0-9]+)/secrets/$', secrets, name='secrets'), url(r'^(?P[0-9]+)/create/$', create_instance, name='create_instance'), + url(r'^(?P[0-9]+)/disk/(?P[\w\-\.\/]+)/buses$', get_compute_disk_buses, name='buses'), ] diff --git a/computes/views.py b/computes/views.py index db3887c..245fdea 100644 --- a/computes/views.py +++ b/computes/views.py @@ -9,7 +9,7 @@ from instances.models import Instance from accounts.models import UserInstance from computes.forms import ComputeAddTcpForm, ComputeAddSshForm, ComputeEditHostForm, ComputeAddTlsForm, ComputeAddSocketForm from vrtManager.hostdetails import wvmHostDetails -from vrtManager.connection import CONN_SSH, CONN_TCP, CONN_TLS, CONN_SOCKET, connection_manager +from vrtManager.connection import CONN_SSH, CONN_TCP, CONN_TLS, CONN_SOCKET, connection_manager, wvmConnect from libvirt import libvirtError @@ -225,3 +225,32 @@ def compute_graph(request, compute_id): response.cookies['mem'] = datasets['mem'] response.write(data) return response + + +@login_required +def get_compute_disk_buses(request, compute_id, disk): + data = {} + compute = get_object_or_404(Compute, pk=compute_id) + try: + conn = wvmConnect(compute.hostname, + compute.login, + compute.password, + compute.type) + + disk_device_types = conn.get_disk_device_types() + disk_bus_types = conn.get_disk_bus_types() + + if disk in disk_device_types: + if disk == 'disk': + data['bus'] = sorted(disk_device_types) + elif disk == 'cdrom': + data['bus'] = ['ide', 'sata', 'scsi',] + elif disk == 'floppy': + data['bus'] = ['fdc',] + elif disk == 'lun': + data['bus'] = ['scsi',] + except libvirtError: + pass + + return HttpResponse(json.dumps(data)) + diff --git a/create/views.py b/create/views.py index d514a22..9879d05 100644 --- a/create/views.py +++ b/create/views.py @@ -12,9 +12,11 @@ from vrtManager import util from libvirt import libvirtError from webvirtcloud.settings import QEMU_CONSOLE_LISTEN_ADDRESSES from webvirtcloud.settings import INSTANCE_VOLUME_DEFAULT_CACHE +from webvirtcloud.settings import INSTANCE_VOLUME_DEFAULT_BUS from django.contrib import messages from logs.views import addlogmsg + @login_required def create_instance(request, compute_id): """ @@ -48,6 +50,9 @@ def create_instance(request, compute_id): default_cache = INSTANCE_VOLUME_DEFAULT_CACHE listener_addr = QEMU_CONSOLE_LISTEN_ADDRESSES mac_auto = util.randomMAC() + disk_devices = conn.get_disk_device_types() + disk_buses = conn.get_disk_bus_types() + default_bus = INSTANCE_VOLUME_DEFAULT_BUS #get_images = sorted(conn.get_storages_images()) except libvirtError as lib_err: error_messages.append(lib_err) @@ -92,7 +97,9 @@ def create_instance(request, compute_id): except libvirtError as lib_err: error_messages.append(lib_err.message) if 'create' in request.POST: - volumes = {} + volume_list = [] + + clone_path = "" form = NewVMForm(request.POST) if form.is_valid(): data = form.cleaned_data @@ -113,7 +120,13 @@ def create_instance(request, compute_id): try: path = conn.create_volume(data['storage'], data['name'], data['hdd_size'], metadata=meta_prealloc) - volumes[path] = conn.get_volume_type(path) + volume = dict() + volume['path'] = path + volume['type'] = conn.get_volume_type(path) + volume['device'] = 'disk' + volume['bus'] = 'virtio' + volume_list.append(volume) + #volumes[path] = conn.get_volume_type(path) except libvirtError as lib_err: error_messages.append(lib_err.message) elif data['template']: @@ -124,16 +137,31 @@ def create_instance(request, compute_id): error_messages.append(error_msg) else: clone_path = conn.clone_from_template(data['name'], templ_path, metadata=meta_prealloc) - volumes[clone_path] = conn.get_volume_type(clone_path) + volume = dict() + volume['path'] = clone_path + volume['type'] = conn.get_volume_type(clone_path) + volume['device'] = 'disk' + volume['bus'] = 'virtio' + volume_list.append(volume) + #volumes[clone_path] = conn.get_volume_type(clone_path) else: if not data['images']: error_msg = _("First you need to create or select an image") error_messages.append(error_msg) else: - for vol in data['images'].split(','): + for idx, vol in enumerate(data['images'].split(',')): try: + path = conn.get_volume_path(vol) - volumes[path] = conn.get_volume_type(path) + volume = dict() + volume['path'] = path + volume['type'] = conn.get_volume_type(path) + volume['device'] = request.POST.get('device' + str(idx), '') + volume['bus'] = request.POST.get('bus' + str(idx), '') + volume_list.append(volume) + + #volumes[path] = conn.get_volume_type(path) + except libvirtError as lib_err: error_messages.append(lib_err.message) if data['cache_mode'] not in conn.get_cache_modes(): @@ -143,7 +171,7 @@ def create_instance(request, compute_id): uuid = util.randomUUID() try: conn.create_instance(data['name'], data['memory'], data['vcpu'], data['host_model'], - uuid, volumes, data['cache_mode'], data['networks'], data['virtio'], + uuid, volume_list, data['cache_mode'], data['networks'], data['virtio'], data["listener_addr"], data["nwfilter"], data["video"], data["console_pass"], data['mac']) create_instance = Instance(compute_id=compute_id, name=data['name'], uuid=uuid) @@ -153,8 +181,9 @@ def create_instance(request, compute_id): addlogmsg(request.user.username, create_instance.name, msg) return HttpResponseRedirect(reverse('instance', args=[compute_id, data['name']])) except libvirtError as lib_err: - if data['hdd_size'] or volumes[clone_path]: - conn.delete_volume(volumes.keys()[0]) + if data['hdd_size'] or volume_list.count() > 0: + for vol in volume_list: + conn.delete_volume(vol['path']) error_messages.append(lib_err) conn.close() return render(request, 'create_instance.html', locals()) diff --git a/instances/templates/add_instance_volume.html b/instances/templates/add_instance_volume.html index dfb4c0f..9cea2fc 100644 --- a/instances/templates/add_instance_volume.html +++ b/instances/templates/add_instance_volume.html @@ -85,7 +85,7 @@