2015-04-02 12:39:40 +00:00
|
|
|
from django.shortcuts import render, get_object_or_404
|
2015-02-27 12:25:41 +00:00
|
|
|
from django.http import HttpResponseRedirect
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from django.core.urlresolvers import reverse
|
2015-12-22 14:09:02 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2015-02-27 12:25:41 +00:00
|
|
|
from computes.models import Compute
|
2015-02-27 12:44:44 +00:00
|
|
|
from create.models import Flavor
|
|
|
|
from create.forms import FlavorAddForm, NewVMForm
|
2015-02-27 12:25:41 +00:00
|
|
|
from instances.models import Instance
|
|
|
|
from vrtManager.create import wvmCreate
|
|
|
|
from vrtManager import util
|
|
|
|
from libvirt import libvirtError
|
2018-09-14 13:23:25 +00:00
|
|
|
from webvirtcloud.settings import QEMU_CONSOLE_LISTEN_ADDRESSES
|
2018-10-10 12:50:59 +00:00
|
|
|
from webvirtcloud.settings import INSTANCE_VOLUME_DEFAULT_CACHE
|
2018-11-23 12:18:32 +00:00
|
|
|
from webvirtcloud.settings import INSTANCE_VOLUME_DEFAULT_BUS
|
2018-09-14 13:23:25 +00:00
|
|
|
from django.contrib import messages
|
2018-11-14 08:46:13 +00:00
|
|
|
from logs.views import addlogmsg
|
2015-02-27 12:25:41 +00:00
|
|
|
|
2018-11-23 12:18:32 +00:00
|
|
|
|
2015-12-22 14:09:02 +00:00
|
|
|
@login_required
|
2015-03-02 08:52:07 +00:00
|
|
|
def create_instance(request, compute_id):
|
2015-02-27 12:25:41 +00:00
|
|
|
"""
|
|
|
|
:param request:
|
2019-09-10 06:48:31 +00:00
|
|
|
:param compute_id:
|
2015-02-27 12:25:41 +00:00
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
2015-03-03 09:34:09 +00:00
|
|
|
if not request.user.is_superuser:
|
|
|
|
return HttpResponseRedirect(reverse('index'))
|
|
|
|
|
2015-02-27 12:25:41 +00:00
|
|
|
conn = None
|
|
|
|
error_messages = []
|
|
|
|
storages = []
|
|
|
|
networks = []
|
|
|
|
meta_prealloc = False
|
2015-04-02 12:39:40 +00:00
|
|
|
compute = get_object_or_404(Compute, pk=compute_id)
|
2015-02-27 12:25:41 +00:00
|
|
|
flavors = Flavor.objects.filter().order_by('id')
|
|
|
|
|
|
|
|
try:
|
|
|
|
conn = wvmCreate(compute.hostname,
|
|
|
|
compute.login,
|
|
|
|
compute.password,
|
|
|
|
compute.type)
|
|
|
|
|
|
|
|
instances = conn.get_instances()
|
2018-09-14 13:23:25 +00:00
|
|
|
videos = conn.get_video()
|
2016-04-17 21:41:40 +00:00
|
|
|
cache_modes = sorted(conn.get_cache_modes().items())
|
2018-10-10 12:50:59 +00:00
|
|
|
default_cache = INSTANCE_VOLUME_DEFAULT_CACHE
|
2018-09-14 13:23:25 +00:00
|
|
|
listener_addr = QEMU_CONSOLE_LISTEN_ADDRESSES
|
2015-02-27 12:25:41 +00:00
|
|
|
mac_auto = util.randomMAC()
|
2018-11-23 12:18:32 +00:00
|
|
|
disk_devices = conn.get_disk_device_types()
|
|
|
|
disk_buses = conn.get_disk_bus_types()
|
|
|
|
default_bus = INSTANCE_VOLUME_DEFAULT_BUS
|
2019-09-10 13:06:30 +00:00
|
|
|
networks = sorted(conn.get_networks())
|
|
|
|
nwfilters = conn.get_nwfilters()
|
|
|
|
storages = sorted(conn.get_storages(only_actives=True))
|
2015-02-27 12:25:41 +00:00
|
|
|
except libvirtError as lib_err:
|
|
|
|
error_messages.append(lib_err)
|
|
|
|
|
|
|
|
if conn:
|
|
|
|
if not storages:
|
2015-05-15 17:54:55 +00:00
|
|
|
msg = _("You haven't defined any storage pools")
|
2015-02-27 12:25:41 +00:00
|
|
|
error_messages.append(msg)
|
|
|
|
if not networks:
|
2015-05-15 17:54:55 +00:00
|
|
|
msg = _("You haven't defined any network pools")
|
2015-02-27 12:25:41 +00:00
|
|
|
error_messages.append(msg)
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
if 'create_flavor' in request.POST:
|
|
|
|
form = FlavorAddForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
data = form.cleaned_data
|
|
|
|
create_flavor = Flavor(label=data['label'],
|
|
|
|
vcpu=data['vcpu'],
|
|
|
|
memory=data['memory'],
|
|
|
|
disk=data['disk'])
|
|
|
|
create_flavor.save()
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
|
|
|
if 'delete_flavor' in request.POST:
|
|
|
|
flavor_id = request.POST.get('flavor', '')
|
|
|
|
delete_flavor = Flavor.objects.get(id=flavor_id)
|
|
|
|
delete_flavor.delete()
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
|
|
|
if 'create_xml' in request.POST:
|
2018-09-21 16:07:47 +00:00
|
|
|
xml = request.POST.get('dom_xml', '')
|
2015-02-27 12:25:41 +00:00
|
|
|
try:
|
|
|
|
name = util.get_xml_path(xml, '/domain/name')
|
2018-09-21 16:07:47 +00:00
|
|
|
except util.etree.Error as err:
|
2015-02-27 12:25:41 +00:00
|
|
|
name = None
|
|
|
|
if name in instances:
|
|
|
|
error_msg = _("A virtual machine with this name already exists")
|
|
|
|
error_messages.append(error_msg)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
conn._defineXML(xml)
|
2015-03-02 08:52:07 +00:00
|
|
|
return HttpResponseRedirect(reverse('instance', args=[compute_id, name]))
|
2015-02-27 12:25:41 +00:00
|
|
|
except libvirtError as lib_err:
|
|
|
|
error_messages.append(lib_err.message)
|
|
|
|
if 'create' in request.POST:
|
2018-11-23 12:18:32 +00:00
|
|
|
volume_list = []
|
|
|
|
|
|
|
|
clone_path = ""
|
2015-02-27 12:25:41 +00:00
|
|
|
form = NewVMForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
data = form.cleaned_data
|
|
|
|
if data['meta_prealloc']:
|
|
|
|
meta_prealloc = True
|
|
|
|
if instances:
|
|
|
|
if data['name'] in instances:
|
|
|
|
msg = _("A virtual machine with this name already exists")
|
|
|
|
error_messages.append(msg)
|
2018-10-04 07:14:08 +00:00
|
|
|
if Instance.objects.filter(name__exact=data['name']):
|
2019-09-10 06:48:31 +00:00
|
|
|
messages.warning(request, _("There is an instance with same name. Are you sure?"))
|
2015-02-27 12:25:41 +00:00
|
|
|
if not error_messages:
|
|
|
|
if data['hdd_size']:
|
|
|
|
if not data['mac']:
|
|
|
|
error_msg = _("No Virtual Machine MAC has been entered")
|
|
|
|
error_messages.append(error_msg)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
path = conn.create_volume(data['storage'], data['name'], data['hdd_size'],
|
|
|
|
metadata=meta_prealloc)
|
2018-11-23 12:18:32 +00:00
|
|
|
volume = dict()
|
|
|
|
volume['path'] = path
|
|
|
|
volume['type'] = conn.get_volume_type(path)
|
|
|
|
volume['device'] = 'disk'
|
|
|
|
volume['bus'] = 'virtio'
|
|
|
|
volume_list.append(volume)
|
2015-02-27 12:25:41 +00:00
|
|
|
except libvirtError as lib_err:
|
|
|
|
error_messages.append(lib_err.message)
|
|
|
|
elif data['template']:
|
|
|
|
templ_path = conn.get_volume_path(data['template'])
|
2018-12-18 07:46:23 +00:00
|
|
|
dest_vol = conn.get_volume_path(data["name"] + ".img", data['storage'])
|
2018-09-14 13:23:25 +00:00
|
|
|
if dest_vol:
|
|
|
|
error_msg = _("Image has already exist. Please check volumes or change instance name")
|
|
|
|
error_messages.append(error_msg)
|
|
|
|
else:
|
2019-01-09 11:16:51 +00:00
|
|
|
clone_path = conn.clone_from_template(data['name'], templ_path, data['storage'], metadata=meta_prealloc)
|
2018-11-23 12:18:32 +00:00
|
|
|
volume = dict()
|
|
|
|
volume['path'] = clone_path
|
|
|
|
volume['type'] = conn.get_volume_type(clone_path)
|
|
|
|
volume['device'] = 'disk'
|
|
|
|
volume['bus'] = 'virtio'
|
|
|
|
volume_list.append(volume)
|
2015-02-27 12:25:41 +00:00
|
|
|
else:
|
|
|
|
if not data['images']:
|
|
|
|
error_msg = _("First you need to create or select an image")
|
|
|
|
error_messages.append(error_msg)
|
|
|
|
else:
|
2018-11-23 12:18:32 +00:00
|
|
|
for idx, vol in enumerate(data['images'].split(',')):
|
2015-02-27 12:25:41 +00:00
|
|
|
try:
|
|
|
|
path = conn.get_volume_path(vol)
|
2018-11-23 12:18:32 +00:00
|
|
|
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)
|
2015-02-27 12:25:41 +00:00
|
|
|
except libvirtError as lib_err:
|
|
|
|
error_messages.append(lib_err.message)
|
2016-04-17 21:41:40 +00:00
|
|
|
if data['cache_mode'] not in conn.get_cache_modes():
|
|
|
|
error_msg = _("Invalid cache mode")
|
|
|
|
error_messages.append(error_msg)
|
2015-02-27 12:25:41 +00:00
|
|
|
if not error_messages:
|
|
|
|
uuid = util.randomUUID()
|
|
|
|
try:
|
|
|
|
conn.create_instance(data['name'], data['memory'], data['vcpu'], data['host_model'],
|
2018-11-23 12:18:32 +00:00
|
|
|
uuid, volume_list, data['cache_mode'], data['networks'], data['virtio'],
|
2018-09-24 11:41:13 +00:00
|
|
|
data["listener_addr"], data["nwfilter"], data["video"], data["console_pass"],
|
2018-11-26 12:40:02 +00:00
|
|
|
data['mac'], data['qemu_ga'])
|
2015-03-10 09:15:36 +00:00
|
|
|
create_instance = Instance(compute_id=compute_id, name=data['name'], uuid=uuid)
|
2015-02-27 12:25:41 +00:00
|
|
|
create_instance.save()
|
2018-11-14 08:46:13 +00:00
|
|
|
msg = _("Instance is created.")
|
|
|
|
messages.success(request, msg)
|
|
|
|
addlogmsg(request.user.username, create_instance.name, msg)
|
2015-03-10 09:15:36 +00:00
|
|
|
return HttpResponseRedirect(reverse('instance', args=[compute_id, data['name']]))
|
2015-02-27 12:25:41 +00:00
|
|
|
except libvirtError as lib_err:
|
2019-09-10 06:48:31 +00:00
|
|
|
if data['hdd_size'] or len(volume_list) > 0:
|
2018-11-23 12:18:32 +00:00
|
|
|
for vol in volume_list:
|
|
|
|
conn.delete_volume(vol['path'])
|
2015-02-27 12:25:41 +00:00
|
|
|
error_messages.append(lib_err)
|
|
|
|
conn.close()
|
2015-04-02 13:20:46 +00:00
|
|
|
return render(request, 'create_instance.html', locals())
|