snapshot: Merge the functionality of the migrate command into setup

- Added a command-line argument called --old-version to the setup command based
  on which it can decide whether to do a new setup or a migration.
- Removed the migrate command.

Signed-off-by: Joseph Nuthalapati <njoseph@thoughtworks.com>
Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
Joseph Nuthalapati 2019-01-08 17:39:40 +05:30 committed by Sunil Mohan Adapa
parent e19cab80ad
commit 4fdd68cb3b
No known key found for this signature in database
GPG Key ID: 43EA1CFF0AA7C5F2
2 changed files with 47 additions and 50 deletions

View File

@ -38,10 +38,10 @@ def parse_arguments():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
subparsers.add_parser('setup', help='Configure snapper')
subparsers.add_parser(
'migrate',
help='Migrate existing user configuration to the new format')
subparser = subparsers.add_parser('setup', help='Configure snapper')
subparser.add_argument(
'--old-version', type=int, required=True,
help='Earlier version of the app that is already setup.')
subparsers.add_parser('list', help='List snapshots')
subparsers.add_parser('create', help='Create snapshot')
subparsers.add_parser('get-config', help='Configurations of snapshot')
@ -70,21 +70,50 @@ def parse_arguments():
return parser.parse_args()
def subcommand_setup(_):
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()
# Create root config if needed.
if 'root' not in output:
command = ['snapper', 'create-config', '/']
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',
]
subprocess.run(command, check=True)
_set_default_config()
_add_fstab_entry('/')
def _set_default_config():
command = [
@ -253,37 +282,6 @@ def subcommand_get_config(_):
print(json.dumps(config))
def subcommand_migrate(_):
"""Migrate existing user configuration for snapshots
to the new configuration format.
Added in version 4 of the snapshots module.
This command will not check or perform first setup steps.
"""
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',
]
subprocess.run(command, check=True)
def subcommand_kill_daemon(_):
"""Kill the snapper daemon.

View File

@ -64,10 +64,9 @@ def init():
def setup(helper, old_version=None):
"""Install and configure the module."""
helper.install(managed_packages)
if old_version:
helper.call('post', actions.superuser_run, 'snapshot', ['migrate'])
else:
helper.call('post', actions.superuser_run, 'snapshot', ['setup'])
helper.call(
'post', actions.superuser_run, 'snapshot',
['setup', '--old-version', str(old_version)])
def load_augeas():