Snapper: Modify configurations to reduce disk usage

Signed-off-by: Aakanksha Saini <aakanksa@thoughtworks.com>
Signed-off-by: Shubham Agarwal <shubhama@thoughtworks.com>
Reviewed-by: Joseph Nuthalapati <njoseph@thoughtworks.com>
This commit is contained in:
Aakanksha Saini 2018-02-01 14:05:22 +05:30 committed by Joseph Nuthalapati
parent 5edf7d00fa
commit ace8abe34a
No known key found for this signature in database
GPG Key ID: 5398F00A2FA43C35
5 changed files with 53 additions and 23 deletions

View File

@ -70,9 +70,19 @@ def subcommand_setup(_):
command = ['snapper', 'create-config', '/']
subprocess.run(command, check=True)
_set_default_config()
_add_fstab_entry('/')
def _set_default_config():
# Software snapshots' default minimum age: 30 days
command = ['snapper','set-config', 'TIMELINE_CREATE=yes',
'TIMELINE_LIMIT_HOURLY=10', 'TIMELINE_LIMIT_MONTHLY=2',
'TIMELINE_LIMIT_WEEKLY=2', 'TIMELINE_LIMIT_YEARLY=0',
'TIMELINE_LIMIT_DAILY=3', 'NUMBER_MIN_AGE=2592000']
subprocess.run(command, check=True)
def _add_fstab_entry(mount_point):
"""Add mountpoint for subvolumes."""
snapshots_mount_point = os.path.join(mount_point, '.snapshots')

View File

@ -59,8 +59,14 @@ def setup(helper, old_version=None):
helper.call('post', actions.superuser_run, 'snapshot', ['setup'])
def is_timeline_snapshots_enabled():
"""Return whether timeline snapshots are enabled."""
def get_configuration():
output = actions.superuser_run('snapshot', ['get-config'])
output = json.loads(output)
return output['TIMELINE_CREATE'] == 'yes'
return {'enable_timeline_snapshots': output['TIMELINE_CREATE'] == 'yes',
'hourly_limit': output['TIMELINE_LIMIT_HOURLY'],
'daily_limit': output['TIMELINE_LIMIT_DAILY'],
'weekly_limit': output['TIMELINE_LIMIT_WEEKLY'],
'yearly_limit': output['TIMELINE_LIMIT_YEARLY'],
'monthly_limit': output['TIMELINE_LIMIT_MONTHLY'],
'number_min_age': round(int(output['NUMBER_MIN_AGE']) / 86400),
'timeline_min_age': round(int(output['TIMELINE_MIN_AGE']) / 86400)}

View File

@ -27,3 +27,9 @@ class SnapshotForm(forms.Form):
label=_('Enable Timeline Snapshots'), required=False, help_text=_(
'Uncheck this to disable timeline snapshots '
'(hourly, daily, monthly and yearly).'))
hourly_limit = forms.IntegerField(label=_('Hourly Snapshots Limit'),required=False, min_value=0)
daily_limit = forms.IntegerField(label=_('Daily Snapshots Limit'),required=False, min_value=0)
weekly_limit = forms.IntegerField(label=_('Weekly Snapshots Limit'),required=False, min_value=0)
monthly_limit = forms.IntegerField(label=_('Monthly Snapshots Limit'),required=False, min_value=0)
yearly_limit = forms.IntegerField(label=_('Yearly Snapshots Limit'),required=False, min_value=0)
number_min_age = forms.IntegerField(label=_('Software Snapshots Minimum Age (days)'), required=False, min_value=0)

View File

@ -23,20 +23,19 @@
{% block configuration %}
<p>
<form class="form" method="post">
<form class="form" method="post">
{% csrf_token %}
<h3>{% trans "Configuration" %}</h3>
{{ form|bootstrap }}
{{ form|bootstrap }}
<input type="submit" class="btn btn-primary" name="update"
value="{% trans "Update setup" %}"/>
<hr>
<hr>
<div class="row">
<div class="col-xs-6 text-left">
<input type="submit" class="btn btn-primary" name="create"
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"

View File

@ -31,13 +31,13 @@ from plinth.errors import ActionError
from plinth.modules import snapshot as snapshot_module
from plinth.utils import yes_or_no
from . import is_timeline_snapshots_enabled
from . import get_configuration
from .forms import SnapshotForm
def index(request):
"""Show snapshot list."""
status = get_status()
status = get_configuration()
if request.method == 'POST':
form = SnapshotForm(request.POST)
if 'create' in request.POST:
@ -45,7 +45,7 @@ def index(request):
messages.success(request, _('Created snapshot.'))
if 'update' in request.POST and form.is_valid():
update_configuration(request, status, form.cleaned_data)
status = get_status()
status = get_configuration()
form = SnapshotForm(initial=status)
else:
form = SnapshotForm(initial=status)
@ -65,17 +65,31 @@ def index(request):
})
def update_key_configuration(key, old_status, new_status, stamp,threshold):
if old_status[key] != new_status[key]:
actions.superuser_run(
'snapshot',
['configure', stamp.format(threshold)])
def update_configuration(request, old_status, new_status):
"""Update configuration of snapshots."""
try:
key = 'enable_timeline_snapshots'
if old_status[key] != new_status[key]:
enable_timeline = yes_or_no(
new_status['enable_timeline_snapshots'])
actions.superuser_run(
'snapshot',
['configure', 'TIMELINE_CREATE={}'.format(enable_timeline)])
messages.success(request,
update_key_configuration('enable_timeline_snapshots', old_status, new_status, 'TIMELINE_CREATE={}', yes_or_no(new_status['enable_timeline_snapshots']))
update_key_configuration('hourly_limit',old_status,new_status,'TIMELINE_LIMIT_HOURLY={}', new_status['hourly_limit'])
update_key_configuration('daily_limit', old_status, new_status, 'TIMELINE_LIMIT_DAILY={}', new_status['daily_limit'])
update_key_configuration('weekly_limit', old_status, new_status, 'TIMELINE_LIMIT_WEEKLY={}', new_status['weekly_limit'])
update_key_configuration('monthly_limit', old_status, new_status, 'TIMELINE_LIMIT_MONTHLY={}', new_status['monthly_limit'])
update_key_configuration('yearly_limit', old_status, new_status, 'TIMELINE_LIMIT_YEARLY={}', new_status['yearly_limit'])
update_key_configuration('number_min_age', old_status, new_status, 'NUMBER_MIN_AGE={}', int(new_status['number_min_age'])*86400)
actions.superuser_run(
'snapshot',
['configure', 'NUMBER_LIMIT=0-0'])
actions.superuser_run(
'snapshot',
['configure', 'NUMBER_LIMIT_IMPORTANT=4-10'])
messages.success(request,
_('Timeline Snapshots configuration updated'))
except ActionError as exception:
messages.error(request,
@ -142,8 +156,3 @@ def rollback(request, number):
'title': _('Rollback to Snapshot'),
'snapshot': snapshot
})
def get_status():
"""Get current status of snapshot configuration."""
return {'enable_timeline_snapshots': is_timeline_snapshots_enabled()}