mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-05-13 10:30:16 +00:00
snapshots: Button to delete all snapshots
- closes #1144 Signed-off-by: Joseph Nuthalapati <njoseph@thoughtworks.com> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
parent
81f4d7ec42
commit
3e5dcfcbf5
@ -15,17 +15,17 @@
|
|||||||
# You should have received a copy of the GNU Affero General Public License
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Configuration helper for filesystem snapshots.
|
Configuration helper for filesystem snapshots.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import augeas
|
|
||||||
import argparse
|
import argparse
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
import augeas
|
||||||
|
|
||||||
FSTAB = '/etc/fstab'
|
FSTAB = '/etc/fstab'
|
||||||
AUG_FSTAB = '/files/etc/fstab'
|
AUG_FSTAB = '/files/etc/fstab'
|
||||||
|
|
||||||
@ -39,9 +39,13 @@ def parse_arguments():
|
|||||||
subparsers.add_parser('list', help='List snapshots')
|
subparsers.add_parser('list', help='List snapshots')
|
||||||
subparsers.add_parser('create', help='Create snapshot')
|
subparsers.add_parser('create', help='Create snapshot')
|
||||||
|
|
||||||
subparser = subparsers.add_parser('delete', help='Delete snapshot')
|
subparser = subparsers.add_parser('delete',
|
||||||
|
help='Delete a snapshot by number')
|
||||||
subparser.add_argument('number', help='Number of snapshot to delete')
|
subparser.add_argument('number', help='Number of snapshot to delete')
|
||||||
|
|
||||||
|
subparser = subparsers.add_parser('delete-all',
|
||||||
|
help='Delete all the snapshots')
|
||||||
|
|
||||||
subparser = subparsers.add_parser('rollback', help='Rollback to snapshot')
|
subparser = subparsers.add_parser('rollback', help='Rollback to snapshot')
|
||||||
subparser.add_argument('number', help='Number of snapshot to rollback to')
|
subparser.add_argument('number', help='Number of snapshot to rollback to')
|
||||||
|
|
||||||
@ -68,8 +72,8 @@ def _add_fstab_entry(mount_point):
|
|||||||
"""Add mountpoint for subvolumes."""
|
"""Add mountpoint for subvolumes."""
|
||||||
snapshots_mount_point = os.path.join(mount_point, '.snapshots')
|
snapshots_mount_point = os.path.join(mount_point, '.snapshots')
|
||||||
|
|
||||||
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +
|
aug = augeas.Augeas(
|
||||||
augeas.Augeas.NO_MODL_AUTOLOAD)
|
flags=augeas.Augeas.NO_LOAD + augeas.Augeas.NO_MODL_AUTOLOAD)
|
||||||
aug.set('/augeas/load/Fstab/lens', 'Fstab.lns')
|
aug.set('/augeas/load/Fstab/lens', 'Fstab.lns')
|
||||||
aug.set('/augeas/load/Fstab/incl[last() + 1]', FSTAB)
|
aug.set('/augeas/load/Fstab/incl[last() + 1]', FSTAB)
|
||||||
aug.load()
|
aug.load()
|
||||||
@ -95,11 +99,15 @@ def _add_fstab_entry(mount_point):
|
|||||||
aug.save()
|
aug.save()
|
||||||
|
|
||||||
|
|
||||||
def subcommand_list(_):
|
def _get_snapper_list():
|
||||||
"""List snapshots."""
|
|
||||||
command = ['snapper', 'list']
|
command = ['snapper', 'list']
|
||||||
process = subprocess.run(command, stdout=subprocess.PIPE, check=True)
|
process = subprocess.run(command, stdout=subprocess.PIPE, check=True)
|
||||||
lines = process.stdout.decode().splitlines()
|
return process.stdout.decode().splitlines()
|
||||||
|
|
||||||
|
|
||||||
|
def subcommand_list(_):
|
||||||
|
"""List snapshots."""
|
||||||
|
lines = _get_snapper_list()
|
||||||
keys = ('type', 'number', 'pre_number', 'date', 'user', 'cleanup',
|
keys = ('type', 'number', 'pre_number', 'date', 'user', 'cleanup',
|
||||||
'description')
|
'description')
|
||||||
snapshots = []
|
snapshots = []
|
||||||
@ -137,15 +145,30 @@ def subcommand_create(_):
|
|||||||
|
|
||||||
|
|
||||||
def subcommand_delete(arguments):
|
def subcommand_delete(arguments):
|
||||||
"""Delete snapshot."""
|
"""Delete a snapshot by number."""
|
||||||
command = ['snapper', 'delete', arguments.number]
|
command = ['snapper', 'delete', arguments.number]
|
||||||
subprocess.run(command, check=True)
|
subprocess.run(command, check=True)
|
||||||
|
|
||||||
|
|
||||||
|
def subcommand_delete_all(_):
|
||||||
|
"""Delete all the snapshots."""
|
||||||
|
lines = _get_snapper_list()
|
||||||
|
snapshot_range = [line.split('|')[1].strip() for line in lines[3:]]
|
||||||
|
if snapshot_range:
|
||||||
|
if len(snapshot_range) == 1:
|
||||||
|
to_delete = snapshot_range[0]
|
||||||
|
else:
|
||||||
|
to_delete = '-'.join([snapshot_range[0], snapshot_range[-1]])
|
||||||
|
command = ['snapper', 'delete', to_delete]
|
||||||
|
subprocess.run(command, check=True)
|
||||||
|
|
||||||
|
|
||||||
def subcommand_rollback(arguments):
|
def subcommand_rollback(arguments):
|
||||||
"""Rollback to snapshot."""
|
"""Rollback to snapshot."""
|
||||||
command = ['snapper', 'rollback', '--description', 'created by rollback',
|
command = [
|
||||||
arguments.number]
|
'snapper', 'rollback', '--description', 'created by rollback',
|
||||||
|
arguments.number
|
||||||
|
]
|
||||||
subprocess.run(command, check=True)
|
subprocess.run(command, check=True)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -22,6 +22,29 @@
|
|||||||
{% load i18n %}
|
{% load i18n %}
|
||||||
|
|
||||||
{% block configuration %}
|
{% block configuration %}
|
||||||
|
<p>
|
||||||
|
<div class="row">
|
||||||
|
<form class="form" method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
<div class="col-xs-6 text-left">
|
||||||
|
<input type="submit" class="btn btn-primary"
|
||||||
|
value="{% trans 'Create Snapshot' %}"/>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<div class="col-xs-6 text-right">
|
||||||
|
<a title="{% trans 'Delete all the snapshots' %}"
|
||||||
|
role="button" class="btn btn-danger"
|
||||||
|
{% if snapshots|length == 1 %}
|
||||||
|
disabled="disabled"
|
||||||
|
{% else %}
|
||||||
|
href="{% url 'snapshot:delete-all' %}"
|
||||||
|
{% endif %}>
|
||||||
|
{% trans 'Delete All' %}
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</p>
|
||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-lg-12">
|
<div class="col-lg-12">
|
||||||
@ -77,13 +100,4 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p>
|
|
||||||
<form class="form" method="post">
|
|
||||||
{% csrf_token %}
|
|
||||||
|
|
||||||
<input type="submit" class="btn btn-primary"
|
|
||||||
value="{% trans "Create Snapshot" %}"/>
|
|
||||||
</form>
|
|
||||||
</p>
|
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|||||||
@ -49,7 +49,7 @@
|
|||||||
<form class="form" method="post">
|
<form class="form" method="post">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
|
|
||||||
<input type="submit" class="btn btn-primary"
|
<input type="submit" class="btn btn-danger"
|
||||||
value="{% blocktrans trimmed with number=snapshot.number %}
|
value="{% blocktrans trimmed with number=snapshot.number %}
|
||||||
Delete Snapshot #{{ number }}
|
Delete Snapshot #{{ number }}
|
||||||
{% endblocktrans %}"/>
|
{% endblocktrans %}"/>
|
||||||
|
|||||||
59
plinth/modules/snapshot/templates/snapshot_delete_all.html
Normal file
59
plinth/modules/snapshot/templates/snapshot_delete_all.html
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
{% comment %}
|
||||||
|
#
|
||||||
|
# This file is part of Plinth.
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Affero General Public License as
|
||||||
|
# published by the Free Software Foundation, either version 3 of the
|
||||||
|
# License, or (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Affero General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
#
|
||||||
|
{% endcomment %}
|
||||||
|
|
||||||
|
{% load bootstrap %}
|
||||||
|
{% load i18n %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<h2>{{ title }}</h2>
|
||||||
|
|
||||||
|
<p>{% trans "Delete the following snapshots permanently?" %}</p>
|
||||||
|
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-lg-12">
|
||||||
|
<table class="table table-bordered table-condensed table-striped">
|
||||||
|
<thead>
|
||||||
|
<th>{% trans "Number" %}</th>
|
||||||
|
<th>{% trans "Date" %}</th>
|
||||||
|
<th>{% trans "Description" %}</th>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for snapshot in snapshots %}
|
||||||
|
<tr>
|
||||||
|
<td>{{ snapshot.number }}</td>
|
||||||
|
<td>{{ snapshot.date }}</td>
|
||||||
|
<td>{{ snapshot.description }}</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<form class="form" method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
|
||||||
|
<input type="submit" class="btn btn-danger"
|
||||||
|
value="{% trans 'Delete Snapshots' %}"/>
|
||||||
|
</form>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
{% endblock %}
|
||||||
@ -14,7 +14,6 @@
|
|||||||
# You should have received a copy of the GNU Affero General Public License
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
|
||||||
"""
|
"""
|
||||||
URLs for the snapshot module.
|
URLs for the snapshot module.
|
||||||
"""
|
"""
|
||||||
@ -23,10 +22,10 @@ from django.conf.urls import url
|
|||||||
|
|
||||||
from . import views
|
from . import views
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^sys/snapshot/$', views.index, name='index'),
|
url(r'^sys/snapshot/$', views.index, name='index'),
|
||||||
url(r'^sys/snapshot/(?P<number>\d+)/delete$', views.delete, name='delete'),
|
url(r'^sys/snapshot/(?P<number>\d+)/delete$', views.delete, name='delete'),
|
||||||
|
url(r'^sys/snapshot/all/delete$', views.delete_all, name='delete-all'),
|
||||||
url(r'^sys/snapshot/(?P<number>\d+)/rollback$', views.rollback,
|
url(r'^sys/snapshot/(?P<number>\d+)/rollback$', views.rollback,
|
||||||
name='rollback'),
|
name='rollback'),
|
||||||
]
|
]
|
||||||
|
|||||||
@ -14,17 +14,17 @@
|
|||||||
# You should have received a copy of the GNU Affero General Public License
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Views for snapshot module.
|
Views for snapshot module.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import json
|
||||||
|
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from django.shortcuts import redirect
|
from django.shortcuts import redirect
|
||||||
from django.template.response import TemplateResponse
|
from django.template.response import TemplateResponse
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
import json
|
|
||||||
|
|
||||||
from plinth import actions
|
from plinth import actions
|
||||||
from plinth.modules import snapshot as snapshot_module
|
from plinth.modules import snapshot as snapshot_module
|
||||||
@ -39,40 +39,55 @@ def index(request):
|
|||||||
output = actions.superuser_run('snapshot', ['list'])
|
output = actions.superuser_run('snapshot', ['list'])
|
||||||
snapshots = json.loads(output)
|
snapshots = json.loads(output)
|
||||||
|
|
||||||
return TemplateResponse(request, 'snapshot.html',
|
return TemplateResponse(request, 'snapshot.html', {
|
||||||
{'title': snapshot_module.name,
|
'title': snapshot_module.name,
|
||||||
'description': snapshot_module.description,
|
'description': snapshot_module.description,
|
||||||
'snapshots': snapshots})
|
'snapshots': snapshots
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
def delete(request, number):
|
def delete(request, number):
|
||||||
"""Show confirmation to delete a snapshot."""
|
"""Show confirmation to delete a snapshot."""
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
actions.superuser_run('snapshot', ['delete', number])
|
actions.superuser_run('snapshot', ['delete', number])
|
||||||
messages.success(
|
messages.success(request,
|
||||||
request, _('Deleted snapshot #{number}.').format(number=number))
|
_('Deleted snapshot #{number}.').format(
|
||||||
|
number=number))
|
||||||
|
return redirect(reverse('snapshot:index'))
|
||||||
|
|
||||||
|
output = actions.superuser_run('snapshot', ['list'])
|
||||||
|
snapshots = json.loads(output)
|
||||||
|
snapshot = next(s for s in snapshots if s['number'] == number)
|
||||||
|
|
||||||
|
return TemplateResponse(request, 'snapshot_delete.html', {
|
||||||
|
'title': _('Delete Snapshot'),
|
||||||
|
'snapshot': snapshot
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
def delete_all(request):
|
||||||
|
"""Show confirmation to delete all snapshots."""
|
||||||
|
if request.method == 'POST':
|
||||||
|
actions.superuser_run('snapshot', ['delete-all'])
|
||||||
|
messages.success(request, _('Deleted all snapshots.'))
|
||||||
return redirect(reverse('snapshot:index'))
|
return redirect(reverse('snapshot:index'))
|
||||||
|
|
||||||
output = actions.superuser_run('snapshot', ['list'])
|
output = actions.superuser_run('snapshot', ['list'])
|
||||||
snapshots = json.loads(output)
|
snapshots = json.loads(output)
|
||||||
|
|
||||||
snapshot = None
|
return TemplateResponse(request, 'snapshot_delete_all.html', {
|
||||||
for current_snapshot in snapshots:
|
'title': _('Delete Snapshots'),
|
||||||
if current_snapshot['number'] == number:
|
'snapshots': snapshots[1:]
|
||||||
snapshot = current_snapshot
|
})
|
||||||
|
|
||||||
return TemplateResponse(request, 'snapshot_delete.html',
|
|
||||||
{'title': _('Delete Snapshot'),
|
|
||||||
'snapshot': snapshot})
|
|
||||||
|
|
||||||
|
|
||||||
def rollback(request, number):
|
def rollback(request, number):
|
||||||
"""Show confirmation to rollback to a snapshot."""
|
"""Show confirmation to rollback to a snapshot."""
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
actions.superuser_run('snapshot', ['rollback', number])
|
actions.superuser_run('snapshot', ['rollback', number])
|
||||||
messages.success(
|
messages.success(request,
|
||||||
request,
|
_('Rolled back to snapshot #{number}.').format(
|
||||||
_('Rolled back to snapshot #{number}.').format(number=number))
|
number=number))
|
||||||
messages.warning(
|
messages.warning(
|
||||||
request,
|
request,
|
||||||
_('The system must be restarted to complete the rollback.'))
|
_('The system must be restarted to complete the rollback.'))
|
||||||
@ -86,6 +101,7 @@ def rollback(request, number):
|
|||||||
if current_snapshot['number'] == number:
|
if current_snapshot['number'] == number:
|
||||||
snapshot = current_snapshot
|
snapshot = current_snapshot
|
||||||
|
|
||||||
return TemplateResponse(request, 'snapshot_rollback.html',
|
return TemplateResponse(request, 'snapshot_rollback.html', {
|
||||||
{'title': _('Rollback to Snapshot'),
|
'title': _('Rollback to Snapshot'),
|
||||||
'snapshot': snapshot})
|
'snapshot': snapshot
|
||||||
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user