Added start/stop functionality for VMs. Refactored a few functions.

This commit is contained in:
M.Ravi 2017-04-11 00:44:55 +05:30
parent c7334dfac2
commit b68fe31361
2 changed files with 58 additions and 60 deletions

View file

@ -6,6 +6,8 @@ from django.template.response import TemplateResponse
from django.conf import settings from django.conf import settings
from utils.mailer import BaseEmail from utils.mailer import BaseEmail
from django import template from django import template
from django.shortcuts import redirect
from django.contrib import messages
import oca import oca
import socket import socket
from oca.exceptions import OpenNebulaException from oca.exceptions import OpenNebulaException
@ -13,17 +15,6 @@ from oca.exceptions import OpenNebulaException
from .forms import HostingOrderAdminForm from .forms import HostingOrderAdminForm
from .models import VirtualMachineType, VirtualMachinePlan, HostingOrder, ManageVMs from .models import VirtualMachineType, VirtualMachinePlan, HostingOrder, ManageVMs
register = template.Library()
def get_vm_state(value):
if value == 1:
return 'PENDING'
#states = {-2: 'Any incl done', -1 : 'Any except done', 0 : 'INIT', 1 : 'PENDING', 2 : 'HOLD', 3 : 'ACTIVE', 4 : 'STOPPED', 5 : 'SUSPENDED', 6 : 'DONE', 7 : 'FAILED'}
#return states.get(value)
return 'UNKNO'
register.filter('get_vm_state', get_vm_state)
class HostingOrderAdmin(admin.ModelAdmin): class HostingOrderAdmin(admin.ModelAdmin):
# fields = ('slug', 'imdb_link', 'start', 'finish', 'added_by') # fields = ('slug', 'imdb_link', 'start', 'finish', 'added_by')
list_display = ('id', 'created_at', 'plan', 'user') list_display = ('id', 'created_at', 'plan', 'user')
@ -113,12 +104,12 @@ class HostingManageVMsAdmin(admin.ModelAdmin):
def get_urls(self): def get_urls(self):
self.client = oca.Client(settings.OPENNEBULA_USERNAME + ':' + settings.OPENNEBULA_PASSWORD, settings.OPENNEBULA_PROTOCOL + '://' + settings.OPENNEBULA_DOMAIN + ':' + settings.OPENNEBULA_PORT + settings.OPENNEBULA_ENDPOINT) self.client = oca.Client(settings.OPENNEBULA_USERNAME + ':' + settings.OPENNEBULA_PASSWORD, settings.OPENNEBULA_PROTOCOL + '://' + settings.OPENNEBULA_DOMAIN + ':' + settings.OPENNEBULA_PORT + settings.OPENNEBULA_ENDPOINT)
urls = super().get_urls() urls = super().get_urls()
#socket.setdefaulttimeout(5)
my_urls = [ my_urls = [
url(r'^$', self.admin_site.admin_view(self.my_view, cacheable=True)), url(r'^$', self.admin_site.admin_view(self.my_view, cacheable=True), name='showvms'),
url(r'^create_vm/$', self.admin_site.admin_view(self.create_vm, cacheable=True), name='createvm'), url(r'^create_vm/$', self.admin_site.admin_view(self.create_vm, cacheable=True), name='createvm'),
url(r'^delete_vm/(?P<vmid>\d+)/$', self.admin_site.admin_view(self.delete_vm, cacheable=True), name='deletevm'), url(r'^delete_vm/(?P<vmid>\d+)/$', self.admin_site.admin_view(self.delete_vm, cacheable=True), name='deletevm'),
#url(r'^my_views/$', self.admin_site.admin_view(self.my_view, cacheable=True)) url(r'^stop_vm/(?P<vmid>\d+)/$', self.admin_site.admin_view(self.stop_vm, cacheable=True), name='stopvm'),
url(r'^start_vm/(?P<vmid>\d+)/$', self.admin_site.admin_view(self.start_vm, cacheable=True), name='startvm'),
] ]
return my_urls + urls return my_urls + urls
@ -142,70 +133,72 @@ class HostingManageVMsAdmin(admin.ModelAdmin):
# key=value, # key=value,
) )
return TemplateResponse(request, "hosting/managevms.html", context) return TemplateResponse(request, "hosting/managevms.html", context)
# Creating VM by using method allocate(client, template)
def create_vm(self, request): def create_vm(self, request):
s_message = '' message = ''
e_message = ''
try : try :
# Lets create a test VM with 128MB of ram and 1 CPU # Lets create a test VM with 128MB of ram and 1 CPU
vm_id = oca.VirtualMachine.allocate(self.client, '<VM><MEMORY>128</MEMORY><CPU>1</CPU></VM>') vm_id = oca.VirtualMachine.allocate(self.client, '<VM><MEMORY>128</MEMORY><CPU>1</CPU></VM>')
s_message = "Created with id = " + str(vm_id) message = "Created with id = " + str(vm_id)
vm_pool = self.get_vms() vm_pool = self.get_vms()
messages.add_message(request, messages.SUCCESS, message)
# Lets print the VMs available in the pool # Lets print the VMs available in the pool
# print("Printing the available VMs in the pool.") # print("Printing the available VMs in the pool.")
# vm_pool = oca.VirtualMachinePool(client) # vm_pool = oca.VirtualMachinePool(client)
# for vm in vm_pool: # for vm in vm_pool:
# print("%s (memory: %s MB)" % ( vm.name, vm.template.memory)) # print("%s (memory: %s MB)" % ( vm.name, vm.template.memory))
except socket.timeout: except socket.timeout:
e_message = "Socket timeout error." messages.add_message(request, messages.ERROR, "Socket timeout error.")
except OpenNebulaException: except OpenNebulaException:
e_message = "OpenNebulaException occurred." messages.add_message(request, messages.ERROR, "OpenNebulaException occurred.")
context = dict( return redirect('admin:showvms')
# Include common variables for rendering the admin template.
self.admin_site.each_context(request), # Retrives virtual machine pool information
error_msg=e_message,
success_msg=s_message,
vms = vm_pool,
# Anything else you want in the context...
# key=value,
)
return TemplateResponse(request, "hosting/managevms.html", context)
def get_vms(self): def get_vms(self):
vm_pool = oca.VirtualMachinePool(self.client) vm_pool = oca.VirtualMachinePool(self.client)
vm_pool.info() vm_pool.info()
return vm_pool return vm_pool
# Delete VM from the pool and DB by using method finalize()
def delete_vm(self, request, vmid): def delete_vm(self, request, vmid):
vm_pool = self.get_vms()
e_message = ''
s_message = ''
# get the desired vm from the pool # get the desired vm from the pool
vm_id = int(vmid) vm_id = int(vmid)
vm = self.get_vm_by_id(vm_id) vm = self.get_vm_by_id(vm_id)
if vm == -1: if vm == -1:
print("Did not find a vm with id = " + str(vm_id)) messages.add_message(request, messages.ERROR, "Did not find a vm with id = " + str(vm_id))
e_message = "Did not find a vm with id = " + str(vm_id)
else : else :
print("Deleting vm_id = " + str(vm_id) + " state = " + vm.str_state) print("Deleting vm_id = " + str(vm_id) + " state = " + vm.str_state)
if vm.str_state == 'PENDING' or vm.str_state =='POWEROFF' or vm.str_state =='ACTIVE': if vm.str_state == 'PENDING' or vm.str_state =='POWEROFF' or vm.str_state =='ACTIVE':
vm.delete() vm.delete()
s_message = "Deleted from " + vm.str_state + " state vm with id = " + str(vm_id) messages.add_message(request, messages.SUCCESS, "Deleted from " + vm.str_state + " state vm with id = " + str(vm_id))
else: else:
s_message = "Deleted from " + vm.str_state + " state vm with id = " + str(vm_id)
vm.finalize() vm.finalize()
vm_pool = self.get_vms() messages.add_message(request, messages.SUCCESS, "Deleted (using finalize()) from " + vm.str_state + " state vm with id = " + str(vm_id))
context = dict( return redirect('admin:showvms')
# Include common variables for rendering the admin template.
self.admin_site.each_context(request), def stop_vm(self, request, vmid):
error_msg=e_message, e_message = ''
success_msg=s_message, s_message = ''
vms = vm_pool, vm_id = int(vmid)
# Anything else you want in the context... vm = self.get_vm_by_id(vm_id)
# key=value, if vm == -1:
) messages.add_message(request, messages.ERROR, "Did not find a vm with id = " + str(vm_id))
return TemplateResponse(request, "hosting/managevms.html", context) else :
vm.stop()
messages.add_message(request, messages.SUCCESS, "Stopped the vm with id = " + str(vm_id))
return redirect('admin:showvms')
def start_vm(self, request, vmid):
vm_id = int(vmid)
vm = self.get_vm_by_id(vm_id)
if vm == -1:
messages.add_message(request, messages.ERROR, "Did not find a vm with id = " + str(vm_id))
else :
vm.resume()
messages.add_message(request, messages.SUCCESS, "Started the vm with id = " + str(vm_id))
return redirect('admin:showvms')
def get_vm_by_id(self, vmid): def get_vm_by_id(self, vmid):
vms = self.get_vms() vms = self.get_vms()
vms vms

