From c9c7023c73246bcea16295c3bfa323e283bee0aa Mon Sep 17 00:00:00 2001 From: Levi Date: Sat, 6 May 2017 18:04:45 -0500 Subject: [PATCH] fixed create vm method --- .../0035_virtualmachineplan_opennebula_id.py | 21 ++++++++++ hosting/models.py | 1 + hosting/opennebula_functions.py | 42 +++++++++++++++++++ hosting/views.py | 3 +- 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 hosting/migrations/0035_virtualmachineplan_opennebula_id.py diff --git a/hosting/migrations/0035_virtualmachineplan_opennebula_id.py b/hosting/migrations/0035_virtualmachineplan_opennebula_id.py new file mode 100644 index 00000000..9b23875d --- /dev/null +++ b/hosting/migrations/0035_virtualmachineplan_opennebula_id.py @@ -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, + ), + ] diff --git a/hosting/models.py b/hosting/models.py index ec746e22..3eaae483 100644 --- a/hosting/models.py +++ b/hosting/models.py @@ -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() diff --git a/hosting/opennebula_functions.py b/hosting/opennebula_functions.py index 5707f128..8a010931 100644 --- a/hosting/opennebula_functions.py +++ b/hosting/opennebula_functions.py @@ -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 = """ + {memory} + {vcpu} + {cpu} + + {disk_type} + {size} + + + """ + 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 diff --git a/hosting/views.py b/hosting/views.py index 4396ede9..912804d1 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -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 = {