snapshot: Refactor configuration migration

- Only do the configuration based on version run the remaining setup code
  without considering version.

- Don't force upgrade/setup configuration when doing from version 4 and above.

- Force set cleanup=yes values during migration too.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
Sunil Mohan Adapa 2019-01-08 13:53:48 -08:00
parent d192e73506
commit 12ea72eb0a
No known key found for this signature in database
GPG Key ID: 43EA1CFF0AA7C5F2

View File

@ -72,48 +72,61 @@ def parse_arguments():
def subcommand_setup(arguments):
"""Configure snapper."""
# Check if root config exists.
command = ['snapper', 'list-configs']
process = subprocess.run(command, stdout=subprocess.PIPE, check=True)
output = process.stdout.decode()
if arguments.old_version == 0:
# Check if root config exists.
command = ['snapper', 'list-configs']
process = subprocess.run(command, stdout=subprocess.PIPE, check=True)
output = process.stdout.decode()
# Create root config if needed.
if 'root' not in output:
command = ['snapper', 'create-config', '/']
subprocess.run(command, check=True)
_set_default_config()
_add_fstab_entry('/')
else:
# Migrate existing user configuration for snapshots
# to the new configuration format.
config = _get_config()
def convert_to_range(key):
value = config[key]
value = value if '-' in value else '0-{}'.format(value)
return '{}={}'.format(key, value)
command = [
'snapper',
'set-config',
'TIMELINE_MIN_AGE=0',
convert_to_range('TIMELINE_LIMIT_HOURLY'),
convert_to_range('TIMELINE_LIMIT_DAILY'),
convert_to_range('TIMELINE_LIMIT_WEEKLY'),
convert_to_range('TIMELINE_LIMIT_MONTHLY'),
convert_to_range('TIMELINE_LIMIT_YEARLY'),
'NUMBER_MIN_AGE=0',
'NUMBER_LIMIT=0-100',
'NUMBER_LIMIT_IMPORTANT=0-20',
'EMPTY_PRE_POST_MIN_AGE=0',
'FREE_LIMIT=0.3',
]
# Create root config if needed.
if 'root' not in output:
command = ['snapper', 'create-config', '/']
subprocess.run(command, check=True)
_add_fstab_entry('/')
if arguments.old_version == 0:
_set_default_config()
elif arguments.old_version <= 3:
_migrate_config_from_version_3()
else:
pass # After version 4 and above don't reset configuration
def _migrate_config_from_version_3():
"""Upgrade configuration from version <=3.
- This configuration was not using ranges for limits which would make free
space setting unused.
- Force set yes to cleanups.
- Reset all number cleanup settings.
- Make free space setting 30% by default instead of 20%.
"""
config = _get_config()
def convert_to_range(key):
value = config[key]
value = value if '-' in value else '0-{}'.format(value)
return '{}={}'.format(key, value)
command = [
'snapper',
'set-config',
'TIMELINE_CLEANUP=yes',
'TIMELINE_MIN_AGE=0',
convert_to_range('TIMELINE_LIMIT_HOURLY'),
convert_to_range('TIMELINE_LIMIT_DAILY'),
convert_to_range('TIMELINE_LIMIT_WEEKLY'),
convert_to_range('TIMELINE_LIMIT_MONTHLY'),
convert_to_range('TIMELINE_LIMIT_YEARLY'),
'NUMBER_CLEANUP=yes',
'NUMBER_MIN_AGE=0',
'NUMBER_LIMIT=0-100',
'NUMBER_LIMIT_IMPORTANT=0-20',
'EMPTY_PRE_POST_MIN_AGE=0',
'FREE_LIMIT=0.3',
]
subprocess.run(command, check=True)
def _set_default_config():
command = [