1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2025-07-31 12:41:08 +00:00

external snapshot implementation

This commit is contained in:
cserma 2023-04-04 09:51:59 +03:00 committed by catborise
parent ad9f1db643
commit fd6b2ec4bf
4 changed files with 315 additions and 7 deletions

101
instances/templates/instances/snapshots_tab.html Normal file → Executable file
View file

@ -13,6 +13,16 @@
{% trans "Manage Snapshots" %}
</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" data-bs-toggle="tab" data-bs-target="#externalSnapshot" type="button" role="tab" aria-controls="externalSnapshot" aria-selected="false">
External Snapshot
</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" data-bs-toggle="tab" data-bs-target="#manageExternalSnapshots" type="button" role="tab" aria-controls="manageExternalSnapshots" aria-selected="false">
Manage External Snapshots
</button>
</li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
@ -29,7 +39,11 @@
<input type="text" class="form-control form-control-lg" name="name" placeholder="{% trans "Snapshot Name" %}" maxlength="14">
<span class="input-group-text">|</span>
<input type="text" class="form-control form-control-lg" name="description" placeholder="{% trans "Snapshot Description" %}" maxlength="45">
<input type="submit" class="btn btn-lg btn-success float-end" name="snapshot" value="{% trans "Take Snapshot" %}" onclick="showPleaseWaitDialog();">
{% if external_snapshots|length > 0 %}
<input type="submit" class="btn btn-lg btn-success disabled float-end" name="snapshot" value="{% trans "Take Snapshot" %}">
{% else %}
<input type="submit" class="btn btn-lg btn-success float-end" name="snapshot" value="{% trans "Take Snapshot" %}" onclick="showPleaseWaitDialog();">
{% endif %}
</div>
</form>
<div class="clearfix"></div>
@ -56,11 +70,11 @@
{% csrf_token %}
<input type="hidden" name="name" value="{{ snap.name }}">
{% if instance.status == 5 %}
<button type="submit" class="btn btn-sm btn-secondary" name="revert_snapshot" title="{% trans 'Revert to this Snapshot' %}" onclick="return confirm('Are you sure?')">
<button type="submit" class="btn btn-sm btn-primary" name="revert_snapshot" title="{% trans 'Revert to this Snapshot' %}" onclick="return confirm('Are you sure?')">
<span class="fa fa-download"></span>
</button>
{% else %}
<button type="button" class="btn btn-sm btn-secondary disabled"
<button type="button" class="btn btn-sm btn-primary disabled"
title="{% trans "To restore snapshots you need Power Off the instance." %}">
<span class="fa fa-download"></span>
</button>
@ -84,5 +98,86 @@
<p>{% trans "You do not have any snapshots" %}</p>
{% endif %}
</div>
<div role="tabpanel" class="tab-pane tab-pane-bordered" id="externalSnapshot">
{% if instance.status != 5 %}
<p>You can get external snapshots within this tab.</p>
<p class="text-primary">External snapshots are experimental in this stage, use it if you know what you are doing.</p>
{% else %}
<p>Create an external snapshot</p>
{% endif %}
<p class="text-danger">Give your External Snapshot a <b>distinctive description</b> so it wouldn't get mixed with other snapshots.</p>
<form action="{% url 'instances:create_external_snapshot' instance.id %}" method="post" role="form" aria-label="Create snapshot form">
{% csrf_token %}
<div class="mb-3" style="white-space:pre-line">
<input type="text" class="form-control form-control-lg" name="name" placeholder="{% trans "Snapshot Name" %}" maxlength="14">
<input type="text" class="form-control form-control-lg" name="description" placeholder="{% trans "Snapshot Description" %}" maxlength="45">
{% if external_snapshots|length > 0 or instance.snapshots|length > 0 %}
<p class="text-danger">WebVirtCloud supports only one External Snapshot at the moment.</p>
<input type="submit" class="btn btn-lg btn-success disabled float-end" name="snapshot" value="{% trans "Take Snapshot" %}">
{% else %}
<input type="submit" class="btn btn-lg btn-success float-end" name="snapshot" value="{% trans "Take Snapshot" %}" onclick="showPleaseWaitDialog();">
{% endif %}
</div>
</form>
<div class="clearfix"></div>
</div>
<div role="tabpanel" class="tab-pane tab-pane-bordered" id="manageExternalSnapshots">
{% if external_snapshots %}
<div class="table-responsive">
<table class="table">
<thead>
<th scope="col">Name</th>
<th scope="col">Date</th>
<th scope="col">Description</th>
<th scope="colgroup" colspan="2">{% trans "Action" %}</th>
</thead>
<tbody>
{% for external_snapshot in external_snapshots %}
<tr>
{% for snapshot_cols in external_snapshot %}
<td>{{snapshot_cols}}</td>
{% endfor %}
<td style="width:30px;">
<form action="{% url 'instances:revert_external_snapshot' instance.id %}" method="post" role="form" aria-label="Restore external snapshot form">
{% csrf_token %}
<input type="hidden" name="name" value="{{ external_snapshot.0 }}">
<input type="hidden" name="date" value="{{ external_snapshot.1 }}">
<input type="hidden" name="desc" value="{{ external_snapshot.2 }}">
{% if instance.status == 5 %}
<button type="submit" class="btn btn-sm btn-primary" name="revert_external_snapshot" title="{% trans 'Revert to this Snapshot' %}" onclick="return confirm('You are going to lose your unsaved work by reverting to this snapshot state. Are you sure?')">
<span class="fa fa-download"></span>
</button>
{% else %}
<button type="button" class="btn btn-sm btn-primary disabled"
title="{% trans "To restore snapshots you need Power Off the instance." %}">
<span class="fa fa-download"></span>
</button>
{% endif %}
</form>
</td>
<td style="width:30px;">
<form action="{% url 'instances:delete_external_snapshot' instance.id %}" method="post" role="form" aria-label="Delete external snapshot form">{% csrf_token %}
<input type="hidden" name="name" value="{{ external_snapshot.0 }}">
{% if instance.status != 5 %}
<button type="submit" class="btn btn-sm btn-danger" name="delete_external_snapshot" title="{% trans 'Delete Snapshot' %}" onclick="return confirm('You are about to delete this snapshot and merge it with base image. Are you sure?')">
{% icon 'trash' %}
</button>
{% else %}
<button type="submit" class="btn btn-sm btn-danger disabled" title="{% trans 'Delete Snapshot' %}">
{% icon 'trash' %}
</button>
{% endif %}
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% else%}
<p>{% trans "You do not have any snapshots" %}</p>
{% endif %}
</div>
</div>
</div>

