From 9b02c64ff44259cae97460bebdf448657d5d6395 Mon Sep 17 00:00:00 2001 From: Retspen Date: Wed, 11 Mar 2015 14:54:34 +0200 Subject: [PATCH] Enabled instances buttons --- instances/views.py | 39 ++++++++++++++++++++++++++++++++------- templates/instances.html | 8 ++++---- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/instances/views.py b/instances/views.py index cc47227..a9e3025 100644 --- a/instances/views.py +++ b/instances/views.py @@ -5,6 +5,7 @@ from computes.models import Compute from instances.models import Instance from users.models import UserInstance from vrtManager.hostdetails import wvmHostDetails +from vrtManager.instance import wvmInstance, wvmInstances from vrtManager.connection import connection_manager from libvirt import libvirtError @@ -45,25 +46,49 @@ def instances(request): usr_inst.instance.compute.password, usr_inst.instance.compute.type) all_user_vms[usr_inst] = conn.get_user_instances(usr_inst.instance.name) - all_user_vms[usr_inst].update({'host_id': usr_inst.instance.compute.id}) + all_user_vms[usr_inst].update({'compute_id': usr_inst.instance.compute.id}) else: - for compute in computes: - if connection_manager.host_is_up(compute.type, compute.hostname): + for comp in computes: + if connection_manager.host_is_up(comp.type, comp.hostname): try: - conn = wvmHostDetails(compute, compute.login, compute.password, compute.type) - all_host_vms[compute.id, compute.name] = conn.get_host_instances() + conn = wvmHostDetails(comp, comp.login, comp.password, comp.type) + all_host_vms[comp.id, comp.name] = conn.get_host_instances() for vm, info in conn.get_host_instances().items(): try: - check_uuid = Instance.objects.get(compute_id=compute.id, name=vm) + check_uuid = Instance.objects.get(compute_id=comp.id, name=vm) if check_uuid.uuid != info['uuid']: check_uuid.save() except Instance.DoesNotExist: - check_uuid = Instance(compute_id=compute.id, name=vm, uuid=info['uuid']) + check_uuid = Instance(compute_id=comp.id, name=vm, uuid=info['uuid']) check_uuid.save() conn.close() except libvirtError as lib_err: error_messages.append(lib_err) + if request.method == 'POST': + name = request.POST.get('name', '') + compute_id = request.POST.get('compute_id', '') + compute = Compute.objects.get(id=compute_id) + try: + conn = wvmInstances(compute.hostname, + compute.login, + compute.password, + compute.type) + if 'start' in request.POST: + conn.start(name) + return HttpResponseRedirect(request.get_full_path()) + if 'destroy' in request.POST: + conn.force_shutdown(name) + return HttpResponseRedirect(request.get_full_path()) + if 'suspend' in request.POST: + conn.suspend(name) + return HttpResponseRedirect(request.get_full_path()) + if 'resume' in request.POST: + conn.resume(name) + return HttpResponseRedirect(request.get_full_path()) + except libvirtError as lib_err: + error_messages.append(lib_err) + return render(request, 'instances.html', locals()) diff --git a/templates/instances.html b/templates/instances.html index 2c4925e..7092734 100644 --- a/templates/instances.html +++ b/templates/instances.html @@ -53,8 +53,8 @@ {{ info.vcpu }} {{ info.memory }} {% trans "MB" %}
{% csrf_token %} - - + + {% ifequal info.status 5 %}