fixed create vm method

This commit is contained in:
Levi 2017-05-06 18:04:45 -05:00
parent 97fecf88db
commit c9c7023c73
4 changed files with 66 additions and 1 deletions

View file

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2017-05-06 23:02
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('hosting', '0034_auto_20170504_0331'),
]
operations = [
migrations.AddField(
model_name='virtualmachineplan',
name='opennebula_id',
field=models.IntegerField(default=0),
preserve_default=False,
),
]

View file

@ -125,6 +125,7 @@ class VirtualMachinePlan(AssignPermissionsMixin, models.Model):
status = models.CharField(max_length=20, choices=VM_STATUS_CHOICES, default=PENDING_STATUS)
ip = models.CharField(max_length=50, blank=True)
configuration = models.CharField(max_length=20, choices=VM_CONFIGURATION)
opennebula_id = models.IntegerField()
objects = VMPlansManager()

View file

@ -87,6 +87,48 @@ class HostingManageVMAdmin(admin.ModelAdmin):
)
return TemplateResponse(request, "hosting/managevms.html", context)
def create_vm_view(self, specs):
try:
# We do have the vm_template param set. Get and parse it
# and check it to be in the desired range.
# We have 8 possible VM templates for the moment which are 1x, 2x, 4x ...
# the basic template of 10GB disk, 1GB ram, 1 vcpu, 0.1 cpu
vm_string_formatter = """<VM>
<MEMORY>{memory}</MEMORY>
<VCPU>{vcpu}</VCPU>
<CPU>{cpu}</CPU>
<DISK>
<TYPE>{disk_type}</TYPE>
<SIZE>{size}</SIZE>
</DISK>
</VM>
"""
vm_id = oca.VirtualMachine.allocate(
self.client,
vm_string_formatter.format(
memory=specs.get('memory'),
vcpu=specs.get('cores'),
cpu=0.1 * specs.get('cores'),
disk_type='fs',
size=specs.get('disk_size')
)
)
# message = _("Created with id = " + str(vm_id))
# messages.add_message(request, messages.SUCCESS, message)
except socket.timeout as socket_err:
logger.error("Socket timeout error: {0}".format(socket_err))
except OpenNebulaException as opennebula_err:
logger.error("OpenNebulaException error: {0}".format(opennebula_err))
except OSError as os_err:
logger.error("OSError : {0}".format(os_err))
except ValueError as value_err:
logger.error("ValueError : {0}".format(value_err))
return vm_id
# Creating VM by using method allocate(client, template)
def create_vm(self, request):
# check if the request contains the template parameter, if it is

View file

@ -350,7 +350,8 @@ class PaymentVMView(LoginRequiredMixin, FormView):
hosting_admin = HostingManageVMAdmin.__new__(HostingManageVMAdmin)
hosting_admin.init_opennebula_client(request)
hosting_admin.create_vm(request)
oppennebula_vm_id = hosting_admin.create_vm_view(vm_type.get_specs())
plan.oppenebula_id = oppennebula_vm_id
# Send notification to ungleich as soon as VM has been booked
context = {