3
instances/urls.py Normal file → Executable file
View file

@ -39,6 +39,9 @@ urlpatterns = [
path("<int:pk>/snapshot/", views.snapshot, name="snapshot"),
path("<int:pk>/delete_snapshot/", views.delete_snapshot, name="delete_snapshot"),
path("<int:pk>/revert_snapshot/", views.revert_snapshot, name="revert_snapshot"),
path("<int:pk>/create_external_snapshot/", views.create_external_snapshot, name="create_external_snapshot"),
path("<int:pk>/revert_external_snapshot/", views.revert_external_snapshot, name="revert_external_snapshot"),
path("<int:pk>/delete_external_snapshot/", views.delete_external_snapshot, name="delete_external_snapshot"),
path("<int:pk>/set_vcpu/", views.set_vcpu, name="set_vcpu"),
path("<int:pk>/set_vcpu_hotplug/", views.set_vcpu_hotplug, name="set_vcpu_hotplug"),
path("<int:pk>/set_autostart/", views.set_autostart, name="set_autostart"),

65
instances/views.py Normal file → Executable file
View file

@ -146,7 +146,9 @@ def instance(request, pk):
instance.drbd = drbd_status(request, pk)
instance.save()
return render(request, "instance.html", locals())
external_snapshots = get_external_snapshots(request,pk)
return render(request, "instance.html", locals(),)
def status(request, pk):
@ -1014,6 +1016,67 @@ def revert_snapshot(request, pk):
addlogmsg(request.user.username, instance.compute.name, instance.name, msg)
return redirect(request.META.get("HTTP_REFERER") + "#managesnapshot")
def create_external_snapshot(request, pk):
instance = get_instance(request.user, pk)
allow_admin_or_not_template = (
request.user.is_superuser or request.user.is_staff or not instance.is_template
)
if allow_admin_or_not_template and request.user.has_perm(
"instances.snapshot_instances"
):
name = request.POST.get("name", "")
desc = request.POST.get("description", "")
instance.proxy.create_external_snapshot(name, instance, desc=desc)
#msg = _("Create snapshot: %(snap)s") % {"snap": name}
#addlogmsg(request.user.username, instance.compute.name, instance.name, msg)
return redirect(request.META.get("HTTP_REFERER") + "#manageExternalSnapshots")
def get_external_snapshots(request, pk):
instance = get_instance(request.user, pk)
allow_admin_or_not_template = (
request.user.is_superuser or request.user.is_staff or not instance.is_template
)
if allow_admin_or_not_template and request.user.has_perm(
"instances.snapshot_instances"
):
external_snapshots = instance.proxy.get_external_snapshots()
#msg = _("Create snapshot: %(snap)s") % {"snap": name}
#addlogmsg(request.user.username, instance.compute.name, instance.name, msg)
return external_snapshots
def revert_external_snapshot(request, pk):
instance = get_instance(request.user, pk)
allow_admin_or_not_template = (
request.user.is_superuser or request.user.is_staff or not instance.is_template
)
if allow_admin_or_not_template and request.user.has_perm(
"instances.snapshot_instances"
):
name = request.POST.get("name", "")
date = request.POST.get("date", "")
desc = request.POST.get("desc", "")
instance.proxy.revert_external_snapshot(name, instance, date, desc)
#msg = _("Create snapshot: %(snap)s") % {"snap": name}
#addlogmsg(request.user.username, instance.compute.name, instance.name, msg)
return redirect(request.META.get("HTTP_REFERER") + "#manageExternalSnapshots")
def delete_external_snapshot(request, pk):
instance = get_instance(request.user, pk)
allow_admin_or_not_template = (
request.user.is_superuser or request.user.is_staff or not instance.is_template
)
if allow_admin_or_not_template and request.user.has_perm(
"instances.snapshot_instances"
):
name = request.POST.get("name", "")
instance.proxy.delete_external_snapshot(name, instance)
#msg = _("Create snapshot: %(snap)s") % {"snap": name}
#addlogmsg(request.user.username, instance.compute.name, instance.name, msg)
return redirect(request.META.get("HTTP_REFERER") + "#manageExternalSnapshots")
@superuser_only
def set_vcpu(request, pk):