Added start/stop functionality for VMs. Refactored a few functions.
This commit is contained in:
		
					parent
					
						
							
								c7334dfac2
							
						
					
				
			
			
				commit
				
					
						b68fe31361
					
				
			
		
					 2 changed files with 58 additions and 60 deletions
				
			
		|  | @ -6,6 +6,8 @@ from django.template.response import TemplateResponse | |||
| from django.conf import settings | ||||
| from utils.mailer import BaseEmail | ||||
| from django import template | ||||
| from django.shortcuts import redirect | ||||
| from django.contrib import messages | ||||
| import oca | ||||
| import socket | ||||
| from oca.exceptions import OpenNebulaException | ||||
|  | @ -13,17 +15,6 @@ from oca.exceptions import OpenNebulaException | |||
| from .forms import HostingOrderAdminForm | ||||
| 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): | ||||
|     # fields = ('slug', 'imdb_link', 'start', 'finish', 'added_by') | ||||
|     list_display = ('id', 'created_at', 'plan', 'user') | ||||
|  | @ -113,12 +104,12 @@ class HostingManageVMsAdmin(admin.ModelAdmin): | |||
|     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) | ||||
|         urls = super().get_urls() | ||||
|         #socket.setdefaulttimeout(5) | ||||
|         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'^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 | ||||
| 
 | ||||
|  | @ -142,70 +133,72 @@ class HostingManageVMsAdmin(admin.ModelAdmin): | |||
|             # key=value, | ||||
|         ) | ||||
|         return TemplateResponse(request, "hosting/managevms.html", context) | ||||
| 
 | ||||
|    | ||||
|     # Creating VM by using method allocate(client, template) | ||||
|     def create_vm(self, request): | ||||
|         s_message = '' | ||||
|         e_message = '' | ||||
|         message = '' | ||||
|         try : | ||||
|             # 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>') | ||||
|             s_message = "Created with id = " + str(vm_id) | ||||
|             message = "Created with id = " + str(vm_id) | ||||
|             vm_pool = self.get_vms() | ||||
|             messages.add_message(request, messages.SUCCESS, message) | ||||
|             # Lets print the VMs available in the pool | ||||
|             # print("Printing the available VMs in the pool.") | ||||
|             # vm_pool = oca.VirtualMachinePool(client) | ||||
|             # for vm in vm_pool: | ||||
|             # 	print("%s (memory: %s MB)" % ( vm.name, vm.template.memory)) | ||||
|         except socket.timeout: | ||||
|             e_message = "Socket timeout error." | ||||
|             messages.add_message(request, messages.ERROR, "Socket timeout error.") | ||||
|         except OpenNebulaException: | ||||
|             e_message = "OpenNebulaException occurred." | ||||
|         context = dict( | ||||
|             # Include common variables for rendering the admin template. | ||||
|             self.admin_site.each_context(request), | ||||
|             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) | ||||
|      | ||||
|             messages.add_message(request, messages.ERROR, "OpenNebulaException occurred.") | ||||
|         return redirect('admin:showvms') | ||||
|     | ||||
|     # Retrives virtual machine pool information | ||||
|     def get_vms(self): | ||||
|         vm_pool = oca.VirtualMachinePool(self.client) | ||||
|         vm_pool.info() | ||||
|         return vm_pool | ||||
| 
 | ||||
|          | ||||
|     # Delete VM from the pool and DB by using method finalize() | ||||
|     def delete_vm(self, request, vmid): | ||||
|         vm_pool = self.get_vms() | ||||
|         e_message = '' | ||||
|         s_message = '' | ||||
|         # get the desired vm from the pool | ||||
|         vm_id = int(vmid) | ||||
|         vm = self.get_vm_by_id(vm_id) | ||||
|         if vm == -1: | ||||
|             print("Did not find a vm with id = " + str(vm_id)) | ||||
|             e_message = "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)) | ||||
|         else : | ||||
|             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': | ||||
|                 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: | ||||
|                 s_message = "Deleted from " + vm.str_state + " state vm with id = " + str(vm_id) | ||||
|                 vm.finalize() | ||||
|         vm_pool = self.get_vms() | ||||
|         context = dict( | ||||
|             # Include common variables for rendering the admin template. | ||||
|             self.admin_site.each_context(request), | ||||
|             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) | ||||
|              | ||||
|                 messages.add_message(request, messages.SUCCESS, "Deleted (using finalize()) from " + vm.str_state + " state vm with id = " + str(vm_id)) | ||||
|         return redirect('admin:showvms') | ||||
| 
 | ||||
|     def stop_vm(self, request, vmid): | ||||
|         e_message = '' | ||||
|         s_message = '' | ||||
|         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.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): | ||||
|         vms = self.get_vms() | ||||
|         vms | ||||
|  |  | |||
|  | @ -1,12 +1,9 @@ | |||
| {% extends "admin/base_site.html" %} | ||||
| {% load staticfiles bootstrap3 i18n %} | ||||
| {% 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> | ||||
| 
 | ||||
| {% if vms %} | ||||
|  | @ -19,7 +16,7 @@ | |||
| <th>Memory</th> | ||||
| <th>Status</th> | ||||
| <th>User Name</th> | ||||
| <th>Manage Vms</th> | ||||
| <th>Actions</th> | ||||
| </tr> | ||||
| </thead> | ||||
| <tbody> | ||||
|  | @ -30,12 +27,20 @@ | |||
| <td>{{vm.template.memory}}</td> | ||||
| <td>{{vm.str_state}}</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' %} | ||||
|    <a href="{% url 'admin:startvm' vm.id %}">Start VM</a> | ||||
| {% endif %} | ||||
|    <a href="{% url 'admin:deletevm' vm.id %}">Delete VM</a> | ||||
| 
 | ||||
| 
 | ||||
| </td> | ||||
| </tr> | ||||
| {% endfor %} | ||||
| </tbody> | ||||
| </table> | ||||
| </section> | ||||
| {% endif %} | ||||
| 
 | ||||
| {% endblock %} | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue