1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-26 16:15:25 +00:00
webvirtcloud/create/views.py

141 lines
6.5 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 12:25:41 +00:00
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse
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
2015-03-02 08:52:07 +00:00
def create_instance(request, compute_id):
2015-02-27 12:25:41 +00:00
"""
:param request:
:return:
"""
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('index'))
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-03-06 13:46:59 +00:00
computes = Compute.objects.all()
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)
storages = sorted(conn.get_storages())
networks = sorted(conn.get_networks())
instances = conn.get_instances()
get_images = sorted(conn.get_storages_images())
mac_auto = util.randomMAC()
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:
xml = request.POST.get('from_xml', '')
try:
name = util.get_xml_path(xml, '/domain/name')
except util.libxml2.parserError:
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:
volumes = {}
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)
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)
volumes[path] = conn.get_volume_type(path)
except libvirtError as lib_err:
error_messages.append(lib_err.message)
elif data['template']:
templ_path = conn.get_volume_path(data['template'])
clone_path = conn.clone_from_template(data['name'], templ_path, metadata=meta_prealloc)
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(','):
try:
path = conn.get_volume_path(vol)
volumes[path] = conn.get_volume_type(path)
except libvirtError as lib_err:
error_messages.append(lib_err.message)
if not error_messages:
uuid = util.randomUUID()
try:
conn.create_instance(data['name'], data['memory'], data['vcpu'], data['host_model'],
uuid, volumes, data['networks'], data['virtio'], data['mac'])
create_instance = Instance(compute_id=compute_id, name=data['name'], uuid=uuid)
2015-02-27 12:25:41 +00:00
create_instance.save()
return HttpResponseRedirect(reverse('instance', args=[compute_id, data['name']]))
2015-02-27 12:25:41 +00:00
except libvirtError as lib_err:
if data['hdd_size']:
conn.delete_volume(volumes.keys()[0])
error_messages.append(lib_err)
conn.close()
2015-04-02 13:20:46 +00:00
return render(request, 'create_instance.html', locals())