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

View File

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