View file

@ -1,12 +1,9 @@
{% extends "admin/base_site.html" %} {% extends "admin/base_site.html" %}
{% load staticfiles bootstrap3 i18n %} {% load staticfiles bootstrap3 i18n %}
{% block content %} {% block content %}
{% if error_msg %}
<p class="alert alert-danger">{{error_msg}}</p>
{% endif %}
{% if success_msg %}
<p class="alert alert-success">{{success_msg}}</p>
{% endif %}
<a href="{% url 'admin:createvm' %}">Create VM</a> <a href="{% url 'admin:createvm' %}">Create VM</a>
{% if vms %} {% if vms %}
@ -19,7 +16,7 @@
<th>Memory</th> <th>Memory</th>
<th>Status</th> <th>Status</th>
<th>User Name</th> <th>User Name</th>
<th>Manage Vms</th> <th>Actions</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -30,12 +27,20 @@
<td>{{vm.template.memory}}</td> <td>{{vm.template.memory}}</td>
<td>{{vm.str_state}}</td> <td>{{vm.str_state}}</td>
<td>{{vm.uname}}</td> <td>{{vm.uname}}</td>
<td><a href="{% url 'admin:deletevm' vm.id %}">Delete VM</a></td> <td>
{% if vm.str_state == 'ACTIVE' %}
<a href="{% url 'admin:stopvm' vm.id %}">Stop VM</a>
{% elif vm.str_state == 'STOPPED' %}
&nbsp;&nbsp;&nbsp;<a href="{% url 'admin:startvm' vm.id %}">Start VM</a>
{% endif %}
&nbsp;&nbsp;&nbsp;<a href="{% url 'admin:deletevm' vm.id %}">Delete VM</a>
</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
</section> </section>
{% endif %} {% endif %}
{% endblock %} {% endblock %}