snapshots: Refactoring and indentation changes

- Made updating snapshot configuration more efficient by running a single
  snapper command instead of one per configuration.
- Set default configuration for NUMBER_LIMIT only once at the time of
  installation. Snapshot version has been incremented to support this.

Signed-off-by: Joseph Nuthalapati <njoseph@thoughtworks.com>
This commit is contained in:
Joseph Nuthalapati 2018-03-09 13:42:54 +05:30
parent 7ff2196d04
commit 9ab0e13ab9
No known key found for this signature in database
GPG Key ID: 5398F00A2FA43C35
6 changed files with 80 additions and 63 deletions

View File

@ -79,7 +79,8 @@ def _set_default_config():
'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=1296000'
'TIMELINE_LIMIT_DAILY=3', 'NUMBER_MIN_AGE=1296000', 'NUMBER_LIMIT=0',
'NUMBER_LIMIT_IMPORTANT=4-10'
]
subprocess.run(command, check=True)

View File

@ -25,7 +25,7 @@ from django.utils.translation import ugettext_lazy as _
from plinth import actions
from plinth.menu import main_menu
version = 2
version = 3
managed_packages = ['snapper']
@ -61,12 +61,23 @@ def setup(helper, old_version=None):
def get_configuration():
output = actions.superuser_run('snapshot', ['get-config'])
output = json.loads(output)
def get_boolean_choice(status):
return ('yes', 'Enabled') if status else ('no', 'Disabled')
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),
'enable_timeline_snapshots':
get_boolean_choice(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),
}

View File

