1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-24 23:25:24 +00:00

Enabled instances buttons

This commit is contained in:
Retspen 2015-03-11 14:54:34 +02:00
parent bf5b0a9a42
commit 9b02c64ff4
2 changed files with 36 additions and 11 deletions

View file

@ -5,6 +5,7 @@ from computes.models import Compute
from instances.models import Instance from instances.models import Instance
from users.models import UserInstance from users.models import UserInstance
from vrtManager.hostdetails import wvmHostDetails from vrtManager.hostdetails import wvmHostDetails
from vrtManager.instance import wvmInstance, wvmInstances
from vrtManager.connection import connection_manager from vrtManager.connection import connection_manager
from libvirt import libvirtError from libvirt import libvirtError
@ -45,25 +46,49 @@ def instances(request):
usr_inst.instance.compute.password, usr_inst.instance.compute.password,
usr_inst.instance.compute.type) usr_inst.instance.compute.type)
all_user_vms[usr_inst] = conn.get_user_instances(usr_inst.instance.name) 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: else:
for compute in computes: for comp in computes:
if connection_manager.host_is_up(compute.type, compute.hostname): if connection_manager.host_is_up(comp.type, comp.hostname):
try: try:
conn = wvmHostDetails(compute, compute.login, compute.password, compute.type) conn = wvmHostDetails(comp, comp.login, comp.password, comp.type)
all_host_vms[compute.id, compute.name] = conn.get_host_instances() all_host_vms[comp.id, comp.name] = conn.get_host_instances()
for vm, info in conn.get_host_instances().items(): for vm, info in conn.get_host_instances().items():
try: 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']: if check_uuid.uuid != info['uuid']:
check_uuid.save() check_uuid.save()
except Instance.DoesNotExist: 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() check_uuid.save()
conn.close() conn.close()
except libvirtError as lib_err: except libvirtError as lib_err:
error_messages.append(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()) return render(request, 'instances.html', locals())

View file

@ -53,8 +53,8 @@
<td>{{ info.vcpu }}</td> <td>{{ info.vcpu }}</td>
<td>{{ info.memory }} {% trans "MB" %}</td> <td>{{ info.memory }} {% trans "MB" %}</td>
<td><form action="" method="post" role="form">{% csrf_token %} <td><form action="" method="post" role="form">{% csrf_token %}
<input type="hidden" name="name" value="{{ info.name }}"/> <input type="hidden" name="name" value="{{ vm }}"/>
<input type="hidden" name="compute" value="{{ host.0 }}"/> <input type="hidden" name="compute_id" value="{{ host.0 }}"/>
{% ifequal info.status 5 %} {% ifequal info.status 5 %}
<button class="btn btn-sm btn-default" type="submit" name="start" title="Start"> <button class="btn btn-sm btn-default" type="submit" name="start" title="Start">
<span class="glyphicon glyphicon-play"></span> <span class="glyphicon glyphicon-play"></span>
@ -121,7 +121,7 @@
<tbody class="searchable"> <tbody class="searchable">
{% for inst, vm in all_user_vms.items %} {% for inst, vm in all_user_vms.items %}
<tr> <tr>
<td><a href="{% url 'instance' vm.host_id vm.name %}">{{ vm.name }}</a></td> <td><a href="{% url 'instance' vm.compute_id vm.name %}">{{ vm.name }}</a></td>
<td>{% ifequal vm.status 1 %} <td>{% ifequal vm.status 1 %}
<p class="text-success">{% trans "Active" %}</p> <p class="text-success">{% trans "Active" %}</p>
{% endifequal %} {% endifequal %}
@ -136,7 +136,7 @@
<td>{{ vm.memory }} {% trans "MB" %}</td> <td>{{ vm.memory }} {% trans "MB" %}</td>
<td><form action="" method="post" role="form">{% csrf_token %} <td><form action="" method="post" role="form">{% csrf_token %}
<input type="hidden" name="name" value="{{ vm.name }}"/> <input type="hidden" name="name" value="{{ vm.name }}"/>
<input type="hidden" name="compute" value="{{ vm.host_id }}"/> <input type="hidden" name="compute_id" value="{{ vm.compute_id }}"/>
{% ifequal vm.status 5 %} {% ifequal vm.status 5 %}
<button class="btn btn-sm btn-default" type="submit" name="start" title="Start"> <button class="btn btn-sm btn-default" type="submit" name="start" title="Start">
<span class="glyphicon glyphicon-play"></span> <span class="glyphicon glyphicon-play"></span>