diff --git a/actions/snapshot b/actions/snapshot index 818fde542..35cc561c0 100755 --- a/actions/snapshot +++ b/actions/snapshot @@ -28,6 +28,7 @@ import augeas FSTAB = '/etc/fstab' AUG_FSTAB = '/files/etc/fstab' +DEFAULT_FILE = '/etc/default/snapper' def parse_arguments(): @@ -54,6 +55,9 @@ def parse_arguments(): subparser = subparsers.add_parser('rollback', help='Rollback to snapshot') subparser.add_argument('number', help='Number of snapshot to rollback to') + subparser = subparsers.add_parser('disable-apt-snapshot', help='enable/disable apt snapshots') + subparser.add_argument('state') + subparsers.required = True return parser.parse_args() @@ -155,6 +159,20 @@ def _get_default_snapshot(): return None +def subcommand_disable_apt_snapshot(arguments): + """Set flag to Enable/Disable apt software snapshots in config files""" + + """Initialize Augeas.""" + aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD + + augeas.Augeas.NO_MODL_AUTOLOAD) + aug.set('/augeas/load/Shellvars/lens', 'Shellvars.lns') + aug.set('/augeas/load/Shellvars/incl[last() + 1]', DEFAULT_FILE) + aug.load() + + aug.set('/files' + DEFAULT_FILE + '/DISABLE_APT_SNAPSHOT', arguments.state) + aug.save() + + def subcommand_create(_): """Create snapshot.""" command = ['snapper', 'create', '--description', 'manually created'] diff --git a/plinth/modules/snapshot/__init__.py b/plinth/modules/snapshot/__init__.py index 364374c47..f9f8110d0 100644 --- a/plinth/modules/snapshot/__init__.py +++ b/plinth/modules/snapshot/__init__.py @@ -18,6 +18,7 @@ FreedomBox app to manage filesystem snapshots. """ +import augeas import json from django.utils.translation import ugettext_lazy as _ @@ -25,7 +26,7 @@ from django.utils.translation import ugettext_lazy as _ from plinth import actions from plinth.menu import main_menu -version = 3 +version = 4 managed_packages = ['snapper'] @@ -52,6 +53,8 @@ service = None manual_page = 'Snapshots' +DEFAULT_FILE = '/etc/default/snapper' + def init(): """Initialize the module.""" @@ -65,7 +68,26 @@ def setup(helper, old_version=None): helper.call('post', actions.superuser_run, 'snapshot', ['setup']) +def load_augeas(): + """Initialize Augeas.""" + aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD + + augeas.Augeas.NO_MODL_AUTOLOAD) + + # shell-script config file lens + aug.set('/augeas/load/Shellvars/lens', 'Shellvars.lns') + aug.set('/augeas/load/Shellvars/incl[last() + 1]', DEFAULT_FILE) + aug.load() + return aug + + +def is_apt_snapshots_enabled(aug): + """Return whether APT snapshots is enabled.""" + value = aug.get('/files' + DEFAULT_FILE + '/DISABLE_APT_SNAPSHOT') + return value != 'yes' + + def get_configuration(): + aug = load_augeas() output = actions.superuser_run('snapshot', ['get-config']) output = json.loads(output) @@ -75,6 +97,8 @@ def get_configuration(): return { 'enable_timeline_snapshots': get_boolean_choice(output['TIMELINE_CREATE'] == 'yes'), + 'enable_software_snapshots': + get_boolean_choice(is_apt_snapshots_enabled(aug)), 'hourly_limit': output['TIMELINE_LIMIT_HOURLY'], 'daily_limit': diff --git a/plinth/modules/snapshot/forms.py b/plinth/modules/snapshot/forms.py index 48be5f3cc..8e6c43d54 100644 --- a/plinth/modules/snapshot/forms.py +++ b/plinth/modules/snapshot/forms.py @@ -29,6 +29,11 @@ class SnapshotForm(forms.Form): '(hourly, daily, monthly and yearly).'), choices=[('yes', 'Enabled'), ('no', 'Disabled')]) + enable_software_snapshots = forms.ChoiceField( + label=_('Software Snapshots'), + help_text=_('Enable or disable software snapshots '), + 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.')) diff --git a/plinth/modules/snapshot/views.py b/plinth/modules/snapshot/views.py index 3860fc861..2baf845dc 100644 --- a/plinth/modules/snapshot/views.py +++ b/plinth/modules/snapshot/views.py @@ -108,6 +108,12 @@ def update_configuration(request, old_status, new_status): ('number_min_age', 'NUMBER_MIN_AGE={}'), ])) + if old_status['enable_software_snapshots'] != new_status['enable_software_snapshots']: + if new_status['enable_software_snapshots'] == 'yes': + actions.superuser_run('snapshot', ['disable-apt-snapshot', 'no']) + else: + actions.superuser_run('snapshot', ['disable-apt-snapshot', 'yes']) + try: actions.superuser_run('snapshot', ['set-config', " ".join(config)])