@ -23,26 +23,27 @@ from django.utils.translation import ugettext_lazy as _
class SnapshotForm(forms.Form):
enable_timeline_snapshots = forms.BooleanField(
label=_('Enable Timeline Snapshots'), required=False, help_text=_(
'Uncheck this to disable timeline snapshots '
'(hourly, daily, monthly and yearly).'))
enable_timeline_snapshots = forms.ChoiceField(
label=_('Timeline Snapshots'),
help_text=_('Enable or disable timeline snapshots '
'(hourly, daily, monthly and yearly).'),
choices=[('yes', 'Enabled'), ('no', 'Disabled')])
hourly_limit = forms.IntegerField(
label=_('Hourly Snapshots Limit'), min_value=0, help_text=_(
'Keep a maximum of this many hourly snapshots.'))
label=_('Hourly Snapshots Limit'), min_value=0,
help_text=_('Keep a maximum of this many hourly snapshots.'))
daily_limit = forms.IntegerField(
label=_('Daily Snapshots Limit'), min_value=0, help_text=_(
'Keep a maximum of this many daily snapshots.'))
label=_('Daily Snapshots Limit'), min_value=0,
help_text=_('Keep a maximum of this many daily snapshots.'))
weekly_limit = forms.IntegerField(
label=_('Weekly Snapshots Limit'), min_value=0, help_text=_(
'Keep a maximum of this many weekly snapshots.'))
label=_('Weekly Snapshots Limit'), min_value=0,
help_text=_('Keep a maximum of this many weekly snapshots.'))
monthly_limit = forms.IntegerField(
label=_('Monthly Snapshots Limit'), min_value=0, help_text=_(
'Keep a maximum of this many monthly snapshots.'))
label=_('Monthly Snapshots Limit'), min_value=0,
help_text=_('Keep a maximum of this many monthly snapshots.'))
yearly_limit = forms.IntegerField(
label=_('Yearly Snapshots Limit'), min_value=0, help_text=_(

View File

@ -22,13 +22,13 @@
{% load i18n %}
{% block configuration %}
<p>
<form class="form-horizontal" method="post">
{% csrf_token %}
<h3>{% trans "Configuration" %}</h3>
<form class="form-horizontal" method="post">
{% csrf_token %}
<h3>{% trans "Configuration" %}</h3>
{{ form|bootstrap_horizontal:'col-sm-4' }}
<input type="submit" class="btn btn-primary" name="update"
value="{% trans "Update setup" %}"/>
<input type="submit" class="btn btn-primary" name="update"
value="{% trans "Update setup" %}"/>
</form>
{% endblock %}

View File

@ -26,19 +26,19 @@
<div class="row">
<form class="form" method="post">
{% csrf_token %}
<div class="col-xs-6 text-left">
<input type="submit" class="btn btn-primary" name="create"
value="{% trans 'Create Snapshot' %}"/>
</div>
<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"
{% if not has_deletable_snapshots %}
disabled="disabled"
{% else %}
href="{% url 'snapshot:delete-all' %}"
{% endif %}>
{% if not has_deletable_snapshots %}
disabled="disabled"
{% else %}
href="{% url 'snapshot:delete-all' %}"
{% endif %}>
{% trans 'Delete All' %}
</a>
</div>

View File

@ -19,25 +19,29 @@ Views for snapshot module.
"""
import json
import subprocess
from django.contrib import messages
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from django.urls import reverse, reverse_lazy
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy
from plinth import actions
from plinth.errors import ActionError
from plinth.modules import snapshot as snapshot_module
from plinth.utils import yes_or_no
from django.utils.translation import ugettext as _, ugettext_lazy
from . import get_configuration
from .forms import SnapshotForm
subsubmenu = [{'url': reverse_lazy('snapshot:index'),
'text': ugettext_lazy('Configure')},
{'url': reverse_lazy('snapshot:manage'),
'text': ugettext_lazy('Manage Snapshots')}]
subsubmenu = [{
'url': reverse_lazy('snapshot:index'),
'text': ugettext_lazy('Configure')
}, {
'url': reverse_lazy('snapshot:manage'),
'text': ugettext_lazy('Manage Snapshots')
}]
def index(request):
@ -84,31 +88,31 @@ def manage(request):
def update_configuration(request, old_status, new_status):
"""Update configuration of snapshots."""
def update_key_configuration(key, stamp, threshold):
def make_config(args):
key, stamp = args[0], args[1]
if old_status[key] != new_status[key]:
actions.superuser_run(
'snapshot', ['configure', stamp.format(threshold)])
return stamp.format(new_status[key])
else:
return None
new_status['number_min_age'] = int(new_status['number_min_age']) * 86400
config = filter(None,
map(make_config, [
('enable_timeline_snapshots', 'TIMELINE_CREATE={}'),
('hourly_limit', 'TIMELINE_LIMIT_HOURLY={}'),
('daily_limit', 'TIMELINE_LIMIT_DAILY={}'),
('weekly_limit', 'TIMELINE_LIMIT_WEEKLY={}'),
('monthly_limit', 'TIMELINE_LIMIT_MONTHLY={}'),
('yearly_limit', 'TIMELINE_LIMIT_YEARLY={}'),
('number_min_age', 'NUMBER_MIN_AGE={}'),
]))
command = ['snapper', 'set-config'] + list(config)
try:
update_key_configuration(
'enable_timeline_snapshots', 'TIMELINE_CREATE={}',
yes_or_no(new_status['enable_timeline_snapshots']))
update_key_configuration('hourly_limit', 'TIMELINE_LIMIT_HOURLY={}',
new_status['hourly_limit'])
update_key_configuration('daily_limit', 'TIMELINE_LIMIT_DAILY={}',
new_status['daily_limit'])
update_key_configuration('weekly_limit', 'TIMELINE_LIMIT_WEEKLY={}',
new_status['weekly_limit'])
update_key_configuration('monthly_limit', 'TIMELINE_LIMIT_MONTHLY={}',
new_status['monthly_limit'])
update_key_configuration('yearly_limit', 'TIMELINE_LIMIT_YEARLY={}',
new_status['yearly_limit'])
update_key_configuration('number_min_age', 'NUMBER_MIN_AGE={}',
int(new_status['number_min_age']) * 86400)
subprocess.run(command, check=True)
actions.superuser_run('snapshot', ['configure', 'NUMBER_LIMIT=0'])
actions.superuser_run('snapshot',
['configure', 'NUMBER_LIMIT_IMPORTANT=4-10'])
messages.success(request, _('Storage snapshots configuration updated'))
except ActionError as exception:
messages.error(request,